1

I asked this question earlier but I think I formulated myself wrong, so this time I will add pictures.

I have a game where there is a man (well actually Donald Trump) at the bottom of the screen shooting bullets upwards to the top of the screen at incoming enemies.

He has a gun, at the end of the gunbarrel I am trying to add that a sprite of a flame will appear when I press space, and after 300ms it will disappear (until I press space again and the cycle continues).

Here is a picture of the game and what I mean:

1 = No keys pressed

2 = Space is pressed

3 = Space is no longer pressed and over 300ms has passed, now I want the flame sprite to go away until space is pressed again

1

How can I do this? :)

eyllanesc
  • 235,170
  • 19
  • 170
  • 241

1 Answers1

0

Simply create a variable where you store the timeout value if the key is pressed and substract the time passed each frame from that value.

If that value is > 0, show the image with the fire. If that value is 0, show the image without the fire.

Here's a simple example I hacked together:

import pygame

class Actor(pygame.sprite.Sprite):
    def __init__(self, *grps):
        super().__init__(*grps)
        # create two images:
        # 1 - the no-fire-image
        # 2 - the with-fire-image
        self.original_image = pygame.Surface((100, 200))
        self.original_image.set_colorkey((1,2,3))
        self.original_image.fill((1,2,3))
        self.original_image.subsurface((0, 100, 100, 100)).fill((255, 255, 255))
        self.fire_image = self.original_image.copy()
        pygame.draw.rect(self.fire_image, (255, 0, 0), (20, 0, 60, 100))

        # we'll start with the no-fire-image
        self.image = self.fire_image
        self.rect = self.image.get_rect(center=(300, 240))

        # a field to keep track of our timeout
        self.timeout = 0

    def update(self, events, dt):

        # every frame, substract the amount of time that has passed
        # from your timeout. Should not be less than 0.
        if self.timeout > 0:
            self.timeout = max(self.timeout - dt, 0)

        # if space is pressed, we make sure the timeout is set to 300ms
        pressed = pygame.key.get_pressed()
        if pressed[pygame.K_SPACE]:
            self.timeout = 300

        # as long as 'timeout' is > 0, show the with-fire-image 
        # instead of the default image
        self.image = self.original_image if self.timeout == 0 else self.fire_image

def main():
    pygame.init()
    screen = pygame.display.set_mode((600, 480))
    screen_rect = screen.get_rect()
    clock = pygame.time.Clock()
    dt = 0
    sprites_grp = pygame.sprite.Group()

    # create out sprite
    Actor(sprites_grp)

    # standard pygame mainloop
    while True:
        events = pygame.event.get()
        for e in events:
            if e.type == pygame.QUIT:
                return

        sprites_grp.update(events, dt)

        screen.fill((80, 80, 80))
        sprites_grp.draw(screen)
        pygame.display.flip()
        dt = clock.tick(60)

if __name__ == '__main__':
    main()
sloth
  • 99,095
  • 21
  • 171
  • 219
  • Sorry for not responding sooner but was busy. First of all, thanks! Seems to work like it should. But one thing: I also have it so that if I press left then the image gets flipped horizontally, if I press right then not horizontally. Do I need four different if's since I will have the flame pic here? Here is the original without the flamepic: – NorwegianNoob May 29 '19 at 19:07
  • self.screen.blit(self.image, self.rect) if self.orientation == "Right": self.screen.blit(self.image, self.rect) elif self.orientation == "Left": self.screen.blit(pygame.transform.flip(self.image, True, False), self.rect) – NorwegianNoob May 29 '19 at 19:08
  • So I mean that, do I need something like: If left and timeout > 0: Show flamepic and flip horizontally elif left and timeout == 0: Flip horizontally elif right and timeout > 0: Show flamepic unflipped elif right and timeout == 0: Show original unflipped I guess you know what I mean hehe? :) Edit: Sorry for posting 3 replies, I kept pressing enter by accident – NorwegianNoob May 29 '19 at 19:12
  • Actually never mind, I got it working with the flip of the sprite also :) Thanks again – NorwegianNoob May 29 '19 at 19:34