def step(self): """Performs one time step.""" TurtleWorld.step(self) # compute average turtle speed total = 0.0 for turtle in self.animals: total += turtle.speed print total / len(self.animals)
def make_world(constructor): #constructor ~ Wobbler # create TurtleWorld world = TurtleWorld() world.delay = 2 world.setup_run( ) #setup_run(self) Adds a row of buttons for run, step, stop and clear. # make three Wobblers with different speed and clumsiness attributes colors = ['orange', 'green', 'purple'] i = 1.0 for color in colors: t = constructor( world, i, i * 30, color ) #Wobbler.__init__(self, world, speed=1, clumsiness=60, color='red') i += 0.5 return world
def __init__(self): TurtleWorld.__init__(self) self.rows = 2.0 self.delay = 0.01 self.colors = color_list.make_color_dict().values()
def __init__(self): TurtleWorld.__init__(self) self.rows = 2.0 self.delay = 0.01 self.colors = swampy.color_list.make_color_dict().values()
#!/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()
#!/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()
if n < 8: self.fd(n) return for angle in [-60, 120, -60, 0]: self.koch(n / 3.0) self.rt(angle) def snowflake(self): """Draws a Koch snowflake.""" for i in range(3): self.koch(300) self.rt(120) self.undraw() def make_threader(world): """Creates a Threader and makes it draw a snowflake.""" t = Threader(world) t.moveto(-150, 90) # TODO: modify this so it runs in a new thread t.snowflake() world = TurtleWorld() world.setup_interactive() # add a button that calls make_threader world.bu(text='Make Threader', command=Callable(make_threader, world)) world.mainloop()
self.fd(n) return for angle in [-60, 120, -60, 0]: self.koch(n/3.0) self.rt(angle) def snowflake(self): """Draws a Koch snowflake.""" for i in range(3): self.koch(300) self.rt(120) self.undraw() def make_threader(world): """Creates a Threader and makes it draw a snowflake.""" t = Threader(world) t.moveto(-150, 90) # TODO: modify this so it runs in a new thread t.snowflake() world = TurtleWorld() world.setup_interactive() # add a button that calls make_threader world.bu(text='Make Threader', command=Callable(make_threader, world)) world.mainloop()
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()