Coverage for /home/kale/kxgames/libraries/kxg/kxg/quickstart.py : 99%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
#!/usr/bin/env python3
host, port, callback=self.on_connection_established)
self.world, self.gui_actor, self.pipe)
host=DEFAULT_HOST, port=DEFAULT_PORT): host, port, num_clients, self.on_clients_connected)
else:
self.world, self.referee, self.ai_actors, self.pipes)
""" Until the player closes the window, keep it as it was when the game ended. """
""" Manage a group of processes running instances of the game loop.
This class wraps around the basic multiprocessing functionality available in the python standard library. There are two nice features provided by this class. The first is that log messages generated in the subprocesses are automatically relayed to the main process, where they are reported with the name of the original process included and without any mangling due to race conditions. The second is that exceptions, which are usually silently ignored in subprocesses, are also relayed to the main process and re-raised.
This class is actually not specific to the game engine at all, so in principle it could be moved into it's own library. I decided not to do that because I can't think of any other scenario where I would want the functionality that this class offers, but maybe I'll think of one later. """
name=name, target=self._run_worker, args=(name, worker) + args, kwargs=kwargs, )
# Configure all logging message generated by this process to go into a # queue that will be read and handled by the supervisor.
# Catch any exceptions generated by the worker and report them to the # supervisor. This is important, because otherwise they would be # silently ignored.
except Exception as exception: self.exception_queue.put_nowait(exception)
""" Poll the queues that the worker can use to communicate with the supervisor, until all the workers are done and all the queues are empty. Handle messages as they appear. """
multiprocessing.active_children() or not self.log_queue.empty() or not self.exception_queue.empty())
# When a log message is received, make a logger with the same # name in this process and use it to re-log the message. It # will get handled in this process.
# When an exception is received, immediately re-raise it.
else:
# Sleep for a little bit, and make sure that the workers haven't # outlived their time limit.
# Make sure the workers don't outlive the supervisor, no matter how the # polling loop ended (e.g. normal execution or an exception).
finally:
""" Simultaneously plays any number of different game theaters, executing each theater in its own process. This greatly facilitates the debugging and testing multiplayer games. """
num_guis=2, ai_actor_cls=None, num_ais=0, theater_cls=PygletTheater, host=DEFAULT_HOST, port=DEFAULT_PORT, log_format= '%(levelname)s: %(processName)s: %(name)s: %(message)s'):
# Members of this class have to be pickle-able, because this object # will be pickled and sent to every process that gets started. That's # why all the game objects are stored as classes (or factories) rather # than instances. Even though some of the game objects can be pickled, # none of them are meant to be and avoiding it reduces the risk that # things will break for strange reasons. The game objects themselves # are instantiated in the worker processes, which is how it would # happen if the user just rame multiple instances of the game anyway.
# Configure the logging system to print to stderr and include the # process name in all of its messages.
# Run the server and the client (each in its own process).
# Defer instantiation of all the game objects until we're inside our # own process, to avoid having to pickle and unpickle things that # should be pickled.
world=self.world_cls(), referee=self.referee_cls(), num_clients=self.num_guis, ai_actors=[self.ai_actor_cls() for i in range(self.num_ais)], host=self.host, port=self.port, )
# Defer instantiation of all the game objects until we're inside our # own process, to avoid having to pickle and unpickle things that # should be pickled.
world=self.world_cls(), gui_actor=self.gui_actor_cls(), host=self.host, port=self.port, )
theater_cls=PygletTheater, default_host=DEFAULT_HOST, default_port=DEFAULT_PORT, argv=None): """ Run a game being developed with the kxg game engine.
Usage: {exe_name} sandbox [<num_ais>] [-v...] {exe_name} client [--host HOST] [--port PORT] [-v...] {exe_name} server <num_guis> [<num_ais>] [--host HOST] [--port PORT] [-v...] {exe_name} debug <num_guis> [<num_ais>] [--host HOST] [--port PORT] [-v...] {exe_name} --help
Commands: sandbox Play a single-player game with the specified number of AIs. None of the multiplayer machinery will be used.
client Launch a client that will try to connect to a server on the given host and port. Once it connects and the game starts, the client will allow you to play the game against any other connected clients.
server Launch a server that will manage a game between the given number of human and AI players. The human players must connect using this command's client mode.
debug Debug a multiplayer game locally. This command launches a server and the given number of clients all in different processes, and configures the logging system such that the output from each process can be easily distinguished.
Arguments: <num_guis> The number of human players that will be playing the game. Only needed by commands that will launch some sort of multiplayer server.
<num_ais> The number of AI players that will be playing the game. Only needed by commands that will launch single-player games or multiplayer servers.
Options: -x --host HOST [default: {default_host}] The address of the machine running the server. Must be accessible from the machines running the clients.
-p --port PORT [default: {default_port}] The port that the server should listen on. Don't specify a value less than 1024 unless the server is running with root permissions.
-v --verbose Have the game engine log more information about what it's doing. You can specify this option several times to get more and more information.
This command is provided so that you can start writing your game with the least possible amount of boilerplate code. However, the clients and servers provided by this command are not capable of running a production game. Once you have written your game and want to give it a polished set of menus and options, you'll have to write new Stage subclasses encapsulating that logic and you'll have to call those stages yourself by interacting more directly with the Theater class. The online documentation has more information on this process. """
format='%(levelname)s: %(name)s: %(message)s', level=nonstdlib.verbosity(args['--verbose']), )
# Use the given game objects and command line arguments to play a game!
****************************** KNOWN BUG WARNING ****************************** In debug mode, every message produced by the logging system gets printed twice. I know vaguely why this is happening, but as of yet I've not been able to fix it. In the mean time, don't let this confuse you! *******************************************************************************""") world_cls, referee_cls, gui_cls, gui_actor_cls, num_guis, ai_actor_cls, num_ais, theater_cls, host, port) else:
world_cls(), referee_cls(), gui_actor_cls(), ai_actors)
world_cls(), gui_actor_cls(), host, port)
world_cls(), referee_cls(), num_guis, ai_actors, host, port)
|