Change the character image for a while in Python Pygame

I am struggling with a problem I can't solve.

I want to change the image of my character when the enemy hurts it.

In pseudocode, it would be like this:

 *If enemy collides -> player close eyes and get red;*

  *After 0.50 seg -> player gets normal back again*

I tried to do it with Clock and Timers but it is very difficult. I only get changing the image, but not getting it back.

Any ideas?

Thank you!

Upvotes: 0

Views: 358

Answers (1)

catsock
catsock

Reputation: 1851

I would assume it's as easy as this. pygame.time.set_timer(x, y) basically creates an x event on the event stack every y milliseconds.

# Main game loop
while True:
    # display stuff and other parts of your game

    # replace this with whatever detection you have for collision
    if enemycollide:
        player.setSprite(1)
        pygame.time.set_timer(14, 500) # 500 milliseconds = .5 seconds

    # event handling
    for event in pygame.event.get():
        if event.type == 14:
            player.setSprite(0)
            pygame.time.set_timer(14, 0) # Disable the timer

Upvotes: 1

Related Questions