python turtle 雪花
import
turtle
import
random
t
=
turtle
t
.
setup
(
800
,
600
)
#設置畫布大小,參數1:x大小,參數2:y大小
t
.
bgcolor
(
"black"
)
#設置畫布顏色,參數1:顏色名/顏色值如:#fff
t
.
speed
(
0
)
#設置畫筆畫圖速度,參數1:速度,0爲最快
t
.
begin_poly
()
#開始記錄多邊形
t
.
color
(
"white"
)
#設置畫筆和圖形顏色,參數1:顏色名/顏色值如:#fff
#繪製雪花
for
i
in
range
(
0
,
8
):
#重複8次,從0開始
t
.
forward
(
10
)
#畫筆前進,參數1:像素
t
.
right
(
90
)
#畫筆右轉:參數1:角度
t
.
forward
(
1
)
t
.
right
(
90
)
t
.
forward
(
10
)
t
.
left
(
135
)
#畫筆左轉:參數1:角度
t
.
end_poly
()
#結束記錄多邊形
t
.
register_shape
(
"xue_hua"
,
t
.
get_poly
())
#註冊多邊形,參數1:名字,參數2:多邊形
t
.
shape
(
"xue_hua"
)
#設置畫筆形狀,參數1:形狀名
clones
=[]
#定義數組
for
i
in
range
(
0
,
100
):
#循環100次,從0開始
clones
.
append
(
t
.
clone
())
#1、 clones.append:向數組中加入元素。2、t.clone():克隆turtle庫對象t
t
.
hideturtle
()
#隱藏畫筆
t
.
clear
()
#清屏
t
.
tracer
(
False
)
#關閉/開啓畫筆在畫圖過程中的動畫,參數1:True開啓,False關閉
while
True
:
#無限循環
for
c
in
clones
:
#遍歷clones對象
c
.penup()
#擡起畫筆
c
.color(
"white"
)
c
.goto(
random
.
randint
(-
400
,
400
),
random
.
randint
(-
300
,
300
))
#1、goto:讓畫筆移到指定的x,y位置。2、random.randint:生成指定範圍內的整形隨機數
t
.
update
()
#刷新屏幕一次,以顯示畫布上的元素的最新狀態或者說位置。在禁用畫筆動畫時候使用
t.done()
#保持住窗口
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.