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

Posted on 2022-06-14 10:26  灯下白头人  阅读(21)  评论(3编辑  收藏  举报

实验内容

实验任务1 略

实验任务2

源码

 1 # 以(0,0)为圆心,绘制9个彩色同心圆,色彩随机生成
 2 
 3 from turtle import *
 4 from random import random
 5 
 6 def moveto(x,y):
 7     '''把画笔移至坐标(x,y)处'''
 8     penup()
 9     goto(x,y)
10     pendown()
11 
12 
13 setup(800, 600)
14 speed(0)
15 
16 radius = 20
17 for i in range(9):
18     moveto(0, -radius)
19     rgb = tuple( random() for i in range(3) ) # 随机生成rgb颜色三元组
20     color(rgb)
21     begin_fill()
22     circle(radius)
23     end_fill()
24     radius += 20
25 
26 hideturtle()
27 done()

从小到大绘制,后面的圆会覆盖前面的区域。

实验任务3

源码

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

运行截图

实验任务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()

运行截图

实验任务5

源码

 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(5):
14         fd(size)
15         angle += 72
16         seth(angle)
17         size += 5
18 
19 hideturtle()
20 done()

运行截图

实验任务6

源码

 1 from turtle import *
 2 import random
 3 
 4 mode("logo")#切换至logo模式
 5 screensize(800,600)#屏幕大小800*600
 6 a=eval(input("Input the size of rector(height):"))
 7 b=eval(input("Input the size of rector(width):"))
 8 number=eval(input("Input the number of rectors you want:"))
 9 
10 def moveto(x,y):
11     '''把画笔移至坐标(x,y)'''
12     penup()
13     goto(x,y)
14     pendown()
15 
16 def reck(a,b,rgb):
17     #矩形长a,宽b,填充颜色color(rgb三元组),竖直绘制,绘制后坐标,朝向不变
18     color(rgb)
19     begin_fill()
20     fd(a/2);rt(90);fd(b);rt(90);fd(a);rt(90);fd(b);rt(90);fd(a/2)
21     end_fill()
22 
23 hideturtle()
24 moveto(-(number*b/2),0)
25 for i in range(1,number+1):
26     rgb = tuple( random.random() for i in range(3) ) # 随机生成rgb颜色三元组
27     reck(a,b,rgb)
28     moveto(-(number*b/2)+i*b,0)

运行截图

实验7,8 略