Skip to content
This repository was archived by the owner on Apr 24, 2025. It is now read-only.

Commit 0b6083b

Browse files
committed
Add: Type Racer Game
1 parent c60fde5 commit 0b6083b

1 file changed

Lines changed: 180 additions & 0 deletions

File tree

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
import tkinter as tk
2+
from tkinter import ttk
3+
import random
4+
import time
5+
6+
# List of random English sentences for the game
7+
sentences = [
8+
"The quick brown fox jumps over the lazy dog",
9+
"Python is an interpreted high-level programming language",
10+
"I'm learning to code with Python and it's fun",
11+
"A journey of a thousand miles begins with a single step",
12+
"Coding is not just about algorithms, it's about logic",
13+
"Practice makes perfect in programming",
14+
"Innovation distinguishes between a leader and a follower",
15+
"The only way to do great work is to love what you do",
16+
"Coding is the language of the future",
17+
"Keep calm and code on",
18+
]
19+
20+
# Initialize the game
21+
score = 0
22+
start_time = 0
23+
current_sentence = random.choice(sentences)
24+
timer_duration = 30 # Countdown timer duration in seconds
25+
countdown_duration = 5 # Countdown timer duration in seconds
26+
27+
# Flag to indicate whether the game has started
28+
game_started = False
29+
30+
# Function to start the game with a countdown
31+
def start_game():
32+
global start_time, current_sentence, timer_duration, score, game_started
33+
score = 0
34+
timer_duration = 30 # Reset the timer duration to 30 seconds
35+
countdown_label.config(text="Get ready!")
36+
root.update()
37+
time.sleep(1)
38+
for i in range(countdown_duration, 0, -1): # Use countdown duration
39+
countdown_label.config(text=str(i))
40+
root.update()
41+
time.sleep(1)
42+
countdown_label.config(text="Go!")
43+
root.update()
44+
time.sleep(1)
45+
countdown_label.config(text="")
46+
root.update()
47+
start_time = time.time()
48+
current_sentence = random.choice(sentences) # Select a new random sentence
49+
sentence_label.config(text=current_sentence)
50+
input_box.delete(0, tk.END)
51+
input_box.focus()
52+
input_box.config(foreground='black', width=40) # Reset text color and increase width
53+
game_started = True # Set the game started flag
54+
start_main_timer() # Start the main game timer after the countdown
55+
56+
# Function to reset the game
57+
def reset_game():
58+
global game_started
59+
game_started = False
60+
countdown_label.config(text="")
61+
submit_score() # Submit the score to reset the progress bar
62+
63+
# Function to start the main game timer
64+
def start_main_timer():
65+
global timer_duration, game_started
66+
if timer_duration > 0 and game_started:
67+
timer_label.config(text=f"Time left: {timer_duration} seconds")
68+
countdown_label.config(text=str(timer_duration)) # Update countdown_label here
69+
timer_duration -= 1
70+
root.after(1000, start_main_timer)
71+
elif game_started:
72+
timer_label.config(text="Time's up!")
73+
submit_score()
74+
75+
# Function to handle typing and change text color
76+
def check_input(event):
77+
input_text = input_box.get()
78+
if current_sentence.startswith(input_text):
79+
input_box.config(foreground='green') # Correct text color
80+
update_progress_bar(len(input_text))
81+
else:
82+
input_box.config(foreground='red') # Incorrect text color
83+
84+
# Function to handle submission of score
85+
def submit_score():
86+
global current_sentence, game_started
87+
end_time = time.time()
88+
time_taken = round(end_time - start_time, 2)
89+
if input_box.get() == current_sentence:
90+
wpm = calculate_wpm(time_taken, len(current_sentence.split()))
91+
result_label.config(text=f"Correct! Time taken: {time_taken} seconds, WPM: {wpm}", foreground='green')
92+
reset_progress_bar()
93+
animate_result_label()
94+
current_sentence = random.choice(sentences) # Select a new random sentence
95+
start_game()
96+
else:
97+
result_label.config(text=f"Incorrect. Time taken: {time_taken} seconds", foreground='red')
98+
animate_result_label()
99+
100+
# Function to animate the result label
101+
def animate_result_label():
102+
result_label.config(foreground='red')
103+
root.after(100, lambda: result_label.config(foreground='black'))
104+
root.after(200, lambda: result_label.config(foreground='red'))
105+
root.after(300, lambda: result_label.config(foreground='black'))
106+
root.after(400, lambda: result_label.config(foreground='red'))
107+
root.after(500, lambda: result_label.config(foreground='black'))
108+
root.after(600, lambda: result_label.config(foreground='red'))
109+
root.after(700, lambda: result_label.config(foreground='black'))
110+
root.after(800, lambda: result_label.config(foreground='red'))
111+
root.after(900, lambda: result_label.config(foreground='black'))
112+
root.after(1000, lambda: result_label.config(foreground='red'))
113+
root.after(1100, lambda: result_label.config(text="", foreground='black'))
114+
115+
# Function to update the progress bar
116+
def update_progress_bar(length):
117+
progress = min(length / len(current_sentence), 1.0) * 100
118+
progressbar_label.config(text=f"Progress: {int(progress)}%")
119+
progressbar['value'] = progress
120+
121+
# Function to reset the progress bar
122+
def reset_progress_bar():
123+
progressbar_label.config(text="Progress: 0%")
124+
progressbar['value'] = 0
125+
126+
# Countdown timer function
127+
def countdown():
128+
global timer_duration, game_started
129+
if timer_duration > 0 and game_started:
130+
timer_label.config(text=f"Time left: {timer_duration} seconds")
131+
countdown_label.config(text=str(timer_duration)) # Update countdown_label here
132+
timer_duration -= 1
133+
root.after(1000, countdown)
134+
135+
# Function to calculate WPM
136+
def calculate_wpm(time_taken, word_count):
137+
minutes = time_taken / 60
138+
wpm = (word_count / 5) / minutes
139+
return round(wpm)
140+
141+
# Initialize the GUI
142+
root = tk.Tk()
143+
root.geometry("800x400")
144+
root.title("TypeRacer")
145+
146+
147+
# Create the widgets with ttk themes
148+
style = ttk.Style()
149+
style.configure("TLabel", foreground="black", font=("Arial", 14))
150+
style.configure("TButton", font=("Arial", 16))
151+
152+
sentence_label = ttk.Label(root, text=current_sentence, wraplength=700, justify='left')
153+
input_box = ttk.Entry(root, font=("Arial", 16), width=40) # Increase width
154+
submit_button = ttk.Button(root, text="Submit", command=submit_score)
155+
reset_button = ttk.Button(root, text="Reset", command=reset_game)
156+
result_label = ttk.Label(root)
157+
progressbar_label = ttk.Label(root, text="Progress: 0%")
158+
progressbar = ttk.Progressbar(root, orient=tk.HORIZONTAL, length=200, mode="determinate")
159+
timer_label = ttk.Label(root, text=f"Time left: {timer_duration} seconds")
160+
countdown_label = ttk.Label(root, font=("Arial", 24), foreground="red")
161+
162+
# Add the widgets to the GUI
163+
sentence_label.pack(pady=20)
164+
input_box.pack(pady=10)
165+
submit_button.pack(side=tk.RIGHT, padx=10)
166+
reset_button.pack(side=tk.RIGHT, padx=10)
167+
result_label.pack(pady=10)
168+
progressbar_label.pack(pady=10)
169+
progressbar.pack(pady=10)
170+
timer_label.pack(pady=10)
171+
countdown_label.pack(pady=10)
172+
173+
# Bind the typing event to the input_box
174+
input_box.bind("<KeyRelease>", check_input)
175+
176+
# Start the game
177+
start_game()
178+
179+
# Start the GUI event loop
180+
root.mainloop()

0 commit comments

Comments
 (0)