Repository navigation
Expand file tree
/
Copy pathcode.py
More file actions
101 lines (84 loc) · 2.64 KB
/
Copy pathcode.py
File metadata and controls
101 lines (84 loc) · 2.64 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
import board
import busio
import adafruit_ssd1306
import os
import hardware
import time
def get_installed_games():
try:
# Get all files in the /games folder
all_files = os.listdir("/games")
# Keep only .py files, remove the .py extension, and ignore __init__.py
games = []
for file in all_files:
if file.endswith(".py") and file != "__init__.py":
games.append(file[:-3])
return games
except OSError:
# This happens if the /games folder doesn't exist yet
return []
def load_game(game_name):
path = "games/" + game_name + ".py"
namespace = {}
with open(path) as f:
exec(f.read(), namespace)
return namespace
installed_games = get_installed_games()
installed_games = sorted(installed_games)
print(installed_games)
oled, buttons = hardware.setup()
oled.contrast = 255
selected_game = 0
display_start = 0
prev_down = True
prev_up = True
prev_a = True
dirty = True
currently_displayed = installed_games[:7]
print(currently_displayed)
while True:
curr_down = buttons.down.value
curr_up = buttons.up.value
curr_a = buttons.a.value
if len(installed_games) > 0:
if prev_down and not curr_down:
if selected_game == len(installed_games) - 1:
selected_game = 0
else:
selected_game += 1
dirty = True
if prev_up and not curr_up:
if selected_game == 0:
selected_game = len(installed_games) - 1
else:
selected_game -= 1
dirty = True
if selected_game < display_start:
display_start = selected_game
elif selected_game >= display_start + 7:
display_start = selected_game - 7 + 1
if prev_a and not curr_a:
print("press")
game = load_game(installed_games[selected_game])
game["run"](oled, buttons)
dirty = True
prev_up = curr_up
prev_down = curr_down
prev_a = curr_a
if dirty:
currently_displayed = installed_games[display_start : display_start + 7]
oled.fill(0)
oled.text(
f"Installed Games: {selected_game + 1}/{len(installed_games)}", 0, 0, 1
)
for i in range(len(currently_displayed)):
real_index = display_start + i
if real_index == selected_game:
text_colour = 0
oled.fill_rect(0, (i + 1) * 8, 128, 8, 1)
else:
text_colour = 1
oled.text(currently_displayed[i], 0, (i + 1) * 8, text_colour)
oled.show()
dirty = False
time.sleep(0.005)