Initial Forays

I have spent a lot of time going over the C# references linked in the last post, I am beginning to get some grasp of how the language works. The logical nature of programming appeals to the scientific side of my mind, which hasn’t had much exercise since I finished high school, although it is still much easier for me to look at examples in terms of game mechanics, and probably always will be.

I have looked with interest over several <1KB Roguelikes, and they seem to be a good starting point to understand the code of the most basic elements of the genre. As RLs tend to be very complicated, and have many, many features, it is difficult to look at the source code and know where to start. The 1KRL that was hosted at http://nrkn.com/1kRl/, which seems to be down at the moment, was the one I chose to dissect first. As the code is heavily abbreviated, I went through and substituted all the letters for the words they stand for, to make it easier to understand. I am not sure of the name of the author of this RL, if you know, tell me in the comments.

You can download the original VS solution here, and the expanded solution (VS2010) here.

I have also been looking with interest at some name generation code, that I would eventually like to play with myself. My understanding of different parts of the code is dubious at the moment, but it is a great way to get direction for my learning. I have uploaded some interesting sites to the reference page, if you are interested.

Anyway, on to actually learning to code something. Based on “How to write a Roguelike in 15 Steps”, and a couple of RL coding tutorials I have looked at, getting my little @ to move around the screen is the first thing to do. I am using this tutorial, although it is in C++, it is easy enough to convert it to C# with some Googling and browsing reference sites.

I’ll document what I did here, because this is my blog and I want to. Here is the first part…

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

namespace RLtest
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Clear();

            int nPlayerX, nPlayerY;

            nPlayerX = 40;
            nPlayerY = 12;

            while (true)
            {
                Console.SetCursorPosition(nPlayerX, nPlayerY);
                Console.Write("@");

            }
        }
    }
}

What this does, which is very exciting for me, and extremely boring for anyone else, is to draw an @ in the middle of the console screen. My first step towards a roguelike!


Cue epic guitar solo!

The next step is to get the symbol to move when the appropriate key is pressed. I prefer using the numpad for input, so I’m going with that. I had a bit of trouble figuring out how to use Console.ReadKey, but I got the solution from that 1KRL I have been studying – if you append .KeyChar to the end of the method, it will grab a char for you rather than the data type it gets by default, which is something like System.ConsoleKeyInfo.

namespace RLtest
{
    class Program
    {
        static void Main(string[] args)
        {

            int nPlayerX, nPlayerY;

            nPlayerX = 40;
            nPlayerY = 12;
            char input;

            while (true)
            {
                //Output
                Console.Clear();
                Console.SetCursorPosition(nPlayerX, nPlayerY);
                Console.Write("@");

                //Input
                input = (char)Console.ReadKey(true).KeyChar;

                //Processing
                switch(input)
                {
                case '8':
                        nPlayerY--;
                        break;
                case '4':
                        nPlayerX--;
                        break;
                case '6':
                        nPlayerX++;
                        break;
                case '2':
                        nPlayerY++;
                        break;
                }
            }
        }
    }
}

So now we have… an @ walking around a screen! Joy! As suggested in the tutorial, I can now add a little extra code to make it impossible to walk off the edge of the screen and crash the program. The values here for the window size are the default window size, which is 80×25, but we may want to change that in the future, so I am going to add some variables so that I can change that easily later. The numbers used here I chose arbitrarily to test if the movement code would work. As well as this, I added the ability to move diagonally as suggested in the tutorial.

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

namespace RLtest
{
    class Program
    {
        static void Main(string[] args)
        {

            int nPlayerX, nPlayerY, ScreenX, ScreenY;

            ScreenX = 100;
            ScreenY = 30;
            nPlayerX = 40;
            nPlayerY = 12;

            System.Console.WindowWidth = ScreenX;
            System.Console.WindowHeight = ScreenY;

            char input;

            while (true)
            {
                //Output
                Console.Clear();     
                Console.SetCursorPosition(nPlayerX, nPlayerY);
                Console.Write("@");

                //Input
                input = (char)Console.ReadKey(true).KeyChar;

                //Processing
                switch(input)
                {
                case '1':
                        if (nPlayerX > 1 && nPlayerY < (ScreenY - 2))
                        {
                            nPlayerY++;
                            nPlayerX--;
                        }
                        break;

                case '2':
                        if (nPlayerY < (ScreenY-2))   
                            nPlayerY++;  
                        break;

                case '3':
                        if (nPlayerX < (ScreenX - 2) && nPlayerY < (ScreenY - 2))
                        {
                            nPlayerY++;
                            nPlayerX++;
                        }
                        break;

                case '4':
                        if (nPlayerX > 1)
                            nPlayerX--;
                        break;

                case '6':
                        if (nPlayerX < (ScreenX-2))
                            nPlayerX++;
                        break;

                case '7':
                        if (nPlayerX > 1 && nPlayerY > 1)
                        {
                            nPlayerY--;
                            nPlayerX--;
                        }
                        break;

                case '8':
                        if (nPlayerY > 1)
                            nPlayerY--;
                        break;

                case '9':
                        if (nPlayerX < (ScreenX - 2) && nPlayerY > 1)
                        {
                            nPlayerY--;
                            nPlayerX++;
                        }
                        break;

                }            
            }
        }
    }
}

I’ll leave this for now, the next step is to deal with map generation.

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