0% found this document useful (0 votes)
6 views15 pages

Lane-Based Car Avoidance Game in Python

This mini project report details the development of a lane-based car avoidance game using Python and the Pygame library, where players navigate a car to avoid incoming enemy vehicles. The project aims to teach game development fundamentals, event-driven programming, and problem-solving skills. The game features multiple stages, sound effects, and collision detection, providing an engaging experience while enhancing programming knowledge.

Uploaded by

harinidevi519
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)
6 views15 pages

Lane-Based Car Avoidance Game in Python

This mini project report details the development of a lane-based car avoidance game using Python and the Pygame library, where players navigate a car to avoid incoming enemy vehicles. The project aims to teach game development fundamentals, event-driven programming, and problem-solving skills. The game features multiple stages, sound effects, and collision detection, providing an engaging experience while enhancing programming knowledge.

Uploaded by

harinidevi519
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

MINI PROJECT REPORT

Lane Based Car Avoidance Game

Problem Solving and Python Programming

Student Name: KRUTHIKA C D


Digital ID: 2510123
Section: S-10
Submitted to: Dr. Anup Kundu
Department: Chemical Engineering
Academic Year: 2025–2026
Abstract
This mini project focuses on the development of a lane-based 2D car
avoidance game using Python and the Pygame library. The player
controls a car that can move left or right between predefined lanes to
avoid incoming enemy cars. The game includes multiple stages such
as a start screen, countdown screen, gameplay, and game over
screen, along with background music and sound effects. This project
helps in understanding event-driven programming, game loops,
collision detection, and basic game design concepts using Python.

Objective
• To learn the fundamentals of game development using Python
and Pygame
• To understand event-driven programming and real-time user
interaction
• To apply problem-solving skills in developing an interactive
application

Software Requirements
• Operating System: Windows / Linux / macOS
• Programming Language: Python 3.x
• Library: Pygame
• IDE: VS Code / PyCharm
System Design and Methodology
The game is developed using the Pygame library by initializing the
game window, loading graphical assets, sound effects, and fonts. The
program is structured using different game states such as start
screen, countdown, playing, and game over.
User inputs are handled using keyboard and mouse events. The
enemy car moves vertically downwards, and its speed increases
gradually based on the player’s score. Collision detection is
implemented to determine when the player’s car collides with the
enemy car, resulting in a game over condition.

Algorithm
i. Start the program
ii. Initialize Pygame and audio modules
iii. Create the game window
iv. Load images, sounds, and fonts
v. Display the start screen and wait for user input
vi. Show countdown before gameplay begins
vii. Run the main game loop
viii. Handle player movement using keyboard inputs
ix. Move enemy car and update score
x. Detect collision and display game over screen
xi. Restart the game or exit
Implementation
import pygame
import random
import sys
# INITIAL SETUP
[Link]()
[Link]()
WIDTH, HEIGHT = 700, 1000
screen = [Link].set_mode((WIDTH, HEIGHT))
[Link].set_caption("Lane Based Car Avoidance Game")
clock = [Link]()

# GAME STATES
START_SCREEN = 0
COUNTDOWN = 1
PLAYING = 2
GAME_OVER_STATE = 3
game_state = START_SCREEN

# MUSIC
try:
[Link]("bg_music.mp3") # WAV file
[Link].set_volume(0.5)
except:
print("⚠ Background music not found")
# SOUNDS
try:
crash_sound = [Link]("[Link]")
crash_sound.set_volume(0.7)
ambulance_sound = [Link]("[Link]")
ambulance_sound.set_volume(0.7)
except:
print("⚠ Sound files not found")
crash_sound = None
ambulance_sound = None

# FONTS
font = [Link](None, 36)
big_font = [Link](None, 80)

# LOAD IMAGES
CAR_WIDTH, CAR_HEIGHT = 80, 130
try:
player_img =
[Link]("player_car.png").convert_alpha()
enemy_img =
[Link]("enemy_car.png").convert_alpha()
road_img = [Link]("[Link]").convert()

player_img = [Link](player_img, (CAR_WIDTH,


CAR_HEIGHT))
enemy_img = [Link](enemy_img, (CAR_WIDTH,
CAR_HEIGHT))
road_img = [Link](road_img, (WIDTH, HEIGHT))
except:
print("⚠ Images not found, using placeholders")
player_img = [Link]((CAR_WIDTH, CAR_HEIGHT))
player_img.fill((0, 0, 255))
enemy_img = [Link]((CAR_WIDTH, CAR_HEIGHT))
enemy_img.fill((255, 0, 0))
road_img = [Link]((WIDTH, HEIGHT))
road_img.fill((50, 50, 50))

# LANES
lanes = [86, 319, 552]
# PLAYER
def reset_player():
global player_lane, player_rect
player_lane = 1
player_rect = [Link](lanes[player_lane], HEIGHT - 300,
CAR_WIDTH, CAR_HEIGHT)

# ENEMY
def reset_enemy():
global enemy_rect, enemy_speed
enemy_rect = [Link]([Link](lanes), -CAR_HEIGHT,
CAR_WIDTH, CAR_HEIGHT)
enemy_speed = 5

