def play():
    pygame.init()
    settings = Settings()
    # Create Window
    screen = pygame.display.set_mode(
        (settings.screen_width, settings.screen_height))
    pygame.display.set_caption("Alien Invasion")

    button = Button(screen, "Play")
    stats = GameStats(settings)
    ship = Ship(settings, screen)
    score = Scoreboard(settings, screen, stats)
    lasers = Group()
    aliens = Group()
    gf.create_aliens(settings, screen, ship, aliens)
    # Game loop
    while True:

        gf.check_user(settings, screen, stats, score, button, ship, aliens,
                      lasers)

        if stats.game_active:
            ship.update()
            gf.update_lasers(settings, screen, stats, score, ship, aliens,
                             lasers)
            gf.update_aliens(settings, screen, stats, score, ship, aliens,
                             lasers)
        gf.update_screen(settings, screen, stats, score, ship, aliens, lasers,
                         button)
示例#2
0
def run_game():
    pygame.init()
    # 引入设置类
    settings = Settings()
    # 首先创建一个用于显示的屏幕,这个很重要,后面一直在他上面显示
    screen = pygame.display.set_mode(
        (settings.screen_width, settings.screen_height))
    # 设置标题
    pygame.display.set_caption('大战外星人')
    # 设置背景颜色
    bg_color = settings.bg_color
    # 创建飞船
    ship = Ship(screen, settings)
    # 创建子弹编组
    bullets = Group()
    # 创建一个外星人编组
    aliens = Group()
    # 创建外星人
    gf.create_aliens(settings, screen, aliens)
    #开始游戏的主循环
    while True:
        # 监视鼠标和键盘事件
        gf.check_events(settings, screen, ship, bullets)
        # 根据上面返回的结果,处理飞船
        ship.update()
        # 绘制子弹
        gf.update_bullet(bullets)
        # 外星人移动
        gf.update_aliens(settings, aliens)
        #绘制屏幕
        gf.update_screen(settings, screen, ship, bullets, aliens)
def run_game():
    ai_settings = Settings()
    ai_settings.initilize_dynamic_settings()

    pygame.init()
    screen = pygame.display.set_mode(
        (ai_settings.screen_width, ai_settings.screen_height))
    pygame.display.set_caption('Alien Invasion')

    ship = Ship(ai_settings, screen, 'ship.jpg')
    bullets = Group()
    aliens = Group()
    stats = GameStats(ai_settings)
    play_button = Button(ai_settings, screen, 'Play')
    sb = Scoreboard(ai_settings, screen, stats)
    gf.create_aliens(ai_settings, screen, ship, aliens)

    while True:
        if stats.active:
            ship.update()

            #bullets.update() #polymorphism && list operation!
            gf.update_bullets(ai_settings, stats, screen, ship, bullets,
                              aliens)

            gf.update_aliens(ai_settings, stats, screen, ship, bullets, aliens)
        gf.update_screen(ai_settings, stats, screen, ship, bullets, aliens,
                         play_button, sb)
        gf.check_events(ai_settings, stats, screen, ship, bullets, aliens,
                        play_button)
示例#4
0
def run_game():
    pygame.init()
    ai_setting = Settings()
    screen = pygame.display.set_mode(
        (ai_setting.screen_width, ai_setting.screen_height))
    pygame.display.set_caption("Alien Invation")
    play_button = Button(ai_setting, screen, "Play")

    ship = Ship(ai_setting, screen)
    bullets = Group()
    #alien = Alien(ai_setting, screen)
    aliens = Group()
    gf.create_aliens(ai_setting, screen, aliens, ship)

    state = GameState(ai_setting)
    board = Scoreboard(ai_setting, screen, state)

    while True:
        gf.check_events(ai_setting, screen, ship, bullets, state, play_button,
                        aliens, board)
        if state.game_active:
            ship.update()
            gf.update_bullets(bullets, aliens, screen, ship, ai_setting, board,
                              state)
            gf.update_aliens(aliens, ai_setting, ship, state, screen, bullets,
                             board)
        else:
            aliens.empty()
            gf.create_aliens(ai_setting, screen, aliens, ship)
        gf.update_screen(ai_setting, screen, ship, bullets, aliens, state,
                         play_button, board)
