实验任务1
from turtle import * def move(x,y): '''画笔移动到坐标(x,y)处''' penup() goto(x,y) pendown() def draw(n,size = 100): '''绘制边长为size的正n边形''' for i in range(n): fd(size) #画笔向前移动size left(360/n) def main(): pensize(2) pencolor('red') move(-200,0) draw(3) move(0,0) draw(4) move(200,0) draw(5) hideturtle() done() main()
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
'''以(0,0)为圆心,绘制9个同心圆''' from turtle import * def moveto(x,y): '''把画笔移动至坐标(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()
'''以(0,0)为圆心,绘制9个彩色同心圆,色彩随机生成''' from turtle import * from random import random def moveto(x,y): '''把画笔移至坐标(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()
实验任务3
from turtle import * def square(size = 50, rgb = 'orange'): '''绘制正方形 :para:size:int 指定边长(如未指定参数,使用默认值) ''' pencolor(rgb) for i in range(4): fd(size) left(90) def main(): setup(800,600) speed(0) n = 10 for i in range(n): square(80) left(360/n) hideturtle() done() main()
from turtle import * setup(800,600) pencolor('pink') n = 10 for i in range(n+1): #绘制一片花瓣 for j in range(2): circle(80,90) left(90) right(360/n) hideturtle() done()
实验任务4
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
from turtle import * def move(x,y): penup() goto(x,y) pendown() def draw(): for i in range(4): fd(200) left(90) def main(): move(-100,-100) color('black') begin_fill() draw() end_fill() move(0,-100) color('red') begin_fill() circle(100,steps = 4) end_fill() hideturtle() done() main()
from turtle import* def move(x,y): '''画笔移动到坐标(x,y)处''' penup() goto(x,y) pendown() pensize(2) pencolor('Blue') n = 5 left(-90) for i in range(n): left(90) move(0,0) fd(40+20*i) left(90) circle(40+20*i,90) left(90) fd(40+20*i) hideturtle() done()
实验任务6
from turtle import * from random import random def move(x,y): penup() goto(x,y) pendown() def gen_color(): return tuple(random() for i in range(3)) def rect(a,b,c): color(c) fd(a) left(90) fd(b) left(90) fd(a) left(90) fd(b) setup(800,600) move(-100,0) for i in range(20): c = gen_color() begin_fill() rect(10,30,c) end_fill() left(90) fd(10) hideturtle() done()
标签:turtle,python,range,绘图,90,main,def,left From: https://www.cnblogs.com/zz666666/p/17474931.html