Map

quest.map.clamp(bounds, val)[source]
class quest.map.Map[source]

Implements a map for a level or stage in the game.

Each QuestGame may have multiple Maps. Each Map represents a game map with multiple layers. You can (optionally) specify a sprite class for each layer. For example, one layer might contain walls, another loot, and another background imagery.

background_color = (0, 0, 0)
tile_scaling = 1
add_layer(layer)[source]

Add a layer the the layers list.

Checks to make sure the layer’s name is unique.

Parameters

layer – The MapLayer to add.

get_layer_by_name(layer_name)[source]

Looks up a map layer by name.

There should only be one layer with each name. If there is not exactly one layer, raises an error.

Parameters

layer_name – The name of the layer.

Returns: The layer.

class quest.map.TiledMap(filename, sprite_classes)[source]

A subclass of Map which loads its layers from a Tiled JSON file. Each layer has a collection of tiles. When TiledMap is initialized, a sprite is created for each of these tiles. The sprite’s image and position are read from the tile layer data. The sprite’s class (which can determine its behavior) can be set using the sprite_classes argument.

Use TiledMap when you want to design your map using [Tiled](https://www.mapeditor.org/). Export your map in JSON format.

Parameters
  • filename (str) – Path to the .tmx tilemap file

  • sprite_classes – {layer_name: SpriteClass} dict whose keys should be the names of layers in the tilemap and whose values should be sprite classes (subclasses of QuestSprite. For each layer, each sprite will be initialized using the given sprite class.

class quest.map.MapLayer(name, sprite_list=None)[source]

Each Map is made up of one or more MapLayers.

Parameters
draw()[source]

Renders the layer by calling

clear()[source]

Delete all this layer’s sprites.

class quest.map.GridMapLayer(name, columns, rows, pixel_width, pixel_height, sprite_filename=None, sprite_class=None)[source]

A MapLayer which is aware of grid coordinates. GridMapLayers are useful in situations when you want to place sprites programmatically. Several methods are provided to support calculations about sprite positions with respect to the grid. This cam be simpler than working in pixel positions. For example, the maze example program generates a maze and then uses a GridMapLayer to place the walls of the maze.

Parameters
  • name (str) – Layer name.

  • columns (int) – Number of columns in the grid.

  • rows (int) – Number of rows in the grid.

  • pixel_width (int) – Width of the grid in pixels.

  • pixel_height (int) – Height of the grid in pixels.

  • sprite_filename (str) – Path to sprite image file. Only needed if you will be creating sprites on this grid layer.

  • sprite_class – Class of sprites to create on this layer.

sprite_class

alias of quest.sprite.QuestSprite

add_sprite(grid_x, grid_y, sprite=None)[source]

Adds a sprite at a given grid position.

Parameters
  • grid_x (int) – The x-coordinate of the grid position

  • grid_y (int) – The y-coordinate of the grid position

  • sprite (QuestSprite) – (Optional) sprite to add to this layer and place at this grid position. If no sprite is given, a new sprite will be created.

create_sprite()[source]

Creates a sprite with image self.sprite_filename and class self.sprite_class.

get_pixel_position(grid_position, center=True)[source]

Converts pixel coordinates to grid coordinates.

Parameters
  • grid_position (int, int) – x and y grid coordinates

  • center (bool) – By default, returns the pixel position of the center

  • False (of the grid tile. When) –

  • lower (returns the pixel position of the) –

  • tile. (left corner of the grid) –

Returns

(float, float) pixel position.

get_grid_position(pixel_position)[source]

Converts grid position to pixel position.

Any pixel position within a grid tile will be converted to that grid tile’s coordinates.

Parameters

pixel_position (float, float) – x and y pixel coordinates.

Returns

(float, float) grid position.

position_in_grid(grid_position)[source]

Returns whether or not grid_position is within the grid.

Parameters

grid_position (int, int) – x and y grid coordinates