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