示例#5
0
def run_game():
    # 初始化游戏
    pygame.init()
    ai_setting = Settings()
    screen = pygame.display.set_mode(
        (ai_setting.screen_width, ai_setting.screen_height))
    pygame.display.set_caption("Alien Invasion")
    play_button = Button(ai_setting, screen, "PLAY")
    pygame.display.set_gamma(0, 255, 0)
    ship = Ship(ai_setting, screen)
    stats = GameStats(ai_setting)
    sb = Scoreboard(ai_setting, screen, stats)

    bullets = Group()
    aliens = Group()

    ship_height = ship.rect.height
    # 创建外星人群
    gf.create_aliens(ai_setting, screen, aliens, ship_height)
    while True:
        gf.check_events(ai_setting, screen, stats, play_button, ship, aliens,
                        bullets, sb)
        if stats.game_active:
            ship.update()
            # 子弹管理更新处理
            gf.update_bullets(ai_setting, screen, ship, bullets, aliens, stats,
                              sb)
            # 更新外星人
            gf.update_aliens(ai_setting, screen, ship, stats, aliens, bullets,
                             sb)
        # 刷新窗口
        gf.update_screen(ai_setting, screen, stats, sb, ship, aliens, bullets,
                         play_button)
示例#6
0
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")
    
    # 创建一个用于存储游戏统计信息的实例
    stats = GameStats(ai_settings)

    # 创建一个飞船
    ship = Ship(ai_settings, screen)

    # 创建一个存储子弹的组
    bullets = Group()

    # 创建一个存储外星人的组
    aliens = Group()
    alien = Alien(ai_settings, screen)

    # 创建外星人群
    gf.create_aliens(ai_settings, screen, ship, aliens)


    # 开始游戏的主循环
    while True:
        gf.check_events(ai_settings, screen, ship, bullets)
        if stats.game_active:
            ship.update()
            gf.update_bullets(ai_settings, screen, ship, aliens, bullets)
            gf.update_aliens(ai_settings, stats, screen, ship, aliens, bullets)
    
        gf.update_screen(ai_settings, screen, ship, aliens, bullets)
def run_game():

	pygame.init()

	# Set screen width and height 255
	size = (800,650)
	screen = pygame.display.set_mode(size)
	sky = pygame.image.load('images/sky.png')
	city = City(screen)

	pygame.display.set_caption('Template')

	# Create object
	ai_settings = Settings()
	ship = Ship(screen,ai_settings)
	clouds = Group()
	aliens = Group()
	bullets = Group()

	clock = pygame.time.Clock()

	while True:
		# --- Main event loop

		# --- Game logic


		# --- Clearing the screen
		screen.blit(sky,(0,0))

		# --- Update the game
		gf.check_events(ai_settings,screen,ship,bullets)
		ship.update(ai_settings,screen)

		city.update()
		city.blitme()
		
		gf.create_cloud(screen,clouds)
		gf.update_clouds(screen,clouds)
		clouds.draw(screen)
		
		gf.create_aliens(screen,aliens,city)
		gf.update_aliens(screen,aliens,ship)
		aliens.draw(screen)

		bullets.draw(screen)

		ship.blitme()

		# --- Updating the screen
		pygame.display.flip()


		# --- Limit to 60 FPS
		clock.tick(60)
示例#8
0
def run_game():
    # 初始化pygame、设置和屏幕对象
    pygame.init()
    ai_settings = Settings()
    screen = pygame.display.set_mode(
        (ai_settings.screen_width, ai_settings.screen_height))
    pygame.display.set_caption('Alien Invasion')

    # 创建play按钮
    play_bt = 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_aliens(ai_settings, screen, ship, aliens)

    # 开始游戏的主循环
    while True:

        # 监视键盘和鼠标事件
        gf.check_events(ai_settings, screen, stats, sb, play_bt, ship, aliens,
                        bullets)

        if stats.game_active:
            # 移动飞船
            ship.update()
            # gf.update_bullets(ai_settings, screen, ship, aliens, bullets)
            gf.update_aliens(ai_settings, screen, stats, sb, ship, aliens,
                             bullets)

            # 删除已消失的子弹
            gf.update_bullets(ai_settings, screen, stats, sb, ship, aliens,
                              bullets)

        # 更新屏幕上的图像,并切换到新屏幕
        gf.update_screen(ai_settings, screen, stats, sb, ship, aliens, bullets,
                         play_bt)
示例#9
0
def run_game():
    # 初始化游戏并创建一个屏幕对象
    pygame.init()
    common_setting = Setting()
    screen = pygame.display.set_mode((common_setting.screen_width, common_setting.screen_height))
    pygame.display.set_caption("Alien Invasion")

    ship = Ship(screen, common_setting)
    bullets = Group()
    aliens = Group()
    gf.create_aliens(screen, common_setting, aliens, ship)

    # 开始游戏主循环
    while True:
        gf.check_events(common_setting, screen, ship, bullets)
        ship.update()
        gf.update_bullets(bullets)
        gf.update_aliens(aliens)
        gf.update_screen(common_setting, screen, ship, aliens, bullets)
