Tuesday, 21 November 2017

Terminating the game!

As we have to game up and running we now need to add in a few extra pieces to complete the game. These things included the game finishing when the player reaches a certain score or is not quick enough to move the paddle and bounce the ball back up, add music into the game and allow the user to mute it if they wish to not have it playing and also add a start screen and a finishing screen showing their score.

In this post I will show you how to terminate the game. For this we need to make a function that will call the methods used to end a pygame. Functions are usually at the top of the program just below the declared variables. I have called mine terminate for convenience. In this function you should add in two lines of code. One is to call the quit method from the pygame class. The second line of code is calling the exit method from the sys class. The quit method stops the game from running and the exit method closes down the game.
This function is then used at the very end of the while loop just above where you redraw the paddle and the block. I decided that when the score is 10 or the block passes by the players paddle, the game should end. However, you can decide on a different score if you want.
So, you check this with an if statement and if true, call the terminate function. 

That's it on how to terminate the game! in the next blog I will show you how to add in a start screen and for the game to start once the player presses any key and a finish screen showing the score.




No comments:

Post a Comment