在代码运行时,如何在Pygame处理后处理rect对象?
我正在用pygame中的角色进行动画,目前,正在发生的动画图像彼此放置,底部的图像仍然可见(它们略微错位)。
这不是我知道很受欢迎的精灵表,因此我在网上收到的指导对我不起作用。每个图像都是独立的。
当我关键时,动画后确实会消失。我只是想让pygame按顺序删除图像,而不是一次处理。
任何建议将不胜感激!
注意:我已经尝试在列表中迭代,其中J1-J8变量是图片列表,并且Pygame要么快速地感知到该运动,或者否则它似乎在全部。
#####示例#######
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_b:
SCREEN.blit(j1, (char_x, char_y))
time.sleep(.1)
pygame.display.update()
SCREEN.blit(j2, (char_x, char_y))
time.sleep(.1)
pygame.display.update()
SCREEN.blit(j7, (char_x, char_y))
time.sleep(.1)
pygame.display.update()
SCREEN.blit(j8, (char_x, char_y))
time.sleep(.1)
pygame.display.update()
I'm animating a character in Pygame, and currently, what is happening is the animating images are being laid on top of each other, with the ones on the bottom still being visible (they are slightly misaligned).
This is not a sprite sheet, which I know are popular, so the guidance I've received online will not work for me. Each image is independent from every other.
When I keyup, the images do disappear after the animation; I'm just trying to have Pygame remove the images in a sequence, as opposed to all at once after all have been processed.
Any advice would be appreciated!
note: I've tried iterating through a list, with the j1-j8 variables being a list of the pictures, and either Pygame was moving to fast for the movement to be perceived, or it otherwise did not seem to work at all.
#####example#######
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_b:
SCREEN.blit(j1, (char_x, char_y))
time.sleep(.1)
pygame.display.update()
SCREEN.blit(j2, (char_x, char_y))
time.sleep(.1)
pygame.display.update()
SCREEN.blit(j7, (char_x, char_y))
time.sleep(.1)
pygame.display.update()
SCREEN.blit(j8, (char_x, char_y))
time.sleep(.1)
pygame.display.update()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一个典型的Pygame主应用程序看起来像:
句柄事件→更新游戏状态→绘制背景→绘制Sprites→
通过重新绘制背景来显示更新,您可以清除先前的图像,以使动画更加干净。
如果您花了太长时间的处理活动,您的窗口管理器会认为您的应用程序已经崩溃。这就是为什么不建议使用
time.sleep()
的原因。如果您的精灵正在动画,那么当满足动画条件时,您将希望它在一组图像中骑自行车。
这是一个带有单个精灵的示例脚本,每两秒钟都会更改其图像。
A typical pygame main application looks something like:
Handle Events → Update Game State → Draw Background → Draw sprites → Update display
By redrawing the background, you clear away previous images so your animation is cleaner.
If you take too long handling events, your window manager will think your application has crashed. This is why calls to
time.sleep()
are discouraged.If your sprite is animating, then you'll want it to be cycling through a set of images when its animation condition is met.
Here's a sample script that has a single sprite that changes its image every two seconds.