Пример #1
0
def run_game():
    pygame.init()
    ai_settings = Settings()  # Setup pygame, settings, and display
    screen = pygame.display.set_mode(
        (ai_settings.screen_width, ai_settings.screen_height))
    pygame.display.set_caption('Space Invaders')
    clock = pygame.time.Clock()

    # Setup game stats and scoreboard
    stats = GameStats(ai_settings)
    sb = Scoreboard(ai_settings, screen, stats)

    # Setup ship, bullets, alien bullets, aliens, and background
    ship = Ship(ai_settings, screen)
    bullets = pygame.sprite.Group()
    alien_bullets = pygame.sprite.Group()
    aliens = pygame.sprite.Group()
    ufo = pygame.sprite.Group()
    background = Background('images/background.png', [0, 0], screen)
    gf.create_fleet(ai_settings, screen, ship, aliens)
    bunkers = pygame.sprite.Group(make_bunker(ai_settings, screen, 0),
                                  make_bunker(ai_settings, screen, 1),
                                  make_bunker(ai_settings, screen, 2),
                                  make_bunker(ai_settings, screen, 3))

    while True:
        clock.tick(120)
        gf.update_screen(ai_settings, screen, stats, sb, ship, aliens,
                         alien_bullets, bullets, bunkers, ufo, background)
        gf.play_bgm(ai_settings, stats)
        if not stats.game_active:
            quit_game = not gf.startup_screen(ai_settings, stats, screen)
            if quit_game:
                pygame.quit()
                break
            gf.start_new_game(ai_settings, screen, stats, sb, ship, aliens,
                              alien_bullets, bullets)
        gf.check_events(ai_settings, screen, stats, ship, bullets)
        if stats.game_active:
            ship.update()
            gf.update_bullets_alien_bullet(ai_settings, screen, stats, sb,
                                           ship, aliens, alien_bullets,
                                           bullets, ufo)
            gf.update_aliens(ai_settings, screen, stats, sb, ship, aliens,
                             alien_bullets, bullets, ufo)
Пример #2
0
def run_game():
    pygame.init()
    ai_settings = Settings()  # Setup pygame, settings, and display
    screen = pygame.display.set_mode(
        (ai_settings.screen_width, ai_settings.screen_height))
    pygame.display.set_caption('Pokemon Invasion')
    clock = pygame.time.Clock()

    # Setup game stats and scoreboard
    stats = GameStats(ai_settings)
    sb = Scoreboard(ai_settings, screen, stats)
    # Setup ship, bullets, beams, aliens, background stars
    ship = Ship(ai_settings, screen)
    bullets = pygame.sprite.Group()
    beams = pygame.sprite.Group()
    aliens = pygame.sprite.Group()
    ufo = pygame.sprite.Group()
    stars = gf.create_stars(ai_settings, screen)
    gf.create_fleet(ai_settings, screen, ship, aliens)
    bunkers = pygame.sprite.Group(make_bunker(ai_settings, screen, 0),
                                  make_bunker(ai_settings, screen, 1),
                                  make_bunker(ai_settings, screen, 2),
                                  make_bunker(ai_settings, screen, 3))

    while True:
        clock.tick(70)  # 70 fps limit
        if not stats.game_active:
            quit_game = not gf.startup_screen(ai_settings, stats, screen)
            if quit_game:
                pygame.quit()
                break
            gf.start_new_game(ai_settings, screen, stats, sb, ship, aliens,
                              beams, bullets)
        gf.check_events(ai_settings, screen, stats, ship, bullets)
        if stats.game_active:
            ship.update()
            gf.update_bullets_beams(ai_settings, screen, stats, sb, ship,
                                    aliens, beams, bullets, ufo)
            gf.update_aliens(ai_settings, screen, stats, sb, ship, aliens,
                             beams, bullets, ufo)
        gf.update_screen(ai_settings, screen, stats, sb, ship, aliens, beams,
                         bullets, bunkers, stars, ufo)
        gf.play_bgm(ai_settings, stats)
