47 lines
2.6 KiB
Plaintext
47 lines
2.6 KiB
Plaintext
import pygame
|
||
from pygame.sprite import Group #导入pygame模块下sprite子模块中的Group类
|
||
from settings import Settings #导入自定义的Settings类
|
||
from ship import Ship #导入自定义的Ship类
|
||
import game_functions as gf #导入自定义的game_functions类,并起别名为gf
|
||
from game_stats import GameStats #导入自定义的GameStats类
|
||
from button import Button #导入自定义的Button类
|
||
from scoreboard import Scoreboard #导入自定义的Scoreboard类
|
||
|
||
|
||
def run_game():
|
||
# 初始化pygame、设置和屏幕对象
|
||
pygame.init()
|
||
ai_settings = Settings() #实例化Settings类
|
||
screen = pygame.display.set_mode((ai_settings.screen_width, ai_settings.screen_height)) #调用display模块的set_mode()方法设置屏幕大小
|
||
pygame.display.set_caption("Alien Invasion") #调用display模块的set_caption()方法设置标题
|
||
|
||
# 创建Play按钮
|
||
play_button = Button(ai_settings, screen, "Play") #实例化Button类
|
||
|
||
# 创建存储游戏统计信息的实例,并创建记分牌
|
||
stats = GameStats(ai_settings) #实例化GameStats类
|
||
sb = Scoreboard(ai_settings, screen, stats) #实例化Scoreboard类
|
||
|
||
# 创建一艘飞船、一个子弹编组和一个外星人编组
|
||
ship = Ship(ai_settings, screen) #实例化Ship类
|
||
bullets = Group() #实例化Group类
|
||
aliens = Group() #实例化Group类
|
||
|
||
# 创建外星人群
|
||
gf.create_fleet(ai_settings, screen, ship, aliens) #调用gf模块的create_fleet()函数
|
||
|
||
# 开始游戏的主循环
|
||
while True:
|
||
# 监视键盘和鼠标事件
|
||
gf.check_events(ai_settings, screen, stats, sb, play_button, ship, aliens, bullets) #调用gf模块的check_events()函数,监测键盘和鼠标的事件
|
||
|
||
if stats.game_active:
|
||
ship.update() #调用Ship类的update()方法
|
||
gf.update_bullets(ai_settings, screen, stats, sb, ship, aliens, bullets) #调用gf模块的update_bullets函数,更新子弹的状态
|
||
gf.update_aliens(ai_settings, screen, stats, sb, ship, aliens, bullets) #调用gf模块的update_aliens函数,更新外星人的状态
|
||
|
||
gf.update_screen(ai_settings, screen, stats, sb, ship, aliens, bullets, play_button) #调用gf模块的update_screen函数,更新屏幕的状态
|
||
|
||
|
||
run_game() #运行游戏
|