task1_1
源代码:
1 from turtle import * 2 3 def move(x,y): 4 '''画笔移动到坐标(x,y)处''' 5 penup() 6 goto(x,y) 7 pendown() 8 9 def draw(n,size = 100): 10 '''绘制边长为size的正n边形''' 11 for i in range(n): 12 fd(size) #画笔向前移动size 13 left(360/n) 14 15 def main(): 16 pensize(2) 17 pencolor('red') 18 19 move(-200,0) 20 draw(3) 21 22 move(0,0) 23 draw(4) 24 25 move(200,0) 26 draw(5) 27 28 hideturtle() 29 done() 30 31 main()
运行截图:
task1_2
源代码:
1 from turtle import * 2 3 def moveto(x,y): 4 '''把画笔移动至坐标(x,y)处''' 5 penup() 6 goto(x,y) 7 pendown() 8 9 def main(): 10 pensize(2) 11 pencolor('blue') 12 13 moveto(-150,0) 14 circle(50) 15 16 moveto(0,0) 17 circle(50,steps = 4) 18 19 moveto(150,0) 20 circle(50,steps = 5) 21 22 moveto(300,0) 23 circle(50,steps = 6) 24 25 hideturtle() 26 done() 27 28 main()
运行截图:
task2_1
源代码:
1 '''以(0,0)为圆心,绘制9个同心圆''' 2 3 from turtle import * 4 5 def moveto(x,y): 6 '''把画笔移动至坐标(x,y)处''' 7 penup() 8 goto(x,y) 9 pendown() 10 11 def main(): 12 setup(800,600) 13 14 radius = 20 #圆初始半径 15 offset = 20 #同心圆每次位移量 16 17 for i in range(9): 18 moveto(0,-radius) 19 circle(radius) 20 radius += offset 21 22 hideturtle() 23 done() 24 25 main()
运行截图:
task2_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 def gen_color(): 13 '''生成一个以rgb三元组表示的颜色值并返回''' 14 return tuple(random() for i in range(3)) 15 16 def main(): 17 setup(800,600) 18 19 radius = 180 #圆初始半径 20 offset = 20 #同心圆每次位移量 21 22 for i in range(8): 23 moveto(0,-radius) 24 color(gen_color()) 25 26 begin_fill() 27 circle(radius) 28 end_fill() 29 30 radius -= offset 31 32 hideturtle() 33 done() 34 35 main()
运行截图:
task3_1
源代码:
1 from turtle import * 2 3 def square(size = 50, rgb = 'orange'): 4 '''绘制正方形 5 :para:size:int 指定边长(如未指定参数,使用默认值) 6 ''' 7 8 pencolor(rgb) 9 for i in range(4): 10 fd(size) 11 left(90) 12 13 def main(): 14 setup(800,600) 15 speed(0) 16 17 n = 10 18 for i in range(n): 19 square(80) 20 left(360/n) 21 22 hideturtle() 23 done() 24 25 main()
运行截图:
task3_2
源代码:
1 from turtle import * 2 3 setup(800,600) 4 pencolor('pink') 5 6 n = 10 7 for i in range(n+1): 8 #绘制一片花瓣 9 for j in range(2): 10 circle(80,90) 11 left(90) 12 13 right(360/n) 14 15 hideturtle() 16 done()
运行截图:
task4
源代码:
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 = 5 #螺旋n边形 12 count = 50 #循环次数 13 for i in range(count): 14 fd(size) 15 angle += 360/n 16 seth(angle) 17 size += 5 18 19 hideturtle() 20 done()
运行截图:
task5_1
源代码:
1 from turtle import * 2 3 def move(x,y): 4 penup() 5 goto(x,y) 6 pendown() 7 8 def draw(): 9 for i in range(4): 10 fd(200) 11 left(90) 12 13 def main(): 14 move(-100,-100) 15 color('black') 16 begin_fill() 17 draw() 18 end_fill() 19 20 move(0,-100) 21 color('red') 22 begin_fill() 23 circle(100,steps = 4) 24 end_fill() 25 hideturtle() 26 done() 27 28 main()
运行截图:
task5_2
源代码:
1 from turtle import* 2 3 def move(x,y): 4 '''画笔移动到坐标(x,y)处''' 5 penup() 6 goto(x,y) 7 pendown() 8 9 pensize(2) 10 pencolor('Blue') 11 n = 5 12 left(-90) 13 14 for i in range(n): 15 left(90) 16 move(0,0) 17 fd(40+20*i) 18 left(90) 19 circle(40+20*i,90) 20 left(90) 21 fd(40+20*i) 22 23 hideturtle() 24 done()
运行截图:
task6 选做第一个
源代码:
1 from turtle import * 2 from random import random 3 4 def move(x,y): 5 penup() 6 goto(x,y) 7 pendown() 8 9 def gen_color(): 10 return tuple(random() for i in range(3)) 11 def rect(a,b,c): 12 color(c) 13 fd(a) 14 left(90) 15 fd(b) 16 left(90) 17 fd(a) 18 left(90) 19 fd(b) 20 21 setup(800,600) 22 move(-100,0) 23 for i in range(20): 24 c = gen_color() 25 begin_fill() 26 rect(10,30,c) 27 end_fill() 28 left(90) 29 fd(10) 30 31 hideturtle() 32 done()
运行截图:
标签:10,20,range,实验,90,def,left From: https://www.cnblogs.com/ljxljx317/p/17467182.html