Java Roguelike Tutorial Lesson Two

From RogueBasin
Revision as of 17:45, 9 March 2012 by XLambda (talk | contribs)
Jump to navigation Jump to search

Lesson Two

Recap

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!


		int x = 0;
		int y = 0;
		boolean stop = false;


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.


		while(!stop){
			csi.cls();
			csi.print(x,y, '@', CSIColor.WHITE);
			csi.refresh();
			CharKey dir = csi.inkey();
			if(dir.isUpArrow()&& (y-1 >= 0)){
				y--;
			}
			if(dir.isDownArrow() && (y+1 < 25)){
				y++;
			}
			if(dir.isLeftArrow() && (x-1 >= 0)){
				x--;
			}
			if(dir.isRightArrow() && (x+1 < 80)){
				x++;
			}
			if(dir.code == CharKey.Q){
				stop = true;
			}
		}


Urgh, what a big bad chunk of code. Let's tackle it in parts.


		while(!stop){
			csi.cls();
			csi.print(x,y, '@', CSIColor.WHITE);
			csi.refresh();


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.


			CharKey dir = csi.inkey();
			if(dir.isUpArrow()&& (y-1 >= 0)){
				y--;
			}


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.


			if(dir.code == CharKey.Q){
				stop = true;
			}


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


import net.slashie.libjcsi.CharKey;


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.