Example #1
0
def square(t, length):
    """ Draw a square with sides of given length
    :param t:
    :param length:
    :return: returns the turtle to staring position or location
    """
    for num in range(4):
        TurtleWorld.fd(t, length)
        TurtleWorld.rt(t)
Example #2
0
def draw(t, length, n):
    if n == 0:
        return
    angle = 50
    TurtleWorld.fd(t, length * n)
    TurtleWorld.lt(t, angle)
    draw(t, length, n - 1)
    TurtleWorld.rt(t, 2 * angle)
    draw(t, length, n - 1)
    TurtleWorld.lt(t, angle)
    TurtleWorld.bk(t, length * n)
Example #3
0
def main():
    # Create TurtleWorld object
    world = TurtleWorld()
    # Create Turtle object
    t = Turtle()
    t.delay = 0.001

    # Draw graphics
    pyramid(t, 5)

    # Press enter to exit
    key = input('Press enter to exit')
    world.destroy()
Example #4
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 = ['orange', 'green', 'purple' ]
    i = 1.0
    for color in colors:
        t = constructor(world, i, i*30, color)
        i += 0.5

    return world
Example #5
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 = ['orange', 'green', 'purple']
    i = 1.0
    for color in colors:
        t = constructor(world, i, i * 30, color)
        i += 0.5

    return world
Example #6
0
 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()
Example #7
0
def crossover(): # very clever!
    print ("What is this, a crossover episode?")
    time.sleep(1.5)

    world= TurtleWorld()

    bob = Turtle()
    bob.delay = 0.0001
    bob.pen_color = "black"
    symbol(bob, 15)


    wait_for_user()
Example #8
0
def main():
    # Create TurtleWorld object
    world = TurtleWorld()
    # Create Turtle object
    crush = Turtle()
    crush.delay = 0.001

    # Draw graphics
    square(crush, 10)
    lt(crush, 45)
    fd(crush, sqrt(2) * 10)
    rt(crush, 45)
    square(crush, 20)
    lt(crush, 45)
    fd(crush, sqrt(2) * 20)
    rt(crush, 45)
    square(crush, 30)
    lt(crush, 45)
    fd(crush, sqrt(2) * 30)
    rt(crush, 45)

    # Press enter to exit
    key = input('Press enter to exit')
    world.destroy()
Example #9
0
def functiontesting():
    world = TurtleWorld()    
    b = Turtle()
    # Draw a square
    b.delay = 0.2
    shape(b, 4, 25)
    world.clear()
    # Draw a polygon
    shape(b, 5, 25)
    world.clear()
    b.delay = 0.01
    # Draw a circle
    shape(b, radius=25)
    world.clear()
    # Draw an arc
    shape(b, radius=86, theta=120)
    wait_for_user()
Example #10
0
def follow(t, S, step = 10, angle = 90):
	"""
	read a string and interpret the symbols as instructions
	
	keyword arguments:
	t -- turtle
	S -- string of instructions
	step -- distance moved each time
	angle -- angle turned each time
	"""
	
	for s in S:								#iterates through the string "S" and interprets the element apropriately, ie. if that element is "F", it moves forward.
		if s == "F":
			fd(t, step)
		elif s == "E":
			fd(t, step)
		elif s == "L":
			lt(t, angle)
		elif s == "R":
			rt(t, angle)
	wait_for_user()

			
if __name__ == "__main__":
	world = TurtleWorld()
	world.delay = 0
	bob = Turtle()
	follow(bob, "FRFLFRFLFRFLFRFLFFFLLFRFLFRFLFRFLFRFLFLLFRLFRFLF", 15, 45)


Example #11
0
from TurtleWorld import *
world = TurtleWorld()
bob = Turtle()

from koch_curve import koch

t = bob
x = 360
bob.delay = .01

koch(t, x)
rt(t, 120)

koch(t, x)
rt(t, 120)

koch(t, x)
rt(t, 120)

# def koch(t,x):
# 	if x<3:

# 		return

