alpha-numworks 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- alpha/__init__.py +7 -0
- alpha/alpha.py +214 -0
- alpha/sprites.py +50 -0
- alpha_numworks-1.0.0.dist-info/METADATA +39 -0
- alpha_numworks-1.0.0.dist-info/RECORD +8 -0
- alpha_numworks-1.0.0.dist-info/WHEEL +5 -0
- alpha_numworks-1.0.0.dist-info/licenses/LICENSE +21 -0
- alpha_numworks-1.0.0.dist-info/top_level.txt +1 -0
alpha/__init__.py
ADDED
alpha/alpha.py
ADDED
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
import os
|
|
2
|
+
os.environ["KANDINSKY_OS_MODE"] = '2' # set the os mode to Omega
|
|
3
|
+
import ion
|
|
4
|
+
import kandinsky
|
|
5
|
+
import time
|
|
6
|
+
import tracemalloc
|
|
7
|
+
|
|
8
|
+
if __package__:
|
|
9
|
+
from .sprites import draw_sprite, draw_sprites, list_sprites
|
|
10
|
+
else:
|
|
11
|
+
from sprites import draw_sprite, draw_sprites, list_sprites
|
|
12
|
+
|
|
13
|
+
## Keyboard
|
|
14
|
+
class Keyboard:
|
|
15
|
+
def __init__(self, *keys):
|
|
16
|
+
self.__press = {k: [False, False] for k in keys}
|
|
17
|
+
|
|
18
|
+
def update(self):
|
|
19
|
+
for k, s in self.__press.items():
|
|
20
|
+
s[0], s[1] = s[1], ion.keydown(k)
|
|
21
|
+
|
|
22
|
+
def press(self, key: int) -> bool:
|
|
23
|
+
return self.__press[key][1] and not self.__press[key][0]
|
|
24
|
+
|
|
25
|
+
def release(self, key: int) -> bool:
|
|
26
|
+
return not self.__press[key][1] and self.__press[key][0]
|
|
27
|
+
|
|
28
|
+
# def watch(self, *keys):
|
|
29
|
+
# for key in keys:
|
|
30
|
+
# self.__press[key] = [False, False]
|
|
31
|
+
|
|
32
|
+
# def unwatch(self, *keys):
|
|
33
|
+
# for key in keys:
|
|
34
|
+
# self.__press.pop(key)
|
|
35
|
+
|
|
36
|
+
## Event bus
|
|
37
|
+
class Event:
|
|
38
|
+
def __init__(self, type, data: dict):
|
|
39
|
+
self.type = type
|
|
40
|
+
self.data = data
|
|
41
|
+
self.cancelled = False
|
|
42
|
+
|
|
43
|
+
class EventBus:
|
|
44
|
+
def __init__(self):
|
|
45
|
+
self.__subs = []
|
|
46
|
+
|
|
47
|
+
def subscribe(self, fn, *names, priority: int=0):
|
|
48
|
+
self.__subs.append((priority, fn, names))
|
|
49
|
+
self.__subs.sort(key=lambda s: s[0])
|
|
50
|
+
return self
|
|
51
|
+
|
|
52
|
+
def dispatch(self, name, **data):
|
|
53
|
+
evt = Event(name, data)
|
|
54
|
+
for _, fn, names in self.__subs:
|
|
55
|
+
if names and name not in names:
|
|
56
|
+
continue
|
|
57
|
+
fn(evt)
|
|
58
|
+
return evt
|
|
59
|
+
|
|
60
|
+
# def clear(self):
|
|
61
|
+
# self.__subs.clear()
|
|
62
|
+
|
|
63
|
+
class Scene:
|
|
64
|
+
def __init__(self):
|
|
65
|
+
self._isDirty: bool = True
|
|
66
|
+
self.name: str = type(self).__name__
|
|
67
|
+
|
|
68
|
+
def mark_dirty(self, evt) -> None:
|
|
69
|
+
""" evt just so I can subscribe events """
|
|
70
|
+
self._isDirty = True
|
|
71
|
+
|
|
72
|
+
def mark_clean(self):
|
|
73
|
+
self._isDirty = False
|
|
74
|
+
|
|
75
|
+
def is_dirty(self) -> bool:
|
|
76
|
+
return self._isDirty
|
|
77
|
+
|
|
78
|
+
def on_start(self, app: "App") -> None:
|
|
79
|
+
# To override
|
|
80
|
+
...
|
|
81
|
+
|
|
82
|
+
def on_dispose(self, app: "App") -> None:
|
|
83
|
+
# To override
|
|
84
|
+
app.bus.clear()
|
|
85
|
+
|
|
86
|
+
def update(self, app: "App") -> None:
|
|
87
|
+
# To override
|
|
88
|
+
...
|
|
89
|
+
|
|
90
|
+
def render(self) -> None:
|
|
91
|
+
# To override
|
|
92
|
+
self._isDirty = False
|
|
93
|
+
|
|
94
|
+
class SceneManager:
|
|
95
|
+
def __init__(self):
|
|
96
|
+
self.__stack = []
|
|
97
|
+
|
|
98
|
+
def push(self, scene, ctx=None) -> Scene:
|
|
99
|
+
self.__stack.append(scene)
|
|
100
|
+
scene.on_start(ctx)
|
|
101
|
+
return scene
|
|
102
|
+
|
|
103
|
+
def pop(self, ctx=None) -> "SceneManager":
|
|
104
|
+
self.__stack.pop().on_dispose(ctx)
|
|
105
|
+
return self
|
|
106
|
+
|
|
107
|
+
def change(self, scene, ctx=None) -> Scene:
|
|
108
|
+
while len(self.__stack) > 0:
|
|
109
|
+
self.pop(ctx)
|
|
110
|
+
self.push(scene, ctx)
|
|
111
|
+
return scene
|
|
112
|
+
|
|
113
|
+
def current(self) -> Scene:
|
|
114
|
+
if not self.__stack:
|
|
115
|
+
raise RuntimeError("No scene in stack")
|
|
116
|
+
return self.__stack[-1]
|
|
117
|
+
|
|
118
|
+
class Debug:
|
|
119
|
+
def __init__(self):
|
|
120
|
+
self._scene_label_width = 0
|
|
121
|
+
tracemalloc.start()
|
|
122
|
+
self._tracemalloc = tracemalloc
|
|
123
|
+
|
|
124
|
+
def show_info(self, app, update_ms, render_ms, worst_ms):
|
|
125
|
+
if isinstance(app, App):
|
|
126
|
+
fps = "--"
|
|
127
|
+
if app.frame_duration is not None and app.frame_duration > 0: # avoid division by 0 on first frame
|
|
128
|
+
fps = str(round(1 / app.frame_duration))
|
|
129
|
+
kandinsky.draw_string(f"FPS: {fps}/{app.tickrate}", 0, 0, (0, 0, 0), (248, 252, 248), True)
|
|
130
|
+
|
|
131
|
+
scene_label = f"Scene: {app.scenes.current().name}"
|
|
132
|
+
kandinsky.draw_string(scene_label, 0, 12, (0, 0, 0), (248, 252, 248), True)
|
|
133
|
+
|
|
134
|
+
gc_label = "GC: n/d"
|
|
135
|
+
if self._tracemalloc is not None:
|
|
136
|
+
used, total = self._tracemalloc.get_traced_memory()
|
|
137
|
+
if total > 0:
|
|
138
|
+
gc_label = f"GC: {used}/{total} ({used * 100 // total}%)"
|
|
139
|
+
kandinsky.draw_string(gc_label, 0, 24, (0, 0, 0), (248, 252, 248), True)
|
|
140
|
+
|
|
141
|
+
w_label = f"U:{update_ms:.1f}ms R:{render_ms:.1f}ms W:{"--" if worst_ms is None else f"{worst_ms:.1f}"}ms"
|
|
142
|
+
kandinsky.draw_string(w_label, 0, 36, (0, 0, 0), (248, 252, 248), True)
|
|
143
|
+
|
|
144
|
+
self._scene_label_width = len(scene_label)
|
|
145
|
+
|
|
146
|
+
class App:
|
|
147
|
+
def __init__(self, debug = False):
|
|
148
|
+
self.events : EventBus = EventBus()
|
|
149
|
+
self.keyboard : Keyboard = Keyboard(ion.KEY_OK, ion.KEY_LEFT, ion.KEY_RIGHT, ion.KEY_UP, ion.KEY_DOWN)
|
|
150
|
+
self.scenes : SceneManager = SceneManager()
|
|
151
|
+
self.is_running: bool = True
|
|
152
|
+
self.debug: bool = debug
|
|
153
|
+
|
|
154
|
+
self.tickrate : int = 30
|
|
155
|
+
self.frame_time: float = 1.0 / self.tickrate
|
|
156
|
+
self.frame_duration = None
|
|
157
|
+
|
|
158
|
+
def run(self):
|
|
159
|
+
debugger = Debug() if self.debug else None # evite d'initialiser une class si on en a pas besoin
|
|
160
|
+
p_frame = None
|
|
161
|
+
worst = 0.0
|
|
162
|
+
worst_shown = None
|
|
163
|
+
frames = 0
|
|
164
|
+
|
|
165
|
+
while self.is_running:
|
|
166
|
+
try:
|
|
167
|
+
ts = time.perf_counter()
|
|
168
|
+
self.frame_duration = (
|
|
169
|
+
ts - p_frame
|
|
170
|
+
if p_frame is not None
|
|
171
|
+
else 0.0
|
|
172
|
+
)
|
|
173
|
+
p_frame = ts
|
|
174
|
+
|
|
175
|
+
self.keyboard.update()
|
|
176
|
+
scene = self.scenes.current()
|
|
177
|
+
|
|
178
|
+
t0 = time.perf_counter()
|
|
179
|
+
scene.update(self)
|
|
180
|
+
t1 = time.perf_counter()
|
|
181
|
+
|
|
182
|
+
scene = self.scenes.current()
|
|
183
|
+
scene.render()
|
|
184
|
+
t2 = time.perf_counter()
|
|
185
|
+
|
|
186
|
+
if self.debug:
|
|
187
|
+
if debugger is None:
|
|
188
|
+
debugger = Debug()
|
|
189
|
+
update_ms = (t1 - t0) * 1000 # U
|
|
190
|
+
render_ms = (t2 - t1) * 1000 # R
|
|
191
|
+
frame_ms = (t2 - t0) * 1000 #
|
|
192
|
+
worst = max(worst, frame_ms) # W
|
|
193
|
+
frames += 1
|
|
194
|
+
if frames >= 60:
|
|
195
|
+
worst_shown = worst
|
|
196
|
+
worst = 0.0
|
|
197
|
+
frames = 0
|
|
198
|
+
debugger.show_info(self, update_ms, render_ms, worst_shown)
|
|
199
|
+
elapsed = time.perf_counter() - ts
|
|
200
|
+
waiting = self.frame_time - elapsed
|
|
201
|
+
if waiting > 0:
|
|
202
|
+
time.sleep(waiting)
|
|
203
|
+
|
|
204
|
+
except RuntimeError:
|
|
205
|
+
break
|
|
206
|
+
|
|
207
|
+
def set_tickrate(self, x : int):
|
|
208
|
+
if x <= 0:
|
|
209
|
+
raise ValueError("Tick rate must be positive")
|
|
210
|
+
self.frame_time = 1.0 / x
|
|
211
|
+
self.tickrate = x
|
|
212
|
+
|
|
213
|
+
def stop(self):
|
|
214
|
+
self.is_running = False
|
alpha/sprites.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
from operator import index
|
|
2
|
+
from pathlib import Path
|
|
3
|
+
import kandinsky
|
|
4
|
+
|
|
5
|
+
SIZE = 16
|
|
6
|
+
|
|
7
|
+
def list_sprites():
|
|
8
|
+
return sorted(
|
|
9
|
+
path.stem
|
|
10
|
+
for path in Path.cwd().iterdir()
|
|
11
|
+
if path.suffix == ".spr" and path.is_file()
|
|
12
|
+
)
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def draw_sprite(name, x, y, /):
|
|
16
|
+
if not isinstance(name, str):
|
|
17
|
+
raise TypeError("sprite name must be a string")
|
|
18
|
+
filename = name + ".spr"
|
|
19
|
+
path = Path(filename)
|
|
20
|
+
if path.name != filename:
|
|
21
|
+
raise ValueError("sprite name must be a basename")
|
|
22
|
+
data = path.read_bytes()
|
|
23
|
+
if len(data) != SIZE ** 2: #validate the file
|
|
24
|
+
raise ValueError(f"{filename} must contain exactly 256 bytes")
|
|
25
|
+
|
|
26
|
+
x, y = index(x), index(y)
|
|
27
|
+
for offset, p in enumerate(data):
|
|
28
|
+
opacity = p & 3
|
|
29
|
+
if opacity == 0:
|
|
30
|
+
continue
|
|
31
|
+
|
|
32
|
+
row, column = divmod(offset, SIZE)
|
|
33
|
+
px, py = x + column, y + row
|
|
34
|
+
# https://github.com/AlphaNumworks/Alpha-Sprites/blob/de30056e42449013921b0a33bf456c67b7354fed/views/CanvaView.hpp#L32-L35
|
|
35
|
+
red = ((p >> 6) * 85)
|
|
36
|
+
green = (((p >> 4) & 3) * 85)
|
|
37
|
+
blue = (((p >> 2) & 3) * 85)
|
|
38
|
+
if opacity < 3:
|
|
39
|
+
bg_red, bg_green, bg_blue = kandinsky.get_pixel(px, py)
|
|
40
|
+
weight = opacity * 85
|
|
41
|
+
background_weight = 256 - weight
|
|
42
|
+
red = ((red * weight + (bg_red) * background_weight) >> 8)
|
|
43
|
+
green = ((green * weight + (bg_green) * background_weight) >> 8)
|
|
44
|
+
blue = ((blue * weight + (bg_blue) * background_weight) >> 8)
|
|
45
|
+
kandinsky.set_pixel(px, py, (red, green, blue))
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def draw_sprites(sprites, /):
|
|
49
|
+
for name, x, y in sprites:
|
|
50
|
+
draw_sprite(name, x, y)
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: alpha-numworks
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: PC bindings for the Alpha OS NumWorks Python module
|
|
5
|
+
Author: AlphaNumworks, Lucas Cialdella
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/AlphaNumworks/Alpha-Numworks
|
|
8
|
+
Project-URL: Issues, https://github.com/AlphaNumworks/Alpha-Numworks/issues
|
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Requires-Python: >=3.9
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
License-File: LICENSE
|
|
14
|
+
Requires-Dist: ion-numworks==2.1
|
|
15
|
+
Requires-Dist: kandinsky==2.7.2
|
|
16
|
+
Dynamic: license-file
|
|
17
|
+
|
|
18
|
+
# Alpha Numworks
|
|
19
|
+
|
|
20
|
+
This is a port of [alpha OS](https://AlphaNumworks/Alpha)'s `alpha` micropython module,
|
|
21
|
+
so you can dev some scripts on your computer and they will work on your Alpha Calculator.
|
|
22
|
+
|
|
23
|
+
This script depends of `kandinsky` and `ion-numworks`. However, this two module will mostly be used
|
|
24
|
+
alongside `alpha-numworks` by you to make your game.
|
|
25
|
+
|
|
26
|
+
Place your Alpha OS `.spr` files in the directory you run the script from.
|
|
27
|
+
Each file contains 256 bytes of RGBA2222 pixel data for a 16 × 16 sprite.
|
|
28
|
+
Use sprite names without their extensions. On PC, `list_sprites()` returns
|
|
29
|
+
those names in alphabetical order.
|
|
30
|
+
|
|
31
|
+
```python
|
|
32
|
+
import alpha
|
|
33
|
+
|
|
34
|
+
print(alpha.list_sprites())
|
|
35
|
+
alpha.draw_sprite("player", 10, 20)
|
|
36
|
+
alpha.draw_sprites([("player", 10, 20), ("tree", 40, 20)])
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
*Awoo*
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
alpha/__init__.py,sha256=PFDsAnSxLl44C78k9L7E3vjZsMxAJdwf7js-O4FHKTA,265
|
|
2
|
+
alpha/alpha.py,sha256=gdVf5URfic4VeGYlgn7pBNXdBrUxfpOOUp8gGsbYXPw,6545
|
|
3
|
+
alpha/sprites.py,sha256=Cgas-81SRTk1g9meEHS0WciMQO0wuuWhwiNSm0_TcZg,1655
|
|
4
|
+
alpha_numworks-1.0.0.dist-info/licenses/LICENSE,sha256=Y_TG902y6k-lWFm3BCF0rJ-bLIL2cK0JJeA9lx58MZY,1070
|
|
5
|
+
alpha_numworks-1.0.0.dist-info/METADATA,sha256=yeTdnGCchQjwSSmHKIZ0E4AlphjLv0TZRC_uvWaY0NU,1368
|
|
6
|
+
alpha_numworks-1.0.0.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
7
|
+
alpha_numworks-1.0.0.dist-info/top_level.txt,sha256=tqmNnOmi2RSSiPo99C03fD5Cc3r9za9xTjPAoQC1EGA,6
|
|
8
|
+
alpha_numworks-1.0.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 AlphaNumworks
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
alpha
|