Midterm – Reflex Shot

Game Idea

Reflex Shot is a 2D arcade shooting game in which enemies pop up on the screen at random locations and the user has to click them as fast as possible to score points. There is a crosshair on screen at all times and each time a shot is fired a gun fire sound is played. Each game lasts 60 seconds and at the end, the final score, high score, accuracy, number of targets hit, number of targets missed and average time taken to hit a target will be displayed on the screen. The game begins with a starting page which gives the player the option to start of change crosshair size. When the start button is clicked, the game instructions are then displayed on the screen and the user has to click the screen again for the game to start. Once the game ends, the statistics will be shown and the user will have the option to play again.

Implementation

In my code, I had 3 classes: a Game class, Crosshair class, and an Enemy class.

In the game class, I controlled what would be shown on the screen depending on the current stage of the game using functions. In the draw function, I had many else ifs with a variable that dictated which stage was currently to be displayed to decide which function within the class was to be called. Also, the Game class had variables that counted all the statistics to be shown at the end of the game. The screen backgrounds were also loaded within the class.  There were also a few functions to update the score and high score when required.

The crosshair class had two attributes, length and gap size. Each time the change crosshair size button is pressed, these two values are incremented by a fixed amount, but when a certain size is reached, the values are reset back to their original values.  Other than this, there was just the drawCrosshair function which drew the four lines required for the crosshair.

The enemy class had two attributes x and y to determine the position of the enemy on the screen, a function to move the enemy when it is shot by the user and a function to draw the enemy. The moveEnemy function is used to make it seem like a new enemy spawns when one is shot, but in reality the current enemy just moves to a different location on the canvas.

Outside these 3 classes, my mousePressed function was very important since my game uses the mouse as its only input device. I had a switch case for the game.state variable in which each case called a function to decide what each mouse click would do. Inside these functions, I used if statements when I had buttons to detect if mouse clicks were inside the buttons. Similarly, to detect if mouse clicks were on enemies, I used the mouseX and mouseY values to see if the clicks were inside an invisible rectangle around the image of the enemy. This did lead to some inaccuracies in the clicks, though, as some clicks were registered as hits even if they were in the empty space under the enemy’s arm and above the leg.

In terms of sounds, I added a gun draw sound that plays when the game starts. I had a gun fire sound that plays whenever the user shoots, and a body hit sound that doesn’t sound too pleasant but is used to indicate when an enemy is hit. When an enemy is clicked on, both the hit marker sound and the gun fire sound play at the same time therefore the gun fire sound can’t be heard when an enemy is hit. When the user misses, however, it is clearly heard and it is clear to the user that the shot didn’t hit an enemy.

Overall, I’m pretty satisfied with my work and I think it is a fun game to play. Hope you have fun playing it!

Leave a Reply