Skip to content Skip to sidebar Skip to footer

Smooth Movement In Pygame

I have just started trying to learn about pygame, and I've run into some trouble when trying to move my rectangle around the screen. I have it set so that when I press each of the

Solution 1:

I had a similar problem, so instead of using get_pressed(), I use a dict and update it whenever a key is pressed:

pressed = {}

while True:
    for event in pygame.event.get():
        if event.type == KEYUP:
            pressed[event.key] = False
        elif event.type == KEYDOWN:
            pressed[event.key] = True

Then to test if a key (e.g. the up arrow) was pressed just use

if pressed.get(K_UP):
    # Do something

inside the main event loop (i.e. the while True).


Post a Comment for "Smooth Movement In Pygame"