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()
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.