为什么当我不动的时候,我的玩家动画仍然在发生



我有它,这样我的角色在向左或向右移动时会播放行走动画,而当他们停止移动时,他们是空闲的。动画部分工作得很好,但当我放开左/右时,它仍然会播放,玩家永远不会空闲。我的播放器动画和播放器控件的代码如下。

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

因为如果未按键,则不会重置LEFTRIGHT。在检查密钥之前,将LEFTRIGHT设置为False。根据按下的键设置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

最新更新