我知道如何使用Pyganim,这非常简单。但是我的问题在于我如何构建游戏,它以每秒60次的速度启动pyganim的动画,因此它根本没有动画(根据人眼)。我需要有关如何确定动画当前是否正在播放,然后再调用该播放是否需要帮助的信息?
我的代码:
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, (0,0))
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', 1), ('3_scaled.png', 1), ('4_scaled.png', 1), ('5_scaled.png', 1), ('6_scaled.png', 1)])
updateDisplay()
def move(self):
global rightPressed
global leftPressed
global facing
global frame
leftPressed = False
rightPressed = False
if 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 rightPressed and (self.position.x < width - 200):
self.position.x += moveSpeed
screen.blit(self.background_S, (0,0))
self.PlayerIdleAnim.stop()
screen.blit(self.player, (self.position.x, height/2))
if not facing:
self.player = pygame.transform.flip(self.player, True, False)
facing = True
updateDisplay()
elif leftPressed and (self.position.x > 20):
self.position.x += -moveSpeed
screen.blit(self.background_S, (0,0))
self.PlayerIdleAnim.stop()
if facing:
self.player = pygame.transform.flip(self.player, True, False)
facing = False
screen.blit(self.player, (self.position.x, height/2))
updateDisplay()
elif not leftPressed and not rightPressed:
if not facing:
self.PlayerIdleAnim.flip(True, False)
self.PlayerIdleAnim.play()
else:
self.PlayerIdleAnim.play()
"""elif rightPressed and not (self.position.x < width - 200):
rightPressed = False
elif leftPressed and not (self.position.x > 200):
leftPressed = False"""
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.move()
updateDisplay()
fpsClock.tick(fps)
最佳答案
动画从技术上讲永远不会运行,因为它总是卡在第一帧上。但是,如果您确实想知道它是否可以运行,如果您给它时间来切换帧,则仅在按下使动画运行的按键时激活循环。就像是 ...
def anim_test():
clock = pg.time.Clock()
while run == true:
pg.display.update()
clock.tick(60)
关于python - Pyganim在Sidescroller游戏中的帮助,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33071152/