Skip to content

Commit

Permalink
feat: Add stop button and reorganize control buttons
Browse files Browse the repository at this point in the history
- Added stop button with confirmation dialog
- Reorganized control buttons in a single frame
- Updated button states during game flow
  • Loading branch information
Hassan Shabbir Ahmed committed Nov 30, 2024
1 parent 9bbdee2 commit bb5cfd1
Showing 1 changed file with 58 additions and 37 deletions.
95 changes: 58 additions & 37 deletions src/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,35 @@ def _create_widgets(self) -> None:
)
self.timer_label.pack(pady=10)

# Control buttons frame
button_frame = ttk.Frame(self.root)
button_frame.pack(pady=10)

# Start button
self.start_button = ttk.Button(
self.root,
button_frame,
text="Start",
command=self.start_game
)
self.start_button.pack(pady=10)
self.start_button.pack(side=tk.LEFT, padx=5)

# Stop button
self.stop_button = ttk.Button(
button_frame,
text="Stop",
command=self.stop_game,
state='disabled'
)
self.stop_button.pack(side=tk.LEFT, padx=5)

# Reset button
self.reset_button = ttk.Button(
button_frame,
text="Reset",
command=self.reset_game,
state='disabled'
)
self.reset_button.pack(side=tk.LEFT, padx=5)

# Stats frame
stats_frame = ttk.Frame(self.root)
Expand All @@ -117,18 +139,6 @@ def _create_widgets(self) -> None:
font=TEXT_FONT
)
self.accuracy_label.pack(side=tk.LEFT, padx=10)

# Control buttons
button_frame = ttk.Frame(self.root)
button_frame.pack(pady=20)

self.reset_button = ttk.Button(
button_frame,
text="Reset",
command=self.reset_game,
state='disabled'
)
self.reset_button.pack(side=tk.LEFT, padx=5)

def _setup_bindings(self) -> None:
"""Setup keyboard bindings."""
Expand All @@ -145,9 +155,42 @@ def start_game(self) -> None:
self.input_field.configure(state='normal')
self.input_field.delete(0, tk.END)
self.start_button.configure(state='disabled')
self.stop_button.configure(state='normal')
self.reset_button.configure(state='normal')
self._update_timer()

def stop_game(self) -> None:
"""Stop the current typing test."""
if not self.game.start_time:
return

if messagebox.askyesno("Confirm Stop", "Are you sure you want to stop the test?"):
self.end_test()

def end_test(self) -> None:
"""End the typing test."""
if not self.game.start_time:
return

results = self.game.calculate_results(self.input_field.get())
self.high_scores.add_score(
results['wpm'],
results['accuracy'],
self.game.difficulty
)

self.input_field.configure(state='disabled')
self.start_button.configure(state='normal')
self.stop_button.configure(state='disabled')
self.reset_button.configure(state='disabled')

messagebox.showinfo(
"Test Complete",
f"WPM: {results['wpm']}\n"
f"Accuracy: {results['accuracy']}%\n"
f"Time: {results['time']} seconds"
)

def reset_game(self) -> None:
"""Reset the game state."""
self.game.reset()
Expand All @@ -164,6 +207,7 @@ def reset_game(self) -> None:
self.input_field.configure(state='disabled')
self.input_field.delete(0, tk.END)
self.start_button.configure(state='normal')
self.stop_button.configure(state='disabled')
self.reset_button.configure(state='disabled')
self.timer_label.configure(text="Time: 0")
self.wpm_label.configure(text="0 WPM")
Expand Down Expand Up @@ -200,29 +244,6 @@ def check_progress(self, event: Optional[tk.Event] = None) -> None:
if len(typed_text) >= len(self.current_text) or self.game.is_time_up():
self.end_test()

def end_test(self) -> None:
"""End the typing test."""
if not self.game.start_time:
return

results = self.game.calculate_results(self.input_field.get())
self.high_scores.add_score(
results['wpm'],
results['accuracy'],
self.game.difficulty
)

self.input_field.configure(state='disabled')
self.start_button.configure(state='normal')
self.reset_button.configure(state='disabled')

messagebox.showinfo(
"Test Complete",
f"WPM: {results['wpm']}\n"
f"Accuracy: {results['accuracy']}%\n"
f"Time: {results['time']} seconds"
)

def _on_difficulty_change(self, *args) -> None:
"""Handle difficulty change."""
difficulty = self.difficulty_var.get()
Expand Down

0 comments on commit bb5cfd1

Please sign in to comment.