Agent#

You will customize your game by defining Agents and adding them to the game. In an adventure game, one agent might represent the player, others might represent enemies, loot, or walls. You don’t need to use the Agent class; you need to write your own class which has some of the same attributes and methods as Agent.

class retro.agent.Agent[source]#

Represents a character in the game. To create an Agent, define a new class with some of the attributes and methods below. You may change any of the Agent’s attributes at any time, and the result will immediately be visible in the game.

After you create your Agents, add them to the Game, either when it is created or using Game.add_agent later on. Then the Game will take care of calling the Agent’s methods at the appropriate times.

position#

(Required) The character’s (int, int) position on the game board.

character#

(Required unless display is False.) A one-character string which will be displayed at the Agent’s position on the game board.

name#

(Optional) If an agent has a name, it must be unique within the game. Agent names can be used to look up agents with retro.game.Game.get_agent_by_name().

color#

(Optional) The agent’s color. Available colors.

Type:

str

display#

(Optional) When False, the Agent will not be displayed on the board. This is useful when you want to create an agent which will be displayed later, or when you want to create an agent which acts on the Game indirectly, for example by spawning other Agents. Defaults to True.

z#

(Optional) When multiple Agents have the same position on the board, the Agent with the highest z value will be displayed. The Game is played on a two-dimensional (x, y) board, but you can think of z as a third “up” dimension. Defaults to 0.

handle_keystroke(keystroke, game)[source]#

If an Agent has a this method, it will be called every time a key is pressed in the game.

Parameters:
  • keystroke (blessed.keyboard.Keystroke) – The key which was pressed. You can compare a Keystroke with a string (e.g. if keystroke == 'q') to check whether it is a regular letter, number, or symbol on the keyboard. You can check special keys using the keystroke’s name (e.g. if keystroke.name == "KEY_RIGHT"). Run your game in debug mode to see the names of keystrokes.

  • game (Game) – The game which is currently being played will be passed to the Agent, in case it needs to check anything about the game or make any changes.

play_turn(game)[source]#

If an Agent has this method, it will be called once each turn.

Parameters:

game (Game) – The game which is currently being played will be passed to the Agent, in case it needs to check anything about the game or make any changes.

class retro.agent.ArrowKeyAgent[source]#

A simple agent which can be moved around with the arrow keys.

handle_keystroke(keystroke, game)[source]#

Moves the agent’s position if the keystroke is one of the arrow keys. One by one, checks the keystroke’s name against each arrow key. Then uses try_to_move() to check whether the move is on the game’s board before moving.

try_to_move(position, game)[source]#

Moves to the position if it is on the game board.