raceeeeeee

racing game
mail@pastecode.io avatar
unknown
plain_text
a year ago
1.5 kB
0
Indexable
Never
import pygame

# Initialize Pygame
pygame.init()

# Set up the screen dimensions
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Race Car Game")

# Load game assets
background_image = pygame.image.load("background.png")
car_image = pygame.image.load("car.png")

# Define variables for the car position and speed
car_x = 400
car_y = 500
car_speed = 0

# Define a function to handle user input
def handle_input():
    global car_speed
    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT]:
        car_speed -= 5
    elif keys[pygame.K_RIGHT]:
        car_speed += 5
    else:
        car_speed = 0

# Define a function to update the game state
def update():
    global car_x
    car_x += car_speed
    if car_x < 0:
        car_x = 0
    elif car_x > screen_width - car_image.get_width():
        car_x = screen_width - car_image.get_width()

# Define a function to draw the game graphics
def draw():
    screen.blit(background_image, (0, 0))
    screen.blit(car_image, (car_x, car_y))

# Game loop
running = True
while running:
    # Handle events
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    # Handle user input
    handle_input()

    # Update the game state
    update()

    # Draw the game graphics
    draw()

    # Update the screen
    pygame.display.update()

# Clean up Pygame
pygame.quit()