예제 #1
0
def run_game():
    pygame.init()

    # display interface
    ai_settings = Settings()
    screen = pygame.display.set_mode(
        (ai_settings.screen_width, ai_settings.screen_height))
    pygame.display.set_caption('Alien Invasion')
    play_button = Button(ai_settings, screen, 'Play')
    stats = GameStats(ai_settings)
    sb = Scoreboard(ai_settings, screen, stats)

    # Creating game objects
    ship = Ship(ai_settings, screen)
    bullets = Group()
    aliens = Group()
    gf.create_fleet(ai_settings, screen, ship, aliens)

    # cycle of displaying game events
    while True:
        gf.check_events(ai_settings, screen, stats, sb, play_button, ship,
                        aliens, bullets)
        if stats.game_active:
            ship.update()
            gf.upd_bullets(ai_settings, screen, stats, sb, ship, aliens,
                           bullets)
            gf.update_aliens(ai_settings, stats, screen, sb, ship, aliens,
                             bullets)
        gf.upd_screen(ai_settings, screen, stats, sb, ship, aliens, bullets,
                      play_button)
예제 #2
0
def run_game():
    # initialize game
    pygame.mixer.pre_init(22050, -16, 2, 512)
    pygame.mixer.init()
    pygame.init()

    screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
    screen_rect = screen.get_rect()

    # images import
    icon = pygame.image.load("resources/icon.jpeg")
    ship_img = pygame.image.load("resources/ship.bmp")
    logo_img = pygame.image.load("resources/logo.png")
    alien_img = pygame.image.load("resources/alien.bmp")
    back_img = pygame.image.load("resources/background.png")
    backg = pygame.transform.scale(back_img,
                                   (screen_rect.width, screen_rect.height))

    # soundtrack
    pygame.mixer.music.load("resources/Starlight.ogg")
    pygame.mixer.music.play(loops=-1)  # -1 = loop

    # sound effects
    laser_shot = pygame.mixer.Sound("resources/laser-shoot.ogg")
    alien_exp = pygame.mixer.Sound("resources/alien-explosion-1.ogg")

    pygame.display.set_icon(icon)

    # class istances
    aliens = Group()
    bullets = Group()
    ai_set = Settings(screen)
    stats = GameStats(ai_set)
    logo = Logo(screen, logo_img)
    sb = Scoreboard(screen, stats, ship_img, ai_set)
    ship = Ship(screen, ai_set, ship_img)
    play_b = Button(ai_set, screen, "Play")

    # functions
    pygame.display.set_caption("Alien Invasion")
    gf.create_fleet(ai_set, screen, aliens, ship, alien_img)

    # main loop
    while True:
        screen.blit(backg, (0, 0))
        gf.check_events(ai_set, screen, aliens, ship, stats, bullets, play_b,
                        alien_img, laser_shot, sb)

        if stats.game_active:
            ship.update()
            gf.update_bullets(bullets, aliens, ai_set, screen, ship, alien_exp,
                              alien_img, stats, sb)

            gf.update_aliens(ai_set, stats, screen, aliens, ship, bullets,
                             alien_img, sb)

        gf.update_screen(ai_set, screen, ship, aliens, bullets, play_b, stats,
                         logo, sb)

    pygame.quit()
예제 #3
0
def run_game():
    pygame.init()
    ai_settings = Settings()
    # stats = GameStats(ai_settings)
    screen = pygame.display.set_mode(
        (ai_settings.screen_width, ai_settings.screen_height))
    pygame.display.set_caption("LALALALALALALAL")
    play_button = Button(ai_settings, screen, "GAY")
    stats = GameStats(ai_settings)
    sb = Scoreboard(ai_settings, screen, stats)
    ship = Ship(ai_settings, screen)
    # alien = Alien(ai_settings, screen)
    bullets = Group()
    aliens = Group()
    gf.create_fleet(ai_settings, stats, screen, ship, aliens, bullets)

    while True:
        gf.check_events(ai_settings, screen, stats, play_button, 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, stats, screen, ship, aliens, bullets)
        gf.update_screen(ai_settings, screen, stats, sb, ship, aliens, bullets,
                         play_button)
