Wednesday, 22 November 2017

Adding a start screen & a Game Over screen!

We are nearly done with completing the game fully! The one few things left are adding music and the start and finish screens.
In this blog, I will start of with how you add an opening screen to the game and to start the game once the player hits any key. 

The first thing we need to add is two functions. One called waitForPlayerToPressKey and the other drawText.  The waitForPlayerToPressKey function must include a while loop that is set to True so it always runs. In this while loop, is a for loop. This for loop is the same for loop as the one we used when setting up the game loop. Then, two if statements are needed, one for if the player decides to quit the game instead of playing it which is just calling the terminate function. The second if statement is the event.type == KEYDOWN, which means if any key is pressed. This just needs to be returned. 

The second function drawText will pass in the text, font, surface, x and y.  You need to create a new object so you can call the render method. I called my object textobj and passed in text, True and the colour. Next, you need to know the size and location of the Surface object. You can get a Rect object with this information from the get_rect Surface method. The Rect object returned from get_rect has a copy of the width and height information from the Surface object which is saved in textRect. We then need to change the location of the textRect by setting a new value for its top-left attribute. At the end you just blit the text onto the screen by passing in textobj and textrect as the parameters.
Once this is done these two functions are all good to use! 


So, when setting up the start screen, we called the drawText method for each line of text we want to add in passing in the text we want displayed, the font variable that we set up in a previous blog, windowSurface and the the x and y position we want to set the top left of the text to. Then we need to update the game and call the waitForPlayerToPressKey function.


The same principles apply for creating the screen when the game is over. Only this time the code is added in down the bottom where you are terminating the game.



No comments:

Post a Comment