Sidescroller游戏中的Pyganim帮助

时间:2015-10-11 23:19:54

标签: python loops pygame

我知道如何使用Pyganim,它相当简单。但我的问题在于如何构建我的游戏,它每秒启动pyganim的动画60次,因此它根本不会动画(根据人眼)。关于如何判断动画当前是否正在播放,我需要帮助,然后调用播放或不播放?

我的代码:

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)

1 个答案:

答案 0 :(得分:3)

动画永远不会在技术上运行,因为它总是卡在第一帧上。但是如果你想确定它是否会运行,如果你给它时间切换帧只是在按下一个键让动画运行时激活一个循环。类似......

def anim_test():
    clock = pg.time.Clock()
    while run == true:
        pg.display.update()
        clock.tick(60)