0% found this document useful (0 votes)
18 views20 pages

f1 2d

The document outlines the development of an immersive 2D racing game titled 'F1: 2D' using Python and the Pygame library, emphasizing object-oriented programming and a user-friendly graphical interface. Key features include collision detection, score tracking, and an engaging gameplay experience. It also provides details on the hardware and software requirements, source code, and references used in the project.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views20 pages

f1 2d

The document outlines the development of an immersive 2D racing game titled 'F1: 2D' using Python and the Pygame library, emphasizing object-oriented programming and a user-friendly graphical interface. Key features include collision detection, score tracking, and an engaging gameplay experience. It also provides details on the hardware and software requirements, source code, and references used in the project.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 20

TABLE OF CONTENTS

S.no Heading

1. ABSTRACT

2. ABOUT PYTHON

3. MODULES USED

4. HARDWARE AND SOFTWARE


REQUIREMENTS
5. SOURCE CODE

6. OUTPUT

7. BIBLIOGRAPHY
ABSTRACT
The project titled "F1 : 2D" is an immersive racing game developed in
Python using the powerful pygame library. This game aims to create an
engaging experience that not only entertains players but also
showcases the application of fundamental programming concepts,
particularly focusing on object-oriented programming (OOP) and
graphical user interface (GUI) development skills.

Key Features

1. Gameplay with Pygame:

The game leverages pygame for a comprehensive range of


functionalities, including handling game graphics, animations, and
sound effects. This utilization enhances the overall gaming experience.
2. Object-Oriented Approach:

One of the standout features of the "F1 : 2D" project is its emphasis
on object-oriented programming principles This approach facilitates the
encapsulation of game-related data and behaviors,.

3. Interactive User Interface:

The project includes a well-designed graphical user interface that


enhances user engagement. Players are greeted with a welcome
screen, where they can easily navigate through different options, such
as selecting the number of laps for the race. Clear and concise visual
prompts guide players throughout their gaming journey, making the
interface intuitive and user-friendly.
4. Collision Detection and Game Logic:

An essential aspect of racing games is the ability to detect collisions


between the player’s car and other entities on the track. This project
implements effective collision detection algorithms, ensuring that
players experience the thrill of close racing while facing the
consequences of crashes. The game logic is carefully crafted to handle
various scenarios, such as completing laps, losing lives upon collisions,
and providing feedback on performance.

5. Score Tracking and Game Progression:

Players can track their progress through a clear scoring system that
displays laps completed, time taken, and remaining lives. This feature
motivates players to improve their performance with each race. The
game also includes a completion message that rewards players for their
achievements, further enhancing the sense of accomplishment.
ABOUT PYTHON
Python is a high-level, interpreted programming language known for its
simplicity, readability, and vast range of applications. Created by Guido
van Rossum and first released in 1991, Python has grown in popularity
over the years due to its clear syntax and dynamic nature, making it an
excellent choice for both beginners and seasoned developers.

Key Features of Python


1. Easy-to-read Syntax:

Python's syntax is often compared to the English language, making it


easy to understand. This clarity allows developers to write code more
quickly and efficiently. Its indentation-based structure ensures that
code is visually clean and readable, reducing errors that come from
complicated syntax seen in other programming languages.

2. Interpreted Language:

Python is an interpreted language, meaning code is executed line-by-


line at runtime without the need for prior compilation. This allows for
rapid testing and debugging, making the development process faster
and more interactive.

3. Extensive Standard Library and Ecosystem:

One of Python's most significant strengths is its extensive standard


library, which includes modules and functions for handling everything
from file I/O to web development and data processing.
4. Cross-platform Compatibility:

Python is cross-platform, meaning it can run on a wide range of


operating systems like Windows, macOS, and Linux without requiring
changes in the code. This ensures that Python programs are portable
and easy to distribute across different environments.
MODULES USED
Pygame

Pygame is a popular open-source Python library designed for creating


video games. Built on top of the Simple DirectMedia Layer (SDL), it
provides functionality for handling game development tasks such as
rendering graphics, playing sound, and managing user input.

Sys

The sys module in Python provides access to system-specific


parameters and functions that interact with the Python runtime
environment. It's a built-in module that allows programmers to

Random

The random module in Python provides functions to generate random


numbers and perform random operations. It is widely used in
simulations, gaming, cryptography, and any application where
unpredictability is required.
HARDWARE AND SOFTWARE REQUIREMENTS

HARDWARE :

DEVICE NAME LAPTOP-PTB0727A

PROCESSOR 13th Gen Intel(R) Core(TM) i7-


1355U
INSTALLED RAM 16 GB

HARD DISK DRIVE Lenovo ThinkServer 300 GB 2.5'


Internal Hard Drive.

SOFTWARE :

PYTHON IDE SPYDER V6


SOURCE CODE:
import pygame

import sys

import random

# Initialize Pygame

pygame.init()

# Constants

WIDTH, HEIGHT = 800, 600

FPS = 60

CAR_WIDTH, CAR_HEIGHT = 50, 100

ROAD_COLOR = (50, 50, 50)

TRACK_COLOR = (200, 200, 200)

BOUNDARY_COLOR = (255, 0, 0) # Red for track boundaries

LAP_TIME_COLOR = (255, 255, 255) # White for lap time display

ENEMY_CAR_COLOR = (0, 255, 0) # Green for enemy cars

