Python Pyganim动画错误
我有一只很奇怪的虫子,我查不清它到底是怎么回事。当我玩我作为一个项目创建的游戏时,我按下攻击按钮J,角色就会像预期的那样播放攻击动画。他用正确的方法做了1-5次。然而,一旦他做了(我不确定它是如何决定它要显示多少次),它将不再播放动画。我非常感谢任何人在这方面的帮助。有关Pyganim的参考信息,请查看 这是我的密码:Python Pyganim动画错误,python,pygame,Python,Pygame,我有一只很奇怪的虫子,我查不清它到底是怎么回事。当我玩我作为一个项目创建的游戏时,我按下攻击按钮J,角色就会像预期的那样播放攻击动画。他用正确的方法做了1-5次。然而,一旦他做了(我不确定它是如何决定它要显示多少次),它将不再播放动画。我非常感谢任何人在这方面的帮助。有关Pyganim的参考信息,请查看 这是我的密码: #Setup movement keys rightPressed = False leftPressed = False facing = True #True if faci
#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)
#设置移动键
rightspressed=False
leftPressed=False
朝向=真#向右为真,向左为假
级别=1
idlePaused=False#检查Pyganim是否暂停动画
runStopped=True#检查正在运行的字符是否已停止
isAttacking=False#如果攻击,则不会播放其他动画
def updateDisplay():
pygame.display.flip()
按def键(输入键):
keysPressed=pygame.key.get_pressed()
如果按下[inputKey]键:
返回真值
其他:
返回错误
类gameStart(pygame.sprite.sprite):
定义初始化(自):
pygame.sprite.sprite.\uuuuu init\uuuuuuu(自我)
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))#缩放背景以匹配屏幕分辨率
屏幕blit(self.background_S,(0,0))
屏幕blit(self.player,(0,高度/2))
blit(self.icon,((width+(self.icon.get_width()),(height+(self.icon.get_height()))
self.position=self.player.get_rect()
自我健康=100
#设置玩家空闲、攻击、攻击2和死亡动画
self.PlayerIdleAnim=pyganim.pyganization([('2_scaled.png',500),('3_scaled.png',500),('4_scaled.png',500),('5_scaled.png',500),('6_scaled.png',500)])
self.playeridleanimfripped=self.PlayerIdleAnim.getCopy()
self.PlayerIdleAnimFlipped.flip(真、假)
self.playeridelanimpripped.play()
self.PlayerIdleAnim.play()
self.PlayerRunAnim=pyganim.pyganim([('25_scaled.png',200),('26_scaled.png',200),('27_scaled.png',200),('28_scaled.png',200),('30_scaled.png',200),('32_scaled.png',200),('33_scaled.png',200)])
self.PlayerAttackAnim=pyganim.pyganim([('15_scaled.png',150),('16_scaled.png',150),('17_scaled.png',150),('18_scaled.png',150),('20_scaled.png',150),('21_scaled.png',150),('22_scaled.png',150)],循环=False)
self.playerattackanimfiled=self.PlayerAttackAnim.getCopy()
self.player.flip.flip(真、假)
updateDisplay()
def密钥处理(自):
全球右翼
全局左压
面向全球
全局闲置
全局运行停止
全球攻击
leftPressed=False
rightspressed=False
如果按下(K_j)键但未连接:
isAttacking=True
如果runStopped==False:
self.playerrunamim.stop()
如果idlePaused==False:
idlepause=True
self.PlayerIdleAnim.pause()
self.PlayerIdleAnimFlipped.pause()
self.PlayerAttackAnim.play()
self.playerat.fliped.play()
按下elif键(K_a):
leftPressed=True
按下elif键(K_d):
rightspressed=True
如果未按下键(K_a):
leftPressed=False
如果未按下键(K_d):
rightspressed=False
如果self.PlayerAttackAnim.isFinished()和isAttacking:
isAttacking=False
如果正在安装:
如果面向:
屏幕.blit(self.background_S,(0,0))
self.PlayerAttackAnim.blit(屏幕,(self.position.x,高度/2))
其他:
屏幕.blit(self.background_S,(0,0))
self.player.fliped.blit(屏幕,(self.position.x,高度/2))
updateDisplay()
如果右键按下或左键按下但未连接:
如果runStopped==True:
self.playerrunamim.play()
runStopped=False
如果idlePaused==False:
idlepause=True
self.PlayerIdleAnim.pause()
self.PlayerIdleAnimFlipped.pause()
如果右键按下且(自身位置x<宽度-200)未连接:
self.position.x+=移动速度
屏幕blit(self.background_S,(0,0))
如果不面对:
self.PlayerRunAnim.flip(真、假)
正面=正确
self.PlayerRunAnim.blit(屏幕,(self.position.x,高度/2))
updateDisplay()
elif leftPressed and(self.position.x>20)且未连接:
self.position.x+=-移动速度
屏幕blit(self.background_S,(0,0))
如果面向:
self.PlayerRunAnim.flip(真、假)
正面=假
self.PlayerRunAnim.blit(屏幕,(self.position.x,高度/2))
updateDisplay()
elif未按左键、未按右键和未连接:
如果runStopped==False:
runStopped=True
self.playerrunamim.stop()
如果未使用:
self.PlayerIdleAnim.play()
self.playeridelanimpripped.play()
IdlePause=False
如果不面对:
屏幕blit(self.background_S,(0,0))
self.PlayerIdleAnimFlipped.blit(屏幕,(self.position.x,高度/2))
其他:
屏幕blit(self.background_S,(0,0))
self.PlayerIdleAnim.blit(屏幕,(self.position.x,高度/2))
updateDisplay()
游戏