Aucune description

game_functions.py 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import sys
  2. from time import sleep
  3. import pygame
  4. from bullet import Bullet
  5. from alien import Alien
  6. def check_keydown_events(event, ai_settings, screen, ship, bullets):
  7. """Respond to keypresses."""
  8. if event.key == pygame.K_RIGHT:
  9. ship.moving_right = True
  10. elif event.key == pygame.K_LEFT:
  11. ship.moving_left = True
  12. elif event.key == pygame.K_SPACE:
  13. fire_bullet(ai_settings, screen, ship, bullets)
  14. elif event.key == pygame.K_q:
  15. sys.exit()
  16. def check_keyup_events(event, ship):
  17. """Respond to key releases."""
  18. if event.key == pygame.K_RIGHT:
  19. ship.moving_right = False
  20. elif event.key == pygame.K_LEFT:
  21. ship.moving_left = False
  22. def check_events(ai_settings, screen, ship, bullets):
  23. """Respond to keypresses and mouse events."""
  24. for event in pygame.event.get():
  25. if event.type == pygame.QUIT:
  26. sys.exit()
  27. elif event.type == pygame.KEYDOWN:
  28. check_keydown_events(event, ai_settings, screen, ship, bullets)
  29. elif event.type == pygame.KEYUP:
  30. check_keyup_events(event, ship)
  31. def fire_bullet(ai_settings, screen, ship, bullets):
  32. """Fire a bullet, if limit not reached yet."""
  33. # Create a new bullet, add to bullets group.
  34. if len(bullets) < ai_settings.bullets_allowed:
  35. new_bullet = Bullet(ai_settings, screen, ship)
  36. bullets.add(new_bullet)
  37. def update_screen(ai_settings, screen, ship, aliens, bullets):
  38. """Update images on the screen, and flip to the new screen."""
  39. # Redraw the screen, each pass through the loop.
  40. screen.fill(ai_settings.bg_color)
  41. # Redraw all bullets, behind ship and aliens.
  42. for bullet in bullets.sprites():
  43. bullet.draw_bullet()
  44. ship.blitme()
  45. aliens.draw(screen)
  46. # Make the most recently drawn screen visible.
  47. pygame.display.flip()
  48. def update_bullets(ai_settings, screen, ship, aliens, bullets):
  49. """Update position of bullets, and get rid of old bullets."""
  50. # Update bullet positions.
  51. bullets.update()
  52. # Get rid of bullets that have disappeared.
  53. for bullet in bullets.copy():
  54. if bullet.rect.bottom <= 0:
  55. bullets.remove(bullet)
  56. check_bullet_alien_collisions(ai_settings, screen, ship, aliens, bullets)
  57. def check_bullet_alien_collisions(ai_settings, screen, ship, aliens, bullets):
  58. """Respond to bullet-alien collisions."""
  59. # Remove any bullets and aliens that have collided.
  60. collisions = pygame.sprite.groupcollide(bullets, aliens, True, True)
  61. if len(aliens) == 0:
  62. # Destroy existing bullets, and create new fleet.
  63. bullets.empty()
  64. create_fleet(ai_settings, screen, ship, aliens)
  65. def check_fleet_edges(ai_settings, aliens):
  66. """Respond appropriately if any aliens have reached an edge."""
  67. for alien in aliens.sprites():
  68. if alien.check_edges():
  69. change_fleet_direction(ai_settings, aliens)
  70. break
  71. def change_fleet_direction(ai_settings, aliens):
  72. """Drop the entire fleet, and change the fleet's direction."""
  73. for alien in aliens.sprites():
  74. alien.rect.y += ai_settings.fleet_drop_speed
  75. ai_settings.fleet_direction *= -1
  76. def ship_hit(ai_settings, stats, screen, ship, aliens, bullets):
  77. """Respond to ship being hit by alien."""
  78. if stats.ships_left > 0:
  79. # Decrement ships_left.
  80. stats.ships_left -= 1
  81. else:
  82. stats.game_active = False
  83. # Empty the list of aliens and bullets.
  84. aliens.empty()
  85. bullets.empty()
  86. # Create a new fleet, and center the ship.
  87. create_fleet(ai_settings, screen, ship, aliens)
  88. ship.center_ship()
  89. # Pause.
  90. sleep(0.5)
  91. def check_aliens_bottom(ai_settings, stats, screen, ship, aliens, bullets):
  92. """Check if any aliens have reached the bottom of the screen."""
  93. screen_rect = screen.get_rect()
  94. for alien in aliens.sprites():
  95. if alien.rect.bottom >= screen_rect.bottom:
  96. # Treat this the same as if the ship got hit.
  97. ship_hit(ai_settings, stats, screen, ship, aliens, bullets)
  98. break
  99. def update_aliens(ai_settings, stats, screen, ship, aliens, bullets):
  100. """
  101. Check if the fleet is at an edge,
  102. then update the postions of all aliens in the fleet.
  103. """
  104. check_fleet_edges(ai_settings, aliens)
  105. aliens.update()
  106. # Look for alien-ship collisions.
  107. if pygame.sprite.spritecollideany(ship, aliens):
  108. ship_hit(ai_settings, stats, screen, ship, aliens, bullets)
  109. # Look for aliens hitting the bottom of the screen.
  110. check_aliens_bottom(ai_settings, stats, screen, ship, aliens, bullets)
  111. def get_number_aliens_x(ai_settings, alien_width):
  112. """Determine the number of aliens that fit in a row."""
  113. available_space_x = ai_settings.screen_width - 2 * alien_width
  114. number_aliens_x = int(available_space_x / (2 * alien_width))
  115. return number_aliens_x
  116. def get_number_rows(ai_settings, ship_height, alien_height):
  117. """Determine the number of rows of aliens that fit on the screen."""
  118. available_space_y = (ai_settings.screen_height -
  119. (3 * alien_height) - ship_height)
  120. number_rows = int(available_space_y / (2 * alien_height))
  121. return number_rows
  122. def create_alien(ai_settings, screen, aliens, alien_number, row_number):
  123. """Create an alien, and place it in the row."""
  124. alien = Alien(ai_settings, screen)
  125. alien_width = alien.rect.width
  126. alien.x = alien_width + 2 * alien_width * alien_number
  127. alien.rect.x = alien.x
  128. alien.rect.y = alien.rect.height + 2 * alien.rect.height * row_number
  129. aliens.add(alien)
  130. def create_fleet(ai_settings, screen, ship, aliens):
  131. """Create a full fleet of aliens."""
  132. # Create an alien, and find number of aliens in a row.
  133. alien = Alien(ai_settings, screen)
  134. number_aliens_x = get_number_aliens_x(ai_settings, alien.rect.width)
  135. number_rows = get_number_rows(ai_settings, ship.rect.height,
  136. alien.rect.height)
  137. # Create the fleet of aliens.
  138. for row_number in range(number_rows):
  139. for alien_number in range(number_aliens_x):
  140. create_alien(ai_settings, screen, aliens, alien_number,
  141. row_number)