-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstroop.py.gui.py
More file actions
136 lines (103 loc) · 4.05 KB
/
Copy pathstroop.py.gui.py
File metadata and controls
136 lines (103 loc) · 4.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import tkinter as tk
from tkinter import messagebox
import random
import time
# ========== GLOBAL VARIABLES ==========
COLORS = ['Red', 'Blue', 'Green', 'Yellow', 'Pink', 'Orange']
score = 0
start_time = 0
trial_data = []
current_trial = 0
max_trials = 5
user_name = ""
users = {} # in-memory user credentials
scores = [] # in-memory list of (username, score)
# ========== FUNCTIONS ==========
def register_user():
new_username = username_entry.get().strip()
new_password = password_entry.get().strip()
if not new_username or not new_password:
messagebox.showerror("Error", "Username and password cannot be empty.")
return
if new_username in users:
messagebox.showerror("Error", "Username already exists.")
else:
users[new_username] = new_password
messagebox.showinfo("Success", "Registration Successful!")
def login_user():
global user_name
entered_username = username_entry.get().strip()
entered_password = password_entry.get().strip()
if users.get(entered_username) == entered_password:
user_name = entered_username
messagebox.showinfo("Login Success", f"Welcome, {user_name}!")
login_frame.pack_forget()
start_game()
else:
messagebox.showerror("Login Failed", "Invalid username or password.")
def start_game():
global current_trial, score, trial_data
current_trial = 0
score = 0
trial_data = []
game_frame.pack()
next_trial()
def next_trial():
global current_trial, start_time
if current_trial >= max_trials:
return game_over()
current_trial += 1
word = random.choice(COLORS)
font_color = random.choice(COLORS)
correct_answer = font_color
def handle_choice(choice):
global score
end_time = time.time()
time_taken = round(end_time - start_time, 2)
is_correct = (choice == correct_answer)
if is_correct:
score += 1
trial_data.append((word, font_color, choice, is_correct, time_taken))
next_trial()
for widget in game_frame.winfo_children():
widget.destroy()
tk.Label(game_frame, text=f"Trial {current_trial} of {max_trials}", font=('Arial', 14)).pack(pady=5)
tk.Label(game_frame, text=word, fg=font_color, font=('Arial', 40)).pack(pady=20)
options = random.sample(COLORS, 4)
if correct_answer not in options:
options[random.randint(0, 3)] = correct_answer
for opt in options:
tk.Button(game_frame, text=opt, font=('Arial', 14), width=20,
command=lambda opt=opt: handle_choice(opt)).pack(pady=5)
start_time = time.time()
def game_over():
global score
for widget in game_frame.winfo_children():
widget.destroy()
# Add this player's score
scores.append((user_name, score))
tk.Label(game_frame, text=f"Game Over! Your score: {score}/{max_trials}", font=('Arial', 16)).pack(pady=10)
show_leaderboard()
def show_leaderboard():
tk.Label(game_frame, text="Leaderboard:", font=('Arial', 14, 'bold')).pack(pady=10)
# Sort scores in descending order
sorted_scores = sorted(scores, key=lambda x: x[1], reverse=True)
for i, (name, scr) in enumerate(sorted_scores[:5], 1):
tk.Label(game_frame, text=f"{i}. {name}: {scr}", font=('Arial', 12)).pack()
tk.Button(game_frame, text="Play Again", font=('Arial', 12), command=start_game).pack(pady=10)
# ========== GUI ==========
root = tk.Tk()
root.title("Stroop Test Game")
root.geometry("500x500")
login_frame = tk.Frame(root)
tk.Label(login_frame, text="Username").pack()
username_entry = tk.Entry(login_frame)
username_entry.pack()
tk.Label(login_frame, text="Password").pack()
password_entry = tk.Entry(login_frame, show="*")
password_entry.pack()
tk.Button(login_frame, text="Register", command=register_user).pack(pady=5)
tk.Button(login_frame, text="Login", command=login_user).pack(pady=5)
login_frame.pack(pady=100)
game_frame = tk.Frame(root)
root.mainloop()