# 	pd(t)
# 	fd(t,x/3)
# 	lt(t, 60)
# 	fd(t,x/3)
# 	rt(t,120)
# 	fd(t,x/3)
# 	lt(t,60)
Example #12
0
def keypress(event):
    # this function gets called when the user presses a key.
    # the following try statement is a hack; don't emulate this.
    try:
        # figure out which function to call, and call it
        func = eval('draw_' + event.char)
    except NameError:
        print "I don't know how to draw an", event.char
        return
    except SyntaxError:
        # this happens when the user presses return
        teleport(bob, -180, bob.y-size*3)
        return

    func(bob, size)
    skip(bob, size/2)

from TurtleWorld import *
world = TurtleWorld()

# create and position the turtle
size = 20
bob = Turtle(world)
bob.delay = 0.01
teleport(bob, -180, 150)

# tell world to call keypress when the user presses a key
world.bind('<Key>', keypress)

world.mainloop()
Example #13
0
def main():
    world = TurtleWorld()
    turtle = Turtle()

    #################################################################################################################
    # Problem 2: Grades
    # TODO: create a for loop that runs until all grades have been entered
    #       The body of the for loop gets a numerical grade from the user, and then passes that numerical grade
    #       to getLetterGrade, which outputs the letter grade, and returns True, if the numerical grade is valid,
    #       and False if the numerical grade was invalid.
    #
    #       The loop should keep separate counts of the valid and invalid grades entered.  Make sure to properly
    #       initialize the two variables that track the counts
    #          If getLetterGrade returns True, update valid_cnt, gpa, and output "Valid grade entered"
    #          If getLetter Grade returns False, update invalid_cnt, and output "Invalid grade entered"
    #       The loop should also accumulate the valid grades in gpa.  make sure to properly initialize gpa, as well.
    #
    # After the for loop terminates, calculate the gpa from the valid grades accumulated in gpa / valid grade count

    # number of grades to enter
    grades = int(
        input('Enter the number of numerical grades to be converted: '))

    # TODO P2-1: Provide an appropriate initialization value for valid_cnt
    # count of valid grades
    valid_cnt = 0

    # TODO P2-1: Provide an appropriate initialization value for invalid_cnt
    # count of invalid grades
    invalid_cnt = 0

    # TODO P2-1: Provide an appropriate initialization value for gpa
    # accumulate valid grades and then calculate gpa
    gpa = 0

    # TODO P2-2: for loop starts here, embed the following lines in the loop, down to line that says "Loop ends here"
    #            the for loop should process all grades entered by the user
    for i in range(grades):
        grade = float(input('\nEnter next numerical grade: '))

        # TODO P2-3: Pass the grade entered above to getLetterGrade
        # TODO P2-4: Provide an if statement that checks the return value of getLetterGrade,
        #            updates valid_cnt, gpa, invalid_cnt, as necessary, and outputs the appropriate message from below
        if (getLetterGrade(grade)):
            gpa = gpa + grade
            valid_cnt = valid_cnt + 1
            print('Valid grade entered')
        else:
            invalid_cnt = invalid_cnt + 1
            print('Invalid grade entered')
    # TODO P2-2: for loop ends here

    # TODO P2-5: Calculate the gpa for the valid grades
    gpa = gpa / valid_cnt

    # After the loop terminates, output the counts of valid and invalid grades, and the gpa
    print('\nValid grades:', valid_cnt, ', Invalid grades:', invalid_cnt,
          ', gpa', gpa, '\n')

    ################################################################################################################
    # Problem 3:

    # TODO P3-2: Validate the user input such that it is greater than 0
    length = 0

    while length <= 0:
        length = int(input('Enter a length value greater than 0: '))

    #TODO P3-3: Call the draw_star function if the user input a value greater than 50
    if length > 50:
        draw_star(turtle, length)
    #        otherwise print an error message
    else:
        print('Length must be greater than 50 to draw a star')

    key = input('Press any key to exit')
    world.destroy()
Example #14
0
from TurtleWorld import *
world = TurtleWorld()
bob = Turtle()

world.clear()

def fdlt(turtle, length=0, angle=90):
  turtle.fd(length)
  turtle.lt(angle)

def fdrt(turtle, length=0, angle=90):
  turtle.fd(length)
  turtle.rt(angle)

