Sidetracking

My next subject to work on was making a dungeon for my little guy to walk about in, but I got sidetracked while messing around with a small tutorial, which involved colouring a character. I eventually want to use Libtcod in my RL, so I decided I had better get used to programming with it. With some Googling, fiddling around and peeking at source code, I managed to get the last program running using Libtcod rather than the system library. Some of the libraries included aren’t actually being used at this stage, I’ll have to clear that up.

I had a bit of confusion about  TCODConsole.Flush() , thinking that it did the same as Console.Clear() for some reason. Turns out what I was looking for was TCODConsole.root.clear(). Flush updates the characters and any colour changes, while clear…well, clears the console. While I remember, I also had a problem running the keyboard input through the switch -turns out that second equals sign is very important!

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 = 60;
        static int ScreenY = 50;
        static int nPlayerX = 40;
        static int nPlayerY = 12;

        static void screenInit()
            {

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

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

                //Input
                TCODKey Key = TCODConsole.checkForKeypress();
                if (Key.KeyCode == TCODKeyCode.KeypadOne)
                {
                    if (nPlayerX > 1 && nPlayerY < (ScreenY - 2))
                    {
                        nPlayerY++;
                        nPlayerX--;
                    }
                }

                else if (Key.KeyCode == TCODKeyCode.KeypadTwo)
                {
                    if (nPlayerY < (ScreenY - 2))
                    {
                        nPlayerY++;
                    }
                }

                else if (Key.KeyCode == TCODKeyCode.KeypadThree)
                {
                    if (nPlayerX < (ScreenX - 2) && nPlayerY < (ScreenY - 2))
                    {
                        nPlayerY++;
                        nPlayerX++;
                    }
                }

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

                else if (Key.KeyCode == TCODKeyCode.KeypadSix)
                {
                    if (nPlayerX < (ScreenX - 2))
                    {
                        nPlayerX++;
                    }
                }

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

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

                else if (Key.KeyCode == TCODKeyCode.KeypadNine)
                {
                    if (nPlayerX < (ScreenX - 2) && nPlayerY > 1)
                    {
                        nPlayerY--;
                        nPlayerX++;
                    }
                }
        }

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

            }
        }
        }
}
Advertisements
This entry was posted in Code and tagged , , , . 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