Text Based Adventure Map

Text Based Adventure Map

Nov 15, 2018. | By: Evan Diamond
2 mins

I decided for my text based adventure game to have a very simple map render when the player types in map. Layout == The map is an array of sprites which are squares representing the locations in the haunted house. If the location was inside the house the square is brown, oustide is green, and the player’s current location is red. The each square is only rendered if the player has been to that location.

Map Layout Code

This is the code to position each sprite in the map:

int x_count = 0;
    int y_count = 0;
    for (int i = 0; i < MAP_SIZE; ++i)
    {
        if(i != 0 && i % 8 == 0)
        {
            x_count = 0;
            ++y_count;
        }
        map[i] = renderer->createRawSprite();
        map[i]->loadTexture("/data/Textures/Map/Map Square.png");
        map[i]->height(SCREEN_HEIGHT / 8);
        map[i]->width(SCREEN_WIDTH / 8);
        map[i]->xPos(map[i]->width() * x_count);
        map[i]->yPos(map[i]->height() * y_count);

        ++x_count;
    }

This code just uses a for loop, two counters (x and y), and the mod operator (%) to see if i is a mutiple of 8 (the width of the map) to reset or increment the counters. The map’s sprites are then positioned based on their width and height times by the x and y counters respectfully.

Map Render Code

This is the code to render the discovered map locations in the correct colour

for (int i = 0; i < MAP_SIZE; ++i)
        {
            if(locations[i]->isDiscovered())
            {
                (i >= 9 && i <= 13)
                || (i >= 17 && i <= 22)
                || (i >= 25 && i <= 30)
                || (i >= 32 && i <= 38)
                || (i >= 40 && i <= 46)
                || (i >= 50 && i <= 52) ?
                map[i]->colour(ASGE::COLOURS::BROWN) : map[i]->colour(ASGE::COLOURS::DARKGREEN);
                map[player->currentPlayerLoc()->getIndex()]->colour(ASGE::COLOURS::RED);
                renderer->renderSprite(*map[i]);
            }
        }

This code goes through all the locations and checks if they have been discovered by the player. If so, then it checks the index to see if it is within specific ranges. If it is then that location on the map is indoors and therefore needs to be a brown colour. If not then it is green. If the player is on that location then it overrides the colour with red.

Map Image

This is an image of a player’s current map

Post Mortem

In the future I would make better map sprites and have them rendered over a blank screen not just over the game.

About

Gamers developing new games.

Social Links

Our Bunker

University of the West of England,
Frenchay Campus, Bristol, BS16 1QY,
United Kingdom.