Gigi Labs

Thursday, May 9, 2013

C#: ASCII Art Game (Part 2)

Hello, and welcome to this second article about creating an ASCII-art game. This article continues where yesterday's article, C#: ASCII Art Game (Part 1), left off. So if you missed it, read it first! :) We're going to continue from the code at the end of that article.

In today's article we'll implement some basic gameplay in our console-window-based game, Chase the Star. We're going to learn about switch statements, randomness, and add a touch of colour to the game. Yippee! ;)

Let's start with that switch statement. In yesterday's code, we had a bunch of if statements that check whether the arrow keys or the ESC key was pressed. When you have a bunch of if statements checking the value of the same thing (in this case keyInfo.Key), it is more convenient to use a switch statement instead, as follows:

In the brackets next to switch, we put the variable whose value we are interested in. Then, we put a case statement for each value that interests us. Optionally, we can also include a default statement. This works pretty much like an else: it is used when none of the case values apply. A break statement must be placed at the end of all case and default sections.

Towards the end of yesterday's article, you might remember that we had a problem when the user runs off the screen. The console is usually 80 characters wide and 25 characters high, so we can check the player's position to ensure she never goes out of bounds:

Now, you'll find you can't run off the edge of the screen! If you're scratching your head a little, just remember that (0, 0) is the top-left corner. x increases as you go right, and y increases as you go down. Since both start at zero, then the highest x value we can accept is 79, not 80. Similarly, the highest y is 24.

Right now, all you can do is run around. Let's make it a little more interesting. We're going to place a star somewhere in the window, and once the user runs into it, it will appear somewhere else, Hence the name, Chase the Star. The first thing we need is a pair of new variables to store the x and y position of the star. Put these towards the beginning, before or after your x and y variables:

int starX = 64;int starY = 5;

After the part where we write our "X" to the console window, let's add code to show our star:

Here, we set the cursor position to the location where the star is. We write an asterisk to represent the star, and we write it in yellow to add a touch of colour. After that, we reset the console colour to the default light grey. If we don't, everything will be written in yellow! Here's what we've got so far:

This small piece of logic takes care of what happens when you run into the star. When that happens, we move the star somewhere else by setting starX and starY to some random values. In order to obtain a random number, we use Random.Next(). We can specify a range (as above) in which the random number should be generated. For starX, for example, we are requesting a random number between 0 and 79 (since the upper bound is not inclusive, the 80 doesn't count).

Awesome! You have just made your first game, and it's even a little bit of fun to play. :D

You should now be extremely proud of yourself, and with some more practice, you might even become a national hero at some point.

If you want to get your hands a bit dirtier, here's a little exercise. Keep track of the player's score, by adding 10 to it each time she hits the star. Display the score in the bottom left part of the console window. Don't allow the player to walk onto the last line (where y is 24).

There will be plenty more interesting articles in the coming days, so keep coming back for more! :)