小编教大家做烟花程序啦!
一定要看到最后!!!
首先我们写代码:
import turtle as t
import time
t.delay(0)
t.tracer(0)
def fire(xx=0,yy=0,type=1,color='red',size=100):
if size == 0:
print('尺寸错误!不能为0')
return
n = 24
spark_list=[]
for x in range(n):
spark=t.Turtle()
spark.color(color)
spark.penup()
spark.goto(xx,yy)
spark.seth(360/n*x)
spark_list.append(spark)
direction = 1
for length in range(1,size):
for x in range(n):
spark_list[x].shapesize(-(length/size)**2+1.01,-(length/size)**2+1.01)
#样式1
if type == 1:
spark_list[x].shape('circle')
if x%2==0:
spark_list[x].circle(size/2,180/size)
else:
spark_list[x].circle(-size/4,180/size)
#样式2
elif type ==2:
if x%3 == 0:
spark_list[x].shape('square')
elif x%3 == 1:
spark_list[x].shape('triangle')
elif x%3 == 2:
spark_list[x].shape('arrow')
if length >= (size/3) and x%3 == 0:
spark_list[x].forward(1)
if length >= (size*2/3) and x%3 == 1:
spark_list[x].forward(1)
if x%3 == 2:
spark_list[x].forward(1)
spark_list[x].tilt(360/size)
#样式3
elif type ==3:
spark_list[x].shape('arrow')
if length%20 == 0 and x == 0:
direction = -1 * direction
spark_list[x].circle(11.5*direction,6)
#样式4
elif type ==4:
spark_list[x].shape('circle')
if length == 1:
for i in range(6):
if x%6 == i:
spark_list[x].seth(60*i)
elif length == size//3:
if x < 12:
spark_list[x].left(30)
else:
spark_list[x].right(30)
elif length == size*2//3:
if x % 12 < 6:
spark_list[x].left(30)
else:
spark_list[x].right(30)
spark_list[x].forward(1)
else:
print('type错误!范围:1~4')
return
time.sleep(0.1/size)
t.update()
######################
for x in spark_list:
x.ht()
del(x)
t.update()
######################
def done():
t.done()
if __name__ == "__main__":
#pass
t.onscreenclick(fire)
done()
最后我们保存这个程序,小编提醒大家一定要记住自己给文件取得名字呀!
接下来我们在新建文件。。。。。。
|