45
45
focus_timeout = 2
46
46
focus_threshold = None
47
47
48
- # Initialize Pygame
49
- pygame .init ()
50
- screen = pygame .display .set_mode ((800 , 600 ))
51
- pygame .display .set_caption ('Beetle Game' )
52
-
53
48
sprite_count = 10
54
- beetle_sprites = [pygame .image .load (f'media/beetle { i } .JPG ' ) for i in range (1 , sprite_count + 1 )]
55
- beetle_sprites = [pygame .transform .scale (sprite , (140 , 160 )) for sprite in beetle_sprites ]
49
+ beetle_sprites = [pygame .image .load (f'media/Beetle { i } .png ' ) for i in range (1 , sprite_count + 1 )]
50
+ beetle_sprites = [pygame .transform .smoothscale (sprite , (140 , 160 )) for sprite in beetle_sprites ]
56
51
57
52
# Animation Variables
58
53
current_sprite = 0
59
54
animation_speed = 100
60
55
sprite_timer = 0
61
56
62
- # Function to display a message on the screen
63
57
def show_message (message , duration = 3 ):
64
58
start_time = time .time ()
65
- font = pygame .font .SysFont ("Arial" , 50 )
59
+ font = pygame .font .SysFont ("Arial" , 36 , bold = True )
66
60
text = font .render (message , True , (0 , 0 , 0 ))
61
+
67
62
text_rect = text .get_rect (center = (400 , 300 ))
63
+ border_rect = pygame .Rect (text_rect .left - 20 , text_rect .top - 20 , text_rect .width + 40 , text_rect .height + 40 )
68
64
69
65
while time .time () - start_time < duration :
70
66
screen .fill ((255 , 255 , 255 ))
67
+ pygame .draw .rect (screen , (0 , 0 , 0 ), border_rect , border_radius = 15 )
68
+ pygame .draw .rect (screen , (255 , 255 , 255 ), border_rect .inflate (- 10 , - 10 ), border_radius = 10 )
71
69
screen .blit (text , text_rect )
72
70
pygame .display .update ()
73
71
@@ -93,6 +91,10 @@ def calculate_focus_level(eeg_data, sampling_rate=500):
93
91
power = (beta_power + gamma_power ) / (delta_power + theta_power + alpha_power + beta_power + gamma_power )
94
92
return power
95
93
94
+ pygame .init ()
95
+ screen = pygame .display .set_mode ((800 , 600 ))
96
+ pygame .display .set_caption ('Beetle Game' )
97
+
96
98
# Calibration Phase
97
99
show_message ("Sit still and relax for 10 seconds" , 3 )
98
100
@@ -114,7 +116,7 @@ def calculate_focus_level(eeg_data, sampling_rate=500):
114
116
mean_focus = np .mean (baseline_focus_levels )
115
117
std_focus = np .std (baseline_focus_levels )
116
118
117
- focus_threshold = mean_focus + 1 .5 * std_focus
119
+ focus_threshold = mean_focus + 0 .5 * std_focus
118
120
print (f"Calibration Complete. Focus Threshold set at: { focus_threshold :.3f} " )
119
121
else :
120
122
print ("Calibration failed due to insufficient data." )
@@ -123,15 +125,22 @@ def calculate_focus_level(eeg_data, sampling_rate=500):
123
125
# Show Game Start Message
124
126
show_message ("Game Starting..." , 1 )
125
127
game_start_time = time .time ()
126
- game_duration = 45 # Game lasts 45 seconds
127
128
128
- # Update beetle position
129
129
def update_beetle_position (focus_level , is_focus_stable ):
130
130
global beetle_y
131
+ top_limit = 10 + beetle_sprites [0 ].get_height () // 2 # Top boundary
132
+ bottom_limit = 580 - beetle_sprites [0 ].get_height () // 2 # Bottom boundary
133
+
131
134
if is_focus_stable :
132
- beetle_y = max (10 + beetle_sprites [0 ].get_height () // 2 , beetle_y - focus_speed_upward )
135
+ if beetle_y > top_limit : # Move up only if not at the top
136
+ beetle_y -= focus_speed_upward
137
+ else :
138
+ beetle_y = top_limit # Stay at the top if already there
133
139
else :
134
- beetle_y = min (580 - beetle_sprites [0 ].get_height () // 2 , beetle_y + focus_speed_downward )
140
+ if beetle_y < bottom_limit : # Move down if below the bottom limit
141
+ beetle_y += focus_speed_downward
142
+ else :
143
+ beetle_y = bottom_limit # Stay at the bottom if already there
135
144
136
145
print ("STARTING GAME..." )
137
146
running = True
@@ -151,7 +160,6 @@ def update_beetle_position(focus_level, is_focus_stable):
151
160
buffer .append (filtered_sample )
152
161
153
162
current_time = time .time ()
154
- elapsed_time = int (current_time - game_start_time )
155
163
156
164
if current_time - last_time >= 1 :
157
165
last_time = current_time
@@ -181,30 +189,15 @@ def update_beetle_position(focus_level, is_focus_stable):
181
189
sprite_timer = 0
182
190
current_sprite = (current_sprite + 1 ) % len (beetle_sprites )
183
191
184
- # Check win condition
185
- if beetle_y <= 80 :
186
- show_message ("You Win!" , 1 )
187
- running = False
188
-
189
- # Check game over condition
190
- if elapsed_time >= game_duration :
191
- show_message ("Game Over! Try Again." , 1 )
192
- running = False
193
-
194
192
# Draw everything
195
193
screen .fill ("#FFFFFF" )
196
- pygame .draw .rect (screen , (0 , 0 , 0 ), (10 , 10 , 780 , 580 ), 5 )
197
- screen .blit (beetle_sprites [current_sprite ], (beetle_x - 40 , beetle_y - 40 ))
198
-
199
- # Display Timer
200
- font = pygame .font .SysFont ("Arial" , 30 )
201
- timer_text = font .render (f"Time: { game_duration - elapsed_time } s" , True , (0 , 0 , 0 ))
202
- screen .blit (timer_text , (650 , 20 ))
194
+ pygame .draw .rect (screen , (50 , 50 , 50 ), (10 , 10 , 780 , 580 ), 5 ) # Border Dark Gray
195
+ screen .blit (beetle_sprites [current_sprite ], (beetle_x - 40 , beetle_y - 40 ))
203
196
204
197
pygame .display .update ()
205
198
206
199
except KeyboardInterrupt :
207
200
print ("Exiting gracefully..." )
208
201
running = False
209
202
210
- pygame .quit ()
203
+ pygame .quit ()
0 commit comments