Is it possible to show an image for a while and th

2019-08-12 00:58发布

问题:

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.



标签: python pygame