0% found this document useful (0 votes)
24 views

C.SC 12 F1 2D PROJECT

The 'F1 Racing' project is an immersive Python game utilizing tkinter and pygame, allowing players to customize characters, select F1 teams, and choose car numbers. It emphasizes object-oriented programming and GUI development, featuring player customization, team selection, and gameplay mechanics like pit stops and damage management. The game is designed for a laptop with specific hardware and software requirements, and includes a detailed source code for implementation.
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)
24 views

C.SC 12 F1 2D PROJECT

The 'F1 Racing' project is an immersive Python game utilizing tkinter and pygame, allowing players to customize characters, select F1 teams, and choose car numbers. It emphasizes object-oriented programming and GUI development, featuring player customization, team selection, and gameplay mechanics like pit stops and damage management. The game is designed for a laptop with specific hardware and software requirements, and includes a detailed source code for implementation.
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/ 15

ABSTRACT

The project titled "F1 Racing" is an immersive Python-based game designed using tkinter
and pygame modules. It offers players a dynamic experience where they can create
personalized characters, select their preferred F1 teams, and choose unique car numbers.
This game is focusing on integrating object-oriented programming concepts and GUI
development skills.

Key Features:

1. Player Customization: Users can create and customize their own characters,
defining attributes such as name, nationality.
2. Team Selection: Players have the freedom to choose from a variety of real-world
F1 teams, each with unique strengths and strategies.
3. Car Number Selection: Personalization extends to selecting a preferred car
number, adding a touch of individuality to their F1 journey.
4. GUI Development with tkinter: The game interface is developed using tkinter,
providing an intuitive and visually appealing user experience with interactive
menus and dialogs.
5. Gameplay with pygame: pygame is utilized for handling game graphics,
animations, and sound effects, enhancing the overall gaming experience with
immersive visuals and audio feedback.
6. Object-Oriented Approach: The project emphasizes object-oriented
programming principles ensuring a well-structured and maintainable codebase.
HARDWARE AND SOFTWARE REQUIREMENTS

HARDWARE :

DEVICE NAME LAPTOP-PTB0727A

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

INSTALLED RAM 16 GB

SOFTWARE :

PYTHON IDE SPYDER V6


ST. JOHN’S ENGLISH SCHOOL AND JUNIOR
COLLEGE

COMPUTER SCIENCE PROJECT

F1 RACING

DONE BY:
ARYAN SINHA
XII-A
12102
SOURCE CODE :

import tkinter as tk

import pygame

import random

import time

# Constants

SCREEN_WIDTH = 1200

SCREEN_HEIGHT = 800

FPS = 60

WHITE = (255, 255, 255)

BLACK = (0, 0, 0)

LIGHT_OFF_COLOR = (100, 100, 100)

LIGHT_ON_COLOR = (255, 255, 0)

PIT_STOP_DURATION = 3 # Time in seconds for pit stop

QUALIFYING_LAPS = 5 # Number of laps for qualifying

class Player:

def __init__(self, name, team):

self.name = name

self.team = team

self.position = (SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2)

self.speed = 0

self.acceleration = 0.1

self.max_speed = 10

self.turbo_active = False

self.drs_active = False

self.turbo_multiplier = 2

self.drs_multiplier = 1.5

self.tyre_condition = 100

self.damage = 0
self.laps_completed = 0

self.pit_stop = False

self.pit_stop_timer = 0

self.pit_stop_duration = PIT_STOP_DURATION

self.current_lap_start_time = 0

self.lap_times = []

self.fastest_lap = float('inf')

def move_left(self):

self.position = (max(0, self.position[0] - self.speed), self.position[1])

def move_right(self):

self.position = (min(SCREEN_WIDTH, self.position[0] + self.speed), self.position[1])

def move_up(self):

self.position = (self.position[0], max(0, self.position[1] - self.speed))

def move_down(self):

self.position = (self.position[0], min(SCREEN_HEIGHT, self.position[1] + self.speed))

def accelerate(self):

self.speed = min(self.max_speed, self.speed + self.acceleration)

def activate_turbo(self):

self.turbo_active = True

def activate_drs(self):

self.drs_active = True

def update(self):

if self.pit_stop:

self.pit_stop_timer += 1 / FPS

if self.pit_stop_timer >= self.pit_stop_duration:

