实验6
task1
from turtle import * def move(x, y): penup() goto(x, y) pendown() def draw(n, size = 100): for i in range(n): fd(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()
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
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
from turtle import * from random import random def moveto(x, y): penup() goto(x, y) pendown() def gen_color(): 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'): 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()
task3_2
from turtle import * setup(800, 600) pencolor('pink') n = 4 for i in range(4): for j 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
import turtle as t t.hideturtle() t.penup() t.goto(50*2**(1/2),50*2**(1/2)) t.pendown() t.begin_fill() for i in range(4): t.right(90) t.forward(100*2**(1/2)) t.fillcolor('black') t.end_fill() t.penup() t.goto(0,50*2**(1/2)) t.pendown() t.begin_fill() t.right(45) for i in range(4): t.forward(100) t.right(90) t.fillcolor('red') t.end_fill() t.exitonclick()
task5_2
import turtle as t
t.pencolor('blue')
r=40
t.penup()
t.goto(0,-60)
t.pendown()
for i in range(5):
r+=20
t.circle(r)
t.penup()
t.goto(0,-(60+20*(i+1)))
t.pendown()
t.begin_fill()
t.penup()
t.goto(0,0)
t.pendown()
t.penup()
t.left(90)
t.forward(140)
t.left(90)
t.fd(140)
t.left(90)
t.fd(280)
t.left(90)
t.fd(280)
t.left(90)
t.fd(140)
t.left(90)
t.fd(140)
t.fillcolor('white')
t.end_fill()
t.penup()
t.goto(0,140)
t.pendown()
t.goto(0,0)
t.goto(140,0)
t.hideturtle()
t.pencolor('white')
t.exitonclick()
task6
import turtle as t t.pensize() t.bgcolor('black') t.begin_fill() t.circle(200,360) t.fillcolor('yellow') t.end_fill() t.penup() t.goto(0,200) t.pendown() t.begin_fill() t.left(45) for i in range(4): t.forward(200) t.right(90) t.fillcolor('black') t.end_fill() t.penup() t.goto(20,300) t.pendown() t.hideturtle() t.begin_fill() t.circle(8) t.fillcolor('black') t.end_fill() t.penup() t.goto(21,306) t.pendown() t.begin_fill() t.circle(2) t.fillcolor('white') t.end_fill() t.exitonclick() t.down()