Sitemap
DataDrivenInvestor

empowerment through data, knowledge, and expertise. Join DDI community at https://join.datadriveninvestor.com

Basic Car-Game using python(Part -6)

--

In the previous part, we learned how to add the restriction to the car. Now we will go one step ahead and will display text, the moment car touches the road borders.

car_crash message

There are a couple of ways to display the text on the game screen:
>>> pygame.font.Font() -create a new Font object from a file
>>> pygame.font.SysFont() -create a Font object from the system fonts

In this article, we will be talking about the SysFont().

SysFont() accepts 4 arguments, in which 2 are optional.
pygame.font.SysFont(fontname,size, bold=False{optional}, italic=Flase{optional})

>>> pygame.font.SysFont(None,’100'):
* Here, “None” means the default font-name, that is set in the method
* And size is 100(say)

>>> myfont = pygame.font.SysFont(“None”,100)
We have created the object for the font.

(Note:- Pygame doesn’t provide a direct way to draw the text on the surface.)
We have to use the render method inside this font method to draw the text on the surface. This render method will create the image of the text. The way we add an image on the screen, the same way we will add text on the screen.
Render() accepts 4 arguments, in which 1 is optional.
pygame.font.render(text, antialias, color, background=None)

>>> render_text = myfont.render(“CAR-CRASHED”,1,(255,255,255))
* Here, “CAR-CRASHED” is the message which we will display on the screen
* “1” is the anti-alias value, which gives smooth edges for the text when it is true, otherwise the edge will be little blurry if the value is false or ‘0’.
* (255,255,255) will be the color of the text in RGB format.

Now, we have to use the blit method to draw this text object on the screen. I have already used the blit method in my previous articles, you can also check the official documentation of the blit method here.

Note:- We have to write the blit statement under the condition of the road-border logic.

car_crash logic

Remove the line “run=True” from the above code and write:

>>> screen.blit(render_text,(80,200))

Get Asish Raz’s stories in your inbox

Join Medium for free to get updates from this writer.

After this, the code will look like this:

car_crash logic

Run the code and I am assuming that we will see the below output:

Press enter or click to view image in full size
CAR CRASHED

Now you can change the font-size and color according to your convenience.
>>> myfont = pygame.font.SysFont(“None”,120)
>>> render_text = myfont.render(“CAR CRASHED”,1,(0,0,0))
>>> screen.blit(render_text,(120,200))

We can see the output like this:

Press enter or click to view image in full size
car crashed

One last thing, when the car-crashed message is getting displayed, we should restart the game after a while. And we should not re-run the code every time but we should write a logic so that after a few seconds, the game should restart.

Import the time module for doing the above thing:
>>> import time

Then in the road_crash logic, after displaying the text, we need to put the timer for 3 seconds(say) and then, we need to call the game_loop function.

timer after crash

Don’t forget to update the screen.

game restart

That’s it…
We learned how to add text on the gaming screen. Short and simple.
I hope, you liked this article and in case you have any doubt regarding the above code, please let me know in the comment section.
Happy to help…

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?

Thank You!!!
Never settle and always Hustle!!!
☺
-Gareeb CODER

(Thanks for your time and do encourage me to write more by clapping.)

Gain Access to Expert View — Subscribe to DDI Intel

--

--