我想使用精灵表在 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)
上面的代码是我所拥有的。如果我按一次空格键,它会遍历动画并停止,但如果我按两次空格键,它会循环播放,因为它是如何编程的。
需要一些动画方面的帮助,谢谢
正确答案
问题是由第二次按下空格时的以下调用引起的:
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)
以上是pygame动画精灵表的详细内容。更多信息请关注PHP中文网其他相关文章!

Python在游戏和GUI开发中表现出色。1)游戏开发使用Pygame,提供绘图、音频等功能,适合创建2D游戏。2)GUI开发可选择Tkinter或PyQt,Tkinter简单易用,PyQt功能丰富,适合专业开发。

Python适合数据科学、Web开发和自动化任务,而C 适用于系统编程、游戏开发和嵌入式系统。 Python以简洁和强大的生态系统着称,C 则以高性能和底层控制能力闻名。

2小时内可以学会Python的基本编程概念和技能。1.学习变量和数据类型,2.掌握控制流(条件语句和循环),3.理解函数的定义和使用,4.通过简单示例和代码片段快速上手Python编程。

Python在web开发、数据科学、机器学习、自动化和脚本编写等领域有广泛应用。1)在web开发中,Django和Flask框架简化了开发过程。2)数据科学和机器学习领域,NumPy、Pandas、Scikit-learn和TensorFlow库提供了强大支持。3)自动化和脚本编写方面,Python适用于自动化测试和系统管理等任务。

两小时内可以学到Python的基础知识。1.学习变量和数据类型,2.掌握控制结构如if语句和循环,3.了解函数的定义和使用。这些将帮助你开始编写简单的Python程序。

如何在10小时内教计算机小白编程基础?如果你只有10个小时来教计算机小白一些编程知识,你会选择教些什么�...

使用FiddlerEverywhere进行中间人读取时如何避免被检测到当你使用FiddlerEverywhere...

Python3.6环境下加载Pickle文件报错:ModuleNotFoundError:Nomodulenamed...


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

安全考试浏览器
Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

螳螂BT
Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。

SublimeText3 英文版
推荐:为Win版本,支持代码提示!

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)