简体   繁体   English

为什么我的播放器 animation 在我不动的时候仍然出现?

[英]Why is my player animation still happening when I am not moving?

I have it so that my character will play a walking animation when they move left or right, and when they stop moving they are idle.我有它,以便我的角色在向左或向右移动时会播放行走的 animation,当他们停止移动时,他们会处于空闲状态。 The animation part works fine, but when I let go of left/right, it still plays and the player never goes idle. animation 部分工作正常,但是当我让左/右的 go 时,它仍然可以播放并且播放器永远不会闲置。 The code for my player animation and player controls are below.我的播放器 animation 和播放器控件的代码如下。

def animation_state():
    global player_surface, player_index, player_rect
    

    player_index += 0.15
    if player_index >= len(player_right_walk):
        player_index = 0
    
    if LEFT == True:
        player_surface = player_left_walk[int(player_index)]
    elif RIGHT == True:
        player_surface = player_right_walk[int(player_index)]


    if LEFT == False and RIGHT == False:
        player_surface = pygame.image.load('graphics/dino_idle_right.png').convert_alpha()
    
    if event.type == pygame.KEYUP:
        if event.key == pygame.K_LEFT:
            player_surface = pygame.image.load('graphics/dino_idle_left.png').convert_alpha()
        elif event.key == pygame.K_RIGHT:
            player_surface = pygame.image.load('graphics/dino_idle_right.png').convert_alpha()

    
    screen.blit(player_surface,player_rect)

player control
def player_control():
    global LEFT, RIGHT
    player_velocity = 0
    player_gravity = 0
    

    player_gravity += 3
    player_rect.y += player_gravity
    if player_rect.bottom >= 500:
        player_rect.bottom = 500
    
    keys = pygame.key.get_pressed()

    if keys[pygame.K_LEFT]:
        player_velocity -= 11
        LEFT = True
        RIGHT = False
        if player_rect.x < -50:
            player_rect.x = 800
    elif keys[pygame.K_RIGHT]:
        player_velocity += 11
        LEFT = False
        RIGHT = True
        if player_rect.x > 800:
            player_rect.x = -50

        
    if event.type == pygame.KEYUP:
        if event.key == pygame.K_LEFT or pygame.K_RIGHT:
                player_velocity = 0

    player_rect.x += player_velocity

Because you do not reset LEFT and RIGHT if no key is pressed.因为如果没有按下任何键,您不会重置LEFTRIGHT Set LEFT and RIGHT to False before checking the keys.在检查键之前将LEFTRIGHT设置为 False。 Set LEFT or RIGHT depending on the key that is pressed:根据按下的键设置LEFTRIGHT

def player_control():
    # [...]

    keys = pygame.key.get_pressed()

    LEFT = False
    RIGHT = False
    if keys[pygame.K_LEFT]:
        player_velocity -= 11
        LEFT = True
        if player_rect.x < -50:
            player_rect.x = 800
    elif keys[pygame.K_RIGHT]:
        player_velocity += 11
        RIGHT = True
        if player_rect.x > 800:
            player_rect.x = -50

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何让我的玩家在空中和移动时仍然使用跳跃动画 - How do I make my player still use jump animation while in air and moving 为什么当我的播放器向右或向左移动时我的平台在移动 - Why is my platform moving when my player is moving right or left 为什么 pygame window 动画只在我移动我的 cursor 时起作用 - Why does pygame window animationonly work when i am moving my cursor Pygame,如何在移动时从玩家身上移除轨迹,我尝试填充屏幕但仍然不适合我 - Pygame, how to remove trail from a player when moving, I tried filling screen but still didnt work for me 当我运行我的程序时,为什么没有发生任何事情 - why is nothing happening when i run my program 当我在 ursina 中向播放器添加相机时,我的播放器正在下降并沿其 y 轴移动 - my player is falling and moving on its y axis when i add a camera to the player in ursina 我是 tkinter 的新手……为什么这些条目在我滚动时没有移动? - I am new to tkinter …Why these entries are not moving when I scrolls? 为什么我在应该解决时仍然收到“NoneType”错误? - Why am I still receiving a 'NoneType' error when it should be resolved? 我想知道为什么当我移动播放器时,我的播放器和气球都是精灵 - I'm wondering why my player and my balloon which are both sprites is stuttering when I move the player 当我的播放器接触地面时,如何让我的闲置 animation 播放 - How do I make my idle animation play when my player touches the ground
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM