Dec-05-2017, 08:51 AM
I really need a second pair of eyes looking at this because its giving me an AttributeError and I can't find it myself. What I'm trying to do is to have N number amount of turtles bouncing off of the window border.
import turtle import random import math SIZE = 500 NUM_TURTLES= 10 # set up the window window = turtle.Screen() window.setup(SIZE, SIZE) window.title("Turtle Bounce") # create a turtle turtle = [] for i in range(NUM_TURTLES): murtle = turtle.Turtle() murtle.penup() x, y = random.randit(-100,100), random.randint(-100,100) murtle.goto(x,y) turtles.append(murtle) # point turtle in random direction dx = random.randint(2,5) flip = random.choice([-1, 1]) dx *=flip dy = random.randint(2,5) flip = random.choice([-1, 1]) dy *=flip window.tracer(2, 30) while True: # infinite loop for i in range(NUM_TURTLES): murtle = turtles[i] # make sure murtle didn't leave the window x, y = murtle.position() # if the turtle hits a wall, move her in a random direction if x < -SIZE/2 or x > SIZE/2: dx = -dx if y < -SIZE/2 or y > SIZE/2: dy = -dy murtle.forward(10) # determine heading based on dx and dy heading = math.atan2(dy, dx) heading = (180/math.pi) * heading murtle.setheading(heading) murtle.forward(5)