我正在努力使角色具有行走动画,但是由于某些原因,即使我的上下行走动画也无法正常工作,左右动画仍不起作用。每当我左右移动时,它都会翻转我的角色,就像他在走路一样,但它也不会播放向上动画。我已经看过一百万遍代码了,但是与左右设置相比,上下动画的方式没有任何区别,所以我只是不知道有什么问题。在下面,我写出了播放器类代码,因为我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
它不起作用,因为如果self.direction
不是"D"
,则无论如何都将self.image
设置为self.walking_frames_u[frame]
。请注意,即使else
是self.direction
或"L"
,也始终执行以下语句中的"R"
情况:
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]