Tutorial Part 3: Movement and Keyboard Input

In this tutorial, we’ll look at how we can move our simple Player character around a single non-scrolling screen using SadConsole’s keyboard handling methods.

Let’s do some housekeeping first. I’m getting tired of staring at “program-example.cs”. Let’s rename the file and the class to something a little more meaningful. Rename the file to GameLoop.cs.

Namespaces

Now let’s rename the namespace and the class. At the top of the GameLoop.cs file, look for:

namespace MyProject

You’ve probably already noticed the many references to SadConsole.Game or SadConsole.Settings or SadConsole.Global. All of those use dot notation to tell the compiler we’re looking for a specific class of objects within a “namespace”. A namespace is a tree diagram of all the different kinds of classes and objects covered under one umbrella term. This tree usually has many different branches within it – for instance, the SadConsole.Entities namespace has all kinds of classes and objects and properties related to Entities. The SadConsole.Console namespace covers anything to do with Consoles. The SadConsole.Game namespace has methods and classes related to the main game loop. Hopefully this makes sense.

So let’s rename our game’s namespace. MyProject isn’t a good description of all the types of classes and methods we plan to build. Rename it to: namespace RogueTutorial. (Or, rename it to whatever you’d like!)

Classes

Now for renaming the class. If you remember from our last tutorial, a class describes an object in a general manner… Cat is the class, but MyCat is one of its instances.

The class we’ve been working, so far, handles the main game loop. Sure there is a bit of game logic in there that creates a player character, but for the most part its job has been to load the game, run it, and handle cleanup when the game closes.

class Program

Rename this to: class GameLoop

There’s another reason we’ve named it GameLoop: that’s because we renamed the file GameLoop.cs earlier. For your sanity and the sanity of others on your project, it’s very important to give your filenames the same name as the classes within them. No, the compiler won’t complain if your class has a different name than its filename… but 100 hours from now when you’re trying to find one file in a folder of hundreds, you’ll thank me!

Movement

Let’s make our little @ character walk around the console at our bidding. Remember the Keyboard handling code we wrote in the Update method in the first tutorial? We’re going to add some more keyboard handling code in the same place, this time to use the arrow keys. Below the first if(…) statement in the Update method, add the following code:

// Keyboard movement for Player character: Up arrow
if (SadConsole.Global.KeyboardState.IsKeyPressed(Microsoft.Xna.Framework.Input.Keys.Up))
{
     player.Position += new Point(0, -1);
}

Notice that we’re doing a couple of different things here:

1. We’re checking the state of the Keyboard – and we’re specifically interested in whether the Up key has been pressed. IsKeyPressed is different from IsKeyReleased. IsKeyPressed only checks if the key is currently pressed down. IsKeyReleased checks if the key was pressed, then released. IsKeyPressed lets us hold down the arrow key and motor our little @ sign around quickly.

2. We’re updating the player’s Position by adding a new Point to it. The += is the addition-assigment operator. It first adds two things together, then makes the first variable equal to that final sum. We could have wrote the same operation as:

player.Position = player.Position + new Point(0, -1);

The += operator is one of the tools in your toolbox that will delay your inevitable early onset carpal tunnel syndrome.

Back to the Position code: the Point(0, -1) we’re adding tells the compiler that the player’s X position should stay the same (by adding zero) and the player’s Y position should decrease by one. Why decrease? Because making the player @ symbol move upwards is a negative Y value in computer coordinate space. The top-left corner of the console is defined as 0,0 and the bottom-left corner of the console is width, height.

Run the program. You should be able to hold down the Up key and watch the little @ zoom upwards. You probably noticed that’s the only direction @ can walk though 🙂 Let’s address that with some more keyboard handling code:

// Keyboard movement for Player character: Down arrow
// Increment player's Y coordinate by 1
if (SadConsole.Global.KeyboardState.IsKeyPressed(Microsoft.Xna.Framework.Input.Keys.Down))
{
     player.Position += new Point(0, 1);
}

// Keyboard movement for Player character: Left arrow
// Decrement player's X coordinate by 1
if (SadConsole.Global.KeyboardState.IsKeyPressed(Microsoft.Xna.Framework.Input.Keys.Left))
{
     player.Position += new Point(-1, 0);
}

// Keyboard movement for Player character: Right arrow
// Increment player's X coordinate by 1
if (SadConsole.Global.KeyboardState.IsKeyPressed(Microsoft.Xna.Framework.Input.Keys.Right))
{
     player.Position += new Point(1, 0);
}

So now you’ve got an @ character that can zoom around the screen! Congrats 🙂

Download the final version of GameLoop.cs for this tutorial here.

In our next tutorial, we’ll work on treating the console like a moving camera using ViewPorts.

Published on October 27, 2018