示例#10
0
def alien():
    #初始化
    pygame.init()

    #进行屏幕的大小和颜色,标题
    pygame.display.set_caption('Alien Invation')
    ai_settings = Setting()

    screen = pygame.display.set_mode(
        (ai_settings.screen_width, ai_settings.screen_height))
    status = GameStatus(ai_settings, game_functions.get_jsonhighscore())

    #创建分数牌
    scores = Score(ai_settings, screen, status)

    bottons = Botton(ai_settings, screen, 'Playing')

    #生成一个飞船,一群子弹和外星人
    ship = Ship(screen, ai_settings)
    bullets = Group()
    aliens = Group()
    #创造一群行星人
    game_functions.create_aliens(aliens, ai_settings, screen, ship)

    while (True):

        #进行响应鼠标和键盘
        game_functions.update_event(aliens, ai_settings, screen, bullets, ship,
                                    status, bottons)
        #键盘控制
        if status.game_active:

            if not status.game_pause:
                #跟新子弹,并删除消失在屏幕的子弹
                game_functions.update_bullets(aliens, ai_settings, screen,
                                              ship, bullets, scores, status)
                game_functions.update_aliens(aliens, ai_settings, ship,
                                             bullets, screen, status, scores)

        #更新屏幕
        game_functions.update_screen(ai_settings, screen, ship, bullets,
                                     aliens, bottons, scores, status)
示例#11
0
def run_game():
    pygame.init()  # 初始化游戏
    pygame.display.set_caption("Alien Invasion")  # 设置标题
    setting = Settings()
    screen = pygame.display.set_mode(
        (setting.screen_width, setting.screen_height))  # 设置屏幕大小,获取screen对象
    ship = Ship(screen)  # 创建一艘飞船
    bullets = Group()  # 创建一个用于存储子弹的编组
    aliens = Group()  # 创建外星人编组
    game_functions.create_aliens(setting, screen, aliens)

    while True:  # 开启游戏主循环
        game_functions.check_event(setting, screen, ship, bullets)  # 监听键盘和鼠标事件
        ship.move()
        game_functions.update_bullet(bullets, aliens)
        game_functions.update_screen(setting, screen, ship, bullets,
                                     aliens)  # 更新屏幕上的图像
        if not aliens:  # 屏幕上没有外星人时,游戏结束
            game_functions.game_success(setting, screen,
                                        screen.get_rect().centerx,
                                        screen.get_rect().centery)
示例#12
0
def main():
    #----------
    #init初始化所有模块,并检查
    pygame.init()
    #----------
    #从配置文件中拿参数
    bj_settings = Settings()
    #----------
    #设置屏幕大小,flags:扩展选项
    screen = pygame.display.set_mode(
        (bj_settings.screen_width, bj_settings.screen_height),
        pygame.RESIZABLE, 32)
    #设置游戏主题
    pygame.display.set_caption('Alien Invasion')
    #设置窗口图标
    icon = pygame.image.load('image/ship.bmp')
    pygame.display.set_icon(icon)
    status = GameStatus(bj_settings)
    sc = Scoreboard(bj_settings, screen, status)
    #-----------
    #创建一个船的对象,调用船的位置方法
    ship = Ship(bj_settings, screen)
    # alien = Alien(screen)
    #-----------
    bullets = Group()
    aliens = Group()
    game_functions.create_aliens(screen, bj_settings, aliens, ship)
    play_button = Button(bj_settings, screen, 'PLAY')
    while True:
        game_functions.check_events(screen, bj_settings, ship, bullets, status,
                                    play_button, aliens, sc)
        game_functions.update_screen(status, screen, bj_settings, ship, aliens,
                                     bullets, play_button, sc)
        if status.game_active:
            ship.continue_update()
            # bullets.update()
            # game_functions.update_bullets(bullets)
            game_functions.update_screen(status, screen, bj_settings, ship,
                                         aliens, bullets, play_button, sc)
示例#13
0
def run_game():

    settings = Settings()
    # 初始化屏幕
    pygame.init()
    screen = pygame.display.set_mode((settings.screen_width, settings.screen_height))
    pygame.display.set_caption('Alien Invasion')

    ship = Ship(screen, settings)
    bullets = Group()
    aliens = Group()
    gf.create_aliens(settings, screen, ship, bullets, aliens)

    #开始游戏主循环
    while True:

        # 监视键盘和鼠标事件
        gf.check_events(settings, screen, ship, bullets)
        gf.update_bullets(settings, screen, ship, bullets, aliens)
        gf.update_aliens(settings, aliens, ship)
        #更新屏幕
        gf.update_screen(settings, screen, ship, bullets, aliens)
