Skip to content

Commit 07e6079

Browse files
committed
Initilizing PYGAME window on correct time, Adding borders around messages
1 parent 33aebb6 commit 07e6079

File tree

1 file changed

+26
-33
lines changed

1 file changed

+26
-33
lines changed

beetle.py

Lines changed: 26 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -45,29 +45,27 @@
4545
focus_timeout = 2
4646
focus_threshold = None
4747

48-
# Initialize Pygame
49-
pygame.init()
50-
screen = pygame.display.set_mode((800, 600))
51-
pygame.display.set_caption('Beetle Game')
52-
5348
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]
5651

5752
# Animation Variables
5853
current_sprite = 0
5954
animation_speed = 100
6055
sprite_timer = 0
6156

62-
# Function to display a message on the screen
6357
def show_message(message, duration=3):
6458
start_time = time.time()
65-
font = pygame.font.SysFont("Arial", 50)
59+
font = pygame.font.SysFont("Arial", 36, bold=True)
6660
text = font.render(message, True, (0, 0, 0))
61+
6762
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)
6864

6965
while time.time() - start_time < duration:
7066
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)
7169
screen.blit(text, text_rect)
7270
pygame.display.update()
7371

@@ -93,6 +91,10 @@ def calculate_focus_level(eeg_data, sampling_rate=500):
9391
power = (beta_power + gamma_power) / (delta_power + theta_power + alpha_power + beta_power + gamma_power)
9492
return power
9593

94+
pygame.init()
95+
screen = pygame.display.set_mode((800, 600))
96+
pygame.display.set_caption('Beetle Game')
97+
9698
# Calibration Phase
9799
show_message("Sit still and relax for 10 seconds", 3)
98100

@@ -114,7 +116,7 @@ def calculate_focus_level(eeg_data, sampling_rate=500):
114116
mean_focus = np.mean(baseline_focus_levels)
115117
std_focus = np.std(baseline_focus_levels)
116118

117-
focus_threshold = mean_focus + 1.5 * std_focus
119+
focus_threshold = mean_focus + 0.5 * std_focus
118120
print(f"Calibration Complete. Focus Threshold set at: {focus_threshold:.3f}")
119121
else:
120122
print("Calibration failed due to insufficient data.")
@@ -123,15 +125,22 @@ def calculate_focus_level(eeg_data, sampling_rate=500):
123125
# Show Game Start Message
124126
show_message("Game Starting...", 1)
125127
game_start_time = time.time()
126-
game_duration = 45 # Game lasts 45 seconds
127128

128-
# Update beetle position
129129
def update_beetle_position(focus_level, is_focus_stable):
130130
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+
131134
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
133139
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
135144

136145
print("STARTING GAME...")
137146
running = True
@@ -151,7 +160,6 @@ def update_beetle_position(focus_level, is_focus_stable):
151160
buffer.append(filtered_sample)
152161

153162
current_time = time.time()
154-
elapsed_time = int(current_time - game_start_time)
155163

156164
if current_time - last_time >= 1:
157165
last_time = current_time
@@ -181,30 +189,15 @@ def update_beetle_position(focus_level, is_focus_stable):
181189
sprite_timer = 0
182190
current_sprite = (current_sprite + 1) % len(beetle_sprites)
183191

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-
194192
# Draw everything
195193
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))
203196

204197
pygame.display.update()
205198

206199
except KeyboardInterrupt:
207200
print("Exiting gracefully...")
208201
running = False
209202

210-
pygame.quit()
203+
pygame.quit()

0 commit comments

Comments
 (0)