要求實(shí)現(xiàn)的功能:
在turtle畫板上隨機(jī)位置(不超過畫板),產(chǎn)生隨機(jī)大小(合適的大小)的科赫雪花,并且雪花的數(shù)量也是隨機(jī)的,在[2,10]之間。
實(shí)現(xiàn)代碼:
# @Time : 2020/4/13
# @File : Chap04.py
# @Title : "科赫雪花小包裹"問題,要求雪花位置隨機(jī)(畫布之內(nèi)),雪花個數(shù)隨機(jī)[2,10]之間。雪花大小隨機(jī)(適合的大?。?# @Software: PyCharm
import turtle as t
import random
#繪制科赫曲線
def drawcurve(len,n):
if n==0:
t.fd(len)
else:
for angle in [0,60,-120,60]:
t.left(angle)
drawcurve(len/3,n-1)
#繪制一朵科赫雪花
def drawsnowflake(size,n):
for i in range(n):
drawcurve(size,n)
t.right(120)
def rand_snow(po_x,po_y,size,n):
#去到隨機(jī)位置
t.penup()
t.goto(po_x,po_y)
t.pendown()
#調(diào)整隨機(jī)數(shù)量,隨機(jī)合適大小
t.begin_fill()
drawsnowflake(size,n)
t.end_fill()
n=3
t.setup(800,800)
t.pensize(1)
t.speed(9)
t.bgcolor('#0032c8')
#畫筆顏色和填充圖形的顏色都是白色
t.color('white','white')
#產(chǎn)生隨機(jī)的雪花數(shù)量
snow_num = random.randint(2,10)
for i in range(snow_num):
#產(chǎn)生隨機(jī)的位置坐標(biāo)po_x,po_y,隨機(jī)雪花大小snow_size
po_x = random.randint(-360,360)
po_y = random.randint(-360,360)
snow_size = random.randint(15,40)
rand_snow(po_x,po_y,snow_size,n)
t.hideturtle()
t.done()