@@ -82,15 +82,9 @@ def eeg_data_thread(eeg_queue):
82
82
83
83
inlet = StreamInlet (streams [0 ])
84
84
channel_assignments = {0 : 'Player A' , 1 : 'Player B' }
85
- sampling_frequency = 250
86
- bands = {
87
- 'Delta' : [0.5 , 4 ],
88
- 'Theta' : [4 , 8 ],
89
- 'Alpha' : [8 , 13 ],
90
- 'Beta' : [13 , 30 ],
91
- 'Gamma' : [30 , 40 ]
92
- }
93
- buffer_length = sampling_frequency * 4
85
+ sampling_frequency = 500
86
+ bands = {'Alpha' : [8 , 13 ],'Beta' : [13 , 30 ]}
87
+ buffer_length = sampling_frequency * 1
94
88
data_buffer = {'Channel1' : [], 'Channel2' : []}
95
89
powerData1 = []
96
90
powerData2 = []
@@ -118,12 +112,8 @@ def eeg_data_thread(eeg_queue):
118
112
if len (data_buffer ['Channel1' ]) >= buffer_length :
119
113
power_data = {'Channel1' : {}, 'Channel2' : {}}
120
114
for band_name , band_freqs in bands .items ():
121
- power_data ['Channel1' ][band_name ] = bandpower (
122
- np .array (data_buffer ['Channel1' ]), sampling_frequency , band_freqs
123
- )
124
- power_data ['Channel2' ][band_name ] = bandpower (
125
- np .array (data_buffer ['Channel2' ]), sampling_frequency , band_freqs
126
- )
115
+ power_data ['Channel1' ][band_name ] = bandpower (np .array (data_buffer ['Channel1' ]), sampling_frequency , band_freqs )
116
+ power_data ['Channel2' ][band_name ] = bandpower (np .array (data_buffer ['Channel2' ]), sampling_frequency , band_freqs )
127
117
128
118
powerData1 .append (power_data ['Channel1' ]['Beta' ] / power_data ['Channel1' ]['Alpha' ])
129
119
powerData2 .append (power_data ['Channel2' ]['Beta' ] / power_data ['Channel2' ]['Alpha' ])
@@ -156,20 +146,23 @@ def eeg_data_thread(eeg_queue):
156
146
eeg_thread .start ()
157
147
158
148
def reset_game ():
159
- global ball_pos , force_player1 , force_player2 , paused , game_started
149
+ global ball_pos , force_player1 , force_player2 , paused , game_started , win_text , win_handled , restart_clicked
160
150
ball_pos = [WIDTH // 2 , HEIGHT // 2 ]
161
151
force_player1 = force_player2 = 0
162
152
paused = False # Ensure the game is not paused after reset
163
153
game_started = True # Ensure the game is marked as started
154
+ win_text = None # Reset win text
155
+ win_handled = False # Reset win handling
156
+ restart_clicked = True # Mark the restart button as clicked
164
157
165
158
# Clear any buffered EEG data
166
159
while not eeg_queue .empty ():
167
160
eeg_queue .get ()
168
- print ("Empty " )
161
+ print ("Game Reset Successfully. " )
169
162
170
163
def update_ball_position (force_player1 , force_player2 ):
171
164
global ball_pos
172
- net_force = force_player2 - force_player1 # force direction
165
+ net_force = force_player1 - force_player2 # force direction
173
166
ball_pos [0 ] += net_force * ball_speed * 0.01
174
167
if ball_pos [0 ] < ball_radius :
175
168
ball_pos [0 ] = ball_radius
@@ -178,15 +171,6 @@ def update_ball_position(force_player1, force_player2):
178
171
179
172
print (f"Force Player 1: { force_player1 :.2f} , Force Player 2: { force_player2 :.2f} , Net Force: { net_force :.2f} " ) # Print the forces to the console
180
173
181
- def handle_input ():
182
- global force_player1 , force_player2
183
- keys = pygame .key .get_pressed ()
184
-
185
- if keys [pygame .K_LEFT ]:
186
- force_player1 += 0.25
187
- if keys [pygame .K_RIGHT ]:
188
- force_player2 += 0.25
189
-
190
174
def draw_buttons (paused , first_attempt ): # Button dimensions and positions
191
175
button_width = 120
192
176
button_height = 40
@@ -235,32 +219,28 @@ def draw_players():
235
219
236
220
def check_win_condition ():
237
221
if ball_pos [0 ] <= ball_radius :
238
- return "PLAYER A WINS!"
239
- elif ball_pos [0 ] >= WIDTH - ball_radius :
240
222
return "PLAYER B WINS!"
223
+ elif ball_pos [0 ] >= WIDTH - ball_radius :
224
+ return "PLAYER A WINS!"
241
225
return None
242
226
243
227
def main ():
244
228
global paused , game_started , first_attempt
245
229
force_player1 = force_player2 = 0
246
230
win_text = None # Initialize win text
247
231
latest_data = (0 , 0 ) # To store the latest EEG data for both players
232
+ win_handled = False # Track if win actions are handled
248
233
249
234
while True :
250
235
screen .fill (BLACK )
251
236
252
- pygame .draw .circle (screen , ball_color , (int (ball_pos [0 ]), int (ball_pos [1 ])), ball_radius ) # Draw the ball
237
+ pygame .draw .circle (screen , ball_color , (int (ball_pos [0 ]), int (ball_pos [1 ])), ball_radius ) # Draw the ball
253
238
254
239
for event in pygame .event .get ():
255
240
if event .type == pygame .QUIT :
256
241
pygame .quit ()
257
242
sys .exit ()
258
243
259
- if event .type == pygame .KEYDOWN :
260
- if event .key == pygame .K_ESCAPE :
261
- pygame .quit ()
262
- sys .exit ()
263
-
264
244
if event .type == pygame .MOUSEBUTTONDOWN :
265
245
mouse_pos = pygame .mouse .get_pos ()
266
246
if event .button == 1 : # Left mouse button
@@ -270,17 +250,20 @@ def main():
270
250
reset_game ()
271
251
game_started = True
272
252
first_attempt = False
273
- win_text = None # Reset win text on new game
253
+ win_text = None
254
+ win_handled = False # Reset win handling
255
+ paused = False # Ensure the game is unpaused
256
+ print ("Gam e Restarted." )
274
257
elif pygame .Rect (WIDTH // 2 - 60 , HEIGHT - 80 , 120 , 40 ).collidepoint (mouse_pos ):
275
258
# Pause/Resume the game
276
259
paused = not paused
260
+ print ("Game Paused!" if paused else "Game Resumed!" )
277
261
elif pygame .Rect (3 * WIDTH // 4 - 60 , HEIGHT - 80 , 120 , 40 ).collidepoint (mouse_pos ):
278
262
pygame .quit ()
279
263
sys .exit ()
280
264
281
265
if game_started :
282
266
if not paused :
283
- handle_input ()
284
267
if not eeg_queue .empty ():
285
268
force_player1 , force_player2 = eeg_queue .get ()
286
269
latest_data = (force_player1 , force_player2 ) # Store latest data
@@ -295,11 +278,10 @@ def main():
295
278
if game_started :
296
279
win_text = check_win_condition ()
297
280
if win_text :
298
- win_sound .play () # Play sound on win
299
- paused = True # Automatically pause the game on win
300
- while not eeg_queue .empty ():
301
- eeg_queue .get ()
302
- force_player1 , force_player2 = latest_data # Store the latest data when the game is won
281
+ if not win_handled : # Ensure win actions execute only once
282
+ win_sound .play () # Play sound on win
283
+ paused = True # Automatically pause the game on win
284
+ win_handled = True # Mark win actions as handled
303
285
304
286
# Draw win text if there is a winner
305
287
if win_text :
0 commit comments