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()