think python turtleworld练习

来源:互联网 发布:微信推广淘宝商品 编辑:程序博客网 时间:2024/05/22 03:33

<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">练习1 </span>

#!/usr/bin/python

from swampy.TurtleWorld import *

world = TurtleWorld()

bob = Turtle()

fd(bob,100)

lt(bob)

fd(bob,100)

print bob

wait_for_user()

#!/usr/bin/python

from swampy.TurtleWorld import *
def square(t):  #定义这个乌龟运动的函数
world = TurtleWorld()
t = Turtle()
for i in range(4):  #用for循环进行遍历
fd(t,100)  #往前都100步
lt(t)    #往左拐
print t        
square('bob')   #进行调用这个函数

wait_for_user()

#!/usr/bin/python  #通过函数可以改变乌龟的行走距离
from swampy.TurtleWorld import *
def square(t,length):
world = TurtleWorld()
t = Turtle()
for i in range(4):
fd(t,length)
lt(t)
print t
square('bob',200)
wait_for_user()

#!/usr/bin/python   #函数再添加一个形参,用来控制遍历的次数、乌龟转弯的角度。本例乌龟爬行的轨迹是正六边形
from swampy.TurtleWorld import *
def square(t,length,n):
world = TurtleWorld()
t = Turtle()
for i in range(n):
fd(t,length)
lt(t,360/n)
print t
square('bob',100,6)
wait_for_user()

乌龟画圆重构后的

from swampy.TurtleWorld import *  from math import *  world = TurtleWorld()def polyline(t,n,length,angle):for i in range(n):fd(t,length)lt(t,angle)def arc(t,r,angle):arc_length = 2 * pi * r * angle /360.0n = int(arc_length / 3) + 1  #每个边长近似为3,已经足够小到画出好看的圆了step_length = arc_length / nstep_angle = float(angle) / npolyline(t,n,step_length,step_angle)bob = Turtle()bob.delay = 0.01arc(bob,70,270)wait_for_user()


0 0
原创粉丝点击