-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython gng_game.py
More file actions
114 lines (92 loc) · 3.92 KB
/
python gng_game.py
File metadata and controls
114 lines (92 loc) · 3.92 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
import tkinter as tk
import random
import csv
import time
import os
class GoNoGoGame:
def __init__(self, master):
self.master = master
self.master.title("Go/No-Go Task Game")
self.name_label = tk.Label(master, text="Enter Your Name:")
self.name_label.pack()
self.name_entry = tk.Entry(master)
self.name_entry.pack()
self.start_button = tk.Button(master, text="Start", command=self.start_game)
self.start_button.pack()
self.result_label = tk.Label(master, text="", font=('Arial', 14))
self.result_label.pack(pady=10)
self.stimulus_label = tk.Label(master, text="", font=('Helvetica', 50), fg="black")
self.stimulus_label.pack(pady=60)
self.master.bind('<space>', self.space_pressed)
self.trials = []
self.current_trial = -1
self.reaction_log = []
self.user_response = False
def start_game(self):
self.username = self.name_entry.get().strip()
if not self.username:
self.result_label.config(text="Please enter your name.")
return
self.name_label.pack_forget()
self.name_entry.pack_forget()
self.start_button.pack_forget()
self.result_label.config(text="Press SPACE on 'GO'. Do nothing on 'NO-GO'.")
self.generate_trials()
self.master.after(1000, self.next_trial)
def generate_trials(self):
self.trials = []
for _ in range(20): # total 20 trials
stimulus = random.choice(["GO", "NO-GO"])
self.trials.append(stimulus)
def next_trial(self):
self.user_response = False
self.current_trial += 1
if self.current_trial >= len(self.trials):
self.end_game()
return
self.stimulus = self.trials[self.current_trial]
self.stimulus_label.config(text=self.stimulus)
self.trial_start_time = time.time()
self.master.after(1500, self.check_response)
def space_pressed(self, event):
self.user_response = True
def check_response(self):
reaction_time = time.time() - self.trial_start_time
correct = False
if self.stimulus == "GO" and self.user_response:
correct = True
elif self.stimulus == "NO-GO" and not self.user_response:
correct = True
self.reaction_log.append({
'trial': self.current_trial + 1,
'stimulus': self.stimulus,
'response': self.user_response,
'correct': correct,
'reaction_time': round(reaction_time, 3)
})
self.stimulus_label.config(text="")
self.master.after(500, self.next_trial)
def end_game(self):
correct_go = sum(1 for r in self.reaction_log if r['stimulus'] == 'GO' and r['correct'])
correct_nogo = sum(1 for r in self.reaction_log if r['stimulus'] == 'NO-GO' and r['correct'])
total = len(self.reaction_log)
accuracy = round((correct_go + correct_nogo) / total * 100, 2)
self.result_label.config(
text=f"Game Over! Accuracy: {accuracy}%\nCorrect GO: {correct_go}, Correct NO-GO: {correct_nogo}"
)
self.stimulus_label.config(text="")
# Save result
self.save_to_csv(correct_go, correct_nogo, accuracy)
def save_to_csv(self, correct_go, correct_nogo, accuracy):
file_exists = os.path.exists("gng_scores.csv")
with open("gng_scores.csv", mode='a', newline='') as file:
writer = csv.writer(file)
if not file_exists:
writer.writerow(["Name", "Correct_GO", "Correct_NO_GO", "Accuracy"])
writer.writerow([self.username, correct_go, correct_nogo, accuracy])
# Run the game
if __name__ == "__main__":
root = tk.Tk()
root.geometry("500x400")
game = GoNoGoGame(root)
root.mainloop()