【发布时间】:2020-10-03 20:47:15
【问题描述】:
所以我试图在 pygame 中通过在他走路时在 2 张图片之间切换来“动画化”我的角色。我尝试使用这里提到的代码:In PyGame, how to move an image every 3 seconds without using the sleep function?,但结果不太好。事实上,我的角色在行走时只使用一张图片。这里是部分代码和一些变量:
- self.xchange:x 轴变化
- self.img:角色静止时的图像
- self.walk1 和 self.walk2:我尝试使用的两个图像 动画我的角色
- self.x 和 self.y 是坐标 屏幕就是表面
.
def draw(self):
self.clock = time.time()
if self.xchange != 0:
if time.time() <= self.clock + 0.25:
screen.blit(self.walk1, (self.x, self.y))
elif time.time() > self.clock + 0.25:
screen.blit(self.walk2, (self.x, self.y))
if time.time() > self.clock + 0.50:
self.clock = time.time()
else:
screen.blit(self.img, (self.x, self.y))
为什么它不起作用?
【问题讨论】:
-
函数的第一行每次都会重置时钟。尝试删除该行。
-
谢谢!我把这条线移到了我班级的 init 部分,谢谢!
-
您可能会发现使用
pygame.time.get_ticks()比使用time.time()更容易/更好,因为前者为您提供整数毫秒计数。
标签: python time pygame pygame-clock pygame-tick