Pygame 动画不适用于左侧运动,只有右侧运动有效

问题描述

我有这个 pygame 程序,我在其中为角色的运动设置动画,每次我按 A 或 D 时,x 坐标都会发生变化,并且角色会闪烁自己移动的图像。

右侧运动、跳跃和站立动画有效,但左侧运动无效。

我已经测试了面向左图像的图像,并且它面向左。所以我很确定这不是变量的问题

视频链接以便于可视化 -> https://www.youtube.com/watch?v=kHnyJqwPUqE 这里是变量 ->

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_height))
P2_right_13=pygame.image.load(os.path.join('','P1(3).png'))
P2_right_3=pygame.transform.scale(P2_right_13,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_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,False)
P2_left_3= pygame.transform.flip(P2_right_3,False)
P2_left_4= pygame.transform.flip(P2_right_4,False)

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

现在是程序 -->

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,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],cy))
        step_index+=1


    else:
        WIN.blit(standing,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()

提前感谢阅读本文的人!

解决方法

您需要使用一个 if-elif-elif 语句而不是 2 个 if-elif 语句:

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))

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...