Installing Pygame
$ python -m pip install --user pygame
- 在mac上如果报错去掉—user
如果装了多个python版本,python改为python3
主文件和settings
把和设置相关的都放在settings.py里作为module引入
class Settings:
"""A class to store all settings for Alien Invasion."""
def __init__(self):
"""Initialize the game's settings."""
# Screen settings
self.screen_width = 1200
self.screen_height = 800
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.”””
def __init__(self):
"""Initialize the game, and create game resources."""
pygame.init()
self.settings = Settings()
self.screen = pygame.display.set_mode(
(self.settings.screen_width, self.settings.screen_height))
pygame.display.set_caption("Alien Invasion")
def run_game(self):
"""Start the main loop for the game."""
while True:
# Watch for keyboard and mouse events.
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
# Redraw the screen during each pass through the loop.
self.screen.fill(self.settings.bg_color)
# Make the most recently drawn screen visible.
pygame.display.flip()
if name == ‘main‘:
# Make a game instance, and run the game.
ai = AlienInvasion()
ai.run_game()
<a name="WmSY5"></a>
### 画飞船
pygame默认支持.bmp格式的图片<br />把图片放在images文件夹<br />图片素材网站:[https://pixabay.com/](https://pixabay.com/)<br />创建ship.py
```python
import pygame
class Ship:
"""A class to manage the ship."""
def __init__(self, ai_game):
"""Initialize the ship and set its starting position."""
self.screen = ai_game.screen
self.screen_rect = ai_game.screen.get_rect()
# Load the ship image and get its rect.
self.image = pygame.image.load('images/ship.bmp')
self.rect = self.image.get_rect()
# Start each new ship at the bottom center of the screen.
self.rect.midbottom = self.screen_rect.midbottom
def blitme(self):
"""Draw the ship at its current location."""
self.screen.blit(self.image, self.rect)
get_rect()可以获取尺寸
pygame把每个物体都看成是长方形
画出飞船
--snip--
from settings import Settings
from ship import Ship
class AlienInvasion:
"""Overall class to manage game assets and behavior."""
def __init__(self):
--snip--
pygame.display.set_caption("Alien Invasion")
self.ship = Ship(self)
def run_game(self):
--snip--
# Redraw the screen during each pass through the loop.
self.screen.fill(self.settings.bg_color)
self.ship.blitme()
# Make the most recently drawn screen visible.
pygame.display.flip()
--snip--
重构Refactoring: The _check_events() and _update_screen()
Methods
在Python, 单下划线开头的代表helper method
将check events 和update screen声明为独立的method, 在class内用self.调用自己的method
--snip--
class AlienInvasion:
"""Overall class to manage game assets and behavior."""
--snip--
def run_game(self):
"""Start the main loop for the game."""
while True:
self._check_events()
self._update_screen()
def _check_events(self):
"""Respond to kekypresses and mouse events."""
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
def _update_screen(self):
"""Update images on the screen, and flip to the new screen."""
self.screen.fill(self.settings.bg_color)
self.ship.blitme()
pygame.display.flip()
--snip--