又是一些代码的集合。turtle 是个挺好玩的库。
用乌龟画一个螺旋楼梯,代码如下
-
#draw a spiral staircase
-
def draw_circle_turtle():
-
wn=turtle.Screen()
-
wn.bgcolor("lightgreen")
-
tess=turtle.Turtle()
-
tess.shape("turtle")
-
tess.color("blue")
-
tess.penup()
-
size=20
-
for i in range(30):
-
tess.stamp()
-
size=size+3
-
tess.forward(size)
-
tess.right(24)
-
wn.mainloop()
画一个五角星:
-
#print a pentagram
-
def draw_pentagram():
-
wn=turtle.Screen()
-
wn.bgcolor("lightgreen")
-
tess=turtle.Turtle()
-
#tess.shape("turtle")
-
tess.color("blue")
-
for i in range(5):
-
tess.forward(100)
-
tess.right(180*4//5)
-
wn.mainloop()
打印一个钟表,这个很好玩,我写了三个方法来实现,不过都有些少许的差异。
-
#print a clock
-
def draw_clock():
-
wn=turtle.Screen()
-
wn.bgcolor("lightgreen")
-
tess=turtle.Turtle()
-
tess.shape("turtle")
-
tess.color("blue")
-
for i in range(12):
-
tess.penup()
-
tess.circle(100,30)
-
tess.pendown()
-
tess.stamp()
-
tess.left(90)
-
tess.penup()
-
tess.forward(100)
-
wn.mainloop()
-
def draw_clock2():
-
wn=turtle.Screen()
-
wn.bgcolor("lightgreen")
-
tess=turtle.Turtle()
-
tess.shape("turtle")
-
tess.color("blue")
-
tess.stamp()
-
tess.penup()
-
tess.forward(100)
-
for i in range(12):
-
tess.stamp()
-
tess.penup()
-
tess.left(360/12)
-
tess.forward(100*3.14*2/12)
-
tess.pendown()
-
wn.mainloop()
-
def draw_clock3():
-
wn=turtle.Screen()
-
wn.bgcolor("lightgreen")
-
tess=turtle.Turtle()
-
tess.shape("turtle")
-
tess.color("blue")
-
tess.stamp()
-
for i in range(12):
-
tess.penup()
-
tess.forward(100)
-
tess.pendown()
-
tess.forward(10)
-
tess.penup()
-
tess.forward(10)
-
tess.stamp()
-
tess.home()
-
tess.left(30*(i+1))
-
wn.mainloop()
阅读(1212) | 评论(0) | 转发(0) |