2017-02-03 249 views
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) 

上面这段代码是什么我有。如果我按空格一旦它会通过动画并停止,但如果我加倍按空间它将循环它,因为它是如何编程的。

想在动画一些帮助,谢谢

回答

0

问题是由以下调用时空间被挤压的第二时间造成的:

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

这同时设置“上一页”和“现在“国家是”攻击“。 其结果是,当国家在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) 
+0

非常感谢你。假设我拥有WASD的控件,并尝试通过将状态设置为“左”向左移动来进行移动。如果我处于攻击中间并点击W,它会覆盖攻击并停止动画。在触发另一个事件之前有什么办法让动画完成? –

+0

您可以使用单独的状态变量,例如 ,例如'self.movement'(“站立”,“左侧”,...) 和'self.attacking'(True,False)。 这样,您可以独立于攻击状态更新移动状态 。 对于动画,如果'self.attacking'为True 则计算当前的攻击帧,否则使用适当的帧作为当前移动的 。 – Meyer