0
votes

Okay, so I have a game made with pygame. The game is that there is a man at the bottom of the screen shooting up upwards the top of the screen to hit enemies. The man can move left and right.

To shoot you press space.

When you shoot I want a small sprite to appear at the end of the gun-barrel so it looks as if flames comes out like a real gun shooting.

I have managed to get the fire sprite to appear when pressing space BUT I also want the sprite to disappear after maybe 0.3 seconds (Ill adjust the time once I get it to disappear).

How do I actually do this? I feel kind of lost here

I have tried to google and look and look here at stackoverflow but I cant find what I need.

game.py

# -- SNIP --


def run_game():

    # -- SNIP ---

    # Start the main loop for the game.
    while True:
        # Watch for keyboard and mouse events.
        gf.check_events(ai_settings, screen, stats, sb, play_button, man, 
        enemies, bullets)

        if stats.game_active:
            man.update()
            gf.update_bullets(ai_settings, screen, stats, sb, man, enemies,
                              bullets)
            gf.update_enemies(ai_settings, stats, screen, sb, man, enemies,
                             bullets)

        gf.update_screen(ai_settings, screen, stats, sb, man, enemies, 
        bullets, play_button)


run_game()

game_functions.py


# -- SNIP --


def check_events(ai_settings, screen, stats, sb, play_button, man, enemies,
                 bullets):
    """Respond to keypresses and mouse events."""
    # -- SNIP --
        elif event.type == pygame.KEYDOWN:
            check_keydown_events(event, ai_settings, screen, stats, sb, man,
                                 enemies, bullets)
        # -- SNIP --



def check_keydown_events(event, ai_settings, screen, stats, sb, man, enemies, bullets):
    """Respond to key presses."""
    if event.key == pygame.K_RIGHT:
        man.moving_right = True
        man.orientation = "Right"
    elif event.key == pygame.K_LEFT:
        man.moving_left = True
        man.orientation = "Left"

    # Draw sprite if space is pressed
    elif event.key == pygame.K_SPACE:
        fire_bullet(ai_settings, bullets, screen, man)
        man.fire_screen.blit(man.fire, man.fire_rect)
    # --- SNIP ---

# -- SNIP --

man.py

class Man(Sprite):
    # -- SNIP --
        # Load the man image and get its rect.
        self.image = pygame.image.load('images/man_gun_large.bmp')
        self.rect = self.image.get_rect()
        self.screen_rect = screen.get_rect()

        # Gun fire sprite
        self.fire = pygame.image.load('images/fire.bmp')
        self.fire_rect = self.fire.get_rect()
        self.screen_fire_rect = self.image.get_rect()
        self.fire_screen = self.image

        self.ai_settings = ai_settings

        # Start each new man at the bottom center of the screen.
        self.rect.centerx = self.screen_rect.centerx
        self.rect.bottom = self.screen_rect.bottom

        # Load the fire sprite at the end of the mans gunbarrel
        self.fire_rect.centerx = self.screen_fire_rect.left + 20
        self.fire_rect.top = self.screen_fire_rect.top

# ---SNIP---

        # Movement flags
        self.moving_right = False
        self.moving_left = False

        self.orientation = False


    def blitme(self):
        """Draw the man at its current location."""
        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)
1
Do you want to shoot multiple bullets at once, which should be "disappear" each after a certain time?Rabbid76
I have it so I can shoot 3 bullets "at once" now. I can shoot 3 bullets, when one of the bullets hits an enemy or leaves the screen then I can shoot the next bullet, so long story short, I can only have 3 bullets on the screen at the same time.NorwegianNoob
But what I want is a sprite being shown for X ammount of time when I press spaceNorwegianNoob
Seems like really good way would be to use pygame.time.set_timer() to cause a designated pygame.USEREVENT to be generated after the desired delay that indicated the time period had elapsed. You could then check for this event in your main event-processing loop and make the sprite disappear.martineau

1 Answers

1
votes

Use pygame.time.get_ticks() to manage the life cycle of the bullets.

Add a list, which contains tuples of the end of life time of a bullet and the bullet itself.

bullets = []

When a bullet spawns, then calculate the end time and append the information to the list:

def check_keydown_events(event, ai_settings, screen, stats, sb, man, enemies, bullets):

    # [...]

    # Draw sprite if space is pressed
    elif event.key == pygame.K_SPACE:

        bullet = # [...]

        end_time = pygame.time.get_ticks() + 300 # 300 millisconds = 0.3 seconds
        bullets.append( (end_time, bullet) )

Check if a bullet has reached the end of life and decide to keep the bullet or to .kill it:

current_time = pygame.time.get_ticks()
current_bullets = bullets
bullets = []
for end_time, bullet in current_bullets:
    if current_time > end_time:
        bullet.kill()
    else:
        bullets.append( (end_time, bullet) )