new to python.
I'm making a space invaders game and I want the game to wait a little each time the player gets hit and then revive the player. My problem is that each time the player gets hit, the first few images of the ship's explosion animation gets drawn to the screen before the pause, and after that when the player is revived and the ship appears again the end part of the explosion animation gets displayed.
Here's the part of my code that I think is relevant to the question.
def __init__(self, x, y, size):
pygame.sprite.Sprite.__init__(self)
self.images = []
for num in range(1, 6):
img = pygame.image.load(f"images/exp{num}.png")
if size == 1:
img = pygame.transform.scale(img, (20, 20))
if size == 2:
img = pygame.transform.scale(img, (50, 50))
if size == 3:
img = pygame.transform.scale(img, (160, 160))
# add the image to the list
self.images.append(img)
self.index = 0
self.image = self.images[self.index]
self.rect = self.image.get_rect()
self.rect.center = [x, y]
self.counter = 0
def update(self):
explosion_speed = 4
# Update explosion animation.
self.counter += 1
if self.counter >= explosion_speed and self.index < len(self.images) - 1:
self.counter = 0
self.index += 1
self.image = self.images[self.index]
# If the animation is complete, delete explosion.
if self.index >= len(self.images) - 1 and self.counter >= explosion_speed:
self.kill()
def update_aliens(ai_settings, screen, stats, sb, ship, aliens, bullets, ai_sounds, explosions, play_button):
"""Check if the fleet is at an edge,
and then update the position of all aliens in the fleet."""
check_fleet_edges(ai_settings, aliens)
# Look for alien_ship collisions
if pygame.sprite.spritecollideany(ship, aliens):
explosion = Explosion(ship.rect.centerx, ship.rect.centery, 3)
explosions.add(explosion)
ai_sounds.play_ship_explosion_sound()
update_screen(ai_settings, screen, stats, sb, ship, aliens, bullets,
play_button, explosions)
ship_hit(ai_settings, screen, stats, sb, ship, aliens, bullets, ai_sounds)
# Look for aliens hitting the bottom of the sscreen
check_aliens_bottom(ai_settings, screen, stats, sb, ship, aliens, bullets, ai_sounds)
aliens.update()
def update_screen(ai_settings, screen, stats, sb, ship, aliens, bullets,
play_button, explosions):
"""Update images on the screen and flip to the new screen."""
# Redraw the screen during each pass through the loop
screen.blit(ai_settings.bg_image_done, (0, 0))
# Redraw all bullets behind ship and aliens
for bullet in bullets.sprites():
bullet.draw_bullet()
ship.blitme()
aliens.draw(screen)
explosions.draw(screen)
# Draw the score information
sb.show_score()
# Draw the play button if the game is inactive.
if not stats.game_active:
play_button.draw_button()
# Make most recently drawn screen visible.
pygame.display.flip()
def revive_player(ai_settings, screen, stats, sb, ship, aliens, bullets):
"""If the player has any lives left revive them."""
# Decrement ships_left.
stats.ships_left -= 1
# Update scoreboard(ship images)
sb.prep_ships()
# Empty the list of aliens and bullets.
aliens.empty()
bullets.empty()
# Create a new fleet and center the ship
create_fleet(ai_settings, screen, ship, aliens)
ship.center_ship()
# Pause.
sleep(0.5)
Am I using sleep() wrong? Should I use another function? or something else is the problem?