39
39
ball_radius = 20
40
40
ball_color = WHITE
41
41
ball_pos = [WIDTH // 2 , HEIGHT // 2 ]
42
- ball_speed = 40
42
+ ball_speed = 30
43
43
44
44
# Player properties
45
45
player_width = 10
56
56
game_started = False
57
57
first_attempt = True # Keeps track if it's the first game or a restart
58
58
59
+ powerData1 = []
60
+ powerData2 = []
61
+
59
62
def bandpower (data , sf , band , window_sec = None , relative = False ):
60
63
band = np .asarray (band )
61
64
low , high = band
@@ -75,13 +78,13 @@ def bandpower(data, sf, band, window_sec=None, relative=False):
75
78
return bp
76
79
77
80
def eeg_data_thread (eeg_queue ):
81
+ global powerData1 , powerData2
78
82
streams = resolve_stream ('name' , 'BioAmpDataStream' )
79
83
if not streams :
80
84
print ("No LSL stream found!" )
81
85
return
82
86
83
87
inlet = StreamInlet (streams [0 ])
84
- channel_assignments = {0 : 'Player A' , 1 : 'Player B' }
85
88
sampling_frequency = 500
86
89
bands = {'Alpha' : [8 , 13 ],'Beta' : [13 , 30 ]}
87
90
buffer_length = sampling_frequency * 1
@@ -94,6 +97,9 @@ def eeg_data_thread(eeg_queue):
94
97
baseline1 = baseline2 = 1 # Initialize baselines
95
98
96
99
while running :
100
+ if paused :
101
+ time .sleep (0.1 ) # Pause the thread when the game is paused
102
+ continue
97
103
try :
98
104
sample , timestamp = inlet .pull_sample ()
99
105
if len (sample ) >= 6 :
@@ -146,9 +152,11 @@ def eeg_data_thread(eeg_queue):
146
152
eeg_thread .start ()
147
153
148
154
def reset_game ():
149
- global ball_pos , force_player1 , force_player2 , paused , game_started , win_text , win_handled , restart_clicked
155
+ global ball_pos , force_player1 , force_player2 , paused , game_started , win_text , win_handled , restart_clicked , powerData1 , powerData2
150
156
ball_pos = [WIDTH // 2 , HEIGHT // 2 ]
151
157
force_player1 = force_player2 = 0
158
+ powerData1 .clear () # Clear force data for player 1
159
+ powerData2 .clear () # Clear force data for player 2
152
160
paused = False # Ensure the game is not paused after reset
153
161
game_started = True # Ensure the game is marked as started
154
162
win_text = None # Reset win text
@@ -160,16 +168,24 @@ def reset_game():
160
168
eeg_queue .get ()
161
169
print ("Game Reset Successfully." )
162
170
163
- def update_ball_position (force_player1 , force_player2 ):
164
- global ball_pos
165
- net_force = force_player1 - force_player2 # force direction
166
- ball_pos [0 ] += net_force * ball_speed * 0.01
171
+ def update_ball_position (force_player1 , force_player2 , threshold = 0.7 ):
172
+ global ball_pos , powerData1 , powerData2
173
+
174
+ # Apply moving average
175
+ average_force1 = np .mean (powerData1 [- 10 :]) if len (powerData1 ) >= 10 else 0
176
+ average_force2 = np .mean (powerData2 [- 10 :]) if len (powerData2 ) >= 10 else 0
177
+
178
+ net_force = average_force1 - average_force2
179
+
180
+ # Apply the threshold
181
+ if abs (net_force ) > threshold :
182
+ ball_pos [0 ] += net_force * ball_speed * 0.01
167
183
if ball_pos [0 ] < ball_radius :
168
184
ball_pos [0 ] = ball_radius
169
185
elif ball_pos [0 ] > WIDTH - ball_radius :
170
186
ball_pos [0 ] = WIDTH - ball_radius
171
187
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
188
+ print (f"Force Player 1: { average_force1 :.2f} , Force Player 2: { average_force2 :.2f} , Net Force: { net_force :.2f} " )
173
189
174
190
def draw_buttons (paused , first_attempt ): # Button dimensions and positions
175
191
button_width = 120
@@ -248,12 +264,13 @@ def main():
248
264
if pygame .Rect (WIDTH // 4 - 60 , HEIGHT - 80 , 120 , 40 ).collidepoint (mouse_pos ):
249
265
# Start or restart the game
250
266
reset_game ()
267
+ time .sleep (1 )
251
268
game_started = True
252
269
first_attempt = False
253
270
win_text = None
254
271
win_handled = False # Reset win handling
255
272
paused = False # Ensure the game is unpaused
256
- print ("Gam e Restarted." )
273
+ print ("Game Restarted." )
257
274
elif pygame .Rect (WIDTH // 2 - 60 , HEIGHT - 80 , 120 , 40 ).collidepoint (mouse_pos ):
258
275
# Pause/Resume the game
259
276
paused = not paused
@@ -292,4 +309,8 @@ def main():
292
309
clock .tick (60 ) # 60 frames per second
293
310
294
311
if __name__ == "__main__" :
295
- main ()
312
+ main ()
313
+
314
+ # Threshold = 0.7
315
+ # Moving Average of last 10 values of powerData.
316
+ # Ball Speed = 30
0 commit comments