예제 #4
0
def run_game():
    pygame.init()
    new_setting = Settings_1()
    screen = pygame.display.set_mode((new_setting.screen_width,
    new_setting.screen_height))
    pygame.display.set_caption("Alian Invasion")
    #创建一个play按钮
    play_button = Button(new_setting,screen,"Play")
    #创建一个用于存储游戏统计信息的实例
    stats = GameStats(new_setting)
    #创建存储游戏统计信息的实例,并创建记分牌
    sb = Scoreboard(new_setting,screen,stats)
    #创建一艘飞船
    ship = Ship(new_setting,screen)
    #创建一个用于存储子弹的编组
    bullets = Group()
    #创建一个外星人
    #alien = Alien(new_setting,screen)
    aliens = Group()
    gf.create_fleet(new_setting,screen,ship,aliens)
    #游戏主循环
    while True:
        #监视键盘和鼠标事件
        gf.check_events(new_setting,screen,stats,sb,play_button,ship,aliens,bullets)
        if stats.game_active:
            ship.update()
            gf.update_bullets(new_setting,screen,stats,sb,ship,aliens,bullets)
            gf.update_aliens(new_setting,stats,sb,screen,ship,aliens,bullets)
        #print(len(bullets))  主句代码用于监测是否所有的子弹都被删除了   
        #让最近绘制的屏幕可见
        gf.update_screen(new_setting,screen,stats,sb,ship,aliens,bullets,play_button)
예제 #5
0
def run_game():
    pg.init()
    ai_settings = Settings()
    screen = pg.display.set_mode(
        (ai_settings.screen_width, ai_settings.screen_height))
    pg.display.set_caption("Alien Invasion")

    ship = Ship(screen, ai_settings)
    bullets = Group()
    aliens = Group()

    gf.create_fleet(ai_settings, screen, aliens)

    while True:
        gf.check_events(ai_settings, screen, ship, bullets)
        ship.update()
        gf.update_bullets(bullets)
        gf.update_screen(
            ai_settings,
            screen,
            ship,
            aliens,
            bullets,
        )
        for event in pg.event.get():
            if event.type == pg.QUIT:
                sys.exit()

        screen.fill(ai_settings.bg_color)
        ship.blitme()
        pg.display.flip()
예제 #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")

    shuttle = Shuttle(ai_settings, screen)  # Cоздание корабля
    bullets = Group()                       # Создание группы для хранения пуль
    aliens = Group()                        # Создание группы для хранения пришельцев

    gf.create_fleet(ai_settings, screen, aliens)  # Создание флота

    # Запуск основго цикла игры
    while True:
        gf.check_events(ai_settings, screen, shuttle, bullets)  # Отслеживание событий
        shuttle.update()
        gf.update_bullets(bullets)
        #  print(len(bullets))
        gf.update_screen(ai_settings, screen, shuttle, aliens, bullets)
예제 #7
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("Old Galaxy")
    stats = GameStats(ai_settings)
    ship = Ship(ai_settings, screen)
    bullets = Group()
    aliens = Group()
    gf.create_fleet(ai_settings, screen, aliens, ship)

    while True:

        gf.check_events(ai_settings, screen, ship, bullets)
        ship.update()
        gf.update_bullets(bullets, aliens, ai_settings, screen, ship)
        gf.update_aliens(ai_settings, aliens, ship, stats, screen, bullets)
        gf.update_screen(ai_settings, screen, ship, aliens, bullets)
예제 #8
0
def run_game():
    pygame.init()
    #get highscore
    path = sys.path[0]
    path += highscore_filename
    print(path)
    high_score = gf.get_saved_highscore(path)
    bg_color =(230,230,230)
    ai_settings = Settings()
    stats  = GameStats(ai_settings)
    if len(high_score) == 2:
        stats.high_score = high_score[0]
        stats.level      = high_score[1]
    screen = pygame.display.set_mode((ai_settings.screen_width,ai_settings.screen_height))
    pygame.display.set_caption('Alien Invasion')
    ship  = Ship(ai_settings,screen)
    aliens = Group()
    #create one bullet
    bullets = Group()
    play_button = Button(ai_settings,screen,"Play")
    sb = Scoreboard(ai_settings,screen,stats)
    gf.create_fleet(ai_settings,screen,ship,aliens)
    #while
    cnt = 0
    while True:
        #event
        gf.check_events(ship,ai_settings,screen,bullets,aliens,stats,play_button,sb)
        if stats.game_active:
            ship.update()
            bullets.update()
            gf.update_bullets(ai_settings,screen,ship,aliens,bullets,stats,sb)
            gf.update_aliens(ai_settings,aliens,ship,stats,screen,bullets,sb)
        else:
            if cnt < 3:
                cnt += 1
                print('game over!')

        # draw
        gf.update_screen(ai_settings,screen,ship,aliens,bullets,stats,play_button,sb)
