Player not moving in pygame; no error messages

Question:

I created this program and when I run it the player is not moving. I checked and everything is right. Just in case you need it I am using python 3.8.6. Can you please help me? Here is my code.

# Game Loop
running = True

while running:
    screen.fill((128, 20, 128))

for event in pygame.event.get():

    player(playerX, playerY)
    pygame.display.update()

        # Movment
if event.type == pygame.KEYDOWN:
    if event.key == pygame.K_a:
        player_x_change -= 10
    if event.key == pygame.K_d:
        player_x_change += 10
    if event.key == pygame.K_w:
            player_y_change -= 10
    if event.key == pygame.K_s:
            player_y_change += 10

if event.type == pygame.KEYUP:
    if event.key == pygame.K_a:
        player_x_change = 0
    if event.key == pygame.K_d:
        player_x_change = 0

      playerX += player_x_change
      playerY += player_y_change
        
    # Close the game
    if event.type == pygame.QUIT:
        running = False
Asked By: DARSIN

||

Answers:

It’s probably just an indentation error (perhaps pasting to the question), but none of your events are being handled because of the if event.type clauses are not inside the for event loop.

Simply fixing this indentation alleviates this.

The next problem is that you calculate the player_x_change and player_y_change, but never apply the amounts to playerX and playerY.

Since these changes only happen when pygame.KEYDOWN is received, it’s easy to simply add this inside the event handler:

    # Movment
    elif event.type == pygame.KEYDOWN:
        if event.key == pygame.K_a:
            player_x_change -= 10
        elif event.key == pygame.K_d:
            player_x_change += 10
        elif event.key == pygame.K_w:
            player_y_change -= 10
        elif event.key == pygame.K_s:
            player_y_change += 10
        # Move the player
        playerX += player_x_change
        playerY += player_y_change

Note the use of if .. elif. If the event is one particular type, it cannot possibly be another type too (at the same time). This means it’s not necessary to keep re-checking the type. Using elif ("else if") helps with these checks, by stopping the check once a match is found. It is more efficient.

Final code:

import pygame

# Window size
WINDOW_WIDTH    = 400
WINDOW_HEIGHT   = 400
WINDOW_SURFACE  = pygame.HWSURFACE|pygame.DOUBLEBUF
BACKGROUND = (128,  20, 128)


### initialisation
pygame.init()
pygame.mixer.init()
pygame.display.set_caption("Not Moving")

# Game Loop
clock = pygame.time.Clock()
running = True

while running:

    for event in pygame.event.get():
        # Close the game
        if event.type == pygame.QUIT:
            running = False

        # Movment
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_a:
                player_x_change -= 10
            elif event.key == pygame.K_d:
                player_x_change += 10
            elif event.key == pygame.K_w:
                player_y_change -= 10
            elif event.key == pygame.K_s:
                player_y_change += 10
            # Move the player
            playerX += player_x_change
            playerY += player_y_change

        # DON'T REALLY NEED THIS
        #elif event.type == pygame.KEYUP:
        #    if event.key == pygame.K_a:
        #        player_x_change = 0
        #    elif event.key == pygame.K_d:
        #        player_x_change = 0

    # Re-draw the screen
    screen.fill( BACKGROUND )
    player(playerX, playerY)
    pygame.display.update()
    clock.tick(60)                # limit the re-fresh rate to save electricity

pygame.quit()

I also added a frame-rate limiter with a pygame.time.Clock() object, just to save some CPU.

Answered By: Kingsley

I was able to fix the problem now, if anyone wants the code here it is. 🙂 It turns out I put the "close program" code too early and it doesn’t work until the close button is pressed. So my player did move, but only for a second.

# Game Loop
running = True
while running:

    if event.type == pygame.QUIT:
        running = False

    # if keystroke is pressed check whether its right or left
    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
if playerX <= 0:
    playerX = 0
elif playerX >= 736:
    playerX = 736
Answered By: DARSIN
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.