框图
主函数
![在这里插入图片描述](https://img-blog.csdnimg.cn/20190307185839478.png)
监视键盘、鼠标
![在这里插入图片描述](https://img-blog.csdnimg.cn/20190307191102268.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQyNzExODE1,size_16,color_FFFFFF,t_70)
子弹检测
![在这里插入图片描述](https://img-blog.csdnimg.cn/20190307192100110.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQyNzExODE1,size_16,color_FFFFFF,t_70)
飞机检测
![在这里插入图片描述](https://img-blog.csdnimg.cn/20190307192718710.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQyNzExODE1,size_16,color_FFFFFF,t_70)
显示
![在这里插入图片描述](https://img-blog.csdnimg.cn/20190307193607153.png)
主程序
import pygame
from settings import Settings
from ship import Ship
import game_functions as gf
from pygame.sprite import Group
from game_stats import GameStats
from button import Button
from scoreboard import Scoreboard
def run_game():
# 初始化背景设置
pygame.init()
ai_settings = Settings()
screen = pygame.display.set_mode((ai_settings.screen_width,ai_settings.screen_height))
# 创建显示窗口
pygame.display.set_caption("Alien Invasion")
# 创建Plya按钮
play_button = Button(ai_settings,screen,"Play")
# 创建一个用于存储游戏统计信息的实例
stats = GameStats(ai_settings)
sb = Scoreboard(ai_settings,screen,stats)
# 创建一艘飞机
ship = Ship(ai_settings,screen)
# 创建一个用于存储子弹的编组
bullets = Group()
aliens = Group()
# 创建外星人群
gf.create_fleet(ai_settings,screen,ship,aliens)
while True:
# 监视键盘和鼠标事件
gf.check_events(ai_settings,screen,stats,sb,play_button,ship,aliens,bullets)
if stats.game_active:
gf.update_bullets(ai_settings,screen,stats,sb,ship,aliens,bullets)
gf.update_aliens(ai_settings,stats,screen,sb,ship,aliens,bullets)
gf.update_screen(ai_settings,screen,stats,sb,ship,aliens,bullets,play_button)
run_game()
功能模块
import sys
import pygame
from time import sleep
from bullet import Bullet
from alien import Alien
def check_keydown_events(event,ai_settings,screen,ship,bullets):
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
elif event.key == pygame.K_SPACE:
# 创建一个子弹
fire_bullet(ai_settings,screen,ship,bullets)
def fire_bullet(ai_settings,screen,ship,bullets):
if len(bullets) < ai_settings.bullets_allowed:
new_bullet = Bullet(ai_settings,screen,ship)
bullets.add(new_bullet)
def check_keyup_events(event,ship):
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 check_events(ai_settings,screen,stats,sb,play_button,ship,aliens,bullets):
for event in pygame.event.get