Mappy

Using this tutorial as a basis, I have a basic map going for my little Roguelike.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using libtcod;

namespace RLtest
{

    public class Program
        {

        static int ScreenX = 20;
        static int ScreenY = 15;
        static int nPlayerX = 10;
        static int nPlayerY = 10;

        const int MAP_X = 20;
        const int MAP_Y = 15;
        const int TILE_FLOOR = 0;
        const int TILE_WALL = 1;

        static int[,] nMapArray = new int[MAP_Y, MAP_X]
        {
    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
    { 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
    { 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0 },
    { 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0 },
    { 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0 },
    { 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0 },
    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0 },
    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0 },
    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
        };

        static void screenInit()
            {
                libtcod.TCODConsole.initRoot(ScreenX, ScreenY, "RLTest", false);
            }

        static void DrawMap()
        {
            for (int x = 0; x < MAP_X; x++)
            {
                for (int y = 0; y < MAP_Y; y++)
                {
                    switch (nMapArray[y, x])
                    {
                        case TILE_FLOOR:
                            TCODConsole.root.putCharEx(x, y, '.', TCODColor.desaturatedRed, TCODColor.black);
                            break;

                        case TILE_WALL:
                            TCODConsole.root.putCharEx(x, y, '#', TCODColor.grey, TCODColor.black);
                            break;
                    }
                }
            }
        }

        static void HandleKeys()
        {
                //Output 

                TCODConsole.root.print(nPlayerX, nPlayerY, "@");
                TCODConsole.flush();

                //Input
                TCODKey Key = TCODConsole.checkForKeypress();
                if (Key.KeyCode == TCODKeyCode.KeypadOne)
                {
                    if (IsPassable(nPlayerX - 1, nPlayerY + 1))
                    {
                        nPlayerY++;
                        nPlayerX--;
                    }
                }

                else if (Key.KeyCode == TCODKeyCode.KeypadTwo)
                {
                    if (IsPassable(nPlayerX, nPlayerY + 1))
                    {
                        nPlayerY++;
                    }
                }

                else if (Key.KeyCode == TCODKeyCode.KeypadThree)
                {
                    if (IsPassable(nPlayerX + 1, nPlayerY + 1))
                    {
                        nPlayerY++;
                        nPlayerX++;
                    }
                }

                else if (Key.KeyCode == TCODKeyCode.KeypadFour)
                {
                    if (IsPassable(nPlayerX - 1, nPlayerY))
                    {
                        nPlayerX--;
                    }
                }

                else if (Key.KeyCode == TCODKeyCode.KeypadSix)
                {
                    if (IsPassable(nPlayerX + 1, nPlayerY))
                    {
                        nPlayerX++;
                    }
                }

                else if (Key.KeyCode == TCODKeyCode.KeypadSeven)
                {
                    if (IsPassable(nPlayerX - 1, nPlayerY - 1))
                    {
                        nPlayerY--;
                        nPlayerX--;
                    }
                }

                else if (Key.KeyCode == TCODKeyCode.KeypadEight)
                {
                    if (IsPassable(nPlayerX, nPlayerY-1))
                    {
                        nPlayerY--;
                    }
                }

                else if (Key.KeyCode == TCODKeyCode.KeypadNine)
                {
                    if (IsPassable(nPlayerX + 1, nPlayerY - 1))
                    {
                        nPlayerY--;
                        nPlayerX++;
                    }
                }
        }
        static bool IsPassable(int x, int y)
        {
            //Check co-ords are valid
            if (x < 0 || x >= MAP_X || y < 0 || y >= MAP_Y)
                return false;
            //Store value specified
            int nTileValue = nMapArray[y, x];
            //Return true if tile is passable
            if (nTileValue == TILE_FLOOR)
                return true;

            return false;

        }

        [STAThread]
        static void Main(string[] args)
        {
            screenInit();
            while (true)
            {
                DrawMap();
                HandleKeys();
                TCODConsole.root.clear();

            }
        }
        }
}

Running this code gives the following little screen, which allows movement of the @ symbol and also prevents the player walking through walls or off the screen.

rltest_map

Maptastic!

Of course I want to have random dungeon generation rather than just a single, hard-coded map, but this is a good starting point to learn how the map is created and drawn. I suppose the next step is to try and change a value from floor to wall using a for loop.

There seem to be several different ways of approaching the programming of a Roguelike, and I will need to learn more about programming to know which way is best. One thing I have seen is the directional values defined as strings like ‘NORTH’ instead of (x, y-1). I might put this in next time.

I particularly need to learn more about how to use methods and functions – I guess a bit of theory is in order! I may write down my understanding of the terms, to clarify them in my mind, and also to be corrected if anyone more knowledgeable that I happens past this blog.

Advertisements
This entry was posted in Code. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s