为啥我的左右移动动画不起作用?
Posted
技术标签:
【中文标题】为啥我的左右移动动画不起作用?【英文标题】:Why does my animation for moving left and right not work?为什么我的左右移动动画不起作用? 【发布时间】:2020-10-03 01:54:00 【问题描述】:我正在努力让我的角色有一个行走动画,但由于某种原因,即使我的上下行走动画是,左右动画也不起作用。每当我向左或向右移动时,它都会翻转我的角色,就好像他正在向上走一样,但它也不会播放向上动画。我已经查看了代码一百万次,但与左右设置相比,我没有看到上下动画的方式有任何区别,所以我只是不知道可能出了什么问题。下面我写出了播放器类代码,因为我 99% 确定问题出在其中。我还将记录游戏中出现的问题的 gif 图像,并放置整个代码的粘贴箱,以防万一 Player 类不够用。 https://pastebin.com/968pjEmchttps://gyazo.com/0dedfae143497bbc4062cf802e18d054
class Player(pygame.sprite.Sprite):
""" This class represents the character you play as """
def __init__(self, x, y):
""" Constructor function """
# Call the parent's constructor
super().__init__()
self.change_x = 0
self.change_y = 0
self.walking_frames_l = []
self.walking_frames_r = []
self.walking_frames_u = []
self.walking_frames_d = []
self.direction = "R"
sprite_sheet = SpriteSheet("character.png")
image = sprite_sheet.get_image(1, 6, 15, 22)
self.walking_frames_d.append(image)
image = sprite_sheet.get_image(17, 6, 15, 22)
self.walking_frames_d.append(image)
image = sprite_sheet.get_image(33, 6, 15, 22)
self.walking_frames_d.append(image)
image = sprite_sheet.get_image(49, 6, 15, 22)
self.walking_frames_d.append(image)
image = sprite_sheet.get_image(0, 69, 15, 23)
self.walking_frames_u.append(image)
image = sprite_sheet.get_image(16, 69, 15, 23)
self.walking_frames_u.append(image)
image = sprite_sheet.get_image(32, 69, 15, 23)
self.walking_frames_u.append(image)
image = sprite_sheet.get_image(48, 69, 15, 23)
self.walking_frames_u.append(image)
image = sprite_sheet.get_image(2, 38, 13, 22)
self.walking_frames_r.append(image)
image = sprite_sheet.get_image(18, 38, 13, 22)
self.walking_frames_r.append(image)
image = sprite_sheet.get_image(34, 38, 13, 22)
self.walking_frames_r.append(image)
image = sprite_sheet.get_image(50, 38, 13, 22)
self.walking_frames_r.append(image)
image = sprite_sheet.get_image(1, 102, 13, 22)
self.walking_frames_l.append(image)
image = sprite_sheet.get_image(17, 102, 13, 22)
self.walking_frames_l.append(image)
image = sprite_sheet.get_image(33, 102, 13, 22)
self.walking_frames_l.append(image)
image = sprite_sheet.get_image(49, 102, 13, 22)
self.walking_frames_l.append(image)
self.image = self.walking_frames_r[0]
self.rect = self.image.get_rect()
self.rect.y = y
self.rect.x = x
def move(self, walls):
""" Moves player """
# Move left/right
self.change_x = 0
pos = self.rect.x
keystate = pygame.key.get_pressed()
if keystate[pygame.K_a]:
self.change_x = -8
self.direction = "L"
if keystate[pygame.K_d]:
self.change_x = 8
self.direction = "R"
self.rect.x += self.change_x
if self.direction == "R":
frame = (pos // 30) % len(self.walking_frames_r)
self.image = self.walking_frames_r[frame]
else:
frame = (pos // 30) % len(self.walking_frames_l)
self.image = self.walking_frames_l[frame]
# Did this update cause us to hit a wall?
block_hit_list = pygame.sprite.spritecollide(self, walls, False)
for block in block_hit_list:
# If we are moving right, set our right side to the left side of
# the item we hit
if self.change_x > 0:
self.rect.right = block.rect.left
else:
# Otherwise if we are moving left, do the opposite.
self.rect.left = block.rect.right
# Move up/down
self.change_y = 0
pos = self.rect.y
keystate = pygame.key.get_pressed()
if keystate[pygame.K_w]:
self.change_y = -8
self.direction = "U"
if keystate[pygame.K_s]:
self.change_y = 8
self.direction = "D"
self.rect.y += self.change_y
if self.direction == "D":
frame = (pos // 30) % len(self.walking_frames_d)
self.image = self.walking_frames_d[frame]
else:
frame = (pos // 30) % len(self.walking_frames_u)
self.image = self.walking_frames_u[frame]
# Check and see if we hit anything
block_hit_list = pygame.sprite.spritecollide(self, walls, False)
for block in block_hit_list:
# Reset our position based on the top/bottom of the object.
if self.change_y > 0:
self.rect.bottom = block.rect.top
else:
self.rect.top = block.rect.bottom
【问题讨论】:
【参考方案1】:它不起作用,因为如果 self.direction
不是 "D"
则 self.image
在任何情况下都由 self.walking_frames_u[frame]
设置。
请注意,即使self.direction
是"L"
或"R"
,以下语句中的else
情况也会始终执行:
if self.direction == "D": frame = (pos // 30) % len(self.walking_frames_d) self.image = self.walking_frames_d[frame] else: frame = (pos // 30) % len(self.walking_frames_u) self.image = self.walking_frames_u[frame]
将设置self.image
的代码组合在一个if
-elif
-else
语句中。
此外,您必须确保使用正确的坐标来计算图像索引。删除变量pos
并分别使用self.rect.x
self.rect.y
代替:
if self.direction == "R":
frame = (self.rect.x // 30) % len(self.walking_frames_r)
self.image = self.walking_frames_r[frame]
elif self.direction == "L":
frame = (self.rect.x // 30) % len(self.walking_frames_l)
self.image = self.walking_frames_l[frame]
elif self.direction == "D":
frame = (self.rect.y // 30) % len(self.walking_frames_d)
self.image = self.walking_frames_d[frame]
elif:
frame = (self.rect.y // 30) % len(self.walking_frames_u)
self.image = self.walking_frames_u[frame]
【讨论】:
感谢您的回复!我将它们组合成一个语句,解决了它不交换左右动画的问题,但它仍然没有播放列表中的其余精灵。这是我更改代码pastebin.com/Z3rxQ3F0后的样子,这是它在游戏中的样子gyazo.com/f8f0dbef54cffd06c84e9ef64efcf3bd @Tripzen 我已经改变了答案。pos
始终设置为 self.rect.y
。去掉pos
,分别使用self.rect.x
self.rect.y
啊,我明白了! pos 仅设置为 player.rect.y,因为我将其合并为一个语句并将其移至两个 pos 语句下方。从那以后就完成了。非常感谢!以上是关于为啥我的左右移动动画不起作用?的主要内容,如果未能解决你的问题,请参考以下文章