Player sprite not changing when moving left or right when using a Playstation 5 Controller, but sprite changing when using keyboard controls

12 views Asked by At

I am making a platform game in which the sprites change per movement (i.e. left and right movement sprites, jump sprites, etc). Now whenever I try moving with the controller left and right, the sprites don't change. But when I use my keyboard to move left and right, the sprites do change! The jump sprites do change when on the controller. The controller I'm using is a PS5 controller

Sprite

# Load player sprites
idle_sprite_right = pygame.image.load("idle_sprite_right.png").convert_alpha()
idle_sprite_left = pygame.image.load("idle_sprite_left.png").convert_alpha()
walk_sprite_right = pygame.image.load("walk_sprite_right.gif").convert_alpha()
walk_sprite_left = pygame.image.load("walk_sprite_left.gif").convert_alpha()
run_sprite_right = pygame.image.load("run_sprite_right.gif").convert_alpha()
run_sprite_left = pygame.image.load("run_sprite_left.gif").convert_alpha()
spin_sprite_right = pygame.image.load("spin_sprite_right.gif").convert_alpha()
spin_sprite_left = pygame.image.load("spin_sprite_left.gif").convert_alpha()

# Adjusted hitbox dimensions for each sprite
idle_sprite_right_rect = pygame.Rect(0, 0, 50, 50)
idle_sprite_left_rect = pygame.Rect(0, 0, 50, 50)
walk_sprite_right_rect = pygame.Rect(0, 0, 50, 50)
walk_sprite_left_rect = pygame.Rect(0, 0, 50, 50)
run_sprite_right_rect = pygame.Rect(0, 0, 50, 50)
run_sprite_left_rect = pygame.Rect(0, 0, 50, 50)
spin_sprite_right_rect = pygame.Rect(0, 0, 50, 50)
spin_sprite_left_rect = pygame.Rect(0, 0, 50, 50)

Initializing Joystick

# Initialize joystick
pygame.joystick.init()
joystick_count = pygame.joystick.get_count()
if joystick_count > 0:
    joystick = pygame.joystick.Joystick(0)
    joystick.init()

Moving Left or Right + Sprint

    # Move left and right
    if keys[pygame.K_a] or (joystick_count > 0 and joystick.get_axis(0) < -0.5) and player_x > 0 and not disable_controls:
        if on_ground:
            acceleration = -current_speed
        else:
            acceleration -= air_acceleration_decay
        player_x += acceleration
        background_x += background_speed  # Shift background right when moving left

    if keys[pygame.K_d] or (joystick_count > 0 and joystick.get_axis(0) > 0.5) and not disable_controls:
        if on_ground:
            acceleration = current_speed
        else:
            acceleration += air_acceleration_decay
        player_x += acceleration
        background_x -= background_speed  # Shift background left when moving right

    if keys[pygame.K_0]:
        with open(high_score_file, "wb") as f:
            pickle.dump(highest_score, f)
        pygame.quit()
        sys.exit()

    # Hold down the shift key to increase speed temporarily
    if (keys[pygame.K_LSHIFT] or keys[pygame.K_RSHIFT]) or (joystick.get_axis(5) > 0.4) and not disable_controls:
        if can_sprint and stamina > 0:
            current_speed = player_speed * shift_speed_multiplier
            stamina -= stamina_depletion_rate
        else:
            if stamina_hehe == False:
                sound_module.stamina_zero_sound()
                stamina_hehe = True
            if stamina_hehe == True:
                pass
            current_speed = player_speed

    else:
        current_speed = player_speed
        if stamina < max_stamina:
            if stamina_hehe == False:
                pass
            if stamina_hehe == True:
                stamina_hehe = False
            stamina += stamina_regeneration_rate
            can_sprint = True  # Enable sprinting when stamina is fully replenished

    # Jump
    if on_ground and (keys[pygame.K_SPACE] or (joystick_count > 0 and joystick.get_button(0))) and not disable_controls:
        continue_on_ground == 0
        sound_module.jump_sound()
        on_ground = False
        jump_velocity = -12
        continue_on_ground -= 1

    # Respawn (R key)
    if keys[pygame.K_r]:
        end_time = time.time()
        if music_play_now == 1:
            sound_module.music_play()
            music_play_now -= 1
        if music_play_now == 0:
            pass
        if hit_lefty == 1:
            hit_lefty -= 1
        if hit_lefty == 0:
            pass
        sound_module.restart_sound()
        disable_controls = False
        reset_and_generate_platforms()
        player_x = platforms[0].rect.left + \
            (platforms[0].rect.width - player_size) // 4
        player_y = platforms[0].rect.top - player_size - 10
        jump_velocity = 0
        stamina = max_stamina
        can_sprint = True
        if on_death == 1:
            on_death -= 1
        if on_death == 0:
            pass
        score = 0  # Reset score when respawning
        background_x = initial_background_x  # Reset background position
        time.sleep(0.1)

Change Sprite

    # Draw the player based on direction and action
    if on_ground:
        if acceleration > 4.9:
            player_sprite = run_sprite_right
        elif acceleration < -4.9:
            player_sprite = walk_sprite_left
        elif -5.2 <= acceleration <= 5.2:  # No horizontal movement
            if acceleration > 0:
                player_sprite = run_sprite_right
            elif acceleration < 0:
                player_sprite = run_sprite_leftrr
            else:
                player_sprite = idle_sprite_right  # Default to right idle sprite
    else:  # Player is in the air
        if acceleration > 0:
            player_sprite = spin_sprite_right
        elif acceleration < 0:
            player_sprite = spin_sprite_left
        else:
            player_sprite = spin_sprite_right

    screen.blit(player_sprite, (player_x, player_y))