示例#14
0
def initiate_game():
    '''set up game unto screen'''
    pygame.init(
    )  #initialises background settings that pygame need to work with
    aa_settings = Settings()
    screen = pygame.display.set_mode(
        (aa_settings.screen_width, aa_settings.screen_height))
    pygame.display.set_caption('ALIEN ASSAULT')

    #draws the ship to screen:
    ship = Ship(aa_settings, screen)  #make an instance first
    bullets = Group()  #makes a group of bullets(instance)
    aliens = Group()  #make a group of aliens(instance)
    #instance of alien:
    create_aliens(aa_settings, screen, ship, aliens)

    #create an instance of game_stats:
    stats = aa_stats(aa_settings)

    #create an instance of scoreboard
    show_scores = scores(aa_settings, screen, stats)

    #displays play button
    play_button = Button(aa_settings, screen, "Play")

    #this  starts the main loop for the game
    while True:
        #observe keyboard and mouse events:
        Check_events(aa_settings, screen, stats, show_scores, play_button,
                     ship, aliens, bullets)
        if stats.game_active:
            ship.update()
            update_bullets(aa_settings, screen, stats, show_scores, ship,
                           aliens, bullets)
            update_aliens(aa_settings, screen, stats, show_scores, ship,
                          aliens, bullets)
        Update_screen(aa_settings, screen, stats, show_scores, ship, aliens,
                      bullets, play_button)
示例#15
0
def main():
    #初始化游戏并创建一个屏幕对象
    pygame.init()
    my_settings = Settings()

    screen = pygame.display.set_mode((my_settings.screen_width, my_settings.screen_height))
    pygame.display.set_caption(my_settings.caption)

    #创建一艘飞船
    my_ship = Ship(my_settings.ship_settings, screen)
    #创建一群外星人
    aliens = Group()
    gf.create_aliens(aliens, my_settings, screen, my_ship)
    #创建存储所有子弹的编组
    bullets = Group()

    #开启屏幕主循环
    while True:
        gf.check_event(my_settings, screen, my_ship, bullets) #监听事件
        my_ship.moving() #更新飞船位置
        #gf.update_aliens(aliens) #更新外星人
        gf.update_bullets(bullets) #更新子弹
        gf.update_screen(my_settings, screen, my_ship, aliens, bullets) #刷新屏幕显示
示例#16
0
def run_game():
    # 初始化游戏并创建一个屏幕对象
    pygame.init()
    pygame.mixer.init()
    pygame.time.delay(1000)  # 等待1秒让mixer完成初始化
    pygame.mixer.music.load("../music/game_music.mp3")
    pygame.mixer.music.play()

    settings = Settings()
    screen = pygame.display.set_mode((settings.screen_width, settings.screen_height))
    pygame.display.set_caption("狙击外星人")
    ship = Ship(screen, settings)
    alien = Alien(screen, settings)
    game_stats = GameStats(settings)
    play_button = Button(settings, screen, 'Play')
    # 创建一个用于存储子弹的编组
    bullets = Group()
    # 创建一个用于存储外星人的编组
    aliens = Group()
    gf.create_aliens(settings, screen, aliens, alien)
    # 创建一个记分牌
    score_board = ScoreBoard(settings, screen, game_stats)

    # 从子弹列表中删除消失的子弹
    # 开始游戏的主旋律
    while 1:
        # 监视用户的键盘和鼠标时间
        gf.check_events(ship, bullets, settings, screen, game_stats, play_button, aliens, alien, score_board)
        # 只有游戏处于激活状态(飞船最大销毁次数不等于0),才会更新各种游戏元素
        if game_stats.game_active:
            ship.update()
            # 更新子弹列表(更新子弹位置和数量)
            gf.update_bullets(bullets, aliens, settings, screen, alien, game_stats, score_board)
            # 更新外星人列表(外星人的位置)
            gf.update_aliens(screen, settings, aliens, ship, game_stats, bullets, alien, score_board)
        # 更新屏幕的渲染
        gf.update_screen(ship, bullets, settings, screen, aliens, game_stats, play_button, score_board)
示例#17
0
def main():
    """Set up main loop for game"""
    pygame.init()
    clock = pygame.time.Clock()
    settings = Settings()
    screen = settings.screen
    ship = Ship(screen)
    aliens = create_aliens(screen)
    projectiles = pygame.sprite.Group()
    barriers = pygame.sprite.Group(Barrier(screen, 1), Barrier(screen, 2),
                                   Barrier(screen, 3), Barrier(screen, 4))
    score = Score()

    while True:
        check_events(screen, ship, projectiles)
        update_objects(ship, projectiles, barriers, aliens, score)
        update_screen(settings, ship, barriers, projectiles, aliens, score)
        clock.tick(60)