r/pygame • • 22d ago

Need help with player movement

/r/PythonLearning/comments/1xru3n1/need_help_with_player_movement/
0 Upvotes

1 comment sorted by

2

u/MadScientistOR 21d ago

This looks like tkinter, not pygame... but the easiest way to do it would be to create a global variable that keeps track of when the circle is moving and disallow further movement based on that variable. For example:

player_moving = False

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

And you'd also update the other direction functions in much the same way. Of course, this would continue to pick up the fact that the key is down after the movement is processed.

A better way to do that (because global variables are icky) would probably be to create a player object that contains both the shape and a variable referencing its moving status. It's far from the only way, however.

I don't know about Tkinter, but the pygame.event queue has KEYUP and KEYDOWN events that might help you if you find that undesirable, as well as methods in pygame.key that return key presses and key releases.