def shift(turtle, distance=10):
  turtle.pu()
  turtle.fd(distance)
  turtle.pd()

def draw_house(turtle, size=10):
  fdlt(turtle, 3*size) # right 3
  fdrt(turtle, 2*size) # up 2
  fdrt(turtle, 1*size) # right 1
  fdlt(turtle, 2*size) # down 2
  turtle.bk(1*size)    # reverse left 1
  fdlt(turtle, 4*size) # right 4
  fdlt(turtle, 4*size, 45) # up 4
  fdlt(turtle, 5*size)     # left/up 5
  fdlt(turtle, 5*size, 45) # left/down 5
  fdlt(turtle, 4*size)     # down 4
  turtle.lt()              # rotate to up
    def test_turtle(self):
        tw = TurtleWorld.TurtleWorld()
        t = TurtleWorld.Turtle()
        t.delay = 0.01
        t.step()

        t.bk(10)
        t.rt(10)
        t.lt(-10)
        t.pu()
        t.pd()
        t.set_color("papaya whip")
        t.set_pen_color("yellow")

        TurtleWorld.fd(t, 10)
        TurtleWorld.bk(t, 10)
        TurtleWorld.lt(t, 10)
        TurtleWorld.rt(t, 10)
        TurtleWorld.pu(t)
        TurtleWorld.pd(t)
        TurtleWorld.set_color(t, "cyan")
        TurtleWorld.set_pen_color(t, "magenta")

        x = t.get_x()
        self.assertAlmostEqual(x, -9.0)

        y = t.get_y()
        self.assertAlmostEqual(y, 0.0)

        heading = t.get_heading()
        self.assertAlmostEqual(heading, -20)

        tw.quit()
Example #16
0
from TurtleWorld import *

world = TurtleWorld()
bob = Turtle()

world.clear()


def fdlt(turtle, length=0, angle=90):
    turtle.fd(length)
    turtle.lt(angle)


def fdrt(turtle, length=0, angle=90):
    turtle.fd(length)
    turtle.rt(angle)


def shift(turtle, distance=10):
    turtle.pu()
    turtle.fd(distance)
    turtle.pd()


def draw_house(turtle, size=10):
    fdlt(turtle, 3 * size)  # right 3
    fdrt(turtle, 2 * size)  # up 2
    fdrt(turtle, 1 * size)  # right 1
    fdlt(turtle, 2 * size)  # down 2
    turtle.bk(1 * size)  # reverse left 1
    fdlt(turtle, 4 * size)  # right 4
Example #17
0
from TurtleWorld import *
world = TurtleWorld()
world.delay = 0  #make the drawing more fast
t = Turtle()
print(t)
from math import cos, sin, pi  #import math elements from math module


def star(turtle, points, R, r):
    A = (2 * pi) / points
    for n in range(points):
        pu(t)  #no drawing when moving
        goto(t, (r * cos((n - 0.5) * A)), (r * sin(
            (n - 0.5) * A)))  #coordinates on the clockwise side of P
        pd(t)  #drawing when moving
        goto(t, (R * cos(A * n)), (R * sin(A * n)))  #coordinates of P
        goto(t, (r * cos((n + 0.5) * A)), (r * sin(
            (n + 0.5) * A)))  #coordinates on th anti-clockwise side of P
        pd(t)


def starA(turtle, points, R, r):
    A = (2 * pi) / points
    for n in range(points):
        pu(t)  #no drawing when moving
        goto(t, (r * cos((n - 0.5) * A)) + 150, (r * sin(
            (n - 0.5) * A)))  #coordinates on the clockwise side of P
        pd(t)  #drawing when moving
        goto(t, (R * cos(A * n) + 150), (R * sin(A * n)))  #coordinates of P
        goto(t, (r * cos((n + 0.5) * A) + 150), (r * sin(
            (n + 0.5) * A)))  #coordinates on th anti-clockwise side of P
