Advertisement
Guest User

Untitled

a guest
Jan 21st, 2020
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. import sys
  2.  
  3. import pygame
  4.  
  5. from settings import Settings
  6.  
  7. class AlienInvasion:
  8. """ Overall class to manage game assets and behavior."""
  9.  
  10. def __init__(self):
  11. """Initialize the game, and create game resources."""
  12. pygame.init()
  13. self.settings = Settings()
  14.  
  15. self.screen = pygame.display.set_mode(
  16. (self.settings.screen_width, self.settings.screen_height))
  17. pygame.display.set_caption("Alien Invasion")
  18.  
  19. self.ship = Ship(self)
  20.  
  21. # set the background color.
  22. self.bg_color = (230, 230, 230)
  23.  
  24. def run_game(self):
  25. """Start the main loop for the game."""
  26. while True:
  27. self._check_events()
  28. self._update_screen()
  29.  
  30. def _check_events(self):
  31. """Respond to a keypresses and mouse events."""
  32. for event in pygame.event.get():
  33. if event.type == pygame.QUIT:
  34. sys.exit()
  35. elif event.type == pygame.KEYDOWN:
  36. if event.key == pygame.K_RIGHT:
  37. # Move the ship to the right.
  38. self.ship_rect.x += 1
  39.  
  40. def _update_screen(self):
  41. """Updates imgaes on the screen, and flip to the new screen."""
  42. self.screen.fill(self.settings.bg_color)
  43. self.ship.blitme()
  44.  
  45. # make the most recently drawn screen visible.
  46. pygame.display.flip()
  47.  
  48. if __name__ == '__main__':
  49. #make a game instance, and run the game
  50. ai = AlienInvasion()
  51. ai.run_game()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement