实验8 python库应用编程与体验

task3

 1 from turtle import *
 2 
 3 def square(size=50, rgb='gold'):
 4     '''绘制正方形
 5     参数size指定边长
 6     参数rgb指定画笔颜色
 7     如果没有给参数,采用默认值
 8     '''
 9     pencolor(rgb)
10     for i in range(4):
11         fd(size)
12         left(90)
13 
14 
15 def main():
16     setup(800, 600)
17     speed(0)
18 
19     n = 10
20     for i in range(n):
21         square(80)
22         left(360/n)
23 
24     hideturtle()
25     done()
26 
27 
28 if __name__ == '__main__':
29     main()

 

 

task4

 1 from turtle import *
 2 
 3 setup(800, 600)
 4 pencolor('pink')
 5 
 6 n = 10
 7 for i in range(n):
 8     # 绘制一片花瓣
 9     for j in range(2):
10         circle(80, 90)
11         left(90)
12 
13     right(360/n)
14 
15 hideturtle()
16 done()

 

 

task5

 1 from turtle import *
 2 
 3 setup(800, 600)
 4 bgcolor('black')
 5 pencolor('white')
 6 speed(0)
 7 
 8 angle = 0
 9 size = 2
10 
11 n = 10
12 for i in range(n):
13     for j in range(4):
14         fd(size)
15         angle += 72
16         seth(angle)
17         size += 5
18 
19 hideturtle()
20 done()

 

 

task6

 1 from turtle import*
 2 from random import random
 3 speed(0)
 4 def rect(x=10,y=50,rgb='black'):
 5     rgb=tuple(random() for i in range(3))
 6     color(rgb)
 7     for i in range(1,5):
 8         if i%2==1:
 9             fd(20)
10         else:
11             fd(50)
12         lt(90)
13 q=-150
14 for i in range(20):
15     penup()
16     goto(q,-10)
17     pendown()
18     fd(20)
19     left(90)
20     fd(50)
21     left(90)
22     fd(20)
23     left(90)
24     fd(50)
25     left(90)
26 
27     rgb = tuple(random() for i in range(3))
28     color(rgb)
29     begin_fill()
30     rect(20,50,rgb)
31     end_fill()
32     q += 20
33 
34 
35 ht()
36 done()

 

posted @ 2022-06-13 22:28  Kyle-K  阅读(8)  评论(1编辑  收藏  举报