Example #18
0
    def test_turtle(self):
        tw = TurtleWorld.TurtleWorld()
        t = TurtleWorld.Turtle()
        t.delay = 0.01
        t.step()

        t.bk(10)
        t.rt(10)
        t.lt(-10)
        t.pu()
        t.pd()
        t.set_color('papaya whip')
        t.set_pen_color('yellow')

        TurtleWorld.fd(t, 10)
        TurtleWorld.bk(t, 10)
        TurtleWorld.lt(t, 10)
        TurtleWorld.rt(t, 10)
        TurtleWorld.pu(t)
        TurtleWorld.pd(t)
        TurtleWorld.set_color(t, 'cyan')
        TurtleWorld.set_pen_color(t, 'magenta')

        x = t.get_x()
        self.assertAlmostEqual(x, -9.0)

        y = t.get_y()
        self.assertAlmostEqual(y, 0.0)

        heading = t.get_heading()
        self.assertAlmostEqual(heading, -20)

        tw.quit()
Example #19
0
	if n < 3:
		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):
    for i in range(3): #3 sided
        koch(t, n)
        rt(t, 120)


world = TurtleWorld()
bob = Turtle()
bob.delay = 0

#offset start point
bob.x = -150
bob.y = 90
bob.redraw()

snowflake(bob, 300)


world.mainloop()	
Example #20
0
def polyline(t, n, length, angle):
    for i in range(n):
        TurtleWorld.fd(t, length)
        TurtleWorld.rt(t, angle)
Example #21
0
"""

Solution to the Koch curve exercise.
Think Python: An Introduction to Software Design
Allen B. Downey

"""

from TurtleWorld import *

world = TurtleWorld()
bob = Turtle()
bob.delay = 0

bob.x = -150
bob.y = 90
bob.redraw()


def koch(t, n):
    if n < 3:
        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)
Example #22
0
import TurtleWorld
import math as m

world = TurtleWorld.TurtleWorld()
bob = TurtleWorld.Turtle()
print(bob)
"""
for i in range(4):
    print ('Hello!')

for num in range(4):
   fd(bob, 100)
  rt(bob)
"""


def square(t, length):
    """ Draw a square with sides of given length
    :param t:
    :param length:
    :return: returns the turtle to staring position or location
    """
    for num in range(4):
        TurtleWorld.fd(t, length)
        TurtleWorld.rt(t)


def polyline(t, n, length, angle):
    for i in range(n):
        TurtleWorld.fd(t, length)
        TurtleWorld.rt(t, angle)
Example #23
0
			heading = get_heading(t)
			stack.append(heading)
			stack.append(y)
			stack.append(x)	
			#set_pen_color(t, colors[(n)%len(colors)])
			#n += 1
		elif s == "]":								#picks up t, and uses goto to make the most recently stored
			pu(t)									#coords and heading the current ones, and deletes them from 
			x = stack.pop(-1)						#the list. works from the back
			y = stack.pop(-1)
			heading = stack.pop(-1)
			goto(t, x, y, heading)
			pd(t)

def fern(S, n, t, stack=[], step=4, angle=25):
	S = fern_write(S, n)
	follow(t, S, stack, step, angle)

if __name__ == "__main__":
	#colors = ['red', 'orange', 'green', 'blue', 'violet', 'magenta']
	world = TurtleWorld()
	bob = Turtle()
	world.delay = 0.01
	pu(bob)
	rt(bob)
	fd(bob, 300)
	lt(bob)
	lt(bob)
	pd(bob)
	fern("A", 6, bob, stack = [], step = 7, angle = 25)
	wait_for_user()
Example #24
0
    # this function gets called when the user presses a key.
    # the following try statement is a hack; don't emulate this.
    try:
        # figure out which function to call, and call it
        func = eval('draw_' + event.char)
    except NameError:
        print "I don't know how to draw an", event.char
        return
    except SyntaxError:
        # this happens when the user presses return
        teleport(bob, -180, bob.y - size * 3)
        return

    func(bob, size)
    skip(bob, size / 2)


from TurtleWorld import *
world = TurtleWorld()

# create and position the turtle
size = 20
bob = Turtle(world)
bob.delay = 0.01
teleport(bob, -180, 150)

# tell world to call keypress when the user presses a key
world.bind('<Key>', keypress)

world.mainloop()