新建一个alien_invasion文件夹

Installing Pygame

  1. $ python -m pip install --user pygame
  • 在mac上如果报错去掉—user
  • 如果装了多个python版本,python改为python3

    主文件和settings

    把和设置相关的都放在settings.py里作为module引入

    1. class Settings:
    2. """A class to store all settings for Alien Invasion."""
    3. def __init__(self):
    4. """Initialize the game's settings."""
    5. # Screen settings
    6. self.screen_width = 1200
    7. self.screen_height = 800
    8. self.bg_color = (230, 230, 230)

    ```python import sys import pygame

from settings import Settings

class AlienInvasion: “””Overall class to manage game assets and behavior.”””

  1. def __init__(self):
  2. """Initialize the game, and create game resources."""
  3. pygame.init()
  4. self.settings = Settings()
  5. self.screen = pygame.display.set_mode(
  6. (self.settings.screen_width, self.settings.screen_height))
  7. pygame.display.set_caption("Alien Invasion")
  8. def run_game(self):
  9. """Start the main loop for the game."""
  10. while True:
  11. # Watch for keyboard and mouse events.
  12. for event in pygame.event.get():
  13. if event.type == pygame.QUIT:
  14. sys.exit()
  15. # Redraw the screen during each pass through the loop.
  16. self.screen.fill(self.settings.bg_color)
  17. # Make the most recently drawn screen visible.
  18. pygame.display.flip()

if name == ‘main‘:

  1. # Make a game instance, and run the game.
  2. ai = AlienInvasion()
  3. ai.run_game()
  1. <a name="WmSY5"></a>
  2. ### 画飞船
  3. pygame默认支持.bmp格式的图片<br />把图片放在images文件夹<br />图片素材网站:[https://pixabay.com/](https://pixabay.com/)<br />创建ship.py
  4. ```python
  5. import pygame
  6. class Ship:
  7. """A class to manage the ship."""
  8. def __init__(self, ai_game):
  9. """Initialize the ship and set its starting position."""
  10. self.screen = ai_game.screen
  11. self.screen_rect = ai_game.screen.get_rect()
  12. # Load the ship image and get its rect.
  13. self.image = pygame.image.load('images/ship.bmp')
  14. self.rect = self.image.get_rect()
  15. # Start each new ship at the bottom center of the screen.
  16. self.rect.midbottom = self.screen_rect.midbottom
  17. def blitme(self):
  18. """Draw the ship at its current location."""
  19. self.screen.blit(self.image, self.rect)

get_rect()可以获取尺寸
pygame把每个物体都看成是长方形
画出飞船

  1. --snip--
  2. from settings import Settings
  3. from ship import Ship
  4. class AlienInvasion:
  5. """Overall class to manage game assets and behavior."""
  6. def __init__(self):
  7. --snip--
  8. pygame.display.set_caption("Alien Invasion")
  9. self.ship = Ship(self)
  10. def run_game(self):
  11. --snip--
  12. # Redraw the screen during each pass through the loop.
  13. self.screen.fill(self.settings.bg_color)
  14. self.ship.blitme()
  15. # Make the most recently drawn screen visible.
  16. pygame.display.flip()
  17. --snip--

重构Refactoring: The _check_events() and _update_screen()

Methods 在Python, 单下划线开头的代表helper method
将check events 和update screen声明为独立的method, 在class内用self.调用自己的method

  1. --snip--
  2. class AlienInvasion:
  3. """Overall class to manage game assets and behavior."""
  4. --snip--
  5. def run_game(self):
  6. """Start the main loop for the game."""
  7. while True:
  8. self._check_events()
  9. self._update_screen()
  10. def _check_events(self):
  11. """Respond to kekypresses and mouse events."""
  12. for event in pygame.event.get():
  13. if event.type == pygame.QUIT:
  14. sys.exit()
  15. def _update_screen(self):
  16. """Update images on the screen, and flip to the new screen."""
  17. self.screen.fill(self.settings.bg_color)
  18. self.ship.blitme()
  19. pygame.display.flip()
  20. --snip--