self.pit_stop = False
self.pit_stop_timer = 0

print(f"Team Radio: {self.name}, pit stop complete. Resuming race.")

else:

if self.turbo_active:

self.speed = min(self.max_speed * self.turbo_multiplier, self.speed + self.acceleration)

elif self.drs_active:

self.speed = min(self.max_speed * self.drs_multiplier, self.speed + self.acceleration)

else:

self.accelerate()

if self.speed > 0:

self.tyre_condition -= 0.01 * self.speed # Adjusted for realistic tyre wear

if self.damage > 0:

self.damage -= 1

if self.tyre_condition <= 20:

print(f"Team Radio: {self.name}, your tyres are wearing out!")

elif self.tyre_condition <= 0:

print(f"Team Radio: {self.name}, your tyres have blown out!")

self.enter_pit_stop() # Enter pit stop when tyres are blown out

def enter_pit_stop(self):

self.pit_stop = True

self.speed = 0

print(f"Team Radio: {self.name}, entering pit stop.")

def receive_damage(self, damage_amount):

self.damage += damage_amount

if self.damage >= 50:

print(f"Team Radio: {self.name}, your car has taken significant damage!")

def radio_message(self, message):


print(f"Box, box, {self.name}! {message}")

def start_lap_timer(self):

self.current_lap_start_time = time.time()

def finish_lap(self):

if self.current_lap_start_time > 0:

lap_time = time.time() - self.current_lap_start_time

self.lap_times.append(lap_time)

self.current_lap_start_time = 0

if lap_time < self.fastest_lap:

self.fastest_lap = lap_time

class AIPlayer:

def __init__(self, name, team):

self.name = name

self.team = team

self.position = (random.randint(50, SCREEN_WIDTH - 50), random.randint(50, SCREEN_HEIGHT - 50))

self.speed = random.randint(3, 8)

self.tyre_condition = 100

self.damage = 0

self.pit_stop = False

self.pit_stop_timer = 0

self.pit_stop_duration = PIT_STOP_DURATION

self.current_lap_start_time = 0

self.lap_times = []

self.fastest_lap = float('inf')

def move(self):

if self.pit_stop:

self.pit_stop_timer += 1 / FPS

if self.pit_stop_timer >= self.pit_stop_duration:

self.pit_stop = False

self.pit_stop_timer = 0
print(f"Team Radio: {self.name}, pit stop complete. Resuming race.")

else:

direction = random.choice(["up", "down", "left", "right"])

if direction == "up":

self.position = (self.position[0], max(0, self.position[1] - self.speed))

elif direction == "down":

self.position = (self.position[0], min(SCREEN_HEIGHT, self.position[1] + self.speed))

elif direction == "left":

self.position = (max(0, self.position[0] - self.speed), self.position[1])

elif direction == "right":

self.position = (min(SCREEN_WIDTH, self.position[0] + self.speed), self.position[1])

if self.speed > 0:

self.tyre_condition -= 0.01 * self.speed # Adjusted for realistic tyre wear

if self.damage > 0:

self.damage -= 1

if self.tyre_condition <= 20:

print(f"Team Radio: {self.name}, your tyres are wearing out!")

elif self.tyre_condition <= 0:

print(f"Team Radio: {self.name}, your tyres have blown out!")

self.enter_pit_stop() # Enter pit stop when tyres are blown out

def enter_pit_stop(self):

self.pit_stop = True

self.speed = 0

print(f"Team Radio: {self.name}, entering pit stop.")

def receive_damage(self, damage_amount):

self.damage += damage_amount

if self.damage >= 50:

print(f"Team Radio: {self.name}, your car has taken significant damage!")


def radio_message(self, message):

print(f"Team Radio: {self.name}, {message}")

def start_lap_timer(self):

self.current_lap_start_time = time.time()

def finish_lap(self):

if self.current_lap_start_time > 0:

lap_time = time.time() - self.current_lap_start_time

self.lap_times.append(lap_time)

self.current_lap_start_time = 0

if lap_time < self.fastest_lap:

self.fastest_lap = lap_time

class F1RacingGame:

def __init__(self, root):

self.root = root

self.root.title("F1 Racing Game")

self.players = []

self.ai_players = []

