I'm trying to make a game but the movement is not working. It keeps registering the keypresses without the keys being pressed.
import turtle
player = turtle.Turtle()
root = turtle.Screen()
#variables
allowmovement = 1
#movement
def moveleft():
player.setheading(180)
player.forward(2)
print("left")
def moveright():
player.setheading(0)
player.forward(2)
print("right")
def jump():
player.setheading(90)
player.forward(2)
print("jump")
root.listen()
#loop
while True:
while allowmovement == 1:
root.onkeypress(moveleft(), "a")
root.onkeypress(moveright(), "d")
root.onkeypress(jump(), " ")
output is:
up
left
right
up
left
right
This goes on forever.
I was expecting to be able to press "a"
and the turtle moves left and when I press " "
it moves up. Same with all of the other keys.
Instead, it kept running moveleft()
, moveright()
, and jump()
repeatedly.
I am completely stumped and I don't even know where to start debugging this.