1

I have this pygame program where I animate the character's movement, everytime i press A or D, the x coordinate changes and the character blits an image of itself moving.

The right movement, jump and standing animations work, but not the left movement.

I've tested the images for the facing left images and it faces left. So im pretty sure its not the variable's problem

link for the video for easier visualization -> https://www.youtube.com/watch?v=kHnyJqwPUqE here are the variables ->

c_scale_width=80
c_scale_height= 90
c_scale_stand_width=90
c_scale_stand_height= 105
c_s_y= 290
cx = 250
cy = 300
move_left = False
move_right = False

standing_1 = pygame.image.load(os.path.join('','P1(stand).png'))
standing=pygame.transform.scale(standing_1,(c_scale_stand_width,c_scale_stand_height))

P2_right_11=pygame.image.load(os.path.join('','P1(1).png'))
P2_right_1=pygame.transform.scale(P2_right_11,(c_scale_width,c_scale_height))
P2_right_12=pygame.image.load(os.path.join('','P1(2).png'))
P2_right_2=pygame.transform.scale(P2_right_12,(c_scale_width,c_scale_height))
P2_right_13=pygame.image.load(os.path.join('','P1(3).png'))
P2_right_3=pygame.transform.scale(P2_right_13,(c_scale_width,c_scale_height))
P2_right_14=pygame.image.load(os.path.join('','P1(4).png'))
P2_right_4=pygame.transform.scale(P2_right_14,(c_scale_width,c_scale_height))
P2_right= [P2_right_1,
           P2_right_2,
           P2_right_3,
           P2_right_4]

P2_left_1= pygame.transform.flip(P2_right_1,True,False)
P2_left_2= pygame.transform.flip(P2_right_2,True,False)
P2_left_3= pygame.transform.flip(P2_right_3,True,False)
P2_left_4= pygame.transform.flip(P2_right_4,True,False)

P2_left= [P2_left_1,
          P2_left_2,
          P2_left_3,
          P2_left_4]
step_index=0

now here is the program -->

def c_move(keys_pressed):
    global move_left
    global move_right
    global jump
    global cx
    global cy
    global jump_vel_y
    global  step_index
    if keys_pressed[pygame.K_a]:  # left
        cx -= Velocity
        move_left = True
        move_right = False
    if keys_pressed[pygame.K_d]:  # right
        cx += Velocity
        move_left = False
        move_right = True
    else:
        move_left = False
        move_right = False
        step_index = 0

    if jump is False and keys_pressed[pygame.K_SPACE]:
        jump = True

def draw():
    global jump
    global jump_vel_y
    global cx
    global cy
    WIN.blit(bg,(0,0))
    global step_index
    if step_index >= 4:
        step_index=0

    elif move_left:
        WIN.blit(P2_left[step_index//2], (cx,cy))
        step_index+=1

    if jump:

        cy -= jump_vel_y
        WIN.blit(standing, (cx, cy))
        jump_vel_y -= 1
        if jump_vel_y < -15:
            jump = False
            jump_vel_y = 15
    elif move_right:
        WIN.blit(P2_right[step_index//2], (cx, cy))
        step_index+=1


    else:
        WIN.blit(standing,(cx,c_s_y))


    pygame.display.update()

def main():


    clock = pygame.time.Clock()
    run = True
    while run:
        clock.tick_busy_loop(FPS)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
                pygame.quit()
                sys.exit()

        keys_pressed = pygame.key.get_pressed()
        c_move(keys_pressed)
        draw()

main()

Thanks in advance to whoever reads this!

1 Answers1

0

You need to use one if-elif-elif statement instead of 2 if-elif statement:

def draw():
    global jump, jump_vel_y
    global cx, cy
    global step_index
    
    if step_index >= 4:
        step_index=0
    current_image = standing

    if jump:
        cy -= jump_vel_y
        jump_vel_y -= 1
        if jump_vel_y < -15:
            jump = False
            jump_vel_y = 15

    elif move_right:
        current_image = P2_right[step_index//2]
        step_index += 1
    
    elif move_left:
        current_image = P2_left[step_index//2]
        step_index += 1

    WIN.blit(bg,(0,0))
    WIN.blit(current_image, (cx, cy))
Rabbid76
  • 202,892
  • 27
  • 131
  • 174