Пример #3
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("Space Invaders")
    clock = pygame.time.Clock()

    # Create an instance to store game statistics, and a scoreboard.
    stats = GameStats(ai_settings)
    sb = Scoreboard(ai_settings, screen, stats)

    # Create entities
    ship = Ship(ai_settings, screen)
    bullets = Group()
    lasers = Group()
    aliens = Group()
    ufo = Group()
    gf.create_fleet(ai_settings, screen, ship, aliens)
    bunkers = gf.resetBunkers(ai_settings, screen)

    # Game Loop
    while True:
        clock.tick(ai_settings.tick_rate)
        if not stats.is_game_active:
            isQuit = not gf.start_screen(ai_settings, stats, screen)
            if isQuit:
                pygame.quit()
                break
            bunkers = gf.resetBunkers(ai_settings, screen)
            ship = Ship(ai_settings, screen)
            gf.start_new_game(ai_settings, screen, stats, sb, ship, aliens,
                              lasers, bullets)

        gf.check_events(ai_settings, screen, stats, ship, bullets)
        if stats.is_game_active:
            if not ship.isDead:
                ship.update()
                gf.update_lasers_bullets(ai_settings,
                                         screen,
                                         stats,
                                         sb,
                                         ship,
                                         aliens,
                                         lasers=lasers,
                                         bullets=bullets,
                                         ufo=ufo)
                gf.update_aliens(ai_settings,
                                 screen,
                                 stats,
                                 sb,
                                 ship,
                                 aliens,
                                 lasers=lasers,
                                 bullets=bullets,
                                 ufos=ufo)
            else:
                screen.fill(ai_settings.bg_color)
                ship.update()
        gf.update_screen(ai_settings=ai_settings,
                         screen=screen,
                         stats=stats,
                         sb=sb,
                         ship=ship,
                         aliens=aliens,
                         lasers=lasers,
                         bullets=bullets,
                         bunkers=bunkers,
                         ufos=ufo)
Пример #4
0
def run_game():
    FPS = 60

    # Initialize game, settings and create a screen object.
    pygame.init()
    fps_clock = pygame.time.Clock()
    ai_settings = Settings()

    # FOR THE DQN #

    agent = DQNAgent()
    counter_games = 0
    score_plot = []
    counter_plot = []
    record = 0

    # FOR THE DQN #

    while counter_games < 150:

        # Create statistics.
        stats = GameStats(ai_settings)

        # Create game items.
        game_items = GameItems(ai_settings, stats)

        # Create a fleet of aliens.
        gf.create_fleet(ai_settings, game_items)
        played = False

        gf.start_new_game(ai_settings, stats, game_items)

        # Start the main loop for the game.
        while stats.game_active:
            stats.time_passed = fps_clock.tick(FPS) / 1000  # Time in seconds since previous loop.

            gf.check_events(ai_settings, stats, game_items)

            if stats.game_active:
                game_items.ship.update(stats)
                gf.update_bullets(ai_settings, stats, game_items)
                gf.update_aliens(ai_settings, stats, game_items)
                # FOR THE DQN #
                agent.epsilon = 80 - counter_games
                state_old = gf.get_state(ai_settings, stats, game_items)
                if randint(0, 200) < agent.epsilon:
                    final_move = to_categorical(randint(0, 3), num_classes=4)
                else:
                    # predict action based on the old state
                    prediction = agent.model.predict(state_old.reshape((1, 3536)))
                    final_move = to_categorical(np.argmax(prediction[0]), num_classes=4)

                # FOR THE DQN #

                # DQN #
                # perform new move and get new state
                gf.do_move(final_move, ai_settings, stats, game_items)


                state_new = gf.get_state(ai_settings, stats, game_items)

                # set reward for the new state
                reward = agent.set_reward(stats.score, stats.ships_left)

                # train short memory base on the new action and state
                agent.train_short_memory(state_old, final_move, reward, state_new, stats.game_active)

                # store the new data into a long term memory
                # TO:DO  agent.remember(state_old, final_move, reward, state_new, game.crash)
                # Get value of played game
                # TO:DO record = get_record(game.score, record)
                # DQN #
                
                
                played = True
            elif played:
                user = ask(game_items.screen)
                if len(user) > 0:
                    coll = connect_and_collect()
                    add_score(user , stats.score, coll)
                played = False

            # gf.update_screen(ai_settings, stats, game_items)


        # FOR THE DQN #
        agent.replay_new(agent.memory)
        counter_games += 1
        print('Game', counter_games, '      Score:', stats.score)
        score_plot.append(stats.score)
        counter_plot.append(counter_games)
    agent.model.save_weights('weights.hdf5')
    plot_seaborn(counter_plot, score_plot)