Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Turtle
#1
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)

        
Reply
#2
Please, post full traceback that you get in error tags. Not all of us want to install turtle package and the traceback itself should be pretty clear in this case
Reply
#3
Ahh so sorry about that I forgot to include it!

Error:
Traceback (most recent call last): File "**************************", line 27, in <module> murtle = turtle.Turtle() AttributeError: 'list' object has no attribute 'Turtle'
Reply
#4
on line 14 you overwrite turtle module and after it name turtle refer to a list object. Just use different name for the list.
EDIT: actually it looks like you want the list to be turtles, not turtle. So it's simply typo
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020