pygamelib.engine.Game

class pygamelib.engine.Game(name='Game', boards={}, menu={}, current_level=None, enable_partial_display=False, partial_display_viewport=None, mode=90000003, user_update=None, input_lag=0.01, enable_physic=False)

A class that serve as a game engine.

This object is the central system that allow the management of a game. It holds boards (see pygamelib.engine.Board), associate it to level, takes care of level changing, etc.

Parameters:
  • name (str) – The Game name.
  • boards (dict) – A dictionnary of boards with the level number as key and a board reference as value.
  • menu (dict) – A dictionnary of menus with a category (str) as key and another dictionnary (key: a shortcut, value: a description) as value.
  • current_level (int) – The current level.
  • enable_partial_display (bool) – A boolean to tell the Game object to enable or not partial display of boards. Default: False.
  • partial_display_viewport (list) – A 2 int elements array that gives the radius of the partial display in number of row and column. Please see display_around().
  • mode (int) – The mode parameter configures the way the run() method is going to behave. The default value is constants.MODE_TBT. TBT is short for “Turn By Turn”. In that mode, the Game object wait for an user input before looping. Exactly like when you wait for user input with get_key(). The other possible value is constants.MODE_RT. RT stands for “Real Time”. In that mode, the Game object waits for a minimal amount of time (0.01 i.e 100 FPS, configurable through the input_lag parameter) in order to get the input from the user and call the update function right away. This parameter is only useful if you use Game.run().
  • user_update (function) – A reference to the main program update function. The update function is called for each new frame. It is called with 3 parameters: the game object, the user input (can be None) and the elapsed time since last frame.
  • input_lag (float|int) – The amount of time the run() function is going to wait for a user input before returning None and calling the update function. Default is 0.01.

Note

The game object has an object_library member that is always an empty array except just after loading a board. In this case, if the board have a “library” field, it is going to be used to populate object_library. This library is accessible through the Game object mainly so people have access to it across different Boards during level design in the editor. That architecture decision is debatable.

Note

The constructor of Game takes care of initializing the terminal to properly render the colors on Windows.

Important

The Game object automatically assumes ownership over the Player.

__init__(name='Game', boards={}, menu={}, current_level=None, enable_partial_display=False, partial_display_viewport=None, mode=90000003, user_update=None, input_lag=0.01, enable_physic=False)

Initialize self. See help(type(self)) for accurate signature.

Methods

__init__([name, boards, menu, …]) Initialize self.
actuate_npcs(level_number[, elapsed_time]) Actuate all NPCs on a given level
actuate_projectiles(level_number[, elapsed_time]) Actuate all Projectiles on a given level
add_board(level_number, board) Add a board for the level number.
add_menu_entry(category, shortcut, message) Add a new entry to the menu.
add_npc(level_number, npc[, row, column]) Add a NPC to the game.
add_projectile(level_number, projectile[, …]) Add a Projectile to the game.
animate_items(level_number[, elapsed_time]) That method goes through all the BoardItems of a given map and call Animation.next_frame().
change_level(level_number) Change the current level, load the board and place the player to the right place.
clear_screen() Clear the whole screen (i.e: remove everything written in terminal)
config([section]) Get the content of a previously loaded configuration section.
create_config(section) Initialize a new config section.
current_board() This method return the board object corresponding to the current_level.
delete_menu_category([category]) Delete an entire category from the menu.
display_board() Display the current board.
display_menu(category[, orientation, paginate]) Display the menu.
display_player_stats([life_model, void_model]) Display the player name and health.
get_board(level_number) This method returns the board associated with a level number.
get_key() Reads the next key-stroke returning it as a string.
get_menu_entry(category, shortcut) Get an entry of the menu.
load_board(filename[, lvl_number]) Load a saved board
load_config(filename[, section]) Load a configuration file from the disk.
move_player(direction[, step]) Easy wrapper for Board.move().
neighbors([radius, obj]) Get a list of neighbors (non void item) around an object.
pause() Set the game engine state to PAUSE.
remove_npc(level_number, npc) This methods remove the NPC from the level in parameter.
save_board(lvl_number, filename) Save a board to a JSON file
save_config([section, filename, append]) Save a configuration section.
start() Set the game engine state to RUNNING.
stop() Set the game engine state to STOPPED.
update_menu_entry(category, shortcut, message) Update an entry of the menu.