1 import turtle 2 3 def draw_square(t,a,c): 4 t.color(c) 5 t.pencolor("black") 6 t.begin_fill() 7 for i in range(4): 8 t.forward(a) 9 t.left(90) 10 t.end_fill() 11 12 13 def locate(t): 14 t.up() 15 t.fd(10) 16 t.left(90) 17 t.fd(10) 18 t.right(90) 19 t.down() 20 21 22 turtle.setup(500,500,200,200) 23 t=turtle.Pen() #这个Pen 第一个字母要大写。 24 t.up() 25 t.fd(-150) 26 t.right(90) 27 t.fd(150) 28 t.left(90) 29 t.down() 30 a=300 31 t.pensize(5) 32 33 for i in range(5): 34 draw_square(t,a,"red") 35 a=a-20 36 locate(t) 37 draw_square(t,a,"yellow") 38 a=a-20 39 locate(t) 40 draw_square(t,a,"blue") 41 a=a-20 42 locate(t)
效果如下: