实验任务1
task1_1
from turtle import * def moveto(x,y): ''' 画笔移动到坐标(x,y)处 ''' penup() goto(x,y) pendown() def draw(n,size = 100): ''' 绘制边长为size的正n边形 ''' for i in range(n): fd(size) left(360/n) def main(): pensize(2) pencolor('red') moveto(-200,0) draw(3) moveto(0,0) draw(4) moveto(200,0) draw(5) hideturtle() done() main()
运行结果如图:
task1_2
# 运用circle函数,实现正多边形绘制 from turtle import * def moveto(x,y): ''' 画笔移动到坐标(x,y)处 ''' penup() goto(x,y) pendown() def main(): pensize(2) pencolor('blue') moveto(-150,0) circle(50) moveto(0,0) circle(50,steps = 4) moveto(150,0) circle(50,steps = 5) moveto(300,0) circle(50,steps = 6) hideturtle() done() main()
运行结果如图:
试验任务2
task2_1
# 用turtle绘制9个同心圆 from turtle import * def moveto(x,y): penup() goto(x,y) pendown() def main(): setup(800,600) radius = 20 # 圆初始半径 offset = 20 # 同心圆每次位移量 for i in range(9): moveto(0,-radius) circle(radius) radius += offset hideturtle() done() main()
运行截图如图:
task2_2_1
# 以(0, 0)为圆心,绘制9个彩色同心圆,色彩随机生成 from turtle import * from random import random def moveto(x, y): penup() goto(x, y) pendown() def gen_color(): ''' 生成一个以rgb三元组表示的颜色值并返回 ''' return tuple((random() for i in range(3))) def main(): setup(800, 600) radius = 180 # 圆初始半径 offset = 20 # 同心圆每次位移量 for i in range(8): moveto(0, -radius) color(gen_color()) begin_fill() circle(radius) end_fill() radius -= offset hideturtle() done() main()
运行截图如图:
颜色会随机变化
若半径从小到大:
task2_2_2
# 以(0, 0)为圆心,绘制9个彩色同心圆,色彩随机生成 from turtle import * from random import random def moveto(x, y): penup() goto(x, y) pendown() def gen_color(): ''' 生成一个以rgb三元组表示的颜色值并返回 ''' return tuple((random() for i in range(3))) def main(): setup(800, 600) radius = 20 # 圆初始半径 offset = 20 # 同心圆每次位移量 for i in range(8): moveto(0, -radius) color(gen_color()) begin_fill() circle(radius) end_fill() radius += offset hideturtle() done() main()
运行结果如图:
半径大的圆会填充覆盖半径小的圆
实验任务3
task3_1
# 用矩形绘制花瓣 from turtle import * def square(size = 50, rgb = 'pink'): ''' 绘制正方形 :para: size:int 指定边长 (如未指定参数,使用默认值) :para: c: str 指定画笔颜色值 (如未指定参数,使用默认值) ''' pencolor(rgb) for i in range(4): fd(size) left(90) def main(): setup(800, 600) speed(0) n = 3 for i in range(n): square(80) left(360/n) hideturtle() done() main()
n=3
n=4
n=5
n=6
task3_2
from turtle import * setup(800, 600) pencolor('pink') n = 10 for i in range(n): # 绘制一片花瓣 for j in range(2): circle(80, 90) left(90) right(360/n) hideturtle() done()
运行结果如图:
实验任务4
task4
from turtle import * setup(800, 600) bgcolor('black') pencolor('white') speed(0) angle = 0 size = 2 n = 5 # 螺旋n边形 count = 50 # 循环次数 for i in range(count): fd(size) angle += 360/n seth(angle) size += 5 hideturtle() done()
实验结果如图:
实验任务5
task5_1
# 先画外围正方形 from turtle import * def moveto(x, y): penup() goto(x, y) pendown() color('black') begin_fill() moveto(-100,-100) for i in range(4): fd(200) left(90) end_fill() # 再画内三角形 color('red') begin_fill() moveto(0,-100) left(45) for i in range(4): fd(100*2**(1/2)) left(90) end_fill() hideturtle() done()
运行结果如图:
task5_2
size = 5 color('blue') radius = 40 for i in range(5): # 扇形的个数 fd(radius) left(90) circle(radius,90) left(90) fd(radius) left(90) radius += 20 hideturtle() done()
运行结果如图:
实验任务6
from turtle import * screensize(100,100,'black') def moveto(x, y): penup() goto(x, y) pendown() # 画脸 color('yellow') begin_fill() left(45) fd(100) left(90) circle(100,270) left(90) fd(100) end_fill() # 画眼睛 moveto(10,65) dot(15,'black') # 画高光 moveto(8,70) dot(8,'white') hideturtle() done()
运行结果如图:
实验结论:
较为简单的画图,仔细观察便可
标签:turtle,moveto,python,range,radius,绘图,def,left From: https://www.cnblogs.com/fengguowanyueyi/p/17461737.html