Battle Balloons Week 2
Player Movement
This post covers the exercises and code written for week 2 of the Battle Balloons Java programming course.
Last week we started with an empty arena and we simply drew our heroine Lexi on the screen as a sprite. This week, we will make Lexi move with keyboard controls.
Setup. It is assumed that you have your computer setup as outlined in the setup from week 1.
Step 1. First, download or clone the code for week 2. If you download the zip file, you will need to unzip it.
Step 2. Open the source code in your editor then open your terminal and navigate to the directory where you downloaded the source code.
Step 3. Now that we will be animating our character, the game will be updating at regular intervals. To show this happening, we will add log statement that tells us which keys are being pressed. To do this, open the file `src/main/java/bb/input/KeyboardManager.java` and add the following to the first line of the updatePlayerIntent
method:
log.trace("keyCode={} onOff={}", keyCode, value);
This will tell us which key code is being pressed and also illustrates that the game is updating at a regular interval (which is required for animation).
Save the file and compile and run the game by going to the terminal and typing:
gradle run
Step 4. Now, we need to tell the program to update not only the gameModel
at the regular interval but also the player
(Lexi). First, add the following to the actionPerformed
method in the file src/main/java/bb/BB.java:
gameModel.update();
Next, add the following to the update
method in the file src/main/java/bb/model/GameModel.java:
player.update();
If we look at the player.update()
method in src/main/java/bb/model/Player.java
, you’ll notice that there is nothing there. This is where we need to connect the key events to the change of the position of the player.
Step 5. To start out, lets just add control for one direction. In the Player.java
file, in the update method, add the following code:
int dx = 0;
int dy = 0;if (moveIntent.up) {
dy -= PLAYER_SPEED;
}this.x += dx;
this.y -= dy;
Now, compile and rerun the game. Use the key T
to move up.
You can experiment simply here by changing some of the code we just wrote.
- What happens if we change the minus(
—
) indy -= PLAYER_SPEED
to a plus (+
)? PLAYER_SPEED
is set in the filesrc/main/java/bb/BBConfig
, what happens when we change that value?
Step 6. Now that we’ve added the control to move up, lets add the rest. In the file Player.java
in the update
method, after the first if
statement, add the following code:
if (moveIntent.down) {
dy += PLAYER_SPEED;
}
if (moveIntent.left) {
dx -= PLAYER_SPEED;
}
if (moveIntent.right) {
dx += PLAYER_SPEED;
}
Now, compile and rerun the game. We should now be able to move the player in all directions.
Step 7. In our game, we want to keep Lexi from going outside of our arena. We can do this by adding the enforceBounds
method, as the last item, to our update
method. Once this is done, our update
method should look like this:
int dx = 0;
int dy = 0;
if (moveIntent.up) {
dy -= PLAYER_SPEED;
}
if (moveIntent.down) {
dy += PLAYER_SPEED;
}
if (moveIntent.left) {
dx -= PLAYER_SPEED;
}
if (moveIntent.right) {
dx += PLAYER_SPEED;
}
this.x += dx;
this.y += dy;
enforceBounds();
There we go! Lexi is now free to roam in our arena. Next week, we will learn how to animate Lexi.
Optional Experimentations.
- To explore how we can move Lexi around, we make changes to a few methods. We can continue to modify the update method as outlined above; modifying
dx
anddy
under the different directionif
statements. - In the
enforceBounds
method, try swapping the contents of each if statement around by simply copying and pasting. This will change the behavior of the player when the arena edge is hit.