[英]Why is my player animation still happening when I am not moving?
我有它,以便我的角色在向左或向右移动时会播放行走的 animation,当他们停止移动时,他们会处于空闲状态。 animation 部分工作正常,但是当我让左/右的 go 时,它仍然可以播放并且播放器永远不会闲置。 我的播放器 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
因为如果没有按下任何键,您不会重置LEFT
和RIGHT
。 在检查键之前将LEFT
和RIGHT
设置为 False。 根据按下的键设置LEFT
或RIGHT
:
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.