0% found this document useful (0 votes)
10 views

Import pygame-WPS Office

car code game

Uploaded by

mohammedkghazal
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Import pygame-WPS Office

car code game

Uploaded by

mohammedkghazal
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

import pygame

import random

# Initialize the game

pygame.init()

# Define colors

WHITE = (255, 255, 255)

BLACK = (0, 0, 0)

RED = (255, 0, 0)

GREEN = (0, 255, 0)

BLUE = (0, 0, 255)

# Set display dimensions

DISPLAY_WIDTH = 800

DISPLAY_HEIGHT = 600

CAR_WIDTH = 50

CAR_HEIGHT = 100

FINISH_LINE_Y = 100 # Y-position of the finish line

# Set frames per second

FPS = 60

# Create the game window

game_display = pygame.display.set_mode((DISPLAY_WIDTH, DISPLAY_HEIGHT))


pygame.display.set_caption("Car Game with Score and Finish Line")

# Create clock object to manage game speed

clock = pygame.time.Clock()

# Function to draw the car as a rectangle

def draw_car(x, y):

pygame.draw.rect(game_display, GREEN, [x, y, CAR_WIDTH, CAR_HEIGHT])

# Function to generate obstacles

def generate_obstacle(obstacle_x, obstacle_y, obstacle_w, obstacle_h, color):

pygame.draw.rect(game_display, color, [obstacle_x, obstacle_y, obstacle_w, obstacle_h])

# Function to draw the finish line

def draw_finish_line():

pygame.draw.line(game_display, BLUE, (0, FINISH_LINE_Y), (DISPLAY_WIDTH, FINISH_LINE_Y), 10)

# Function to display text

def display_message(text):

font = pygame.font.SysFont(None, 75)

text_surface = font.render(text, True, RED)

text_rect = text_surface.get_rect(center=(DISPLAY_WIDTH / 2, DISPLAY_HEIGHT / 2))

game_display.blit(text_surface, text_rect)

pygame.display.update()

pygame.time.wait(2000)
# Function to display score

def display_score(score):

font = pygame.font.SysFont(None, 35)

score_text = font.render("Score: " + str(score), True, BLACK)

game_display.blit(score_text, (10, 10))

# Function to handle crash

def crash():

display_message("You Crashed!")

game_loop()

# Function to handle win

def win():

display_message("You Win!")

game_loop()

# Main game loop

def game_loop():

# Starting position of the car

car_x = DISPLAY_WIDTH * 0.45

car_y = DISPLAY_HEIGHT * 0.8

# Car movement speed

car_x_change = 0
# Obstacle parameters

obstacle_x = random.randrange(0, DISPLAY_WIDTH)

obstacle_y = -600

obstacle_speed = 7

obstacle_width = 100

obstacle_height = 100

# Initial score

score = 0

game_exit = False

while not game_exit:

for event in pygame.event.get():

if event.type == pygame.QUIT:

pygame.quit()

quit()

# Detect key presses to move the car

if event.type == pygame.KEYDOWN:

if event.key == pygame.K_LEFT:

car_x_change = -5

if event.key == pygame.K_RIGHT:

car_x_change = 5
if event.type == pygame.KEYUP:

if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:

car_x_change = 0

# Update car position

car_x += car_x_change

# Fill the background

game_display.fill(WHITE)

# Draw the finish line

draw_finish_line()

# Draw obstacles

generate_obstacle(obstacle_x, obstacle_y, obstacle_width, obstacle_height, BLACK)

obstacle_y += obstacle_speed

# Draw the car

draw_car(car_x, car_y)

# Update and display the score

score += 1 # Increment score each frame

display_score(score)
# Check for collisions

if car_x > DISPLAY_WIDTH - CAR_WIDTH or car_x < 0:

crash()

if obstacle_y > DISPLAY_HEIGHT:

obstacle_y = 0 - obstacle_height

obstacle_x = random.randrange(0, DISPLAY_WIDTH)

# Check if car hits an obstacle

if car_y < obstacle_y + obstacle_height:

if car_x > obstacle_x and car_x < obstacle_x + obstacle_width or \

car_x + CAR_WIDTH > obstacle_x and car_x + CAR_WIDTH < obstacle_x + obstacle_width:

crash()

# Check if car crosses the finish line

if car_y < FINISH_LINE_Y:

win()

pygame.display.update()

clock.tick(FPS)

# Start the game

game_loop()

# Quit pygame
pygame.quit()

quit()

You might also like