You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
1.9 KiB

6 months ago
import pygame
from ship import Ship
from settings import Settings
from pygame.sprite import Group
import game_functions as gf
from game_stats import GameStats
from button import Button
from scoreboard import Scoreboard
def run_game():
# 初始化pygame、设置和屏幕对象
pygame.init()
6 months ago
# 设置声音模块的初始化参数:这里设定了声音的频率、大小、通道和缓冲区。
6 months ago
pygame.mixer.init(frequency=22050, size=16, channels=2, buffer=512)
6 months ago
# 创建一个Settings类的实例ai_settings用于存储游戏的各种设置
6 months ago
ai_settings = Settings()
6 months ago
# 设置游戏窗口大小以及初始化屏幕对象
6 months ago
screen = pygame.display.set_mode((ai_settings.screen_width, ai_settings.screen_height))
pygame.display.set_caption("Alien Invasion")
6 months ago
# 加载并循环播放背景音乐
pygame.mixer.music.load('bgm/bgm.ogg')
6 months ago
pygame.mixer.music.play(-1)
# 创建Play按钮
play_botton = Button(ai_settings, screen, "Play")
# 创造一艘飞船
ship = Ship(ai_settings, screen)
# 创建一个用于存储子弹的编组
bullets = Group()
# 创建一个用于存储外星人的编组
aliens = Group()
# 创建一个用于存储游戏统计信息的实例,并创建记分牌
stats = GameStats(ai_settings)
sb = Scoreboard(ai_settings, screen, stats)
# 创建外星人人群
gf.create_fleet(ai_settings, screen, ship, aliens)
# 开始游戏主循环
while True:
gf.check_events(ai_settings, screen, stats, sb, play_botton, ship, aliens, bullets)
if stats.game_active:
ship.update()
gf.update_bullets(ai_settings, screen, stats, sb, ship, aliens, bullets)
gf.update_aliens(ai_settings, screen, stats, sb, ship, aliens, bullets)
gf.update_screen(ai_settings, screen, stats, sb, ship, aliens, bullets, play_botton)
# run_game()