python - pygame动画 Sprite 表

标签 python animation pygame sprite-sheet

我想使用 Sprite 表在 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)

上面的代码是我所拥有的。如果我按一下空格键,它会播放动画并停止,但如果我按两次空格键,它会循环播放,因为它是如何编程的。

在动画方面需要一些帮助,谢谢

最佳答案

问题是第二次按下空格时调用了下面的方法:

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

这会将“prev”和“now”状态都设置为“attacking”。 结果,当在 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)

关于python - pygame动画 Sprite 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42030728/

相关文章:

python - Django教程: TypeError at/polls/3/vote/reverse() takes no keyword arguments

c# - 绑定(bind) EasingColorKeyframe 值

python - pygame 移动用户控制的对象直到释放键?

python - 占位符类方法 Python 3

python - 在 PyCharm 中将 python 2 代码转换为 3

python - 循环很难 [Python]

Python Flask - 便捷的查询功能

swift - Swift 中的多 View Controller 导航

jQuery 无法使 .click 处理此代码

python - 为什么显示退出状态 1?