Page 1 of 1

A basic C# console game movement engine

#1 maffelu  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 43
  • View blog
  • Posts: 190
  • Joined: 21-August 08

Posted 12 February 2011 - 01:48 AM

If you've thinking about doing a console based game you're 20 year late. But don't let that stop you. It could still be a fun experiance and you just might learn something from it.

The first thing you may want to accomplish is comming up with a way for the player to move around on a console window. This is a lot simpler than it sounds, but it can still be hard if you haven't used the ReadKey method that comes with the Console class.
This method will the basis of our movement engine that we are going to build.

Notice that in this tutorial I'll be placing all the logic in the Program.cs file, this is because it is a short tutorial and I want to avoid confusion. It might be a good idea to separate the logic a bit further if you're planning on using it in a bigger game.

When using the ReadKey method you can use an overload that allows you to control whether or not to accept the input on the screen, or just read it for programmatic use. We won't output the user input, just use it, so we're going to use this overload thus using it with a 'true' parameter (Console.ReadKey(true)) meaning we accept the input, but we don't output it.

ConsoleKeyInfo keyInfo;
while ((keyInfo = Console.ReadKey(true)).Key != ConsoleKey.Escape)
{
    //Do something
}



The above code accepts one user key input letting us read what key the user pressed, but hiding the input. This way, we can choose to do something if they press anything other than Escape.

We will only use the four arrows on a computer keyboard:

            ConsoleKeyInfo keyInfo;
            while ((keyInfo = Console.ReadKey(true)).Key != ConsoleKey.Escape)
            {
                switch (keyInfo.Key)
                {
                    case ConsoleKey.UpArrow:
                        break;

                    case ConsoleKey.RightArrow:
                        break;

                    case ConsoleKey.DownArrow:
                        break;

                    case ConsoleKey.LeftArrow:
                        break;
                }
            }


Now we can handle all the input we need, if anything else, don't do anything.

After knowing this, you can actually do whatever you want.
This is how I implemented a short game where the player can mover around a screen (he/she can't actually do anything else, but it's a good start):

(The code is commented, so I don't think any more introduction to the code is needed)

using System;

namespace MaffeluDemo
{
    class Program
    {
        const ConsoleColor HERO_COLOR = ConsoleColor.DarkBlue;
        const ConsoleColor BACKGROUND_COLOR = ConsoleColor.Green;

        public static Coordinate Hero { get; set; } //Will represent our here that's moving around :P/>

        static void Main(string[] args)
        {
            InitGame();

            ConsoleKeyInfo keyInfo;
            while ((keyInfo = Console.ReadKey(true)).Key != ConsoleKey.Escape)
            {
                switch (keyInfo.Key)
                {
                    case ConsoleKey.UpArrow:
                        MoveHero(0, -1);
                        break;

                    case ConsoleKey.RightArrow:
                        MoveHero(1, 0);
                        break;

                    case ConsoleKey.DownArrow:
                        MoveHero(0, 1);
                        break;

                    case ConsoleKey.LeftArrow:
                        MoveHero(-1, 0);
                        break;
                }
            }
        }

        /// <summary>
        /// Paint the new hero
        /// </summary>
        static void MoveHero(int x, int y)
        {
            Coordinate newHero = new Coordinate()
            {
                X = Hero.X + x,
                Y = Hero.Y + y
            };

            if (CanMove(newHero))
            {
                RemoveHero();

                Console.BackgroundColor = HERO_COLOR;
                Console.SetCursorPosition(newHero.X, newHero.Y);
                Console.Write(" ");

                Hero = newHero;
            }
        }

        /// <summary>
        /// Overpaint the old hero
        /// </summary>
        static void RemoveHero()
        {
            Console.BackgroundColor = BACKGROUND_COLOR;
            Console.SetCursorPosition(Hero.X, Hero.Y);
            Console.Write(" ");
        }

        /// <summary>
        /// Make sure that the new coordinate is not placed outside the
        /// console window (since that will cause a runtime crash
        /// </summary>
        static bool CanMove(Coordinate c)
        {
            if (c.X < 0 || c.X >= Console.WindowWidth)
                return false;

            if (c.Y < 0 || c.Y >= Console.WindowHeight)
                return false;

            return true;
        }

        /// <summary>
        /// Paint a background color
        /// </summary>
        /// <remarks>
        /// It is very important that you run the Clear() method after
        /// changing the background color since this causes a repaint of the background
        /// </remarks>
        static void SetBackgroundColor()
        {
            Console.BackgroundColor = BACKGROUND_COLOR;
            Console.Clear(); //Important!
        }

        /// <summary>
        /// Initiates the game by painting the background
        /// and initiating the hero
        /// </summary>
        static void InitGame()
        {
            SetBackgroundColor();

            Hero = new Coordinate()
            {
                X = 0,
                Y = 0
            };

            MoveHero(0, 0);

        }
    }

    /// <summary>
    /// Represents a map coordinate
    /// </summary>
    class Coordinate
    {
        public int X { get; set; } //Left
        public int Y { get; set; } //Top
    }
}



This is how it could look (I've moved my character a couple of steps):

Attached Image

Is This A Good Question/Topic? 4
  • +

Replies To: A basic C# console game movement engine

#2 rnty  Icon User is offline

  • D.I.C Head

Reputation: 19
  • View blog
  • Posts: 166
  • Joined: 14-August 12

Posted 16 October 2012 - 03:35 AM

Loved it.One more Console game please.

ONE MAN WITH A REQUEST MAKES AN ARMY.

Please one more console based game.Preference:Complicated type.
Was This Post Helpful? 2
  • +
  • -

#3 lloricode  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 21-May 14

Posted 21 May 2014 - 11:39 PM

waw thanks for this very awesome code :)

View Postrnty, on 16 October 2012 - 03:35 AM, said:

Loved it.One more Console game please.

ONE MAN WITH A REQUEST MAKES AN ARMY.

Please one more console based game.Preference:Complicated type.


yeah! i more!

View Postrnty, on 16 October 2012 - 03:35 AM, said:

Loved it.One more Console game please.

ONE MAN WITH A REQUEST MAKES AN ARMY.

Please one more console based game.Preference:Complicated type.


yeah! one more!
Was This Post Helpful? 0
  • +
  • -

#4 Ald0s  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 21-September 14

Posted 21 September 2014 - 08:40 PM

This is a good tutorial, thank you.
I like the idea of these simple but attractive games.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1