-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.py
More file actions
1520 lines (1260 loc) · 50.9 KB
/
Copy pathcode.py
File metadata and controls
1520 lines (1260 loc) · 50.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os
import json
import time
import math
import board
import busio
import displayio
import digitalio
import neopixel
import pwmio
import random
import terminalio
from adafruit_display_text import label
import i2cdisplaybus
import adafruit_displayio_ssd1306
import adafruit_adxl34x
from filter import EMAFilterAccelerometer
from rotary_encoder import RotaryEncoder
from adafruit_debouncer import Debouncer
from Enemy import Enemy
from Food import Food
from SignalController import SignalController
from RotaryDecoder import RotaryDecoder
from WallUtils import WallUtils
# ================================
# Screen parameters
# ================================
SCREEN_WIDTH = 128
SCREEN_HEIGHT = 64
BALL_SIZE = 5
ENEMY_SIZE = 8
WALL_OFFSET = 5
# ================================
# Physics simulation parameters
# ================================
ACC_SCALE = 0.3
FRICTION = 0.90
MAX_SPEED = 2.5
BIT_FILE = "/bit.txt"
TIME_FILE = "time_survived.txt"
# ================================
# Rotary Encoder Setup
# ================================
rotary = RotaryDecoder(board.D7, board.D8, pulses_per_detent=3)
# ================================
# Button Setup (using D9)
# ================================
pin = digitalio.DigitalInOut(board.D9)
pin.direction = digitalio.Direction.INPUT
pin.pull = digitalio.Pull.UP
button = Debouncer(pin)
# ================================
# OLED Setup
# ================================
displayio.release_displays()
i2c = busio.I2C(board.SCL, board.SDA)
display_bus = i2cdisplaybus.I2CDisplayBus(i2c, device_address=0x3C)
display = adafruit_displayio_ssd1306.SSD1306(display_bus, width=128, height=64)
# ================================
# PIXEL Setup
# ================================
pixel_up_pin = board.D10
pixel_down_pin = board.D0
pixel_left_pin = board.D1
pixel_right_pin = board.D2
pixels_up = neopixel.NeoPixel(pixel_up_pin, 1, brightness=0.3, auto_write=True)
pixels_down = neopixel.NeoPixel(pixel_down_pin, 1, brightness=0.3, auto_write=True)
pixels_left = neopixel.NeoPixel(pixel_left_pin, 1, brightness=0.3, auto_write=True)
pixels_right = neopixel.NeoPixel(pixel_right_pin, 1, brightness=0.3, auto_write=True)
# ================================
# BUZZER Setup
# ================================
BUZZER_PIN = board.D3
# ================================
# accelerometer Setup
# ================================
accel = EMAFilterAccelerometer(adafruit_adxl34x.ADXL345(i2c), alpha=0.3)
def play_intro_animation():
width = display.width
height = display.height
# --- Stage 1: Display title in the center ---
group = displayio.Group()
display.root_group = group
title_text = label.Label(terminalio.FONT, text="The Devour", color=0xFFFFFF)
title_text.anchor_point = (0.5, 0.5)
title_text.anchored_position = (width // 2, height // 2)
group.append(title_text)
time.sleep(1)
# --- Stage 2: Fill screen with white "<" triangles ---
bitmap = displayio.Bitmap(width, height, 2)
palette = displayio.Palette(2)
palette[0] = 0x000000
palette[1] = 0xFFFFFF
tile_grid = displayio.TileGrid(bitmap, pixel_shader=palette)
group.append(tile_grid)
duration = 3
start_time = time.monotonic()
while time.monotonic() - start_time < duration:
elapsed = time.monotonic() - start_time
progress = elapsed / duration
max_x = int(width * progress)
for y in range(height):
if y <= height // 2:
x_limit = max_x - int((y / (height // 2)) * max_x)
else:
y_mirror = height - y - 1
x_limit = max_x - int((y_mirror / (height // 2)) * max_x)
for x in range(x_limit):
bitmap[x, y] = 1
display.refresh(minimum_frames_per_second=0)
time.sleep(0.02)
# --- Stage 3: White curtains close from top and bottom ---
duration = 2
start_time = time.monotonic()
while time.monotonic() - start_time < duration:
elapsed = time.monotonic() - start_time
progress = min(elapsed / duration, 1.0) # 0 - 1
max_y = int((height // 2) * progress)
for y in range(max_y + 3):
if y < height // 2:
for x in range(width):
bitmap[x, y] = 1
for y in range(height - 1, height - 4 - max_y, -1):
if y >= height // 2:
for x in range(width):
bitmap[x, y] = 1
display.refresh(minimum_frames_per_second=0)
time.sleep(0.01)
for y in range(height // 2 - 1, height // 2 + 2):
for x in range(width):
bitmap[x, y] = 1
display.refresh(minimum_frames_per_second=0)
# --- Stage 4: Draw black smiley face on white background ---
face_text = label.Label(terminalio.FONT, text="=)", color=0x000000)
face_text.anchor_point = (0.5, 0.5)
face_text.anchored_position = (width // 2, height // 2)
group.append(face_text)
time.sleep(2)
# ================================
# Game Data Management
# ================================
def load_game_data():
"""Load game data from bit.txt. Return default values if file doesn't exist."""
default_data = {
"times": -1,
"easyleft": -1,
"mediumleft": -1,
"hardleft": -1,
"success": -1
}
try:
with open(BIT_FILE, "r") as f:
data = json.load(f)
# Fill missing fields with default values
for key in default_data:
if key not in data:
data[key] = default_data[key]
return data
except OSError:
# File does not exist
return default_data
except Exception as e:
print("read bit.txt error:", e)
return default_data
def save_game_data(times, easyleft, mediumleft, hardleft, success):
"""Save current game data to bit.txt (overwrite file)."""
data = {
"times": times,
"easyleft": easyleft,
"mediumleft": mediumleft,
"hardleft": hardleft,
"success": success
}
# Remove existing file if it exists
try:
os.remove(BIT_FILE)
except OSError:
pass # File not present is fine
# Save new data
with open(BIT_FILE, "w") as f:
json.dump(data, f)
f.flush()
# Optional: print saved content
with open(BIT_FILE, "r") as f:
print("Saved content:", f.read())
# ================================
# High Score Management
# ================================
def load_high_scores():
"""Load high scores from TIME_FILE. Return default top-3 if file doesn't exist."""
default_scores = [
{"name": "__", "time": 0},
{"name": "__", "time": 0},
{"name": "__", "time": 0}
]
try:
with open(TIME_FILE, "r") as f:
data = json.load(f)
# Ensure we have exactly 3 entries
for i in range(3):
if i >= len(data):
data.append({"name": "__", "time": 0})
else:
if "name" not in data[i]: data[i]["name"] = "__"
if "time" not in data[i]: data[i]["time"] = 0
return data[:3]
except OSError:
return default_scores
except Exception as e:
print("read time_survived.txt error:", e)
return default_scores
def save_high_scores(high_scores):
"""Save high scores to TIME_FILE (overwrite file)."""
try:
# Remove existing file
try: os.remove(TIME_FILE)
except OSError: pass
with open(TIME_FILE, "w") as f:
json.dump(high_scores, f)
f.flush()
# Optional: print saved content
with open(TIME_FILE, "r") as f:
print("Saved high_scores:", f.read())
except Exception as e:
print("save high_scores error:", e)
def update_high_scores(new_name, survived_time):
"""Update high scores. Insert new score if it enters Top 3."""
high_scores = load_high_scores()
high_scores.append({"name": new_name, "time": survived_time})
# Sort descending by survived time
high_scores = sorted(high_scores, key=lambda x: x["time"], reverse=True)
# Keep top 3
high_scores = high_scores[:3]
save_high_scores(high_scores)
return high_scores
# ================================
# User Input
# ================================
def enter_name(group, font=terminalio.FONT):
"""
Let the user enter a 2-letter name using a rotary encoder and a button.
group: displayio.Group()
font: display font
Returns: 2-letter string, e.g., "AB"
"""
idx = [0, 0] # Current letter indices
cur = 0 # Currently editing position (0 or 1)
letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
# Display label
label_obj = label.Label(
font,
text=f"{letters[idx[0]]} {letters[idx[1]]}",
color=0xFFFFFF,
x=10, y=10
)
group.append(label_obj)
while True:
step = rotary.update()
if step != 0:
idx[cur] = (idx[cur] + step) % 26
label_obj.text = f"{letters[idx[0]]} {letters[idx[1]]}"
display.refresh(minimum_frames_per_second=0)
button.update()
if button.fell:
time.sleep(0.2)
if cur == 0:
cur = 1
else:
# Finished input
name = letters[idx[0]] + letters[idx[1]]
group.remove(label_obj)
return name
# ================================
# Sound Effects
# ================================
def play_tone(freq, duration=0.1, volume=60000):
"""
Play a short tone.
freq: frequency in Hz
duration: seconds
volume: PWM duty cycle
"""
buzzer = pwmio.PWMOut(BUZZER_PIN, frequency=freq, duty_cycle=volume)
time.sleep(duration)
buzzer.deinit()
time.sleep(random.uniform(0.05, 0.12)) # Simulate key press interval
def typing_sound(num_taps=20):
"""
Simulate typing sound effect.
num_taps: number of key presses
"""
possible_freqs = [200, 220, 240, 260, 280, 300]
for _ in range(num_taps):
freq = random.choice(possible_freqs)
duration = random.uniform(0.08, 0.12)
play_tone(freq, duration)
# ================================
# Shake Detection
# ================================
SHAKE_THRESHOLD = 1.5 # g units
SHAKE_COOLDOWN = 0.5 # seconds
last_shake_time = 0
def detect_shake(ax, ay, az):
"""
Detect shake motion from accelerometer readings.
Returns True if shake detected.
"""
global last_shake_time
now = time.monotonic()
accel_magnitude = math.sqrt(ax*ax + ay*ay + az*az)
if accel_magnitude > SHAKE_THRESHOLD and (now - last_shake_time) > SHAKE_COOLDOWN:
last_shake_time = now
return True
return False
# ================================
# Random Positions & Collision
# ================================
def generate_random_positions(player_x, player_y, n, margin=15):
"""
Generate n random coordinates ensuring:
1. Distance from player is at least `margin` pixels
2. Distance from wall is at least WALL_OFFSET pixels
"""
positions = []
attempts = 0
max_attempts = n * 20 # prevent infinite loop
while len(positions) < n and attempts < max_attempts:
attempts += 1
x = random.randint(WALL_OFFSET + ENEMY_SIZE, SCREEN_WIDTH - WALL_OFFSET - ENEMY_SIZE)
y = random.randint(WALL_OFFSET + ENEMY_SIZE, SCREEN_HEIGHT - WALL_OFFSET - ENEMY_SIZE)
# Skip if too close to player
if abs(x - player_x) < margin and abs(y - player_y) < margin:
continue
# Acceptable, add to list
positions.append((x, y))
if len(positions) < n:
print("Warning: not enough food")
return positions
def check_direction_collision(x, y):
"""Check if the ball hits UP / DOWN / LEFT / RIGHT wall based on its position"""
if y <= WALL_OFFSET:
return "UP"
if y >= SCREEN_HEIGHT - BALL_SIZE - WALL_OFFSET:
return "DOWN"
if x <= WALL_OFFSET:
return "LEFT"
if x >= SCREEN_WIDTH - BALL_SIZE - WALL_OFFSET:
return "RIGHT"
return None
def enter_next_tile(hit_dir, x, y):
"""Teleport the ball to the opposite side based on collision direction"""
if hit_dir == "UP":
y = SCREEN_HEIGHT - BALL_SIZE - WALL_OFFSET - BALL_SIZE/2
elif hit_dir == "DOWN":
y = WALL_OFFSET + BALL_SIZE/2
elif hit_dir == "LEFT":
x = SCREEN_WIDTH - BALL_SIZE - WALL_OFFSET - BALL_SIZE/2
elif hit_dir == "RIGHT":
x = WALL_OFFSET + BALL_SIZE/2
return x, y
# ================================
# Menu / Text Display
# ================================
def split_text_to_lines(text, max_chars_per_line=16):
"""Split long text into multiple lines, each line <= max_chars_per_line"""
words = text.split(' ')
lines = []
current_line = ""
for word in words:
if len(current_line + ' ' + word) <= max_chars_per_line:
if current_line:
current_line += ' ' + word
else:
current_line = word
else:
lines.append(current_line)
current_line = word
if current_line:
lines.append(current_line)
return lines
def refresh_display(option_labels, options, selection):
"""Update menu display to highlight the current selection"""
for idx, lbl in enumerate(option_labels):
if idx == selection:
lbl.text = "> " + options[idx] + " <"
else:
lbl.text = " " + options[idx] + " "
def display_lines(num_lines, options, with_typing_sound=False):
"""
Display menu or dialogue options.
num_lines: number of lines to display
options: list of option strings
Returns: selected line index (0-based)
"""
group = displayio.Group()
option_labels = []
# vertical positioning
y_start = 22
line_height = 12
current_y = y_start
for i, text in enumerate(options):
# split lines if too long
lines = split_text_to_lines(text, max_chars_per_line=16)
for j, line_text in enumerate(lines):
lbl = label.Label(
terminalio.FONT,
text=line_text,
anchored_position=(64, current_y),
anchor_point=(0.5, 0.5)
)
option_labels.append(lbl)
group.append(lbl)
current_y += line_height
if num_lines == 1 and i == 0:
arrow_label = label.Label(
terminalio.FONT,
text="v",
anchored_position=(110, current_y),
anchor_point=(0.5, 0.5)
)
group.append(arrow_label)
arrow_last_toggle = time.monotonic()
arrow_visible = True
ARROW_BLINK_INTERVAL = 0.5 # blink every 0.5s
display.root_group = group
if with_typing_sound:
num_words = len(line_text.split())
taps = max(1, num_words)
typing_sound(taps)
# single line logic: wait for button press
if num_lines == 1:
while True:
now = time.monotonic()
if now - arrow_last_toggle > ARROW_BLINK_INTERVAL:
arrow_last_toggle = now
arrow_visible = not arrow_visible
arrow_label.text = "v" if arrow_visible else ""
button.update()
if button.fell:
return 0
time.sleep(0.01)
# multi-line logic: use rotary to select
selection = 0
refresh_display(option_labels, options, selection) # initialize display
while True:
move = rotary.update()
if move != 0:
selection = (selection + move) % num_lines
refresh_display(option_labels, options, selection)
button.update()
if button.fell:
time.sleep(0.2)
return selection
time.sleep(0.01)
def clear(group):
"""Clear all items from a display group"""
for i in range(len(group)):
group.pop()
return
def turn_off_all_lights(controllers):
"""Turn off all light controllers"""
for ctrl in controllers.values():
ctrl.stop()
def generate_tile_data(allow_dir):
"""
Generate enemy and food counts for four directions:
Food: 5~20, Enemy: 0~3
Also returns: direction(s) with most food and direction(s) with most enemies
"""
data = {}
# Generate data
for d in allow_dir:
data[d] = {
"food": random.randint(5, 20),
"enemy": random.randint(0, 3)
}
# ---- Find directions with maximum values ----
max_food = max(data[d]["food"] for d in allow_dir)
max_enemy = max(data[d]["enemy"] for d in allow_dir)
food_max_dirs = [d for d in allow_dir if data[d]["food"] == max_food]
enemy_max_dirs = [d for d in allow_dir if data[d]["enemy"] == max_enemy]
return data, food_max_dirs, enemy_max_dirs
def choose_difficulty(Easy_left, Medium_left, Hard_left, sound):
"""
Display the difficulty selection menu and return the player's choice,
while updating the remaining attempts for each difficulty.
Returns: (chosen_difficulty_index, Easy_left, Medium_left, Hard_left)
"""
if Easy_left + Medium_left + Hard_left == 0:
return -2, Easy_left, Medium_left, Hard_left
# Prompt for choice
display_lines(1, ["Make a choice."], sound)
while True:
choice_index = display_lines(3, ["Easy", "Medium", "Hard"], sound)
if choice_index == 0:
if Easy_left == 0:
display_lines(1, ["Sadly there's no Easy left :("], sound)
continue
Easy_left -= 1
break
elif choice_index == 1:
if Medium_left == 0:
display_lines(1, ["Sadly there's no Medium left :("], sound)
continue
Medium_left -= 1
break
else:
if Hard_left == 0:
display_lines(1, ["Sadly there's no Hard left :("], sound)
continue
Hard_left -= 1
break
return choice_index, Easy_left, Medium_left, Hard_left
def end_game(sound):
"""
Handle game over:
1. Display options: Continue or End Game
2. If End Game is selected, clear OLED and enter shake detection loop
3. If shake is detected, return True to indicate game can restart
"""
# Display menu options
choice = display_lines(2, ["Continue", "End Game"], sound)
if choice == 0:
# Continue game
return
else:
# End game: OLED black screen
choice = display_lines(1, ["Fine :("], sound)
display.root_group = displayio.Group() # Clear screen
display.refresh()
# Continuous shake detection
while True:
ax, ay, az = accel.read_filtered()
if accel.detect_shake(x=ax, y=ay, z=az):
choice = display_lines(1, ["Hey you're back! Let's continue :D"], sound)
return
time.sleep(0.05) # Prevent high CPU usage
def run_game(mode, choice, times, sound):
"""Main game entry point. """
if mode == "Tutorial":
return tutorial_game()
elif mode == "Boss":
return boss_game()
else:
return normal_game(choice, times, sound)
def tutorial_game():
group = displayio.Group()
wall_utils = WallUtils()
lives = 3
wall_utils.draw_lives(group, lives)
current_dir = "UP"
shield_dirs = ["UP", "LEFT", "RIGHT", "DOWN"]
protected = False # Whether player is protected
foods = [] # List of food items currently on screen
score = 0 # Player score
# Create ball bitmap
bitmap = displayio.Bitmap(BALL_SIZE, BALL_SIZE, 1)
palette = displayio.Palette(1)
palette[0] = 0xFFFFFF
ball_tile = displayio.TileGrid(bitmap, pixel_shader=palette)
group.append(ball_tile)
display.root_group = group
# Initial ball state
x = SCREEN_WIDTH // 2
y = SCREEN_HEIGHT // 2
vx = 0.0
vy = 0.0
# Generate allowed directions for the first tile
allowed_dirs = wall_utils.generate_random_directions("UP")
wall_utils.draw_block_walls(group, allowed_dirs)
wall_utils.draw_score(group, initial_score=0)
tile_count = 0
enemy = [] # List of enemies not yet spawned
while True:
# --- Update speed & position ---
ax, ay, az = accel.read_filtered()
vx += ax * ACC_SCALE
vy -= ay * ACC_SCALE
vx = max(-MAX_SPEED, min(MAX_SPEED, vx))
vy = max(-MAX_SPEED, min(MAX_SPEED, vy))
vx *= FRICTION
vy *= FRICTION
x += vx
y += vy
# Boundary hard limits
x = max(WALL_OFFSET, min(SCREEN_WIDTH - BALL_SIZE-WALL_OFFSET, x))
y = max(WALL_OFFSET, min(SCREEN_HEIGHT - BALL_SIZE-WALL_OFFSET, y))
# Update ball position
ball_tile.x = int(x)
ball_tile.y = int(y)
# --- Check for collisions with walls ---
hit_dir = check_direction_collision(x, y)
if hit_dir:
if hit_dir in allowed_dirs:
# Allowed direction: move to next tile
x, y = enter_next_tile(hit_dir, x, y)
ball_tile.x = int(x)
ball_tile.y = int(y)
tile_count += 1
# Remove existing food objects from display
if len(foods) > 0:
for food_obj in foods:
if food_obj.tile in group:
group.remove(food_obj.tile)
# Generate food after passing 2 tiles
if tile_count >= 2:
if tile_count == 2:
display_lines(1, ["Get some scores"])
display.root_group = group
num_foods = random.randint(1, 5) # Random number 1~5
rand_positions = generate_random_positions(x, y, num_foods, margin=10)
foods = [Food(group, SCREEN_WIDTH, SCREEN_HEIGHT) for _ in range(num_foods)]
# Assign positions to food objects
for food_obj, (fx, fy) in zip(foods, rand_positions):
food_obj.x = fx
food_obj.y = fy
food_obj.tile.x = fx
food_obj.tile.y = fy
if food_obj.tile not in group:
group.append(food_obj.tile)
# Remove existing enemies from display
if len(enemy) > 0:
for e in enemy:
if e.tile in group:
group.remove(e.tile)
enemy = []
# Spawn enemies after 4 tiles
if tile_count == 6:
display_lines(1, ["Be careful"])
display.root_group = group
rand_positions = generate_random_positions(x, y, 1)
enemy = [Enemy(group, px, py, size=8, style="spiky_circle", teeth_count=12) for px, py in rand_positions]
# Spawn random enemies after 6 tiles
if tile_count > 6:
num_enemies = random.randint(0, 3)
rand_positions = generate_random_positions(x, y, num_enemies)
enemy = [Enemy(group, px, py, size=8, style="spiky_circle", teeth_count=12) for px, py in rand_positions]
# Tutorial messages and level completion
if tile_count == 8:
display_lines(1, ["Get specific scores to beat the level"])
display_lines(1, ["10 will be enough"])
display.root_group = group
if score >= 10:
# Clear screen
for i in range(len(group)):
group.pop()
display_lines(1, ["Actually you've achieved it"])
display_lines(1, ["You did a great job :)"])
return
if tile_count > 8 and score >= 10:
for i in range(len(group)):
group.pop()
display_lines(1, ["Congratulations"])
return
# Generate allowed directions for next tile
allowed_dirs = wall_utils.generate_random_directions(hit_dir)
wall_utils.draw_block_walls(group, allowed_dirs)
# Check if player collects food
for food_obj in foods[:]:
if food_obj.check_collision(x, y, BALL_SIZE):
score += food_obj.points
wall_utils.update_score(score)
foods.remove(food_obj)
# Player shield logic
if protected:
move = rotary.update() # Only returns 0 or 1
if move != 0:
dirs = ["UP", "RIGHT", "DOWN", "LEFT"]
idx = dirs.index(current_dir)
current_dir = dirs[(idx + 1) % 4] # Rotate clockwise by 1
shield_dirs = [current_dir]
wall_utils.draw_player_shields(group, x, y, shield_dirs)
wall_utils.update_shields_position(x, y)
# Enemy behavior
if len(enemy) > 0:
for e in enemy:
e.check_activation(x, y)
e.update(x, y)
# Check if enemy hits shield
if e.check_hit_shield(wall_utils.shield_list):
group.remove(e.tile)
enemy.remove(e)
continue
# Check collision with player
if e.has_collision(x, y, BALL_SIZE) and not protected:
wall_utils.draw_lives(group, lives)
display_lines(1, ["If life gets zero, the game is over."])
display_lines(1, ["It's just a simulation. They are not harmful."])
display_lines(1, ["Use my weapon to eliminate them"])
display_lines(1, ["Spin the button to change direction"])
display.root_group = group
shield_dirs = [current_dir]
wall_utils.draw_player_shields(group, x, y, shield_dirs)
protected = True
time.sleep(0.015)
def normal_game(mode, times, sound):
# Initialize parameters
if mode == 0:
time_limit = 60
lives = 5
elif mode == 1:
time_limit = 40
lives = 3
else:
time_limit = 30
lives = 1
target_score = 1
if times == 20:
time_limit = 1000
target_score = 1000
display_lines(1, ["RUN! =D"], sound)
start_time = time.monotonic()
group = displayio.Group()
wall_utils = WallUtils()
wall_utils.draw_lives(group, lives)
# Initialize countdown display
wall_utils.draw_countdown(group, time_limit)
current_dir = "UP"
shield_dirs = ["UP", "LEFT", "RIGHT", "DOWN"]
foods = [] # List of food items currently on screen
score = 0 # Player score
# Create ball bitmap
bitmap = displayio.Bitmap(BALL_SIZE, BALL_SIZE, 1)
palette = displayio.Palette(1)
palette[0] = 0xFFFFFF
ball_tile = displayio.TileGrid(bitmap, pixel_shader=palette)
group.append(ball_tile)
display.root_group = group
# Initial ball state
x = SCREEN_WIDTH // 2
y = SCREEN_HEIGHT // 2
vx = 0.0
vy = 0.0
allowed_dirs = wall_utils.generate_random_directions("UP")
wall_utils.draw_block_walls(group, allowed_dirs)
wall_utils.draw_score(group, initial_score=0)
if times > 6:
wall_utils.draw_player_shields(group, x, y, [current_dir])
tile_count = 0
enemy = [] # List of enemies not yet spawned
invincible = False
invincible_end_time = 0
blink_state = True
blink_timer = 0
# Light controllers initialization
controllers = {
"UP": SignalController(pixels_up),
"LEFT": SignalController(pixels_left),
"RIGHT": SignalController(pixels_right),
"DOWN": SignalController(pixels_down)
}
# ======== Generate random number of enemies/food for four directions ========
num_foods = 10 # init
rand_positions = generate_random_positions(x, y, num_foods, margin=10)
foods = [Food(group, SCREEN_WIDTH, SCREEN_HEIGHT) for _ in range(num_foods)]
# Assign generated positions to each Food object
for food_obj, (fx, fy) in zip(foods, rand_positions):
food_obj.x = fx
food_obj.y = fy
food_obj.tile.x = fx
food_obj.tile.y = fy
if food_obj.tile not in group:
group.append(food_obj.tile)
if len(enemy) > 0: # Remove existing enemies from display
for e in enemy:
if e.tile in group:
group.remove(e.tile)
enemy = [] # Clear list
# Spawn enemies
num_enemies = 1
rand_positions = generate_random_positions(x, y, num_enemies)
# Create enemy list
enemy = [Enemy(group, px, py, size=8, speed=0.1 + times*0.1 , activate_dist=10 + 2 * times, style="spiky_circle", teeth_count=12) for px, py in rand_positions]
tile_data, food_max_dirs, enemy_max_dirs = generate_tile_data(allowed_dirs)
# Light indicators
if times > 3:
SignalController.direction_signal(food_max_dirs, enemy_max_dirs, controllers)
while True:
# --- Update countdown ---
elapsed = time.monotonic() - start_time
remaining_time = max(0, int(time_limit - elapsed))
wall_utils.update_countdown(remaining_time)
if remaining_time <= 0:
clear(group)
if times == 20:
display_lines(1, ["That's..unexpected....:o"], sound)
else:
display_lines(1, ["Time's up!"], sound)
display_lines(1, ["Try again, I believe in you"], sound)
turn_off_all_lights(controllers)
return False
# --- Invincibility blink logic ---
if invincible:
now = time.monotonic()
# End invincibility after timeout
if now >= invincible_end_time:
invincible = False
ball_tile.hidden = False # Restore visibility
else:
# Toggle blink state every 0.15 seconds
if now - blink_timer > 0.15:
blink_timer = now
blink_state = not blink_state
ball_tile.hidden = blink_state
# --- Update speed & position ---
ax, ay, az = accel.read_filtered()
vx += ax * ACC_SCALE
vy -= ay * ACC_SCALE
vx = max(-MAX_SPEED, min(MAX_SPEED, vx))
vy = max(-MAX_SPEED, min(MAX_SPEED, vy))
vx *= FRICTION
vy *= FRICTION
x += vx
y += vy
# Boundary hard limits
x = max(WALL_OFFSET, min(SCREEN_WIDTH - BALL_SIZE-WALL_OFFSET, x))
y = max(WALL_OFFSET, min(SCREEN_HEIGHT - BALL_SIZE-WALL_OFFSET, y))
# Update ball position
ball_tile.x = int(x)
ball_tile.y = int(y)
# --- Check for collisions with walls ---
hit_dir = check_direction_collision(x, y)
if hit_dir:
if hit_dir in allowed_dirs:
# Allowed direction: move to next tile
x, y = enter_next_tile(hit_dir, x, y)
ball_tile.x = int(x)
ball_tile.y = int(y)