Keyboard input - Moon lander

In this java tutorial you will learn how to get keyboard input and moving the object based on that input.
We will make a game with the goal to land the rocket on the landing area as fast as possible.

Moon lander game screenshot

What we will do

First when game opens we will show main menu image with background and game title and text that let player know how to start the game. Player will be able to start the game with pressing any key.
When player starts the game we will call newGame() method that will create the game and start it. In the game player will have to softly land the rocket. If the rockets speed is too high when landing or if player trays to land out of landing area, then the rocket crashes. Gravity will pull the rocket down to the moon. Player will control the rocket with W A D keys. When player lands or crash the rocket the game will ends.
When the game ends we will draw on the screen how to restart the game. Player will be able to restart the game with space or enter key.

Download the game source code at the bottom of this page so you could check it while reading this. Also the source code is commented, so read the comments.

Game classes

This game has six classes – four from game framework and two additional – Window.java, Canvas.java, Framework.java, Game.java, PlayerRocket.java and LandingArea.java.

Window.java

In this class I set up game title to be “Moon lander” and window size to 800 x 600.

Canvas.java

In this class I didn’t set anything.

Framework.java

In this class I add one variable – moonLanderMenuImg. Its type is BufferedImage and will contain image for main menu. In method LoadContent() I add code to load this image.
Into Draw(Graphics2D g2d) method I have inserted code for three states. Into “GAMEOVER” state I add call to game.DrawGameOver(g2d, mousePosition(), gameTime) method that draws a “game over screen”. We will see this method in Game.java class. The second state into which I inserted the code is “MAIN_MENU”. Here I draw the image for main menu and text which tells player how to start and which keys to use for playing. Last, the third state is “GAME_CONTENT_LOADING”; in this state I add code to draw on the screen text “game is loading”.
Last thing that I add in this class is code in keyReleasedFramework(KeyEvent e) method. Here I add switch statement for game states. When we are in “MAIN_MENU” state and any key is released a newGame() method will be called so that the game will begin. And when we are in “GAMEOVER” state we check if space or enter key were released and if they were we call restartGame() method.

Game.java

Here we have four variables. One represents rocket – playerRocket; another represents landing area – landingArea. Another two are images; one for the background, another one for red frame border when rocket crashed.
We initialize variables and load images in methods Initialize() and LoadContent().
In the RestartGame() method we call playerRocket.ResetPlayer(); so that rocket is reset when we restart the game.
In the UpdateGame(long gameTime, Point mousePosition) method we move rocket and check if the rocket is still in the space or is it landed or crashed.
In the Draw(Graphics2D g2d, Point mousePosition) method we draw game background, the landing area and the rocket.
In DrawGameOver(Graphics2D g2d, Point mousePosition, long gameTime) method we draw “game over screen”. First we call Draw(g2d, mousePosition) method so that game background, the landing area and the rocket are drowned. Then we draw some text and red frame border depending on whether the rocket is landed or crashed.

LandingArea.java

This class represents a landing area on which the rocket will have to land.

We have the variables x and y representing the coordinates of where the landing area will be drawn on the game frame/window. In the upper left corner coordinates are 0:0.
Next variable is landingAreaImg which will contain image of landing area. Last variable is landingAreaImgWidth which will contain width of landing area image.

In Initialize() method we set x and y coordinate for landing area.
In LoadContent() method we load an image of landing area and initialize variable landingAreaImgWidth now that we can get width of the image. In Draw(Graphics2D g2d) method we draw landing area on the screen.

PlayerRocket.java

This class represents a rocket that player will have to land.

Here we have variables x and y for the rocket coordinates that we will change during the game. Then we have boolean variables landed and crashed that indicate whether the rocket is landed or crashed, if both are false then rocket is still in the space. Then we have some other variables, just look at the source code and read the comments.

In the class constructor we set starting x coordinate for the rocket. The reason why here, is because we dynamically set starting x coordinate for the rocket and in Initialize() method we didn’t yet have a width of the rocket image, but here we have.

Let’s take a look at the Update() method. In this method we move the rocket. First we check if player hold down W key and if it, then we subtract variable speedY by speedAccelerating. If W key isn’t down then we add to variable speedY. Then we do almost the same for moving the rocket left. Only difference here is that we only add to speedX until the rocket stops, until we reach the zero. We do the same for moving right except that we add to speedX when player holds down D key.
At the end of Update() method we actually move the rocket – we change the rocket coordinates by the speed that we calculated above.

In Draw(Graphics2D g2d) method we draw rocket coordinates and the rocket image.

Exercise

Make an meteor between rocket and moon so that player will have to avoid it.



Download game  |  Java web start
Download source code