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

2022/6/15 1:22:39

本文主要是介绍实验8 Python库应用编程与体验,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

task3

 1 from turtle import *
 2 
 3 def square(size=50, rgb='orange'):
 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()

task 4

 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()

task 5

from turtle import *

setup(800, 600)
bgcolor('black')
pencolor('white')
speed(0)

angle = 0
size = 2

n = 10
for i in range(n):
    for j in range(4):
        fd(size)
        angle += 72
        seth(angle)
        size += 5

hideturtle()
done()

task 6

 1 from turtle import *
 2 from random import random
 3 
 4 def move_to(a, b):
 5     penup()
 6     goto(a, b)
 7     pendown()
 8 
 9 def get_rgb():
10     list = []
11     rgb = tuple(random() for i in range(3))
12     while rgb in list:
13         rgb = tuple(random() for i in range(3))
14     list.append(rgb)
15     return rgb
16 
17 def draw_rectangle():
18     begin_fill()
19     rgb = get_rgb()
20     color(rgb, rgb)
21     for i in range(2):
22         fd(20)
23         left(90)
24         fd(70)
25         left(90)
26     end_fill()
27 
28 setup(800, 600)
29 speed(0)
30 
31 for i in range(20):
32     move_to(-20*10+20*i, 0)
33     draw_rectangle()
34 
35 hideturtle()
36 done()

 

  



这篇关于实验8 Python库应用编程与体验的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程