Handling keyboard input

I want to be able to move the spaceship around the screen using the keyboard's arrow keys. To register whether any of the arrow keys are pressed I'll create a new class - named InputHandler - that will set a boolean variable to true whenever an arrow key is pressed.
The Player class' update() method will then check which arrow key is pressed and update the ships position accordingly.
Before we create the InputHandler class let's first add the next couple of variables to the Variables class.

Now create a new class in the (com.norakomi.mambow2 package) and name it InputHandler.

Code Analysis:

With every frame update the boolean variables corresponding to the arrow keys are set back to false (line 12-16) and subsequently set to true if the corresponding arrow key is touched (line 17-29). Don't forget to import the static variables from the Variables class (line 6).
Once you've creted InputHandler go back to ManbowGame.java and update the code liek this:

Code Analysis:

Besides some minor changes to the code we've now create an instance InputHandler (line 5+16) which update() method we call at line 27 before we call the player's update() method. In the player's update() method we'll now update the ships position and texture depending on which arrow keys are touched. But there are also a couple other changes I'd like to make. Let's have a look at our Player class:

Code Analysis:

First I've added two variables: speed on line 5 which will be used as the "speed" of the ship when the ships coordinate gets update (line 16-27) and ship that will point to one of the three ships' textures depending on which arrow key are touched (line 29-36). You'll see (line 30 f.e.) that when I check to see whether the up or down arrow is touched I also check if the opposing direction is NOT touched. If this would be the case their movement update would negate each other and the ship's texture should be the default texture (ship_middle).
When the ship gets drawn (line 41) it take whatever texture ship is pointing to and draws that texture on screen.

Float delta & DeltaTime

Like I mentioned earlier the update() method takes in a float named delta as it's parameter. If you look at our starting class' render method you'll see that the value of the float is determined by the Delta Time. The delta time is the time between two frame updates. Since our game usually runs - but not exactly(!) - at ~60 frames/second the value of delta would lie around 1/60th of a second (give or take a couple of milliseconds since the deltaTime is not constantly the same). Now when we press the right arrow key our ships position would be updated by 1/60th (deltaTime) * 60 (speed) ~= 1 pixel.
Now why would we want to make use of the delta time in our movement update if we could just update our ship position with 1 pixel with every loop through the update method?
The reason we want to do that is that say for example our frame rate drops to 30 frames/sec instead of 60 fps due to another application starts up and runs at the same time. Now in 1 second the ship would only move 30 pixels instead of 60 pixels. If we would make use of the deltaTime what would happen is that when the frame rate drops to 30 fps (half the frames per second) the value of delta would be doubled (instead of 1/60th it would be 1/30th). This would mean that the even though the ship's position gets update only half as much as it would be at 60 fps, with every frame update the position actually gets update with twice the value as it would be at 60 fps making sure the ship moves at the same speed at both frame rates.


If you test your code and fly around with your ship you'll notice that the ship is able to fly off screen. That's probably not what you want in your game.
Try to write some code yourself that prevents your ship from flying off screen and where (which class, method...) would you place it?

In the next section we'll take a look at a program called Tiled and tile maps and I'll show you how to use it to create our games' background image and more...