2

I am creating a simple game in PyGame. But the problem is that whenever I hold down a key, my character stops moving after one step and does not move until I release the key and press it again. The code is following.


pygame.init()

screen_width = 800
screen_height = 800

screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("SPACE INVADER")
icon = pygame.image.load("game-controller.png")
pygame.display.set_icon(icon)

player_img = pygame.image.load("Webp.net-resizeimage.png")
playerX = 400
playerY = 700
playerX_change = 0


def player(x, y):
    screen.blit(player_img, (x, y))


running = True

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                playerX_change = -5
            if event.key == pygame.K_RIGHT:
                playerX_change = 5
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                playerX_change = 0

        playerX += playerX_change
        screen.fill((0, 0, 0))
        player(playerX, playerY)
        pygame.display.update()
Aayush Shukla
  • 55
  • 1
  • 6

1 Answers1

2

It's a matter of Indentation. You have to move the object and update the scene in the application loop instead of the event loop:

running = True

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                playerX_change = -5
            if event.key == pygame.K_RIGHT:
                playerX_change = 5
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                playerX_change = 0

    # INDENTATION

    #<--|

    playerX += playerX_change
    screen.fill((0, 0, 0))
    player(playerX, playerY)
    pygame.display.update()

However, use pygame.key.get_pressed() instead of the keyboard events.

running = True

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT]:
        playerX -= 5
    if keys[pygame.K_RIGHT]:
        playerX += 5

    playerX += playerX_change
    screen.fill((0, 0, 0))
    player(playerX, playerY)
    pygame.display.update()

Explanation:

The keyboard events (see pygame.event module) occur only once when the state of a key changes. The KEYDOWN event occurs once every time a key is pressed. KEYUP occurs once every time a key is released. Use the keyboard events for a single action or a step-by-step movement.

pygame.key.get_pressed() returns a list with the state of each key. If a key is held down, the state for the key is True, otherwise False. Use pygame.key.get_pressed() to evaluate the current state of a button and get continuous movement.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174