Skip to content

Commit 457e55e

Browse files
committed
Applying Threshold = 0.7, Moving Average of last 10 values of powerData and Ball Speed reduced to 30
1 parent a3b9541 commit 457e55e

File tree

1 file changed

+31
-10
lines changed

1 file changed

+31
-10
lines changed

applications/game.py

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
ball_radius = 20
4040
ball_color = WHITE
4141
ball_pos = [WIDTH // 2, HEIGHT // 2]
42-
ball_speed = 40
42+
ball_speed = 30
4343

4444
# Player properties
4545
player_width = 10
@@ -56,6 +56,9 @@
5656
game_started = False
5757
first_attempt = True # Keeps track if it's the first game or a restart
5858

59+
powerData1 = []
60+
powerData2 = []
61+
5962
def bandpower(data, sf, band, window_sec=None, relative=False):
6063
band = np.asarray(band)
6164
low, high = band
@@ -75,13 +78,13 @@ def bandpower(data, sf, band, window_sec=None, relative=False):
7578
return bp
7679

7780
def eeg_data_thread(eeg_queue):
81+
global powerData1, powerData2
7882
streams = resolve_stream('name', 'BioAmpDataStream')
7983
if not streams:
8084
print("No LSL stream found!")
8185
return
8286

8387
inlet = StreamInlet(streams[0])
84-
channel_assignments = {0: 'Player A', 1: 'Player B'}
8588
sampling_frequency = 500
8689
bands = {'Alpha': [8, 13],'Beta': [13, 30]}
8790
buffer_length = sampling_frequency * 1
@@ -94,6 +97,9 @@ def eeg_data_thread(eeg_queue):
9497
baseline1 = baseline2 = 1 # Initialize baselines
9598

9699
while running:
100+
if paused:
101+
time.sleep(0.1) # Pause the thread when the game is paused
102+
continue
97103
try:
98104
sample, timestamp = inlet.pull_sample()
99105
if len(sample) >= 6:
@@ -146,9 +152,11 @@ def eeg_data_thread(eeg_queue):
146152
eeg_thread.start()
147153

148154
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
150156
ball_pos = [WIDTH // 2, HEIGHT // 2]
151157
force_player1 = force_player2 = 0
158+
powerData1.clear() # Clear force data for player 1
159+
powerData2.clear() # Clear force data for player 2
152160
paused = False # Ensure the game is not paused after reset
153161
game_started = True # Ensure the game is marked as started
154162
win_text = None # Reset win text
@@ -160,16 +168,24 @@ def reset_game():
160168
eeg_queue.get()
161169
print("Game Reset Successfully.")
162170

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
167183
if ball_pos[0] < ball_radius:
168184
ball_pos[0] = ball_radius
169185
elif ball_pos[0] > WIDTH - ball_radius:
170186
ball_pos[0] = WIDTH - ball_radius
171187

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}")
173189

174190
def draw_buttons(paused, first_attempt): # Button dimensions and positions
175191
button_width = 120
@@ -248,12 +264,13 @@ def main():
248264
if pygame.Rect(WIDTH // 4 - 60, HEIGHT - 80, 120, 40).collidepoint(mouse_pos):
249265
# Start or restart the game
250266
reset_game()
267+
time.sleep(1)
251268
game_started = True
252269
first_attempt = False
253270
win_text = None
254271
win_handled = False # Reset win handling
255272
paused = False # Ensure the game is unpaused
256-
print("Gam e Restarted.")
273+
print("Game Restarted.")
257274
elif pygame.Rect(WIDTH // 2 - 60, HEIGHT - 80, 120, 40).collidepoint(mouse_pos):
258275
# Pause/Resume the game
259276
paused = not paused
@@ -292,4 +309,8 @@ def main():
292309
clock.tick(60) # 60 frames per second
293310

294311
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

Comments
 (0)