我正试图在pygame中制作一把剑“摆动”的动画。我或多或少都在使用这样的方法
def animate(self):
self.image1 = pg.Surface((40, 30))
self.image1.fill(255,0,0)
self.image2 = pg.Surface((60, 20))
self.image2.fill(0,0,255)
self.image = self.image1
#not the real images, but you get the point
now = pg.time.get_ticks()
keys = pg.key.get_pressed()
if keys[pg.K_l] or keys[pg.K_j]:
self.slashing = True
else:
self.slashing = False
#player image starts out as self.image1
if self.slashing:
self.image = self.image2
if now - self.last_slash > 750:
self.last_slash = now
self.image = self.image1
它可以切换到图像二,但我的计时器并没有像我希望的那样返回图像一。它只是停留在图像2中
如果您只想切换两个图像,您可以这样做:当用户斜杠时,将
self.image
设置为第二个剑图像,当时间结束或用户释放键时,将self.image
重置为第一个剑图像。我认为在这种情况下,使用事件循环并在类中处理事件更容易相关问题 更多 >
编程相关推荐