def __init__(self, *args, **kwds): Turtle.__init__(self, *args, **kwds) self.delay = 0 self.color = self.world.random_color() self.position = random.randrange(0, self.world.get_length()) self.speed_limit = 4 self.speed = random.randrange(0, self.speed_limit) self.safe_distance = 30 self.heading = self.world.lane_heading() self.next = None self.project() self.redraw()
def __init__(self, world, speed=1, clumsiness=60, color='red'): Turtle.__init__(self, world) self.delay = 0 self.speed = speed self.clumsiness = clumsiness self.color = color # move to the starting position self.pu() #Puts the pen up (inactive). self.rt(randint( 0, 360)) #rt(self, angle=90) Turns right by the given angle. self.bk( 150 ) #bk(self, dist=1) Moves the turtle backward by the given distance.
def draw_spiral(t, n, length=3, a=0.1, b=0.0002): """Draws an Archimedian spiral starting at the origin. Args: n: how many line segments to draw length: how long each segment is a: how loose the initial spiral starts out (larger is looser) b: how loosly coiled the spiral is (larger is looser) http://en.wikipedia.org/wiki/Spiral """ theta = 0.0 for i in range(n): fd(t, length) dtheta = 1 / (a + b * theta) lt(t, dtheta) theta += dtheta # create the world and bob world = TurtleWorld() bob = Turtle() bob.delay = 0 draw_spiral(bob, n=1000) wait_for_user()
def drawPie(sides, radius): world = TurtleWorld() t = Turtle() t.delay = 0.001 pie(t, sides, radius)
def drawFlower(n, rf, re): world = TurtleWorld() t = Turtle() t.delay = 0.001 flower(t, n, rf, re)
#!/usr/bin/python """ This module is part of Swampy, a suite of programs available from allendowney.com/swampy. Copyright 2005 Allen B. Downey Distributed under the GNU General Public License at gnu.org/licenses/gpl.html. """ from swampy.TurtleWorld import TurtleWorld, Turtle # create the GUI world = TurtleWorld(interactive=True) # create the Turtle turtle = Turtle() # wait for the user to do something world.mainloop()
for i in range(n): petal(t, r, angle) lt(t, 360/n) def move(t , length): pu(t) fd(t, length) pd(t) world = TurtleWorld() bob = Turtle() bob.set_color("yellow") bob.set_pen_color("red") bob.delay = 0.01 #square(bob, 50) #polygon(bob,4,45) #circle(bob,50) #arc(bob,100,45) #petal(bob,100,45) #flower(bob,6,100,45) move(bob, -100) flower(bob, 7, 60.0, 60.0)
#!/usr/bin/python """ This module is part of Swampy, a suite of programs available from allendowney.com/swampy. Copyright 2005 Allen B. Downey Distributed under the GNU General Public License at gnu.org/licenses/gpl.html. """ import swampy.World from swampy.TurtleWorld import TurtleWorld, Turtle import swampy.Lumpy lumpy = Lumpy.Lumpy() lumpy.opaque_class(World.Interpreter) lumpy.make_reference() world = TurtleWorld() bob = Turtle(world) lumpy.object_diagram() lumpy.class_diagram()
Distributed under the GNU General Public License at gnu.org/licenses/gpl.html. """ from swampy.TurtleWorld import Turtle import Lumpy # create a Lumpy object and capture reference state lumpy = Lumpy.Lumpy() lumpy.opaque_class(Turtle) lumpy.make_reference() # run the test code z = 5 bob = Turtle() for i in range(3): x = i def function(parameter): local = parameter + 1 # draw the state while function is running lumpy.object_diagram() variable = 3 function(variable + 1)
for i in range(n): petal(t, r, angle) lt(t, 360.0/n) def move(t, length): """Move Turtle (t) forward (length) units without leaving a trail. Leaves the pen down. """ pu(t) fd(t, length) pd(t) world = TurtleWorld() bob = Turtle() bob.delay = 0.001 # draw a sequence of three flowers, as shown in the book. move(bob, -100) flower(bob, 7, 60.0, 60.0) move(bob, 100) flower(bob, 10, 40.0, 80.0) move(bob, 100) flower(bob, 20, 140.0, 20.0) die(bob)
angle: peak angle in degrees """ y = r * math.sin(angle * math.pi / 180) rt(t, angle) fd(t, r) lt(t, 90+angle) fd(t, 2*y) lt(t, 90+angle) fd(t, r) lt(t, 180-angle) # create the world and bob world = TurtleWorld() bob = Turtle() bob.delay = 0.01 pu(bob) bk(bob, 130) pd(bob) # draw polypies with various number of sides size = 40 draw_pie(bob, 5, size) draw_pie(bob, 6, size) draw_pie(bob, 7, size) draw_pie(bob, 8, size) die(bob) # dump the contents of the campus to the file canvas.eps #world.canvas.dump()
def __init__(self, world): Turtle.__init__(self, world) self.delay = 0.005 self.set_color('purple')
teleport(bob, -180, bob.y-size*3) bob.busy = False return # figure out which function to call, and call it try: func = eval('draw_' + event.char) except NameError: print ("I don't know how to draw an", event.char) bob.busy = False return func(bob, size) skip(bob, size/2) bob.busy = False world = TurtleWorld() # create and position the turtle size = 20 bob = Turtle(world) bob.delay = 0.01 bob.busy = False teleport(bob, -180, 150) # tell world to call keypress when the user presses a key world.bind('<Key>', keypress) world.mainloop()
# =============== Player_1_Name ================ def player_1(): print '----- First Turtle -----' global player_1_name player_1_name = raw_input('Please name your Turtle: ') player_1() while player_1_name == '' : print print 'You Cannot Leave This Field Empty' print player_1() turtle_1 = Turtle() # Turtle_1_Player_1 # =============== Player_1_Color =============== def turtle_1_color(): global player_1_color player_1_color = raw_input('Please select a color for you Turtle (red,blue,yellow)') turtle_1_color() while player_1_color == 'red' or 'yellow' or 'blue': if player_1_color == 'red': turtle_1.set_color('red') break elif player_1_color == 'blue': turtle_1.set_color('blue')