Basic Car-Game using Python (Part -2)

Asish Raz
DataDrivenInvestor
Published in
3 min readMay 10, 2020

--

We have created the gaming screen, and now we are good to go for further implementations. In case you missed the first part, please click here.
At first, we need to learn, how to change the console name. As we can see, we are having the default name ‘pygame window’ and that can be changed.

pygame window

There is a method called ‘set_caption()’ in pygame.display module.
>>> pygame.display.set_caption(“CAR GAME”)
Save the code and run it. We can see, the gaming console name change to “CAR GAME”.

CAR GAME caption

Now, we will add the background color for the window. For that, we have to fill the color above the surface of the screen/window.
>>> screen.fill((120,120,120))
fill() method is responsible for filling the surface with a solid color. And it accepts the argument in the tuple.
Here, the color format is RGB. And we are changing the background color from black to grey.
After writing this line, we need to update the screen by update() method. Otherwise, the background color will not be going to change. This fill() is a function under pygame.display module.
>>> pygame.display.update()

bg color of the console

Before going further, we should make a game loop where we will add all the functionality of the game.
First, take a variable that will accept the boolean value. And we can use this variable for running the game loop.
>>> run = False
Now, whenever the condition is true, the game loop will run and keep on running, until the condition goes wrong.
Then we have to create a logic for quit operation, as of now, we are unable to close the gaming window. Let's create an event handling logic.
Whenever the mouse clicks on the CROSS (X) icon, the window/screen should get closed. This can be done through event handling.

First, we need to use pygame.event module, which is responsible for interacting with events and queues in pygame. To capture the event, we need to use the method inside this module called, get() method. We have to iterate over this method and capture the event type and once the event type is pygame.QUIT, the window gets closed.
pygame.quit() method, which uninitialized all pygame modules.

Save the code and run it, and now you can see the moment you click on the CLOSE icon, the game window will disappear or gets closed.

Great!!!
We have reached the end of this section and I hope, you all understood this article and able to execute all the logic which we learned in this part.
If in case, you have any issue or if you haven’t understood any portion of the article, please feel free to mention or highlight the parts. I will be helping you all. I will be posting the next article, at the earliest.
And in case, you want to see the video of the above code, please click here.
I have written a few articles on other games too, do visit my page in case you are interested in learning coding through games. Please click here and let me know, what do you think about it?

Till then, KEEP CODING, KEEP LEARNING!!!
-Gareeb CODER

--

--

entrepreneur, moody, moody writer, moody singer, traveller, hangout lover