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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
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 ) |