pygame动画精灵表

时间:2017-02-03 18:27:17

标签: python animation pygame sprite-sheet

我想使用精灵表在pygame中创建一个自上而下的RPG。

我希望能够按空格,例如,攻击会触发攻击动画,然后恢复正常

import pygame
from pygame.locals import *

pygame.init()

image = pygame.image.load("sprite_sheet.png")

clock = pygame.time.Clock()

screen =  pygame.display.set_mode((400, 250))

class Player(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()

        self.current_animation = 0
        self.max_animation = 5

        self.animation_cooldown = 150
        self.last_animation = pygame.time.get_ticks()

        self.status = {"prev": "standing",
                       "now": "standing"}

    def animate_attack(self):
        time_now = pygame.time.get_ticks()

        if time_now - self.last_animation >= self.animation_cooldown:
            self.last_animation = pygame.time.get_ticks()
            if self.current_animation == self.max_animation:
                self.current_animation = 0

                joshua.status["now"] = joshua.status["prev"]
            else:
                self.current_animation += 1


joshua = Player()

while True:
    screen.fill(0)
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                joshua.status["prev"] = joshua.status["now"]
                joshua.status["now"] = "attacking"

    if joshua.status["now"] == "attacking":
        joshua.animate_attack()

    screen.blit(image, (0, 0), (joshua.current_animation * 64, 0, 64, 64))

    pygame.display.flip()

    clock.tick(60)

上面的代码就是我所拥有的。如果我按空格一旦它将通过动画并停止但如果我双按空格它将循环它因为它的编程方式。

想要一些动画方面的帮助,谢谢

1 个答案:

答案 0 :(得分:1)

当第二次按下空格时,问题是由以下调用引起的:

joshua.status["prev"] = joshua.status["now"]

这将设置" prev"和"现在"声明要攻击"。 因此,当以animate_attack()方法重置状态时, 它将保持"攻击":

joshua.status["now"] = joshua.status["prev"]

作为快速修复,请确保仅在状态尚未设置时更改状态:

if event.key == pygame.K_SPACE:
    if not joshua.status["now"] == "attacking":
        joshua.status["prev"] = joshua.status["now"]
        joshua.status["now"] = "attacking"

作为更好的解决方案,您应该封装状态, 所以只有Player类才会处理自己的状态,例如:

class Player():
    def __init__(self):
        self.current_animation = 0
        self.max_animation = 5
        self.animation_cooldown = 150
        self.last_animation = pygame.time.get_ticks()
        self.status = "standing" # Simplified state

    def attack(self):
        self.status = "attacking"

    def animate_attack(self):
        if self.status == "attacking":
            time_now = pygame.time.get_ticks()
            if time_now - self.last_animation >= self.animation_cooldown:
                self.last_animation = pygame.time.get_ticks()
                if self.current_animation == self.max_animation:
                    self.current_animation = 0
                    self.status = "standing" # Reset state
                else:
                    self.current_animation += 1

这样,就不需要了解课外的状态:

while True:
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                joshua.attack()

    joshua.animate_attack()

    screen.fill(0)
    screen.blit(image, (0, 0), (joshua.current_animation * 64, 0, 64, 64))
    pygame.display.flip()
    clock.tick(60)