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()
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Augustus
  • 23
  • 1
  • 6

1 Answers1

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.

ggorlen
  • 44,755
  • 7
  • 76
  • 106
Ryan
  • 1,081
  • 6
  • 14