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, allowing the map to describe walls, loot, and multiple layers of background imagery.

background_color

a 3-tuple of integers for red, green, blue. Each from 0-255. The color module also predefines many colors.

tile_scaling

Factor by which to scale all map tiles. Default is 1.

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=None)[source]

A subclass of Map which loads its layers from a TMX 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/). This app saves maps as TMX files.

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

  • sprite_classes – {layer_name: SpriteClass} dict specifying the sprite class that should be used for each layer.

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

  • the grid tile. When False, returns the pixel position of the lower (of) –

  • corner of the grid tile. (left) –

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