Mouse input - Shoot the duck

In this java tutorial you will learn how to get mouse input.
The goal of this game will be to kill as many ducks as possible before 200 ducks runaway.

Shoot the duck 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 and how to close it. Player will be able to start the game with clicking left mouse button. When player starts the game we will call newGame() method that will create the game and start it. Game will close by pressing ESC key.
In the game ducks will come from right side of the screen and they will sailed toward the left side of the screen. When player will click on ducks head or body, the duck will disappear. We will have statistic for how many ducks were killed, how many ducks runaway, how many shots were fired and for each duck that was killed we will add points. We will have four lines of ducks and in each line ducks will have different speed. Faster ducks will be worth more. When 200 ducks runs away we will end the game and draw to the screen how to restart the game.

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 five classes – four from game framework and one additional – Window.java, Canvas.java, Framework.java, Game.java and Duck.java.

Window.java

In this class I set up game title to be “Shoot the duck” and that game will be in full screen mode.

Canvas.java

In this class I set up that mouse cursor disappear because we will draw shotgun sight.

Framework.java

Like in previous tutorial we have in this class main menu image that we draw when we are in “MAIN_MENU” state of the game. When we are in this game state we also draw to the screen some text (how to start the game, …).
Like I said it before the game starts when player clicks left mouse button. So let’s take a look at mouseClicked(MouseEvent e) method. So this method is called every time that mouse button is clicked and we check in it if left mouse button is clicked when we are in “MAIN_MENU” state. If it was clicked, then we call newGame() method.
In keyReleasedFramework(KeyEvent e) method I put code that checks if ESC key was released when we are in “GAMEOVER”, “PLAYING” or “MAIN_MENU” state and if it was, then we close the game. In this method in “GAMEOVER” state I also checks if space or enter key was released and if it was, then we restart the game.

Game.java

In this class we have few variables for recording the statistics and few for images. An important variable is array list ducks. In this variable we will save all ducks that we create.
Let’s take a look at UpdateGame(long gameTime, Point mousePosition) method. In this method we create a new ducks, if it's the time, and add them to the array list. We also update all this ducks – we move them and check if are still on the screen. Then we check if player shoots and if he is, then we check if he is hit some. Last thing that we do here, is to check how many ducks run away and change game state to “GAMEOVER” if necessary. When we create a duck, we create it with parameters for line on which we are adding a duck. Lines and its properties are specified in Ducks.java class in table duckLines. When we create a new duck you can see that we add to x coordinate of a duck random number between 0 and 200. We do that so that the distance between ducks isn’t always the same.

	ducks.add(new Duck(
	Duck.duckLines[Duck.nextDuckLines][0] + random.nextInt(200), 
	Duck.duckLines[Duck.nextDuckLines][1], 
	Duck.duckLines[Duck.nextDuckLines][2], 
	Duck.duckLines[Duck.nextDuckLines][3], duckImg));
	

When a player shoots and we go through all ducks with for loop to check if player hit some, we check if duck was hit into head or into body.

	if(new Rectangle(ducks.get(i).x + 18, ducks.get(i).y
	                 , 27, 30).contains(mousePosition) ||
	   new Rectangle(ducks.get(i).x + 30, ducks.get(i).y + 30
	                 , 88, 25).contains(mousePosition))
	

If we draw rectangles with those properties above we can see hit area. Put code below into Draw(Graphics2D g2d, Point mousePosition) method into for loop where we draw all the ducks and you will see.

	g2d.drawRect(ducks.get(i).x + 18, ducks.get(i).y     , 27, 30);
	g2d.drawRect(ducks.get(i).x + 30, ducks.get(i).y + 30, 88, 25);
	
Duck.java

In this class we have some static variable that holds information about when is the time to create new duck (timeBetweenDucks, lastDuckTime), on which line a duck have to be created (nextDuckLines) and information about lines and ducks on each line (duckLines).
Then we have variables that represent a duck. We have position variables x and y, variable for duck speed, variable that holds scores for this duck and last one is image of a duck.
In Update() method we move the duck.
In Draw(Graphics2D g2d) method we draw the duck on the screen.



Download game  |  Java web start
Download source code