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.

46 lines
1.6 KiB

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()
pygame.mixer.init(frequency=22050, size=16, channels=2, buffer=512)
ai_settings = Settings()
screen = pygame.display.set_mode((ai_settings.screen_width, ai_settings.screen_height))
pygame.display.set_caption("Alien Invasion")
pygame.mixer.music.load('bgm/bgm.mp3')
pygame.mixer.music.play(-1)
pygame.image.load("images/bg.jpg").convert()
# 创建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()