-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhello_world.py
More file actions
97 lines (79 loc) · 2.88 KB
/
hello_world.py
File metadata and controls
97 lines (79 loc) · 2.88 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
#!/usr/bin/env python3
"""
Hello World - Sifteo V1 Example Game.
Each cube displays a solid color. Press the button to change color.
Tilt a cube to shift the color. Neighbor two cubes to sync their colors.
Usage:
python3 -m examples.hello_world
# or, if sifteo is installed:
python3 examples/hello_world.py
"""
import sys
import os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', 'src'))
from sifteo import BaseApp, Cube
# A palette of distinct colors (R, G, B)
COLORS = [
(255, 0, 0), # Red
(0, 255, 0), # Green
(0, 0, 255), # Blue
(255, 255, 0), # Yellow
(255, 0, 255), # Magenta
(0, 255, 255), # Cyan
(255, 128, 0), # Orange
(255, 255, 255), # White
]
class HelloWorld(BaseApp):
def setup(self):
self.color_index = {}
for cube in self.cubes:
self._init_cube(cube)
def _init_cube(self, cube):
self.color_index[cube.id] = 0
self._paint_cube(cube)
def _paint_cube(self, cube):
idx = self.color_index.get(cube.id, 0)
r, g, b = COLORS[idx % len(COLORS)]
cube.fill(r, g, b)
def on_new_cube(self, cube):
print(f"Welcome cube {cube.id}!")
self._init_cube(cube)
def on_button(self, cube, pressed):
if pressed:
# Cycle to next color
idx = self.color_index.get(cube.id, 0)
self.color_index[cube.id] = (idx + 1) % len(COLORS)
self._paint_cube(cube)
print(f"Cube {cube.id}: color -> {COLORS[self.color_index[cube.id]]}")
def on_tilt(self, cube, x, y, z):
# Shift color index based on tilt direction
idx = self.color_index.get(cube.id, 0)
if x == 0:
self.color_index[cube.id] = (idx - 1) % len(COLORS)
self._paint_cube(cube)
elif x == 2:
self.color_index[cube.id] = (idx + 1) % len(COLORS)
self._paint_cube(cube)
def on_shake(self, cube):
# Random color on shake
import random
self.color_index[cube.id] = random.randint(0, len(COLORS) - 1)
self._paint_cube(cube)
print(f"Cube {cube.id}: shaken! -> {COLORS[self.color_index[cube.id]]}")
def on_neighbor_add(self, cube, side, neighbor, neighbor_side):
# Sync colors when cubes touch
self.color_index[neighbor.id] = self.color_index.get(cube.id, 0)
self._paint_cube(neighbor)
print(f"Cube {cube.id} <-> Cube {neighbor.id}: colors synced!")
def on_flip(self, cube, face_down):
if face_down:
cube.fill(0, 0, 0) # Black when face down
print(f"Cube {cube.id}: face down")
else:
self._paint_cube(cube)
print(f"Cube {cube.id}: face up")
if __name__ == "__main__":
print("Sifteo V1 - Hello World")
print("Press button to change color, tilt to shift, shake for random")
print()
HelloWorld().run()