Part 7

Adding a score to the game


Now that the game is all working, one more thing that we can do is add a scoring system, that way players know how well they are doing.

Still in the “Main” file, add the code as shown here.

Luckily, our speed goes up by one every time we hit the ball, so we can use this to keep track of our score.

Getting the score to appear


Now that we have a score, we need some code to show it on the screen.

Inside the “paint” function, add the code as shown here.

The first two lines set the color and the font we use for the text.

We then use the “drawString” function to draw the score on the given coordinates, here x = 10 and y = 30.

Showing score on game over


Lastly, go back to where we coded the “Game Over” message. Let’s also add our score in this message.

Testing the final game


Now let’s test it all out! Run the code and you should see your score in the game now. If not, go back and double check your code for errors.

Congratulations! You’ve now completed your very first video game in Java! See if there are any things you may want to add/modify in this game, challenge yourself and see if you can code these yourself. The possibilites are endless!