# Set up the display

screen = pygame.display.set_mode((WIDTH, HEIGHT))

pygame.display.set_caption('F1 : 2D')

# Load player car image

car_image = pygame.image.load('car.png').convert_alpha() # Load your car image


car_image = pygame.transform.scale(car_image, (CAR_WIDTH, CAR_HEIGHT)) # Scale if
necessary

# Load enemy car image

enemy_car_image = pygame.image.load('enemy_car.png').convert_alpha() # Load enemy car


image

enemy_car_image = pygame.transform.scale(enemy_car_image, (CAR_WIDTH, CAR_HEIGHT))


# Scale if necessary

# Load track image

track_image = pygame.image.load('track.png').convert_alpha() # Load your track image

track_image = pygame.transform.scale(track_image, (WIDTH, HEIGHT)) # Scale to fit the


window

# Car position and speed

car_x = WIDTH // 2 - CAR_WIDTH // 2

car_y = HEIGHT - CAR_HEIGHT - 20 # Initialize car_y

car_speed = 0

max_speed = 10 # Maximum speed

acceleration = 0.5 # Acceleration rate

braking = 1 # Braking rate

# Track boundaries

LEFT_BOUNDARY = int(WIDTH // 4) # Ensure these are integers

RIGHT_BOUNDARY = int(WIDTH * 0.75)

# Lap and scoring variables

laps = 0

lap_start_time = 0

lap_duration = 0
# Enemy cars

enemy_cars = []

NUM_ENEMY_CARS = 5

ENEMY_SPEED = 3

def draw_road():

# Draw the track image

screen.blit(track_image, (0, 0)) # Draw the track image

pygame.draw.rect(screen, BOUNDARY_COLOR, (LEFT_BOUNDARY - 5, 0, 5, HEIGHT)) # Left


boundary

pygame.draw.rect(screen, BOUNDARY_COLOR, (RIGHT_BOUNDARY, 0, 5, HEIGHT)) # Right


boundary

def display_score():

font = pygame.font.SysFont(None, 36)

score_text = font.render(f'Laps: {laps} Time: {lap_duration // 1000}s', True,


LAP_TIME_COLOR)

screen.blit(score_text, (10, 10))

def create_enemy_cars():

for _ in range(NUM_ENEMY_CARS):

enemy_x = random.randint(LEFT_BOUNDARY, RIGHT_BOUNDARY - CAR_WIDTH)

enemy_y = random.randint(-150, -50) # Start above the screen

enemy_cars.append((enemy_x, enemy_y))

def move_enemy_cars():

for i, (x, y) in enumerate(enemy_cars):


enemy_cars[i] = (x, y + ENEMY_SPEED) # Move enemy cars down

# Reset enemy car position if it goes off the screen

if y > HEIGHT:

enemy_x = random.randint(LEFT_BOUNDARY, RIGHT_BOUNDARY - CAR_WIDTH)

enemy_cars[i] = (enemy_x, random.randint(-150, -50))

def check_collision():

for (enemy_x, enemy_y) in enemy_cars:

if (car_x < enemy_x + CAR_WIDTH and

car_x + CAR_WIDTH > enemy_x and

car_y < enemy_y + CAR_HEIGHT and

car_y + CAR_HEIGHT > enemy_y):

return True # Collision detected

return False

def main():

global car_x, car_y, car_speed, lap_start_time, laps, lap_duration

lap_start_time = pygame.time.get_ticks() # Start timing the lap

create_enemy_cars() # Initialize enemy cars

clock = pygame.time.Clock() # Initialize clock

while True:

for event in pygame.event.get():

if event.type == pygame.QUIT:

pygame.quit()

sys.exit()
# Movement

keys = pygame.key.get_pressed()

if keys[pygame.K_LEFT] and car_x > LEFT_BOUNDARY:

car_x -= 5 # Move left

if keys[pygame.K_RIGHT] and car_x < RIGHT_BOUNDARY - CAR_WIDTH:

car_x += 5 # Move right

if keys[pygame.K_UP]: # Accelerate

car_speed += acceleration

if keys[pygame.K_DOWN]: # Brake

car_speed -= braking

# Cap speed

if car_speed > max_speed:

car_speed = max_speed

if car_speed < 0:

car_speed = 0

# Move the car based on speed

car_y -= car_speed # Move the car upwards

# Move enemy cars

move_enemy_cars()

# Check for lap completion

if car_y < 0:

laps += 1

lap_duration = pygame.time.get_ticks() - lap_start_time


lap_start_time = pygame.time.get_ticks() # Reset lap timer

car_y = HEIGHT - CAR_HEIGHT - 20 # Reset car position

car_speed = 0 # Reset speed when lap is completed

# Check for collisions

if check_collision():

print("Collision detected! Game Over.")

pygame.quit()

sys.exit()

# Draw everything

draw_road()

screen.blit(car_image, (car_x, car_y))

# Draw enemy cars using the loaded image

for enemy_x, enemy_y in enemy_cars:

screen.blit(enemy_car_image, (enemy_x, enemy_y))

display_score()

pygame.display.flip()

clock.tick(FPS)

if _name_ == '_main_':

main()
OUTPUT
BIBLIOGRAPHY:

 Google.com

 chat.openai.com

 www.Formula1.com

 Geeksforgeeks.com

You might also like