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)
pygame.time.set_timer()
to cause a designatedpygame.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