C.SC 12 F1 2D PROJECT
C.SC 12 F1 2D PROJECT
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 :
INSTALLED RAM 16 GB
SOFTWARE :
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
BLACK = (0, 0, 0)
class Player:
self.name = name
self.team = team
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):
def move_right(self):
def move_up(self):
def move_down(self):
def accelerate(self):
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
self.pit_stop = False
self.pit_stop_timer = 0
else:
if self.turbo_active:
elif self.drs_active:
else:
self.accelerate()
if self.speed > 0:
if self.damage > 0:
self.damage -= 1
def enter_pit_stop(self):
self.pit_stop = True
self.speed = 0
self.damage += damage_amount
def start_lap_timer(self):
self.current_lap_start_time = time.time()
def finish_lap(self):
if self.current_lap_start_time > 0:
self.lap_times.append(lap_time)
self.current_lap_start_time = 0
self.fastest_lap = lap_time
class AIPlayer:
self.name = name
self.team = team
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
self.pit_stop = False
self.pit_stop_timer = 0
print(f"Team Radio: {self.name}, pit stop complete. Resuming race.")
else:
if direction == "up":
if self.speed > 0:
if self.damage > 0:
self.damage -= 1
def enter_pit_stop(self):
self.pit_stop = True
self.speed = 0
self.damage += damage_amount
def start_lap_timer(self):
self.current_lap_start_time = time.time()
def finish_lap(self):
if self.current_lap_start_time > 0:
self.lap_times.append(lap_time)
self.current_lap_start_time = 0
self.fastest_lap = lap_time
class F1RacingGame:
self.root = root
self.players = []
self.ai_players = []
self.teams = [
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",
self.selected_track = None
self.total_laps = 3
self.qualifying_laps = QUALIFYING_LAPS
self.game_running = False
self.init_gui()
pygame.init()
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.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.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.label_track.pack()
self.option_menu_track.pack()
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.pack()
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.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()
print("\nQualifying Results:")
self.start_race()
def run_single_lap(self):
self.player.start_lap_timer()
self.player.update()
self.clock.tick(FPS)
self.player.finish_lap()
def start_race(self):
print("\nStarting Race")
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
player.lap_times = []
player.fastest_lap = float('inf')
def handle_events(self):
if event.type == pygame.QUIT:
self.game_running = False
self.game_over = True
self.player.move_left()
self.player.move_right()
self.player.move_up()
self.player.move_down()
self.player.accelerate()
self.player.start_lap_timer()
self.player.activate_turbo()
self.player.activate_drs()
self.player.enter_pit_stop()
def update_game(self):
if self.countdown_started:
if elapsed_time < 1:
self.light_sequence[0] = LIGHT_ON_COLOR
self.light_sequence[1] = LIGHT_ON_COLOR
self.light_sequence[2] = LIGHT_ON_COLOR
self.light_sequence[3] = LIGHT_ON_COLOR
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()
ai_player.move()
self.player.receive_damage(10)
if self.player:
self.player.laps_completed += 1
self.game_running = False
def draw_game(self):
self.screen.fill(WHITE)
if self.selected_track:
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()