0

I'm trying to build a game similar to snake with the turtle library. I was able to make the turtle continually move forward with a while True loop, and also make turns without breaking the while loop.

Now I'm trying to figure out a way to exit the while loop that makes the turtle go forward in order to end the game. My aim is to allow the player to exit the loop by entering 'e' on their keyboard.

This code currently results in: AttributeError: 'Turtle' object has no attribute 'done'

def forward():
  while True:
    snake.forward(0.8) 
    if window.onkey(exit,"e"):
      exit()

def left():
  snake.left(90)

def right():
  snake.right(90)

def back():
  snake.back(0.8)

def exit():
  snake.done()

#the function that actually moves the snake 
def movesnake():
    while True:
      window.listen()
      
      window.onkey(forward, "w")
      window.onkey(left, "a")
      window.onkey(right, "d")
      window.onkey(back, "s")
      
      window.mainloop()

movesnake()
1
  • Why not call exit, like you did in the forward loop? Commented Jul 11, 2022 at 19:10

1 Answer 1

2

If you just want the snake to stop moving, snake.done() should be turtle.done(). done is a turtle module function, not a turtle.Turtle method, so you can call it as a function, but not on the Turtle object.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.