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.
