0% found this document useful (0 votes)
8 views3 pages

Untitled Document 1

This document is a Python script for a simple car racing game using Pygame. It initializes the game, creates a car and obstacles, handles movement and collision detection, and displays the score. The game runs in a loop until the player collides with an obstacle, after which a 'Game Over' message is displayed.

Uploaded by

vaidhyanathsah77
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views3 pages

Untitled Document 1

This document is a Python script for a simple car racing game using Pygame. It initializes the game, creates a car and obstacles, handles movement and collision detection, and displays the score. The game runs in a loop until the player collides with an obstacle, after which a 'Game Over' message is displayed.

Uploaded by

vaidhyanathsah77
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

import pygame

import random
import sys

# Initialize Pygame
pygame.init()

# Screen dimensions
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600

# Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)

# Initialize screen
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Car Racing Game")

# Clock for controlling frame rate


clock = pygame.time.Clock()

# Load images
car_img = pygame.image.load("car.png") # Replace with your car image
car_img = pygame.transform.scale(car_img, (50, 100))
obstacle_img = pygame.image.load("obstacle.png") # Replace with your obstacle image
obstacle_img = pygame.transform.scale(obstacle_img, (50, 50))

# Car class
class Car:
def __init__(self):
self.image = car_img
self.rect = self.image.get_rect()
self.rect.center = (SCREEN_WIDTH // 2, SCREEN_HEIGHT - 100)
self.speed = 5

def move(self):
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and self.rect.left > 0:
self.rect.x -= self.speed
if keys[pygame.K_RIGHT] and self.rect.right < SCREEN_WIDTH:
self.rect.x += self.speed

def draw(self):
screen.blit(self.image, self.rect)
# Obstacle class
class Obstacle:
def __init__(self):
self.image = obstacle_img
self.rect = self.image.get_rect()
self.rect.x = random.randint(0, SCREEN_WIDTH - self.rect.width)
self.rect.y = -self.rect.height
self.speed = random.randint(5, 10)

def move(self):
self.rect.y += self.speed

def draw(self):
screen.blit(self.image, self.rect)

def off_screen(self):
return self.rect.y > SCREEN_HEIGHT

# Game variables
car = Car()
obstacles = []
spawn_timer = 0
score = 0
font = pygame.font.SysFont("comicsans", 40)

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

# Spawn obstacles
spawn_timer += 1
if spawn_timer > 60: # Spawn a new obstacle every second
obstacles.append(Obstacle())
spawn_timer = 0

# Update game objects


car.move()
for obstacle in obstacles:
obstacle.move()
if obstacle.off_screen():
obstacles.remove(obstacle)
score += 1

# Check for collisions


for obstacle in obstacles:
if car.rect.colliderect(obstacle.rect):
running = False

# Draw everything
screen.fill(WHITE)
car.draw()
for obstacle in obstacles:
obstacle.draw()

# Display score
score_text = font.render(f"Score: {score}", True, BLACK)
screen.blit(score_text, (10, 10))

# Update display
pygame.display.update()

# Cap frame rate


clock.tick(60)

# Game over
game_over_text = font.render("Game Over!", True, RED)
screen.blit(game_over_text, (SCREEN_WIDTH // 2 - 100, SCREEN_HEIGHT // 2))
pygame.display.update()
pygame.time.wait(2000)

# Quit Pygame
pygame.quit()
sys.exit()

You might also like