# SCORE
score = 0
# START BUTTON
button_rect = [Link](WIDTH//2 - 120, HEIGHT//2, 240, 70)

# COUNTDOWN
countdown_words = ["GET", "SET", "GO"]
countdown_colors = [(255, 0, 0), (255, 200, 0), (0, 200, 0)]
count_index = 0
count_start_time = 0

# FUNCTIONS
def show_score():
text = [Link](f"Score: {score}", True, (255, 255, 255)) # White
score
[Link](text, (10, 10))

def draw_start_screen():
[Link]((20, 20, 20))
title = big_font.render("CAR CRASH", True, (255, 255, 255))
[Link](title, (WIDTH//2 - title.get_width()//2, 200))
[Link](screen, (0, 160, 0), button_rect,
border_radius=10)
text = [Link]("START", True, (255, 255, 255))
[Link](text, (
button_rect.centerx - text.get_width()//2,
button_rect.centery - text.get_height()//2
))

def draw_countdown():
[Link]((0, 0, 0))
color = countdown_colors[count_index]
word = countdown_words[count_index]
[Link](screen, color, (WIDTH//2, HEIGHT//2 - 50), 40)
text = big_font.render(word, True, color)
[Link](text, (WIDTH//2 - text.get_width()//2, HEIGHT//2 + 20))
[Link]()

def game_over_screen():
[Link]((0, 0, 0))
text1 = big_font.render("GAME OVER", True, (200, 0, 0))
text2 = [Link]("PRESS ENTER TO RESTART", True, (255, 255,
255))
[Link](text1, (WIDTH//2 - text1.get_width()//2, HEIGHT//2 -
50))
[Link](text2, (WIDTH//2 - text2.get_width()//2, HEIGHT//2 +
50))
[Link]()

def reset_game():
global score, game_state, count_index, count_start_time,
enemy_speed
score = 0
reset_player()
reset_enemy()
enemy_speed = 5
if [Link].get_busy():
[Link]()
try:
[Link](-1) # loop background music
except:
pass
count_index = 0
count_start_time = [Link].get_ticks()
game_state = COUNTDOWN
# INITIAL RESET
reset_player()
reset_enemy()

# GAME LOOP
running = True
while running:
dt = [Link](60)

for event in [Link]():


if [Link] == [Link]:
[Link]()
[Link]()

if game_state == START_SCREEN:
if [Link] == [Link]:
if button_rect.collidepoint([Link]):
count_index = 0
count_start_time = [Link].get_ticks()
try:
[Link](-1)
except:
pass
game_state = COUNTDOWN

elif game_state == PLAYING:


if [Link] == [Link]:
if [Link] == pygame.K_LEFT and player_lane > 0:
player_lane -= 1
if [Link] == pygame.K_RIGHT and player_lane < 2:
player_lane += 1

elif game_state == GAME_OVER_STATE:


if [Link] == [Link] and [Link] ==
pygame.K_RETURN:
if ambulance_sound:
ambulance_sound.stop()
reset_game()
# STATE HANDLING
if game_state == START_SCREEN:
draw_start_screen()

elif game_state == COUNTDOWN:


now = [Link].get_ticks()
if now - count_start_time > 800:
count_index += 1
count_start_time = now
if count_index >= len(countdown_words):
game_state = PLAYING
else:
draw_countdown()
elif game_state == PLAYING:
[Link](road_img, (0, 0))
player_rect.x = lanes[player_lane]

# Enemy movement
enemy_rect.y += enemy_speed
if enemy_rect.y > HEIGHT:
enemy_rect.y = -CAR_HEIGHT
enemy_rect.x = [Link](lanes)
score += 1
# Gradually increase speed based on score
enemy_speed = 5 + score * 0.3
enemy_speed = min(enemy_speed, 15) # max speed cap

# Collision
if player_rect.colliderect(enemy_rect):
if crash_sound:
crash_sound.play()
if [Link].get_busy():
[Link]()
if ambulance_sound:
ambulance_sound.play(-1) # loop until restart
game_state = GAME_OVER_STATE

[Link](player_img, player_rect)
[Link](enemy_img, enemy_rect)
show_score()

elif game_state == GAME_OVER_STATE:


game_over_screen()

[Link]()
Screenshots and Explanation:
Start Screen:

The start screen displays the game title


and a start button. The game begins
when the user clicks the start button
using the mouse.

Countdown Screen

Before the game starts, a countdown screen shows “GET”, “SET”, and
“GO” with changing traffic light colors to prepare the player.
Gameplay Screen

During gameplay, the player controls the


car using left and right arrow keys. Enemy
cars move downward in random lanes. The
score increases when the enemy car is
successfully avoided.

Game Over Screen

When the player’s car collides with


the enemy car, the game ends. A
game over screen is displayed with
an option to restart the game.
Result
The lane-based car avoidance game runs successfully with smooth
graphics, sound effects, and responsive controls. The game accurately
detects collisions and updates the score in real time. The gradual
increase in enemy speed increases the difficulty level, making the
game engaging and challenging.

Conclusion
This mini project provided practical experience in Python
programming and game development using the Pygame library. It
enhanced understanding of core concepts such as event handling,
collision detection, game state management, and multimedia
integration. The project improved logical thinking and confidence in
developing interactive applications using Python.

Future Enhancements
• Adding multiple enemy cars
• Introducing different difficulty levels
• Adding pause and resume functionality
• Improving graphics and animations

You might also like