一、游戏介绍
二、模块
- alien_invasion 主程序
import sys
import pygame
from settings import Settings
from ship import Ship
import game_function as gf
def run_game():
# 初始化游戏并创建一个屏幕对象P208
pygame.init()
ai_settings = Settings()
screen = pygame.display.set_mode((ai_settings.screen_width, ai_settings.screen_height)) # 为什么必须两个括号???
# screen = pygame.display.set_mode((1200,600)),被上面使用类的语句替换
pygame.display.set_caption("Alien Invasion")
# 创建一艘飞船
ship = Ship(ai_settings, screen)
# 设置值背景颜色
# bg_color = (230,230,230)被上面使用类的语句替换
# 开始游戏循环
while True:
gf.check_events(ship) # 见模块game_function
ship.update() # 见模块ship
gf.update_screen(ai_settings, screen, ship) # 见模块game_function
run_game()
- ship.py 定义飞船类
# coding=gbk
import pygame
class Ship:
def __init__(self, ai_settings, screen):
"""初始化飞船并设置其初始位置"""
self.screen = screen
self.ai_settings = ai_settings
# 加载飞船图像并获取其外接矩形
self.image = pygame.image.load('images/ship.png')
self.rect = self.image.get_rect()
self.screen_rect = screen.get_rect()
# 将每艘新飞船放在屏幕底部中央
self.rect.centerx = self.screen_rect.centerx
self.rect.bottom = self.screen_rect.bottom
# 在飞船的属性center中存储小数值
self.centerx = float(self.rect.centerx)
self.centery = float(self.rect.centery)
# 移动标志, 用于判断是否长按
self.moving_right = False
self.moving_left = False
self.moving_up = False
self.moving_down = False
def blitme(self):
"""在指定位置绘制飞船"""
self.screen.blit(self.image, self.rect)
def update(self):
"""根据移动标志调整飞船位置"""
# 更新飞船的center值,而不是rect
if self.moving_right and self.rect.right < self.screen_rect.right: # 限定飞机活动范围(右边缘)
self.centerx += self.ai_settings.ship_speed_factor
# self.rect.centerx += 1 被上面 的语句替代
if self.moving_left and self.rect.left > 0: # 限定飞机活动范围(左边缘)
self.centerx -= self.ai_settings.ship_speed_factor
# self.rect.centerx -= 1 被上面 的语句替代
if self.moving_up and self.rect.top > 0: # 限定飞机活动范围(上边缘)
# self.rect.centery -= 1
self.centery -= self.ai_settings.ship_speed_factor
if self.moving_down and self.rect.bottom < self.screen_rect.bottom: # 限定飞机活动范围(下边缘)
# self.rect.centery += 1
self.centery += self.ai_settings.ship_speed_factor
# 根据self.center更新rect对象
self.rect.centerx = self.centerx
self.rect.centery = self.centery
- settings.py 设定
# coding=gbk
class Settings():
"""存储外星人入侵的所有设置的类"""
def __init__(self):
"""初始化游戏的设置"""
# 屏幕的设置
self.screen_width = 1200
self.screen_height = 600
self.bg_color =(230,230,230)
# 设置飞船速度
self.ship_speed_factor = 1.5
- game_function.py 游戏基本功能
import sys
import pygame
def check_events(ship):
# 监视键盘和鼠标事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN: # 如果有键被按下, (判断的是按下的动作)
if event.key == pygame.K_RIGHT:
ship.moving_right = True # 移动标志, 用于判断键盘长按状态; 长按则继续移动
elif event.key == pygame.K_LEFT:
ship.moving_left = True
elif event.key == pygame.K_UP:
ship.moving_up = True
elif event.key == pygame.K_DOWN:
ship.moving_down = True
# 向右移动飞船
# ship.rect.centerx += 1 使用移动标志后可以不用单独设置
elif event.type == pygame.KEYUP: # 如果有键被松开
if event.key == pygame.K_RIGHT:
ship.moving_right = False # 移动标志, 用于判断键盘长按状态; 松开则停止移动
elif event.key == pygame.K_LEFT:
ship.moving_left = False
elif event.key == pygame.K_UP:
ship.moving_up = False
elif event.key == pygame.K_DOWN:
ship.moving_down = False
def update_screen(ai_settings, screen, ship):
# 每次循环都重绘屏幕
screen.fill(ai_settings.bg_color)
ship.blitme()
# 让最近绘制的屏幕可见
pygame.display.flip()