第五部分4

This commit is contained in:
206530327 2021-06-18 18:26:19 +08:00
parent 6bb988160f
commit 4f1ed48754
1 changed files with 49 additions and 0 deletions

View File

@ -152,3 +152,52 @@ def change_fleet_direction(ai_settings, aliens):
alien.rect.y += ai_settings.fleet_drop_speed
ai_settings.fleet_direction *= -1
def ship_hit(ai_settings, screen, stats, sb, ship, aliens, bullets):
"""Respond to ship being hit by alien."""
if stats.ships_left > 0:
# Decrement ships_left.
stats.ships_left -= 1
# Update scoreboard.
sb.prep_ships()
else:
stats.game_active = False
pygame.mouse.set_visible(True)
# 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)
def check_aliens_bottom(ai_settings, screen, stats, sb, ship, aliens,
bullets):
"""Check if any aliens have reached the bottom of the screen."""
screen_rect = screen.get_rect()
for alien in aliens.sprites():
if alien.rect.bottom >= screen_rect.bottom:
# Treat this the same as if the ship got hit.
ship_hit(ai_settings, screen, stats, sb, ship, aliens, bullets)
break
def update_aliens(ai_settings, screen, stats, sb, ship, aliens, bullets):
"""
Check if the fleet is at an edge,
then update the postions of all aliens in the fleet.
"""
check_fleet_edges(ai_settings, aliens)
aliens.update()
# Look for alien-ship collisions.
if pygame.sprite.spritecollideany(ship, aliens):
ship_hit(ai_settings, screen, stats, sb, ship, aliens, bullets)
# Look for aliens hitting the bottom of the screen.
check_aliens_bottom(ai_settings, screen, stats, sb, ship, aliens, bullets)