Agent

Agents are the units that make up the “active” portion of the model. They generally move around the model area, interacting with each other and the area itself.

class agents.Agent

Creates an agent with a random position, direction and color. Has no initial model; this must be provided by Agent.set_model.

agents_nearby(distance, agent_type=None)

Returns a list of nearby agents. May take a type as argument and only return agents of that type.

Parameters:
  • distance – The radius around the agent to search in.
  • agent_type – If provided, only returns agents of this type.
backward(distance=None)

Moves the agent in the opposite direction of its current orientation.

Parameters:Distance – The distance to move the agent. If none is specified, it moves a distance equal to its speed-attribute.
center_in_tile()

Move the agent to the center of the tile it is standing on.

color

The color of the agent. Must be provided as an RGB 3-tuple, e.g. (255, 255, 255) to color the agent white.

current_tile()

Returns the tile that the agent is currently standing on, based on its coordinates.

The tile returned is the one that overlaps with the exact center of the agent, so even if the agent visually covers multiple tiles due to its size, only one tile is returned.

destroy()

Marks the agent for destruction, removing it from the set of agents in the model.

direction

The direction of the agent, measured in degrees.

direction_to(other_x, other_y)

Calculate the direction in degrees from the agent to a given point.

Parameters:
  • other_x – The x-coordinate of the target point.
  • other_y – The y-coordinate of the target point.
distance_to(other_x, other_y)

Returns the distance between the agent and another point.

Parameters:
  • other_x – The x-coordinate of the target point.
  • other_y – The y-coordinate of the target point.
forward(distance=None)

Moves the agent forward in the direction it is currently facing.

Parameters:Distance – The distance to move the agent. If none is specified, it moves a distance equal to its speed-attribute.
is_destroyed()

Returns True or False whether or not the agent is destroyed.

jump_to(x, y)

Move the agent to a specified point.

Parameters:
  • x – Destination x-coordinate.
  • y – Destination y-coordinate.
jump_to_tile(t)

Move the agent to the center of a specified tile.

Parameters:t – Destination tile.
nearby_tiles(x1, y1, x2, y2)

Returns a rectangle of tiles relative to the agent’s current position.

Parameters:
  • x1 – The x-coordinate of the top-left tile (relative to the current tile).
  • y1 – The y-coordinate of the top-left tile (relative to the current tile).
  • x2 – The x-coordinate of the bottom-right tile (relative to the current tile).
  • y2 – The y-coordinate of the bottom-right tile (relative to the current tile).
neighbor_tiles()

Returns the surrounding tiles as a 3x3 grid. Includes the current tile.

point_towards(other_x, other_y)

Make the agent orient itself towards a given point.

Parameters:
  • other_x – The x-coordinate of the target point.
  • other_y – The y-coordinate of the target point.
rotate(degrees)

Make the agent turn the given number of degrees. Positive is counter-clockwise, negative is clockwise.

Parameters:degrees – The amount of degrees to turn.
set_model(model)

Provides the Model object that the agents belongs to.

The stored model is used in other methods such as Agent.agents_nearby and Agent.current_tile, which rely on information about other objects in the model.

Parameters:model – The model to assign the agent to.
setup(model)

This method is run when the agent is added to a model. The method is empty by default, and is intented to be overwritten by a subclass.

Parameters:model – The model object that the agent has been added to.
shape

The shape of the agent.

size

The size of the agent. For an agent with the circle shape, this corresponds to its radius.

update_current_tile()

Updates the tile that the agent is currently standing on.

Effectively, this removes the agent from the set of agents standing on the previous tile, and adds it to the set of agents standing on the current tile.