예제 #9
0
def run_game():
    #initialize
    pygame.init()
    ai_settings = Settings()

    #screen
    screen = pygame.display.set_mode((ai_settings.screen_width,
                ai_settings.screen_heigth))
    pygame.display.set_caption("Alien Invasion")

    #Erstellt die PlaySchaltfläche
    play_button = Button(ai_settings, screen, "Play")

    #Erstellt eine Instanz zur Speicherung von Statistiken
    stats = GameStats(ai_settings)

    #ship
    ship = Ship(ai_settings, screen)
    #bullets
    bullets = Group()
    #Aliens
    aliens = Group()

    #Erstellt die Invasionflotte
    gf.create_fleet(ai_settings, screen, ship, aliens)

    #Hauptschleife
    while True:
        gf.check_events(ai_settings, screen, stats, play_button, ship, aliens,
            bullets)
        if stats.game_active == True:
            ship.update_self()
            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, stats, ship, bullets, aliens,
            play_button)
예제 #10
0
def run_game():
    # 初始化游戏并创建游戏窗口
    pygame.init()
    pygame.mixer.init()
    pygame.mixer.music.load("back_music.mp3")
    pygame.mixer.music.set_volume(0.5)

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

    play_button = Button(ai_settings, screen, 'Play')

    ship = Ship(screen, ai_settings)  # 创建飞船
    aliens = Group()  # 创建外星人
    bullets = Group()  # 创建子弹
    stats = GameStats(ai_settings)
    scoreboard = ScoreBoard(ai_settings, screen, stats)

    gf.create_fleet(ai_settings, screen, aliens, ship)

    # 开始游戏主循环
    while True:
        '''监视鼠标和键盘'''
        gf.high_score_memory(stats)
        gf.check_events(ship, screen, ai_settings, bullets, stats, play_button,
                        aliens)
        if stats.game_active:
            ship.update()
            gf.update_bullets(aliens, bullets, ai_settings, screen, ship,
                              stats, scoreboard)
            gf.aliens_update(ai_settings, aliens, ship, stats, screen, bullets,
                             scoreboard)
        gf.update_screen(screen, ai_settings, ship, bullets, aliens, stats,
                         play_button, scoreboard)
예제 #11
0
def run_game():
    # game init
    pygame.init()
    
    # Set the width and height of the screen [width, height], and set Title of the game
    ai_settings = Settings()

    size = (ai_settings.screen_width, ai_settings.screen_height)
    screen = pygame.display.set_mode(size)

    pygame.display.set_caption("Alien Invasion")

    play_button = Button(ai_settings, screen, "Play")
    lose_button = Button(ai_settings, screen, "You Lose! Start Again")
    
    # Loop until the user clicks the close button.
    done = False
    
    # Used to manage how fast the screen updates
    clock = pygame.time.Clock()
    
    # prepare for the war ship drawing
    ship = Ship(ai_settings, screen)

    # prepare for bullets
    bullets = Group()

    # prepare for aliens
    aliens = Group()

    stats = GameStats(ai_settings)

    score = Scoreboard(ai_settings, screen, stats)

    gf.create_fleet(ai_settings, screen, ship, aliens)

    center_button = play_button
    
    # -------- Main Program Loop -----------
    while not done:
        # --- Main event loop
        gf.check_event(ai_settings, screen, stats, score, ship, aliens, bullets, center_button)
        
        if stats.game_active:

            if center_button == lose_button:
                center_button = play_button

            ship.update()
            
            gf.update_bullets(ai_settings, screen, stats, score, ship, aliens, bullets)

            gf.update_aliens(ai_settings, stats, screen, score, ship, aliens, bullets)
        
        if not gf.check_ship_remain(stats):
            center_button = lose_button

        # --- Drawing code should go here
        gf.update_screen(ai_settings, screen, stats, score, ship, aliens, bullets, center_button)

        # --- Go ahead and update the screen with what we've drawn.
        pygame.display.flip()
        
        # --- Limit to 60 frames per second
        clock.tick(60)
    
    # Close the window and quit.
    pygame.quit()