Java Roguelike Tutorial Lesson Two
This is part of a series of tutorials; the main page can be found here.
So, before we continue, let's take a look at what we can do now. We can create a game window and print colored text into it.
I think you'll agree with me that the next logical step would be to print some sweet @s. However, that would be too easy. We want an @that MOVES. *gasp!*
Now let's take a look at the code from lesson one. We are going to simply enhance that code and replace the boring csi.print() with something more exciting.
The Moving @
Let's just dive right into the code!
Alright, variables. If we want a moving @, we need coordinates for it. These are, logically, made of x and y. We want to start off in the upper left corner, so both are zero.
The whole thing is going to run in a loop, until we press a certain button to quit. Therefore, we need to have a variable that tells the loop whether it should continue. Let's call it stop, and we'll set it to false initially. If we set it true, the loop will stop.
And here's our loop.
Urgh, what a big bad chunk of code. Let's tackle it in parts.
Okay, we have the loop head. While stop is not true, do the following. Let's first clear the screen with cls(). It removes anything that was in the game window. What we want to remove here is the previous @, if there was one. Then we print a new @ in white, and we'll print it at the coordinates x,y. Then, we refresh the screen to make all of that visible on the screen.
First, we wait for a keypress. csi.inkey() gives us a CharKey object, which offers us several options of identifying the key that was pressed.
The if clause below that line is just one of five clauses - four for the four directions, one for our quit button. They may look scary, but they all work the same way. If one of the arrow keys was pressed, then you can find out which one it was by calling the methods isUpArrow, isDownArrow, isLeftArrow and isRightArrow.
In this case, if the Up arrow key was pressed, we want to go up, that means we have to decrease our y ordinate by one. However, we have to make sure we don't run off the screen (that would result in an exception), so we also have to check whether our future y ordinate is still inside the window boundaries. Our game window is 80x25, so the y ordinate must be at least zero, but smaller than 25. And our x ordinate must be at least zero, but smaller than 80. It is that easy. All of the other cases work exactly the same way. Take a look at the big block above and try to figure out which boundaries we have to check for, and on which side of the game window they are.
Every CharKey object has a code attribute that's public, meaning you can access it. The CharKey class has all of the possible key codes listed, so you just compare the code of the CharKey object with the code of the key you want to check for. In this case, we want to know whether dir is the key Q, and therefore we compare dir.code with CharKey.Q. If that is the case, then we want to set stop true, so in the next turn, the loop won't be entered anymore.
And that is it! We are ready to... no, wait. CharKey? Have we had that class before? No, we'll have to import it first. Just add
to the other imports.
Now we are really done. Compile and run with the commands outlined in Lesson One and admire the wonders of the moving @!
You may proceed to the next lesson. :D
Code available here.