Skip to content

Commit 0bdd8bb

Browse files
committed
Fully Working Force Ball Game
1 parent c2e0bf2 commit 0bdd8bb

File tree

1 file changed

+24
-42
lines changed

1 file changed

+24
-42
lines changed

applications/game.py

Lines changed: 24 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,9 @@ def eeg_data_thread(eeg_queue):
8282

8383
inlet = StreamInlet(streams[0])
8484
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
9488
data_buffer = {'Channel1': [], 'Channel2': []}
9589
powerData1 = []
9690
powerData2 = []
@@ -118,12 +112,8 @@ def eeg_data_thread(eeg_queue):
118112
if len(data_buffer['Channel1']) >= buffer_length:
119113
power_data = {'Channel1': {}, 'Channel2': {}}
120114
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)
127117

128118
powerData1.append(power_data['Channel1']['Beta'] / power_data['Channel1']['Alpha'])
129119
powerData2.append(power_data['Channel2']['Beta'] / power_data['Channel2']['Alpha'])
@@ -156,20 +146,23 @@ def eeg_data_thread(eeg_queue):
156146
eeg_thread.start()
157147

158148
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
160150
ball_pos = [WIDTH // 2, HEIGHT // 2]
161151
force_player1 = force_player2 = 0
162152
paused = False # Ensure the game is not paused after reset
163153
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
164157

165158
# Clear any buffered EEG data
166159
while not eeg_queue.empty():
167160
eeg_queue.get()
168-
print("Empty")
161+
print("Game Reset Successfully.")
169162

170163
def update_ball_position(force_player1, force_player2):
171164
global ball_pos
172-
net_force = force_player2 - force_player1 # force direction
165+
net_force = force_player1 - force_player2 # force direction
173166
ball_pos[0] += net_force * ball_speed * 0.01
174167
if ball_pos[0] < ball_radius:
175168
ball_pos[0] = ball_radius
@@ -178,15 +171,6 @@ def update_ball_position(force_player1, force_player2):
178171

179172
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
180173

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-
190174
def draw_buttons(paused, first_attempt): # Button dimensions and positions
191175
button_width = 120
192176
button_height = 40
@@ -235,32 +219,28 @@ def draw_players():
235219

236220
def check_win_condition():
237221
if ball_pos[0] <= ball_radius:
238-
return "PLAYER A WINS!"
239-
elif ball_pos[0] >= WIDTH - ball_radius:
240222
return "PLAYER B WINS!"
223+
elif ball_pos[0] >= WIDTH - ball_radius:
224+
return "PLAYER A WINS!"
241225
return None
242226

243227
def main():
244228
global paused, game_started, first_attempt
245229
force_player1 = force_player2 = 0
246230
win_text = None # Initialize win text
247231
latest_data = (0, 0) # To store the latest EEG data for both players
232+
win_handled = False # Track if win actions are handled
248233

249234
while True:
250235
screen.fill(BLACK)
251236

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
253238

254239
for event in pygame.event.get():
255240
if event.type == pygame.QUIT:
256241
pygame.quit()
257242
sys.exit()
258243

259-
if event.type == pygame.KEYDOWN:
260-
if event.key == pygame.K_ESCAPE:
261-
pygame.quit()
262-
sys.exit()
263-
264244
if event.type == pygame.MOUSEBUTTONDOWN:
265245
mouse_pos = pygame.mouse.get_pos()
266246
if event.button == 1: # Left mouse button
@@ -270,17 +250,20 @@ def main():
270250
reset_game()
271251
game_started = True
272252
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.")
274257
elif pygame.Rect(WIDTH // 2 - 60, HEIGHT - 80, 120, 40).collidepoint(mouse_pos):
275258
# Pause/Resume the game
276259
paused = not paused
260+
print("Game Paused!" if paused else "Game Resumed!")
277261
elif pygame.Rect(3 * WIDTH // 4 - 60, HEIGHT - 80, 120, 40).collidepoint(mouse_pos):
278262
pygame.quit()
279263
sys.exit()
280264

281265
if game_started:
282266
if not paused:
283-
handle_input()
284267
if not eeg_queue.empty():
285268
force_player1, force_player2 = eeg_queue.get()
286269
latest_data = (force_player1, force_player2) # Store latest data
@@ -295,11 +278,10 @@ def main():
295278
if game_started:
296279
win_text = check_win_condition()
297280
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
303285

304286
# Draw win text if there is a winner
305287
if win_text:

0 commit comments

Comments
 (0)