In this article, you will get a simple step-by-step guide on how to create a racing game in Python. The library that we are going to use for this project is Pygame. Conceptually, the game is about driving a car along an endless track with other cars that may either move or destruct in front of you. You’ll be required to control your vehicle using arrow keys (left, right, and up) and If you crash into another car you’ll lose so be careful. Let’s start building it!

Steps on how to create a racing game in Python

  1. Install and Import required libraries.
  2. Set display size, color, and window name of the game.
  3. Set car image and background image in the display.
  4. Show game over message and restart the game.
  5. Set the car position.
  6. Set movement of the car
  7. Apply restrictions to the car.
  8. Police cars will come randomly.
  9. Quit code to stop the game.

Prerequisites

Pygame:

Pygame is a set of Python modules that provides the programmer with low-level access to the hardware. It is designed to help make highly-interactive games. Pygame is a cross-platform, free, and open-source that has been around for many years.

To install pygame use the following command in your terminal or command prompt

pip install pygame

Step 1: Import required libraries

#import pygame package
import pygame 
#import the time package to restart the game
import time   
#import for the random library to randomize the car coming from the opposite side.
import random   

Step 2: Set display size, color, and window name of the game

#initiate package
pygame.init() 
#color code in RGB form
gray=(60,60,60)
#color code in RGB form
black=(255,0,0) 
#set width & height of the display
display=pygame.display.set_mode((830,600))
#set name of the game window
pygame.display.set_caption("Racing game built with python")    

Step 3: Set car and background image in the display

#load car image
carimg=pygame.image.load("car1.png")
#load background image for the left side
backgroundleft=pygame.image.load("left.png")
#load background image for the right side
backgroundright=pygame.image.load("right.png")
#Defined car width
car_width=23

#define car functions that are coming from the opposite side.#(we are naming car that are coming from the opposite side policecar)
def policecar(police_startx,police_starty,police):
    if police==0:
        #for police car no 2
        police_come=pygame.image.load("car2.png") 
    if police==1: 
        #for police car no 3
        police_come=pygame.image.load("car3.png")
    if police==2:
        #for police car no 1
        police_come=pygame.image.load("car1.png") 
    #display the police car
    display.blit(police_come,(police_startx,police_starty))

def background():
    #defining the position of background image for left side in x axis & y axis
    display.blit(backgroundleft,(0,0))
    #defining the position of background image for right side in x axis & y axis
    display.blit(backgroundright,(700,0)) 

Step 4: Set crash message then restart the game

#creating the function that will display the game over message
def crash():       
    message_display("Game Over")

#create function for customizing the game over message
def message_display(text):     
    #set font style and size of the message
    large_text=pygame.font.Font("freesansbold.ttf",80) 
    #set function to edit the message
    textsurf,textrect=text_object(text,large_text) 
    #set the position of the message on the screen
    textrect.center=((400),(300)) 
    #display the message
    display.blit(textsurf,textrect)
    pygame.display.update()
    #After the car has crashed, wait 3 seconds.
    time.sleep(3)     
    #call the loop function to restart the game
    loop()     

#This function will display the message after the car has crashed.
def text_object(text,font):    
    #set color of the message
    text_surface=font.render(text,True,black) 
    #after that restart the game & ready to give some input 
    return text_surface,text_surface.get_rect()  

Step 5: Set car position

#create car function
def car(x,y): 
    #set position of the car
    display.blit(carimg,(x,y))

#all the function are called using this function
def loop():
    #set car position for x and y axis  
    x=400 
    y=540 
    #set changing position of the car
    x_change=0 
    y_change=0
    #set police car speed
    policecar_speed=9
    #set starting stage for the police car 
    police=0   
    #with this police car will come randomly
    police_startx=random.randrange(130,(700-car_width))
    #police car will comes in y axis in negative value because car is coming from opposite side 
    police_starty=-600
    # set police car height and width
    police_width=23
    police_height=47

Step 6: Set movement of the car

#if the game doesn't have any problem to start
    bumped=False
    #start the game 
    while not bumped: 
        #defining the input of the game
        for event in pygame.event.get():   
            #if quit input is given
            if event.type==pygame.QUIT:   
            #   bumped=True     #game will stop
                pygame.quit()
                quit()

            #defining the arrow keys
            if event.type==pygame.KEYDOWN: 
                #if user is pressing the left arrow
                if event.key==pygame.K_LEFT:
                    #car will move left side -1 
                    x_change=-1   
                #if user is pressing the right arrow
                if event.key==pygame.K_RIGHT: 
                    #car will move left side 1
                    x_change=1     
            #if any key is not being pressed then stop the car
            if event.type==pygame.KEYUP:   
                x_change=0
        x+=x_change

Step 7: Apply restrictions to the car

        #setting the color of the road
        display.fill(gray) 
        #car speed that are coming from opposite side(y axis)
        background()
        police_starty-=(policecar_speed/1.2)   
        policecar(police_startx,police_starty,police) 
        #police car speed will increase slowly
        police_starty+=policecar_speed         
        car(x,y)   
        #if the car goes out of range(side wall of the road)
        if x<130 or x>700-car_width:       
        #bumped=True    #game is over
            #call crash function
            crash()

Step 8: Police cars will come randomly

        #seting how far the police car will go
        if police_starty>600:     
            #only one car will cross the road in one time
            police_starty=0-police_height 
            #then other car will come
            police_startx=random.randrange(130,(1000-300)) 
            #set how many car will come
            police=random.randrange(0,2)   

        #if the police car does not cross the road then crash the car
        if y<police_starty+police_height:
            if x > police_startx and x < police_startx + police_width or x + car_width > police_startx and x + car_width < police_startx + police_width :
                crash()   

Step 9: quit code to stop the game

        #restart the game
        pygame.display.update() 
loop() # exiting from game
pygame.quit() 
quit()

Output

Final Words

Thank you so much for reading this tutorial, we are really happy that you find it useful and informative. In the tutorial, we learned about how to create a car racing game using the popular library pygame, which is used to create fun games in Python. We learned how to manipulate the movement of a race car in your program by applying various “speeds” and how to make the car collide with the wall and other cars. You can also add different types of cars change the background to your liking etc.

Here are some useful tutorials that you can read: