-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCube.py
More file actions
executable file
·127 lines (98 loc) · 4.18 KB
/
Cube.py
File metadata and controls
executable file
·127 lines (98 loc) · 4.18 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
import threading
import time
import Direction
import Exit
import FFT
import FrameCollection2D as Frames
import Game as Game
import Pong
import PongMulti
import Snake
import Weather
import api
class LEDCube(threading.Thread):
cube_games = []
current_item = 0
current_game = None
current_face = api.Face.FRONT
def __init__(self, cube_size: int, delay):
super().__init__()
api.cubeSize = cube_size
api.delay = delay
# api.setup()
def register(self, *games: Game.CubeGame):
for game in games:
if game.get_menu_frame() is not None:
self.cube_games = self.cube_games + [game]
def run(self):
self.open_menu()
def unregister_all(self):
self.cube_games = []
def reset_all(self):
self.unregister_all()
Direction.direction = None
def open_menu(self):
if not self.cube_games:
self.show_2d_frame(Frames.question_mark)
else:
while True:
while self.current_game is None:
updated = False
if self.cube_games[self.current_item].has_menu_animation():
self.cube_games[self.current_item].play_animation()
while not updated:
self.show_menu_state()
if api.get_pressed_enter():
self.cube_games[self.current_item].close_animation()
self.current_game = self.cube_games[self.current_item]
api.set_pressed_enter(False)
updated = True
if Direction.direction_p_1.value == int(Direction.Direction.RIGHT) and self.current_item < len(
self.cube_games) - 1:
self.cube_games[self.current_item].close_animation()
self.current_item += 1
Direction.direction_p_1.value = 0
api.clear_all()
updated = True
if Direction.direction_p_1.value == int(Direction.Direction.LEFT) and self.current_item > 0:
self.cube_games[self.current_item].close_animation()
self.current_item -= 1
Direction.direction_p_1.value = 0
api.clear_all()
updated = True
# Otherwise we will get array out of bounds exception (similar to mathf.clamp)
# self.current_item = max(0, min(self.current_item, len(self.cube_games) - 1))
time.sleep(0.2)
api.clear_all()
self.start_game(self.current_item)
api.clear_all()
# Reset everything for next menu launch
self.reset_all()
register_all()
api.set_pressed_enter(False)
self.current_game = None
def show_2d_frame(self, frame):
api.change_face(self.current_face, 0, frame)
def start_game(self, game_id):
self.cube_games[game_id].start()
self.cube_games[game_id].join()
Direction.direction_p_1.value = 0
def kill_game(self, game_id):
pass
def show_menu_state(self):
if not self.cube_games[self.current_item].is_animation_alive():
if self.current_item > 0:
api.change_face(api.Face.LEFT, 0, Frames.arrow_left)
if self.current_item < len(self.cube_games) - 1:
api.change_face(api.Face.RIGHT, 0, Frames.arrow_right)
self.show_2d_frame(self.cube_games[self.current_item].get_menu_frame())
def register_all():
if led_cube is not None:
led_cube.register(Snake.Snake(api.cubeSize, frame_size), Pong.Pong(api.cubeSize, frame_size),
PongMulti.PongMulti(api.cubeSize, frame_size), Weather.Weather(api.cubeSize, frame_size),
FFT.AudioVis(api.cubeSize, frame_size), Exit.Exit(api.cubeSize, frame_size))
frame_size = (8, 8)
led_cube = LEDCube(8, 0.001)
register_all()
led_cube.start()
api.start()