0% found this document useful (0 votes)
32 views2 pages

Racing Game - Py

This document contains a simple racing game implemented in Python using the Pygame library. The game features a player-controlled car that can move left and right to avoid falling enemy cars. The game ends when the player's car collides with an enemy car.

Uploaded by

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

Racing Game - Py

This document contains a simple racing game implemented in Python using the Pygame library. The game features a player-controlled car that can move left and right to avoid falling enemy cars. The game ends when the player's car collides with an enemy car.

Uploaded by

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

import pygame

import random

# Initialize
[Link]()

# Screen setup
WIDTH, HEIGHT = 500, 600
screen = [Link].set_mode((WIDTH, HEIGHT))
[Link].set_caption("Simple Racing Game")

# Colors
WHITE = (255, 255, 255)
GRAY = (100, 100, 100)
RED = (255, 0, 0)
YELLOW = (255, 255, 0)

# Clock
clock = [Link]()

# Player car
player_car = [Link](WIDTH // 2 - 25, HEIGHT - 100, 50, 80)

# Enemy cars
enemy_car = [Link]([Link](0, WIDTH - 50), -100, 50, 80)
enemy_speed = 5

# Game loop
running = True
while running:
[Link](GRAY)

# Event handling
for event in [Link]():
if [Link] == [Link]:
running = False

# Move player
keys = [Link].get_pressed()
if keys[pygame.K_LEFT] and player_car.left > 0:
player_car.move_ip(-5, 0)
if keys[pygame.K_RIGHT] and player_car.right < WIDTH:
player_car.move_ip(5, 0)

# Move enemy
enemy_car.move_ip(0, enemy_speed)
if enemy_car.top > HEIGHT:
enemy_car.top = -100
enemy_car.left = [Link](0, WIDTH - 50)

# Check collision
if player_car.colliderect(enemy_car):
print("You Crashed!")
running = False

# Draw cars
[Link](screen, RED, player_car)
[Link](screen, YELLOW, enemy_car)
# Update display
[Link]()
[Link](60)

[Link]()

You might also like