Is it possible to show an image for a while and then change it back?

六月ゝ 毕业季﹏ 提交于 2020-01-05 09:34:55

问题


def monkey_bomb(self):
    self.image = pygame.image.load("./images/monkey_bomb.png")
    delay(1000)
    self.image = pygame.image.load("./images/monkey.png")

This is one of the method in my player sprite class
so what I wanted to do is that when the player sprite hits a bomb, it will show a explosion effect, then go back to the normal image for the player sprite
but seems like pygame doesn't support the whole 'delay' thing, how do I do this in another way?


回答1:


You don't want to delay in the middle of the game: that would freeze everything. Instead, you should load both images in advance, then in the situation where you want to switch to another you:

  1. Save the current time, either from e.g. time.time() or a frame counter.
  2. Swap the images. Either literally a, b = b, a or change a flag that says which image to display.
  3. When sufficient time has passed (e.g. time.time() > saved_time + 1), you swap them back.

Where and how you do the last step depends on how the rest of your code is structured.



来源:https://stackoverflow.com/questions/23982287/is-it-possible-to-show-an-image-for-a-while-and-then-change-it-back

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!