r/PythonLearning • • 6d ago

Help Request Need help with player movement

Hi everyone, I need some help.

I'm making a game for my school project. I made a simple movement test, and it worked well when I pressed the WASD keys individually. However, when I held down a key, the circle moved farther than I expected.

How can I prevent new key events from being processed while the circle is still moving?

I am sending photo of code down.

2 Upvotes

2 comments sorted by

1

u/PureWasian 5d ago

Keep track of if the circle is already moving using a boolean. Example:

is_moving = False

def up(event):
    global is_moving
    if is_moving:
        return
    is_moving = True
    for i in range(25):
        canvas.move(player, 0, -2)
        canvas.update()
        canvas.after(5)
    is_moving = False

global tells Python to reference the variable defined outside of the function

1

u/notyboy01 3d ago

The boolean flag is doing exactly what you need here. It prevents another movement from starting until the current one finishes.