Car Racing Game 🎮




import pygame
import random
import time
# Initialize Pygame
pygame.init()
# Game constants
WIDTH = 800
HEIGHT = 600
PLAYER_SPEED = 5
ENEMY_SPEED = 3
FPS = 60
# Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
# Set up game window
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption(“Car Racing Game”)
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, (60, 100))
enemy_img = pygame.image.load(‘enemy.png’) # Replace with enemy car image
enemy_img = pygame.transform.scale(enemy_img, (60, 100))
class Player(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = car_img
self.rect = self.image.get_rect()
self.rect.center = (WIDTH//2, HEIGHT-100)
def update(self):
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and self.rect.left > 0:
self.rect.x -= PLAYER_SPEED
if keys[pygame.K_RIGHT] and self.rect.right < WIDTH:
self.rect.x += PLAYER_SPEED
class Enemy(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = enemy_img
self.rect = self.image.get_rect()
self.rect.center = (random.randint(40, WIDTH-40), -100)
self.speed = ENEMY_SPEED
def update(self):
self.rect.y += self.speed
if self.rect.top > HEIGHT:
self.rect.center = (random.randint(40, WIDTH-40), -100)
self.speed = random.randint(3, 7)
# Create sprite groups
all_sprites = pygame.sprite.Group()
enemies = pygame.sprite.Group()
player = Player()
all_sprites.add(player)
# Create enemies
for _ in range(5):
enemy = Enemy()
all_sprites.add(enemy)
enemies.add(enemy)
# Game variables
score = 0
game_over = False
# Fonts
font = pygame.font.Font(None, 74)
def show_text(text, color, y_offset=0):
text_surface = font.render(text, True, color)
text_rect = text_surface.get_rect(center=(WIDTH//2, HEIGHT//2 + y_offset))
screen.blit(text_surface, text_rect)
# Game loop
running = True
while running:
# Event handling
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if not game_over:
# Update
all_sprites.update()
# Collision detection
if pygame.sprite.spritecollide(player, enemies, False):
game_over = True
# Score increment
score += 1
# Increase difficulty
if score % 1000 == 0:
ENEMY_SPEED += 0.5
# Drawing
screen.fill(GREEN) # Road color
# Draw road lines
pygame.draw.rect(screen, WHITE, (WIDTH//2 – 2, 0, 4, HEIGHT))
all_sprites.draw(screen)
# Display score
score_text = font.render(f”Score: {score}”, True, BLACK)
screen.blit(score_text, (10, 10))
if game_over:
show_text(“GAME OVER!”, RED)
show_text(“Press SPACE to restart”, BLACK, 60)
show_text(f”Final Score: {score}”, BLUE, 120)
keys = pygame.key.get_pressed()
if keys[pygame.K_SPACE]:
# Reset game
game_over = False
score = 0
player.rect.center = (WIDTH//2, HEIGHT-100)
for enemy in enemies:
enemy.rect.center = (random.randint(40, WIDTH-40), -100)
enemy.speed = ENEMY_SPEED
pygame.display.flip()
clock.tick(FPS)
pygame.quit()
How to run the game:**
1. Install Pygame: `pip install pygame`
2. Create two car images (player and enemy) named `car.png` and `enemy.png` in the same directory
3. Run the script
**Game Features:**
– Player controls using left/right arrow keys
– Random enemy car spawning
– Increasing difficulty as score increases
– Collision detection
– Score tracking
– Game over screen with restart option
**Controls:**
– Left/Right Arrow: Move player car
– SPACE: Restart after game over
– ESC: Quit game
**To enhance the game, you can add:**
1. Sound effects and background music
2. Different car skins
3. Power-ups
4. Multiple levels
5. High score system
6. Better graphics and animations
7. Traffic system
8. Nitro boost functionality
Remember to create or download appropriate car images (`car.png` and `enemy.png`) for the game to work properly. You can find free game assets on websites like OpenGameArt.org or Kenney.nl.
No responses yet