2727# Flag to indicate whether the game has started
2828game_started = False
2929
30+
3031# Function to start the game with a countdown
3132def start_game ():
3233 global start_time , current_sentence , timer_duration , score , game_started
@@ -49,17 +50,21 @@ def start_game():
4950 sentence_label .config (text = current_sentence )
5051 input_box .delete (0 , tk .END )
5152 input_box .focus ()
52- input_box .config (foreground = 'black' , width = 40 ) # Reset text color and increase width
53+ input_box .config (
54+ foreground = "black" , width = 40
55+ ) # Reset text color and increase width
5356 game_started = True # Set the game started flag
5457 start_main_timer () # Start the main game timer after the countdown
5558
59+
5660# Function to reset the game
5761def reset_game ():
5862 global game_started
5963 game_started = False
6064 countdown_label .config (text = "" )
6165 submit_score () # Submit the score to reset the progress bar
6266
67+
6368# Function to start the main game timer
6469def start_main_timer ():
6570 global timer_duration , game_started
@@ -72,14 +77,16 @@ def start_main_timer():
7277 timer_label .config (text = "Time's up!" )
7378 submit_score ()
7479
80+
7581# Function to handle typing and change text color
7682def check_input (event ):
7783 input_text = input_box .get ()
7884 if current_sentence .startswith (input_text ):
79- input_box .config (foreground = ' green' ) # Correct text color
85+ input_box .config (foreground = " green" ) # Correct text color
8086 update_progress_bar (len (input_text ))
8187 else :
82- input_box .config (foreground = 'red' ) # Incorrect text color
88+ input_box .config (foreground = "red" ) # Incorrect text color
89+
8390
8491# Function to handle submission of score
8592def submit_score ():
@@ -88,40 +95,49 @@ def submit_score():
8895 time_taken = round (end_time - start_time , 2 )
8996 if input_box .get () == current_sentence :
9097 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' )
98+ result_label .config (
99+ text = f"Correct! Time taken: { time_taken } seconds, WPM: { wpm } " ,
100+ foreground = "green" ,
101+ )
92102 reset_progress_bar ()
93103 animate_result_label ()
94104 current_sentence = random .choice (sentences ) # Select a new random sentence
95105 start_game ()
96106 else :
97- result_label .config (text = f"Incorrect. Time taken: { time_taken } seconds" , foreground = 'red' )
107+ result_label .config (
108+ text = f"Incorrect. Time taken: { time_taken } seconds" , foreground = "red"
109+ )
98110 animate_result_label ()
99111
112+
100113# Function to animate the result label
101114def 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' ))
115+ result_label .config (foreground = "red" )
116+ root .after (100 , lambda : result_label .config (foreground = "black" ))
117+ root .after (200 , lambda : result_label .config (foreground = "red" ))
118+ root .after (300 , lambda : result_label .config (foreground = "black" ))
119+ root .after (400 , lambda : result_label .config (foreground = "red" ))
120+ root .after (500 , lambda : result_label .config (foreground = "black" ))
121+ root .after (600 , lambda : result_label .config (foreground = "red" ))
122+ root .after (700 , lambda : result_label .config (foreground = "black" ))
123+ root .after (800 , lambda : result_label .config (foreground = "red" ))
124+ root .after (900 , lambda : result_label .config (foreground = "black" ))
125+ root .after (1000 , lambda : result_label .config (foreground = "red" ))
126+ root .after (1100 , lambda : result_label .config (text = "" , foreground = "black" ))
127+
114128
115129# Function to update the progress bar
116130def update_progress_bar (length ):
117131 progress = min (length / len (current_sentence ), 1.0 ) * 100
118132 progressbar_label .config (text = f"Progress: { int (progress )} %" )
119- progressbar ['value' ] = progress
133+ progressbar ["value" ] = progress
134+
120135
121136# Function to reset the progress bar
122137def reset_progress_bar ():
123138 progressbar_label .config (text = "Progress: 0%" )
124- progressbar ['value' ] = 0
139+ progressbar ["value" ] = 0
140+
125141
126142# Countdown timer function
127143def countdown ():
@@ -132,12 +148,14 @@ def countdown():
132148 timer_duration -= 1
133149 root .after (1000 , countdown )
134150
151+
135152# Function to calculate WPM
136153def calculate_wpm (time_taken , word_count ):
137154 minutes = time_taken / 60
138155 wpm = (word_count / 5 ) / minutes
139156 return round (wpm )
140157
158+
141159# Initialize the GUI
142160root = tk .Tk ()
143161root .geometry ("800x400" )
@@ -149,13 +167,15 @@ def calculate_wpm(time_taken, word_count):
149167style .configure ("TLabel" , foreground = "black" , font = ("Arial" , 14 ))
150168style .configure ("TButton" , font = ("Arial" , 16 ))
151169
152- sentence_label = ttk .Label (root , text = current_sentence , wraplength = 700 , justify = ' left' )
170+ sentence_label = ttk .Label (root , text = current_sentence , wraplength = 700 , justify = " left" )
153171input_box = ttk .Entry (root , font = ("Arial" , 16 ), width = 40 ) # Increase width
154172submit_button = ttk .Button (root , text = "Submit" , command = submit_score )
155173reset_button = ttk .Button (root , text = "Reset" , command = reset_game )
156174result_label = ttk .Label (root )
157175progressbar_label = ttk .Label (root , text = "Progress: 0%" )
158- progressbar = ttk .Progressbar (root , orient = tk .HORIZONTAL , length = 200 , mode = "determinate" )
176+ progressbar = ttk .Progressbar (
177+ root , orient = tk .HORIZONTAL , length = 200 , mode = "determinate"
178+ )
159179timer_label = ttk .Label (root , text = f"Time left: { timer_duration } seconds" )
160180countdown_label = ttk .Label (root , font = ("Arial" , 24 ), foreground = "red" )
161181
0 commit comments