self.teams = [

"Mercedes", "Red Bull Racing", "Ferrari", "McLaren", "AlphaTauri",

"Alpine", "Aston Martin", "Williams", "Alfa Romeo", "Haas"

self.tracks = [

"Australian Grand Prix", "Bahrain Grand Prix", "Vietnamese Grand Prix", "Chinese Grand Prix",

"Spanish Grand Prix", "Monaco Grand Prix", "Azerbaijan Grand Prix", "Canadian Grand Prix",

"French Grand Prix", "Austrian Grand Prix", "British Grand Prix", "Hungarian Grand Prix",

"Belgian Grand Prix", "Dutch Grand Prix", "Italian Grand Prix", "Russian Grand Prix",

"Singapore Grand Prix", "Japanese Grand Prix", "United States Grand Prix", "Mexican Grand Prix",

"Brazilian Grand Prix", "Qatar Grand Prix", "Saudi Arabian Grand Prix", "Abu Dhabi Grand Prix",

"Imola Grand Prix"


]

self.selected_track = None

self.total_laps = 3

self.qualifying_laps = QUALIFYING_LAPS

self.skip_qualifying = False # Flag to skip qualifying session

self.game_running = False

self.init_gui()

pygame.init()

self.screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))

pygame.display.set_caption("F1 Racing Game")

self.clock = pygame.time.Clock()

self.player = None

self.game_over = False

self.light_sequence = [LIGHT_OFF_COLOR] * 5

self.light_timer = 0

self.countdown_started = False

def init_gui(self):

self.label_name = tk.Label(self.root, text="Name:")

self.entry_name = tk.Entry(self.root)

self.label_name.pack()

self.entry_name.pack()

self.team_var = tk.StringVar(self.root)

self.team_var.set(self.teams[0])

self.label_team = tk.Label(self.root, text="Select Team:")

self.option_menu_team = tk.OptionMenu(self.root, self.team_var, *self.teams)

self.label_team.pack()

self.option_menu_team.pack()
self.label_track = tk.Label(self.root, text="Select Track:")

self.track_var = tk.StringVar(self.root)

self.track_var.set(self.tracks[0])

self.option_menu_track = tk.OptionMenu(self.root, self.track_var, *self.tracks)

self.label_track.pack()

self.option_menu_track.pack()

self.label_laps = tk.Label(self.root, text="Number of Laps:")

self.entry_laps = tk.Entry(self.root)

self.entry_laps.insert(0, "3")

self.label_laps.pack()

self.entry_laps.pack()

self.skip_qualifying_var = tk.IntVar()

self.checkbutton_skip_qualifying = tk.Checkbutton(self.root, text="Skip Qualifying", variable=self.skip_qualifying_var)

self.checkbutton_skip_qualifying.pack()

self.button_create_player = tk.Button(self.root, text="Create Player", command=self.create_player)

self.button_create_player.pack()

def create_player(self):

name = self.entry_name.get()

team = self.team_var.get()

selected_track = self.track_var.get()

total_laps = int(self.entry_laps.get())

self.player = Player(name, team)

self.players.append(self.player)

self.selected_track = selected_track

self.total_laps = total_laps

print(f"Player {name} created with team {team}, selected track {selected_track}, and {total_laps} laps race.")

if not self.skip_qualifying_var.get():
self.qualifying_session()

else:

self.start_race()

def qualifying_session(self):

print("Qualifying Session")

for _ in range(self.qualifying_laps):

self.run_single_lap()

# Sort players by fastest lap time

self.players.sort(key=lambda p: min(p.lap_times) if p.lap_times else float('inf'))

print("\nQualifying Results:")

for i, player in enumerate(self.players):

print(f"{i+1}. {player.name} - Fastest Lap: {min(player.lap_times) if player.lap_times else 'N/A'} seconds")

self.start_race()

def run_single_lap(self):

self.player.start_lap_timer()

while self.player.current_lap_start_time > 0:

self.player.update()

self.clock.tick(FPS)

self.player.finish_lap()

def start_race(self):

print("\nStarting Race")

# Reset game variables

self.game_running = True

self.light_sequence = [LIGHT_OFF_COLOR] * 5

self.countdown_started = False

self.light_timer = time.time()
# Reset lap data for players

for player in self.players:

player.lap_times = []

player.fastest_lap = float('inf')

def handle_events(self):

for event in pygame.event.get():

if event.type == pygame.QUIT:

