turtle实例

1、彩虹

(1)

 1 from turtle import *
 2  
 3 def HSB2RGB(hues):
 4     hues = hues * 3.59 #100转成359范围
 5     rgb=[0.0,0.0,0.0]
 6     i = int(hues/60)%6
 7     f = hues/60 -i
 8     if i == 0:
 9         rgb[0] = 1; rgb[1] = f; rgb[2] = 0
10     elif i == 1:
11         rgb[0] = 1-f; rgb[1] = 1; rgb[2] = 0
12     elif i == 2:
13         rgb[0] = 0; rgb[1] = 1; rgb[2] = f
14     elif i == 3:
15         rgb[0] = 0; rgb[1] = 1-f; rgb[2] = 1
16     elif i == 4:
17         rgb[0] = f; rgb[1] = 0; rgb[2] = 1
18     elif i == 5:
19         rgb[0] = 1; rgb[1] = 0; rgb[2] = 1-f
20     return rgb
21      
22 def rainbow():
23     hues = 0.0
24     color(1,0,0)
25     #绘制彩虹
26     hideturtle()
27     speed(100)
28     pensize(3)
29     penup()
30     goto(-400,-300)
31     pendown()
32     right(110)
33     for i in range (100):
34         circle(1000)#圆,彩虹
35         right(0.13)
36         hues = hues + 1
37         rgb = HSB2RGB(hues)
38         color(rgb[0],rgb[1],rgb[2])    
39     penup()
40      
41 def main():
42     setup(800, 600,0,0)
43     bgcolor((0.8, 0.8, 1.0))
44     #tracer(False)
45     rainbow()
46     #输出文字
47     tracer(False)
48     goto(100,-100)
49     pendown()
50     color("red")
51     write("Rainbow",align="center",
52           font=("Script MT Bold", 80, "bold"))
53     #tracer(True)
54      
55     mainloop()
56  
57 if __name__ == "__main__":
58     main()

 

 2、雪花

 1 from turtle import *
 2 from random import *   
 3  
 4 def ground():#画下面的土地
 5     hideturtle()  #隐藏乌龟
 6     speed(100)  #速度
 7     for i in range(400):
 8         pensize(randint(5,10))#画笔尺寸
 9         
10         x = randint(-400,350)#位置
11         y = randint(-280,-1)
12         
13         r = -y/280
14         g = -y/280
15         b = -y/280
16         
17         pencolor((r, g, b))#颜色暗色调, (1,1,1)白色;(0,0,0)黑色;(100)红色;(010)绿色;(001)蓝色
18         penup()
19         goto(x,y)#移动x轴
20         pendown()
21         
22         forward(randint(40,100))
23  
24 def snow():    
25     hideturtle()
26     pensize(2)
27     speed(100)
28     for i in range(100):
29         r = random()
30         g = random()
31         b = random()
32         pencolor(r, g, b)#随机生成
33 
34         penup()
35         setx(randint(-350,350))
36         sety(randint(1,270))
37         pendown()
38 
39         dens = randint(8,12)#雪瓣
40         snowsize = randint(10,14)
41         for j in range(dens):
42             forward(snowsize)
43             backward(snowsize)
44             right(360/dens)
45          
46  
47 def main():
48     setup(800, 600, 0, 0)
49     tracer(False)
50     bgcolor("black")    
51     snow()
52     ground()
53     tracer(True)
54     mainloop()
55      
56 if __name__ == "__main__":
57     main()

 

posted on 2017-08-26 00:01  箬笠蓑衣  阅读(1310)  评论(0编辑  收藏  举报