Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
Resource icon

Simple pygame dodge car game - The Road

In this tutorial we will be creating the road. Although there are several ways of doing this, I created three functions.
One function for the blacktop part, one for the 2 yellow lines, and one for the white dotted line

Python:
# define a function to draw the yellow lines
def line(surf, x):
    line = pygame.Surface((2, 720))
    line.fill('yellow')
    surf.blit(line, (x, 0))

# Define a function to draw the dotted white line
def line2(surf, x):
    line = pygame.Surface((2, 20))
    line.fill('white')
    for i in range(720):
        if i % 10 == 0:
            surf.blit(line,(x, i*10))

# Define a function to draw the road
def road(screen):
    surf = pygame.Surface((300, 720))
    surf.fill('black')  
    screen.blit(surf, (490,0))

Down in the game loop I called the functions to blit the road

Python:
    # Blit the road
    road(screen)
    line(screen, 493)
    line(screen, 785)
    line2(screen, 640)


Now our car.py should look something like
Python:
# Do the imports
import pygame
import os

'''
 Get the path of the working directory
 Should get the correct path in console or editor
 We will use this to access directories containing
 images and sound
'''
path = os.path.realpath(os.path.dirname(__file__))

'''
 Initiate pygame to acces all classes and functions
'''

pygame.init()

'''
 Setup the pygame time clock.
 We will use this to set the framerate
'''
clock = pygame.time.Clock()

# Set the framerate
fps = 60

'''
 Setup our screen window. A tuple is needed for setting width and height
 I'm going to use a 1280 width and 720 height
'''
screen_size = (1280, 720)
screen = pygame.display.set_mode(screen_size)

# Set the caption for the window
pygame.display.set_caption('Dodge Car')



# define a function to draw the yellow lines
def line(surf, x):
    line = pygame.Surface((2, 720))
    line.fill('yellow')
    surf.blit(line, (x, 0))

# Define a function to draw the dotted white line
def line2(surf, x):
    line = pygame.Surface((2, 20))
    line.fill('white')
    for i in range(720):
        if i % 10 == 0:
            surf.blit(line,(x, i*10))

# Define a function to draw the road
def road(screen):
    surf = pygame.Surface((300, 720))
    surf.fill('black')  
    screen.blit(surf, (490,0))


running = True

# Start the game loop
while running:

    # Set background color
    screen.fill('seagreen')

    '''
     Get pygame events. There is 2 ways to do this.
     We can use pygame.event.poll() - return one event or
     we can use a loop and get all events. We will use the latter -
     pygame.event.get()
    '''

    # Get keys that are pressed
    key = pygame.key.get_pressed()

    for event in pygame.event.get():

        # A way to exit pygame
        if event.type == pygame.QUIT:
            running = False

        # Can press the esc key and exit the game
        if key[pygame.K_ESCAPE]:
            running = False

   
        # Blit the road
    road(screen)
    line(screen, 493)
    line(screen, 785)
    line2(screen, 640)

   
    # Update the screen
    pygame.display.update()

    # Set our framerate
    clock.tick(fps)
pygame.quit()

Which gives us this screen

In the next tutorial we will create the player and mobs
  • Kazam_screenshot_00000.png
    Kazam_screenshot_00000.png
    7.5 KB · Views: 51
Back
Top Bottom