I am making a platform game in which the sprites change per movement (i.e. left and right movement sprites, jump sprites, etc). Now whenever I try moving with the controller left and right, the sprites don't change, but when I use my keyboard to move left and right, the sprites do change! The jump sprites do change when on the controller. The controller I'm using is a PS5 controller.

Sprite

# Load player sprites
idle_sprite_right = pygame.image.load("idle_sprite_right.png").convert_alpha()
idle_sprite_left = pygame.image.load("idle_sprite_left.png").convert_alpha()
walk_sprite_right = pygame.image.load("walk_sprite_right.gif").convert_alpha()
walk_sprite_left = pygame.image.load("walk_sprite_left.gif").convert_alpha()
run_sprite_right = pygame.image.load("run_sprite_right.gif").convert_alpha()
run_sprite_left = pygame.image.load("run_sprite_left.gif").convert_alpha()
spin_sprite_right = pygame.image.load("spin_sprite_right.gif").convert_alpha()
spin_sprite_left = pygame.image.load("spin_sprite_left.gif").convert_alpha()

# Adjusted hitbox dimensions for each sprite
idle_sprite_right_rect = pygame.Rect(0, 0, 50, 50)
idle_sprite_left_rect = pygame.Rect(0, 0, 50, 50)
walk_sprite_right_rect = pygame.Rect(0, 0, 50, 50)
walk_sprite_left_rect = pygame.Rect(0, 0, 50, 50)
run_sprite_right_rect = pygame.Rect(0, 0, 50, 50)
run_sprite_left_rect = pygame.Rect(0, 0, 50, 50)
spin_sprite_right_rect = pygame.Rect(0, 0, 50, 50)
spin_sprite_left_rect = pygame.Rect(0, 0, 50, 50)

Initializing Joystick

# Initialize joystick
pygame.joystick.init()
joystick_count = pygame.joystick.get_count()
if joystick_count > 0:
    joystick = pygame.joystick.Joystick(0)
    joystick.init()

Moving Left or Right + Sprint

    # Move left and right
    if keys[pygame.K_a] or (joystick_count > 0 and joystick.get_axis(0) < -0.5) and player_x > 0 and not disable_controls:
        if on_ground:
            acceleration = -current_speed
        else:
            acceleration -= air_acceleration_decay
        player_x += acceleration
        background_x += background_speed  # Shift background right when moving left

    if keys[pygame.K_d] or (joystick_count > 0 and joystick.get_axis(0) > 0.5) and not disable_controls:
        if on_ground:
            acceleration = current_speed
        else:
            acceleration += air_acceleration_decay
        player_x += acceleration
        background_x -= background_speed  # Shift background left when moving right

    if keys[pygame.K_0]:
        with open(high_score_file, "wb") as f:
            pickle.dump(highest_score, f)
        pygame.quit()
        sys.exit()

    # Hold down the shift key to increase speed temporarily
    if (keys[pygame.K_LSHIFT] or keys[pygame.K_RSHIFT]) or (joystick.get_axis(5) > 0.4) and not disable_controls:
        if can_sprint and stamina > 0:
            current_speed = player_speed * shift_speed_multiplier
            stamina -= stamina_depletion_rate
        else:
            if stamina_hehe == False:
                sound_module.stamina_zero_sound()
                stamina_hehe = True
            if stamina_hehe == True:
                pass
            current_speed = player_speed

    else:
        current_speed = player_speed
        if stamina < max_stamina:
            if stamina_hehe == False:
                pass
            if stamina_hehe == True:
                stamina_hehe = False
            stamina += stamina_regeneration_rate
            can_sprint = True  # Enable sprinting when stamina is fully replenished

    # Jump
    if on_ground and (keys[pygame.K_SPACE] or (joystick_count > 0 and joystick.get_button(0))) and not disable_controls:
        continue_on_ground == 0
        sound_module.jump_sound()
        on_ground = False
        jump_velocity = -12
        continue_on_ground -= 1

    # Respawn (R key)
    if keys[pygame.K_r]:
        end_time = time.time()
        if music_play_now == 1:
            sound_module.music_play()
            music_play_now -= 1
        if music_play_now == 0:
            pass
        if hit_lefty == 1:
            hit_lefty -= 1
        if hit_lefty == 0:
            pass
        sound_module.restart_sound()
        disable_controls = False
        reset_and_generate_platforms()
        player_x = platforms[0].rect.left + \
            (platforms[0].rect.width - player_size) // 4
        player_y = platforms[0].rect.top - player_size - 10
        jump_velocity = 0
        stamina = max_stamina
        can_sprint = True
        if on_death == 1:
            on_death -= 1
        if on_death == 0:
            pass
        score = 0  # Reset score when respawning
        background_x = initial_background_x  # Reset background position
        time.sleep(0.1)

Change Sprite

    # Draw the player based on direction and action
    if on_ground:
        if acceleration > 4.9:
            player_sprite = run_sprite_right
        elif acceleration < -4.9:
            player_sprite = walk_sprite_left
        elif -5.2 <= acceleration <= 5.2:  # No horizontal movement
            if acceleration > 0:
                player_sprite = run_sprite_right
            elif acceleration < 0:
                player_sprite = run_sprite_leftrr
            else:
                player_sprite = idle_sprite_right  # Default to right idle sprite
    else:  # Player is in the air
        if acceleration > 0:
            player_sprite = spin_sprite_right
        elif acceleration < 0:
            player_sprite = spin_sprite_left
        else:
            player_sprite = spin_sprite_right

    screen.blit(player_sprite, (player_x, player_y))
0

There are 0 answers