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) 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
源代码
from turtle import * def moveto(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()
测试结果
task2-1
源代码
'''以(0,0)为圆心,绘制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
源代码
'''以(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()
测试结果
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()
测试结果
源代码
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 trangle(size=50): for i in range(3): fd(size) left(120) setup(800, 600) speed(0) n = 10 for i in range(n): square(80) left(360/n) hideturtle() done()
测试结果
task3-2
源代码
from turtle import * setup(800, 600) pencolor('pink') speed(0) n=10 for i in range(n): for i in range(2): circle(80, 90) left(90) right(360/n) hideturtle() done()
测试结果
task4
源代码
from turtle import* setup(800, 600) bgcolor('black') pencolor('white') speed(0) angle = 0 size = 2 n = 4 # 螺旋n边形 count = 50 for i in range(count): fd(size) angle += 360/n+1 seth(angle) size += 5 hideturtle() done()
测试结果
task5-1
源代码
from turtle import * def square(size): for i in range(4): fd(size) left(90) penup() goto(-100, -100) pendown() begin_fill() fillcolor('black') square(200) end_fill() penup() goto(0, -100) pendown() left(45) begin_fill() fillcolor('red') square(100*(2**0.5)) end_fill() hideturtle() done()
测试结果
task5-2
源代码
from turtle import * pensize(2) pencolor('blue') radius = 40 offset = 20 for i in range(5): home() setheading(0) forward(radius) left(90) circle(radius, 90) left(90) forward(radius) radius += offset done()
task6
源代码
from turtle import * def moveto(x, y): penup() goto(x, y) pendown() bgcolor('black') pencolor('yellow') begin_fill() fillcolor('yellow') goto(100, 100) left(135) circle(100*2**0.5, 270) end_fill() pencolor('black') begin_fill() fillcolor('black') moveto(0, 80) circle(15) end_fill() moveto(-15, 90) begin_fill() fillcolor('white') circle(5) end_fill() hideturtle() done()
测试结果
标签:turtle,python,range,绘图,radius,源代码,def,size From: https://www.cnblogs.com/tyy2004/p/17473687.html