self.game_running = False

self.game_over = True

elif event.type == pygame.KEYDOWN:

if event.key == pygame.K_LEFT or event.key == pygame.K_a:

self.player.move_left()

elif event.key == pygame.K_RIGHT or event.key == pygame.K_d:

self.player.move_right()

elif event.key == pygame.K_UP or event.key == pygame.K_w:

self.player.move_up()

elif event.key == pygame.K_DOWN or event.key == pygame.K_s:

self.player.move_down()

elif event.key == pygame.K_SPACE:

self.player.accelerate()

elif event.key == pygame.K_RETURN:

self.player.start_lap_timer()

elif event.key == pygame.K_w or event.key == pygame.K_UP:

self.player.activate_turbo()

elif event.key == pygame.K_z:

self.player.activate_drs()

elif event.key == pygame.K_p:

self.player.enter_pit_stop()

def update_game(self):

if self.countdown_started:

elapsed_time = time.time() - self.light_timer

if elapsed_time < 1:
self.light_sequence[0] = LIGHT_ON_COLOR

elif elapsed_time < 2:

self.light_sequence[1] = LIGHT_ON_COLOR

elif elapsed_time < 3:

self.light_sequence[2] = LIGHT_ON_COLOR

elif elapsed_time < 4:

self.light_sequence[3] = LIGHT_ON_COLOR

elif elapsed_time < 5:

self.light_sequence[4] = LIGHT_ON_COLOR

else:

self.game_running = True

self.countdown_started = False

self.light_sequence = [LIGHT_OFF_COLOR] * 5

if self.player:

self.player.update()

if self.player.damage >= 50:

self.player.radio_message("your car has taken significant damage!")

for ai_player in self.ai_players:

ai_player.move()

if ai_player.damage >= 50:

ai_player.radio_message("your car has taken significant damage!")

if self.player and (self.player.position[0] <= 0 or self.player.position[0] >= SCREEN_WIDTH or

self.player.position[1] <= 0 or self.player.position[1] >= SCREEN_HEIGHT):

self.player.receive_damage(10)

print(f"Team Radio: {self.player.name}, you've hit the barrier! Damage incurred.")

if self.player:

self.player.laps_completed += 1

if self.player.laps_completed >= self.total_laps:

print(f"Race Over! {self.player.name} completed {self.total_laps} laps.")

self.game_running = False
def draw_game(self):

self.screen.fill(WHITE)

if self.selected_track:

pygame.draw.rect(self.screen, BLACK, (50, 50, SCREEN_WIDTH - 100, SCREEN_HEIGHT - 100), 3)

pygame.draw.rect(self.screen, BLACK, (100, 100, SCREEN_WIDTH - 200, SCREEN_HEIGHT - 200), 3)

pygame.draw.rect(self.screen, BLACK, (150, 150, SCREEN_WIDTH - 300, SCREEN_HEIGHT - 300), 3)

pygame.draw.rect(self.screen, BLACK, (200, 200, SCREEN_WIDTH - 400, SCREEN_HEIGHT - 400), 3)

pygame.draw.rect(self.screen, BLACK, (250, 250, SCREEN_WIDTH - 500, SCREEN_HEIGHT - 500), 3)

pygame.draw.rect(self.screen, BLACK, (300, 300, SCREEN_WIDTH - 600, SCREEN_HEIGHT - 600), 3)

pygame.draw.rect(self.screen, BLACK, (350, 350, SCREEN_WIDTH - 700, SCREEN_HEIGHT - 700), 3)

pygame.draw.rect(self.screen, BLACK, (400, 400, SCREEN_WIDTH - 800, SCREEN_HEIGHT - 800), 3)

pygame.draw.rect(self.screen, BLACK, (450, 450, SCREEN_WIDTH - 900, SCREEN_HEIGHT - 900), 3)

pygame.draw.rect(self.screen, BLACK, (500, 500, SCREEN_WIDTH - 1000, SCREEN_HEIGHT - 1000), 3)

pygame.display.flip()

def run_game(self):

self.game_running = True

while self.game_running:

self.handle_events()

self.update_game()

self.draw_game()

self.clock.tick(FPS)

pygame.quit()

self.root.destroy()

if __name__ == "__main__":

root = tk.Tk()

game = F1RacingGame(root)

game.run_game()

You might also like