我有一个非常奇怪的错误,我无法深究。当我玩我作为项目创建的游戏时,我按下攻击按钮J角色会像他应该的那样播放攻击动画。他以1-5倍的正确方式完成任务。然而,一旦他这样做(并且我不确定它如何决定它想要显示多少次),它将不再播放动画。我真的很感激任何人对此的帮助。有关Pyganim的参考,请查看here。
这是我的代码:
#Setup movement keys
rightPressed = False
leftPressed = False
facing = True #True if facing right, false if facing left
level = 1
idlePaused = False#Checks for Pyganim pausing the animation
runStopped = True#Checks if the character running is stopped
isAttacking = False#If attacking no other animation will play
def updateDisplay():
pygame.display.flip()
def keyPressed(inputKey):
keysPressed = pygame.key.get_pressed()
if keysPressed[inputKey]:
return True
else:
return False
class gameStart(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.background = pygame.image.load("BG" + str(level) + ".png")
self.player = pygame.image.load("2_scaled.png")
self.icon = pygame.image.load("1_scaled.png")
self.background_S = pygame.transform.scale(self.background, (width, height)) #Scale the background to match the screen resolution
screen.blit(self.background_S, (0,0))
screen.blit(self.player, (0, height/2))
screen.blit(self.icon, ((width+(self.icon.get_width())),(height+(self.icon.get_height()))))
self.position = self.player.get_rect()
self.health = 100
#Setup the players Idle, Attack, Attack 2 and Death Animations
self.PlayerIdleAnim = pyganim.PygAnimation([('2_scaled.png', 500), ('3_scaled.png', 500), ('4_scaled.png', 500), ('5_scaled.png', 500), ('6_scaled.png', 500)])
self.PlayerIdleAnimFlipped = self.PlayerIdleAnim.getCopy()
self.PlayerIdleAnimFlipped.flip(True, False)
self.PlayerIdleAnimFlipped.play()
self.PlayerIdleAnim.play()
self.PlayerRunAnim = pyganim.PygAnimation([('25_scaled.png', 200), ('26_scaled.png', 200), ('27_scaled.png', 200), ('28_scaled.png', 200), ('29_scaled.png', 200), ('30_scaled.png', 200), ('32_scaled.png', 200), ('33_scaled.png', 200)])
self.PlayerAttackAnim = pyganim.PygAnimation([('15_scaled.png', 150), ('16_scaled.png', 150), ('17_scaled.png', 150), ('18_scaled.png', 150), ('19_scaled.png', 150), ('20_scaled.png', 150), ('21_scaled.png', 150), ('22_scaled.png', 150)], loop=False)
self.PlayerAttackAnimFlipped = self.PlayerAttackAnim.getCopy()
self.PlayerAttackAnimFlipped.flip(True, False)
updateDisplay()
def keyHandling(self):
global rightPressed
global leftPressed
global facing
global idlePaused
global runStopped
global isAttacking
leftPressed = False
rightPressed = False
if keyPressed(K_j) and not isAttacking:
isAttacking = True
if runStopped == False:
self.PlayerRunAnim.stop()
if idlePaused == False:
idlePaused = True
self.PlayerIdleAnim.pause()
self.PlayerIdleAnimFlipped.pause()
self.PlayerAttackAnim.play()
self.PlayerAttackAnimFlipped.play()
elif keyPressed(K_a):
leftPressed = True
elif keyPressed(K_d):
rightPressed = True
elif not keyPressed(K_a):
leftPressed = False
elif not keyPressed(K_d):
rightPressed = False
if self.PlayerAttackAnim.isFinished() and isAttacking:
isAttacking = False
if isAttacking:
if facing:
screen.blit(self.background_S, (0, 0))
self.PlayerAttackAnim.blit(screen, (self.position.x, height/2))
else:
screen.blit(self.background_S, (0, 0))
self.PlayerAttackAnimFlipped.blit(screen, (self.position.x, height/2))
updateDisplay()
if rightPressed or leftPressed and not isAttacking:
if runStopped == True:
self.PlayerRunAnim.play()
runStopped = False
if idlePaused == False:
idlePaused = True
self.PlayerIdleAnim.pause()
self.PlayerIdleAnimFlipped.pause()
if rightPressed and (self.position.x < width - 200) and not isAttacking:
self.position.x += moveSpeed
screen.blit(self.background_S, (0,0))
if not facing:
self.PlayerRunAnim.flip(True, False)
facing = True
self.PlayerRunAnim.blit(screen, (self.position.x, height/2))
updateDisplay()
elif leftPressed and (self.position.x > 20) and not isAttacking:
self.position.x += -moveSpeed
screen.blit(self.background_S, (0,0))
if facing:
self.PlayerRunAnim.flip(True, False)
facing = False
self.PlayerRunAnim.blit(screen, (self.position.x, height/2))
updateDisplay()
elif not leftPressed and not rightPressed and not isAttacking:
if runStopped == False:
runStopped = True
self.PlayerRunAnim.stop()
if idlePaused:
self.PlayerIdleAnim.play()
self.PlayerIdleAnimFlipped.play()
idlePaused = False
if not facing:
screen.blit(self.background_S, (0,0))
self.PlayerIdleAnimFlipped.blit(screen, (self.position.x, height/2))
else:
screen.blit(self.background_S, (0,0))
self.PlayerIdleAnim.blit(screen, (self.position.x, height/2))
updateDisplay()
game = gameStart()
while not gameQuit:
for event in pygame.event.get():
if event.type == QUIT:
gameQuit = True
elif event.type == KEYDOWN:
if event.key == K_ESCAPE:
gameQuit = True
game.keyHandling()
updateDisplay()
fpsClock.tick(fps)
我为代码块道歉,但所有这些都有必要向您展示最新情况。