2
我想用精灵表在pygame中创建一个自顶向下的RPG。pygame动画精灵表
我希望能够按空间,例如,攻击可能会触发攻击动画,然后回到正常
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)
上面这段代码是什么我有。如果我按空格一旦它会通过动画并停止,但如果我加倍按空间它将循环它,因为它是如何编程的。
想在动画一些帮助,谢谢
非常感谢你。假设我拥有WASD的控件,并尝试通过将状态设置为“左”向左移动来进行移动。如果我处于攻击中间并点击W,它会覆盖攻击并停止动画。在触发另一个事件之前有什么办法让动画完成? –
您可以使用单独的状态变量,例如 ,例如'self.movement'(“站立”,“左侧”,...) 和'self.attacking'(True,False)。 这样,您可以独立于攻击状态更新移动状态 。 对于动画,如果'self.attacking'为True 则计算当前的攻击帧,否则使用适当的帧作为当前移动的 。 – Meyer