Пример #1
0
    def game_loop(self):
        """
        The main loop of the game execution
        """
        keep_going = lambda: not quit_or_esc()

        comm.wait_all_ml_ready()

        while keep_going():
            scene_info = self._scene.fill_scene_info_obj(SceneInfo())

            # Send the scene info to the ml processes and wait for instructions
            instruction_1P, instruction_2P = self._make_ml_execute(scene_info)

            scene_info.command_1P = instruction_1P.command.value
            scene_info.command_2P = instruction_2P.command.value
            self._record_handler(scene_info)

            # Update the scene
            game_status = self._scene.update( \
                instruction_1P.command, instruction_2P.command)

            self._draw_scene()

            # If either of two sides wins, reset the scene and wait for ml processes
            # getting ready for the next round
            if game_status == GameStatus.GAME_1P_WIN or \
               game_status == GameStatus.GAME_2P_WIN:
                scene_info = self._scene.fill_scene_info_obj(SceneInfo())
                self._record_handler(scene_info)
                comm.send_to_all_ml(scene_info)

                print("Frame: {}, Status: {}\n-----" \
                    .format(scene_info.frame, game_status.value))

                if self._game_over(game_status):
                    break

                self._scene.reset()
                self._frame_delayed = [0, 0]
                # Wait for ml processes doing their resetting jobs
                comm.wait_all_ml_ready()

        self._print_result()
        pygame.quit()
Пример #2
0
    def game_loop(self):
        """
        The main loop of the game execution
        """
        comm.wait_all_ml_ready()

        while not quit_or_esc():
            scene_info = self._scene.get_scene_info()

            # Send the scene info to the ml processes and wait for commands
            command_1P, command_2P = self._make_ml_execute(scene_info)

            scene_info["command_1P"] = command_1P.value
            scene_info["command_2P"] = command_2P.value
            self._record_handler(scene_info)

            # Update the scene
            game_status = self._scene.update(command_1P, command_2P)

            self._screen.update(self._score, self._scene._ball.speed)

            # If either of two sides wins, reset the scene and wait for ml processes
            # getting ready for the next round
            if game_status != GameStatus.GAME_ALIVE:
                scene_info = self._scene.get_scene_info()
                comm.send_to_all_ml(scene_info)

                scene_info["command_1P"] = scene_info["command_2P"] = None
                self._record_handler(scene_info)

                print("Frame: {}, Status: {}".format(scene_info["frame"],
                                                     game_status.value))

                if self._game_over(game_status):
                    break

                self._scene.reset()
                self._frame_delayed = [0, 0]
                # Wait for ml processes doing their resetting jobs
                comm.wait_all_ml_ready()

        self._print_result()