* A top-down map explorer.
const int MAX_MAP_ROWS = 20;
const int MAX_MAP_COLS = 20;
const int TILE_WIDTH = 60;
const int TILE_HEIGHT = 60;
* There are different kinds of tiles in the map.
* @field WATER_TILE A water tile
* @field GRASS_TILE A grass tile
* @field DIRT_TILE A dirt tile
* @field SAND_TILE A sand tile
* @field kind The kind of tile
* A map containing a two dimensional array of tiles.
* @field tiles The tiles in the map
tile_data tiles[MAX_MAP_COLS][MAX_MAP_ROWS];
* Initialise the map with all grass tiles.
* @param map the map to initialise
void init_map(map_data &map)
for(int c = 0; c < MAX_MAP_COLS; c++)
for(int r = 0; r < MAX_MAP_ROWS; r++)
map.tiles[c][r].kind = GRASS_TILE;
* Returns the color for the indicated tile kind.
* @param kind the tile kind to get the color for
* @return color the color for the tile kind
color color_for_tile_kind(tile_kind kind)
// Return colors for each of the tile kinds
case WATER_TILE: return color_blue();
case GRASS_TILE: return color_lawn_green();
case SAND_TILE: return color_blanched_almond();
case DIRT_TILE: return color_rosy_brown();
default: return color_white();
* Draw a tile at the indicated location.
* @param tile the tile to draw, passed by constant reference
* @param x the x location of the top left of the tile
* @param y the y location of the top left of the tile
void draw_tile(const tile_data &tile, double x, double y)
color tile_color = color_for_tile_kind(tile.kind);
fill_rectangle(tile_color, x, y, TILE_WIDTH, TILE_HEIGHT);
* Draw the tiles in the map to the screen.
void draw_map(const map_data &map)
for(int c = 0; c < MAX_MAP_COLS; c++)
for(int r = 0; r < MAX_MAP_ROWS; r++)
draw_tile(map.tiles[c][r], c * TILE_WIDTH, r * TILE_HEIGHT);
open_window("Map Explorer", 800, 600);
while( ! quit_requested() )
clear_screen(color_white());