def world(): global turtle global world world = TurtleWorld() turtle = Turtle() world.wm_minsize(width=800, height=800) turtle.delay = 0.00000001 start(turtle, (0, 0))
def make_world(constructor): # create TurtleWorld world = TurtleWorld() world.delay = .01 world.setup_run() # make three Wobblers with different speed and clumsiness attributes colors = ['red', 'green', 'blue' ] i = 1.0 for color in colors: t = constructor(world, i, i*30, color) i += 0.5 return world
def make_world(constructor): # create TurtleWorld world = TurtleWorld() world.delay = .01 world.setup_run() # 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) i += 0.5 return world
def main(): world = TurtleWorld() bob = Turtle() bob.delay = 0.001 # draw a pie #make 5 triangles and have them created in rotation rt(bob, 200) pd(bob) polygon(bob, 3, 150) lt(bob, 60.1) pd(bob) polygon(bob, 3, 150) lt(bob, 61) pd(bob) polygon(bob, 3, 150) lt(bob, 60.1) pd(bob) polygon(bob, 3, 150) lt(bob, 60.1) pd(bob) polygon(bob, 3, 150) lt(bob, 60.1) pd(bob) polygon(bob, 3, 150) wait_for_user()
def setup_canvas(numturtles): canvas = TurtleWorld() turtles = [] for i in range(numturtles): bob = Turtle() turtles.append(bob) return turtles
def main(): world = TurtleWorld() bob = Turtle() bob.delay = 0.001 # draw a square polygon(bob, 4, 40) # draw a circle centered on the origin radius = 25 pu(bob) fd(bob, radius) lt(bob) pd(bob) circle(bob, radius) my_start = (0, 10) # draw a circle centered on the origin radius = 50 pu(bob) fd(bob, radius) lt(bob) pd(bob) circle(bob, radius) pu(bob) #draw triangle fd(bob, 50) pd(bob) polygon(bob, 3, 30)
def main(): world = TurtleWorld() bob = Turtle() bob.delay = 0.001 # Move the turtle into place pu(bob) lt(bob, 180) fd(bob, 150) pd(bob) # draw pie 1 polypie(bob, 5, 50) # Move the turtle into place pu(bob) lt(bob, 180) fd(bob, 150) pd(bob) # draw pie 2 polypie(bob, 6, 50) # Move the turtle into place pu(bob) fd(bob, 150) pd(bob) # draw pie 3 polypie(bob, 7, 50) wait_for_user()
def drawTriangle(n): world = TurtleWorld() bob = Turtle() # bob.delay=0.0001 for i in range(n): rt(bob,180) triangle(bob,360.0/n,50) wait_for_user()
def main(): world = TurtleWorld() bob = Turtle() bob.delay = 0.000001 pumpkin(bob)
def drawFlower(n): world = TurtleWorld() bob = Turtle() bob.delay=0.0001 for i in range(1,n+1): petal(bob,200,360/n) lt(bob,360/n) wait_for_user()
def test_turtle_world(self): tw = TurtleWorld.TurtleWorld(interactive=True) tw.setup_run() tw.delay = 0.01 control = tw.make_turtle() control.set_color('magenta') control.move_turtle(-1) tw.clear() tw.quit()
def exercise_1(): def square(t): for _ in range(4): fd(t, 200) lt(t) world = TurtleWorld() bob = Turtle() square(bob)
def initTurtle(delay=0.01): """ Initializes a turtle object :param delay: Delay before each action of the turtle. Lower it is the faster the turtle moves. :return: turtle object """ TurtleWorld() t = Turtle() t.delay = delay return t
def exercise_3(): def polygon(t, l, n): for _ in range(n): fd(t, l) rt(t, 360.0 / n) world = TurtleWorld() bob = Turtle() length = 50 polygon(bob, length, 8)
def exercise_2(): def square(t, l): for _ in range(4): fd(t, l) rt(t) world = TurtleWorld() bob = Turtle() length = 200 square(bob, length)
def make_square(init_x, init_y, side): world = TurtleWorld() pat = Turtle() pat.set_delay(.001) for i in range(400): pat.x = init_x - (i * 5 / 2) pat.y = init_y - (i * 5 / 2) for j in range(4): pat.fd(side + i * 5) pat.lt(90) wait_for_user()
def make_polygon(init_x, init_y, sides, length): world = TurtleWorld() pat = Turtle() pat.set_delay(.001) angle_int = 360 / sides pat.x = init_x pat.y = init_y for i in range(sides): pat.fd(length) pat.lt(angle_int) wait_for_user()
def exercise_5(): def arc(t, r, a): for _ in range(a % 360): fd(t, 2 * math.pi * r * (1 / 360)) rt(t, 1) world = TurtleWorld() bob = Turtle() bob.delay = 0.01 radius = 50 angle = 180 arc(bob, radius, angle)
def main(): world = TurtleWorld() bob = Turtle() bob.delay = 0.005 # square(bob,45) # square(bob,100) # square(bob,180) # polygon(bob,15,30) # square(bob,60) circle(bob, 60) #arc(bob,80,180) polyline wait_for_user()
def setup_canvas(numturtles): canvas = TurtleWorld() turtles = [canvas] angle = 360.0 / float(numturtles) for i in range(numturtles): bob = Turtle() bob.delay = 0 turtles.append(bob) pu(bob) lt(bob, angle * i) fd(bob, 100) pd(bob) return turtles
def main(): world = TurtleWorld() bob = Turtle() bob.delay = 0.001 # draw a circle centered on the origin radius = 100 pu(bob) fd(bob, radius) lt(bob) pd(bob) circle(bob, radius) wait_for_user()
def main(): world = TurtleWorld() bob = Turtle() bob.delay = 0.000001 # draw a circle centered on the origin radius = 100 pu(bob) fd(bob, radius) lt(bob) pd(bob) circle(bob, radius) pu(bob) lt(bob, 90) fd(bob, 100) radius = 50 pu(bob) fd(bob, radius) lt(bob) pd(bob) circle(bob, radius) pu(bob) lt(bob, 90) fd(bob, 150) lt(bob, 90) pd(bob) for i in range(4): fd(bob, 100) lt(bob, ) fd(bob, 100) lt(bob, 60) for c in range(3): fd(bob, 180) lt(bob, 120) wait_for_user()
def main(): world = TurtleWorld() bob = Turtle() bob.delay = 0.0001 # draw a circle centered on the origin radius = 100 pu(bob) fd(bob, radius) lt(bob) pd(bob) circle(bob, radius) pu(bob) lt(bob) fd(bob, radius / 2) rt(bob) fd(bob, radius / 32) pd(bob) circle(bob, radius / 2) pu(bob) rt(bob) fd(bob, radius / 2) rt(bob) fd(bob, radius) lt(bob, 180) pd(bob) square(bob, radius * 2) pu(bob) lt(bob) fd(bob, radius) rt(bob) fd(bob, radius) rt(bob) fd(bob, radius) pd(bob) polyline(bob, 3, radius, 120) wait_for_user()
def main(): world = TurtleWorld() bob = Turtle() bob.delay = 0.001 pu(bob) fd(bob, -150) pd(bob) pie(bob, 5, 60) pu(bob) fd(bob, 150) pd(bob) pie(bob, 6, 55) pu(bob) fd(bob, 150) pd(bob) pie(bob, 7, 50) wait_for_user()
def main(): world = TurtleWorld() bob = Turtle() bob.delay = 0.001 # draw a circle centered on the origin radius = 100 pu(bob) fd(bob, radius) lt(bob) pd(bob) circle(bob, radius) # draws a square rt(bob) square(bob, 100) lt(bob) # resets turtle for problem 2 lt(bob) pu(bob) fd(bob, 200) # draws triangle pd(bob) polygon(bob, 3, 100) # sets turtle for next circle pu(bob) fd(bob, -50) lt(bob) # draws second circle within first pd(bob) circle(bob, radius / 2) wait_for_user()
def main(): world = TurtleWorld() bob = Turtle() bob.delay = 0.001 # draw a circle centered on the origin radius = 100 pu(bob) fd(bob, radius) lt(bob) pd(bob) circle(bob, radius) pu(bob) fd(bob, -radius) pd(bob) square(bob, radius * 2) pu(bob) fd(bob, radius) pd(bob) bob.lt(60) polygon(bob, 3, radius * (1 + (35 / 48))) pu(bob) bob.rt(60) lt(bob) fd(bob, radius) rt(bob) fd(bob, radius / 2) lt(bob) pd(bob) circle(bob, radius / 2) wait_for_user()
def main(): """ I don't know what I'm doing""" world = TurtleWorld() bob = Turtle() bob.delay = 0.000001 print("-----Menu-----".center(40)) print(" Task 1: Turtle draws 2 cirlces, a square, and a triangle.") print(" Task 2: Turtle draws 3 different pies.") choice = input("Pick a task: ") if choice == "1": illuminati(bob) elif choice == "2": pu(bob) lt(bob, 180) fd(bob, 125) rt(bob, 180) five_pie(bob) six_pie(bob) seven_pie(bob)
def draw_y(self, size): self.skip(size/4) self.top(3*size/4) self.vshape(size/4, size/4) self.jump(-3*size/4) self.skip(size/4) def draw_z(self, size): self.frift(size, size/2) self.diagonal(size/2, size) self.fd(size) def draw_(self, size): """Draw a space.""" self.skip(size) if __name__ == '__main__': world = TurtleWorld() bob = SmartTurtle() size = 20 bob.delay = 0.01 for s in 'hello': m = 'draw_' + s method = getattr(bob, m) method(size) bob.skip(size/2) world.mainloop()
from swampy.TurtleWorld import * def koch(t, x): if x < 3.0: fd(t, x) return koch(t, x/3.0) lt(t, 60) koch(t, x/3.0) rt(t, 120) koch(t, x/3.0) lt(t, 60) koch(t, x/3.0) def snowflake(t, x): koch(t, x) rt(t, 120) koch(t, x) rt(t, 120) koch(t, x) world = TurtleWorld() world.delay = 0 bob = Turtle() snowflake(bob, 500) wait_for_user()
# call the function that keeps the Wanderers in bounds self.keep_in_bounds(dist) # choose a random direction and turn dir = randint(0, self.clumsiness) - randint(0, self.clumsiness) self.rt(dir) # move forward according to the speed attribute self.fd(self.speed) def keep_in_bounds(self, dist): """Turns the Turtle in order to keep it in bounds.""" # you should modify this method pass # create a new TurtleWorld world = TurtleWorld() # add the Run, Stop, Step and Clear buttons world.setup_run() # make three Wanderers with different speed and clumsiness attributes for i in range(1, 4): Wanderer(i, i * 45) # tell world to start processing events (button presses, etc) wait_for_user()
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()
if event.char in ['\n', '\r']: bob.teleport(-180, bob.y-size*3) bob.busy = False return try: if event.char == ' ': bob.draw_(size) bob.busy = False return method = getattr(bob, 'draw_' + event.char) method(size) except AttributeError: print 'I don\'t know how to draw an', event.char bob.busy = False return bob.skip(size/2) bob.busy = False if __name__ == '__main__': world = TurtleWorld() bob = TyperTurtle() bob.delay = 0 size = 22 bob.busy = False bob.teleport(-180, 150) world.bind('<Key>', keypress) world.mainloop()
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()
#sudo pip install swampy from swampy.TurtleWorld import * world = TurtleWorld() world.ca_width=800 world.ca_height=800 bob = Turtle() def draw(t,length,n): if n == 0 : return angle = 50 fd(t, length*n) lt(t,angle) draw(t, length, n-1) rt(t, 2*angle) draw(t, length, n-1) lt(t, angle) bk(t, length*n) # draw(bob,4,20) # print("finished painting") # wait_for_user() def drawFigure(n): for i in range(n): fd(bob,50)
# call the function that keeps the Wanderers in bounds self.keep_in_bounds(dist) # choose a random direction and turn dir = randint(0,self.clumsiness) - randint(0,self.clumsiness) self.rt(dir) # move forward according to the speed attribute self.fd(self.speed) def keep_in_bounds(self, dist): """Turns the Turtle in order to keep it in bounds.""" # you should modify this method pass # create a new TurtleWorld world = TurtleWorld() # add the Run, Stop, Step and Clear buttons world.setup_run() # make three Wanderers with different speed and clumsiness attributes for i in range(1,4): Wanderer(i, i*45) # tell world to start processing events (button presses, etc) wait_for_user()
from swampy.TurtleWorld import * from swampy.Gui import * from math import pi, sin world = TurtleWorld() bob = Turtle() print bob bob.delay = 0.01 def polyline(t, n, length, angle): """Draws lines of given length at given angle""" for i in range(n): fd(t, length) lt(t, angle) def arc(t, r, angle): """Draws arcs of the given radis and angle""" arc_length = 2 * pi * r * angle / 360 n = int(arc_length / 3) + 1 step_length = arc_length / n step_angle = float(angle) / n polyline(t, n, step_length, step_angle) def petal(t, r, angle): """Draws a single petal at given radius and angle""" arc(t, r, angle) lt(t, 180 - angle) arc(t, r, angle) rt(t, 180 - angle)
fd(t, n) return m = n / 3.0 koch(t, m) lt(t, 60) koch(t, m) rt(t, 120) koch(t, m) lt(t, 60) koch(t, m) def snowflake(t, n): """Draws a snowflake (a triangle with a Koch curve for each side).""" for i in range(3): koch(t, n) rt(t, 120) world = TurtleWorld() bob = Turtle() bob.delay = 0 bob.x = -150 bob.y = 90 bob.redraw() snowflake(bob, 300) world.mainloop()
The turtle starts and ends at the peak, facing the middle of the base. t: Turtle r: length of the equal legs angle: peak angle in degrees """ y = r * sin(angle * 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) if __name__ == '__main__': world = TurtleWorld() bob = Turtle() bob.delay = 0.01 size = 40 draw_pie(bob, 5, size) draw_pie(bob, 6, size) draw_pie(bob, 7, size) draw_pie(bob, 8, size) wait_for_user()
topics: encapsulation: wrapping a piece of code in a function generalization: adding a parameter to a function keyword arguments: for clarity, include name of parameters in argument list when calling function ex polygon(bob ,length=50 ,n=5) interface: summary of how the function is used. what are the parameters? FLOW: polygon() calls polyline() circle() calls polygon() calls polyline() arc() calls polyline() """ from swampy.TurtleWorld import * import math world = TurtleWorld() bob = Turtle() jay = Turtle() bob.delay = 0.01 jay.delay = 0.01 world.ca_width = 700 world.ca_height = 700 world.canvas = world.ca(world.ca_width, world.ca_height, bg='white') def drawSquare(d, t, length): '''Draw square with edge length 'length'. Turtle instance, edge length ===> square