matrixrender 0.1.0__tar.gz

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.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 General Software Development
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,18 @@
1
+ Metadata-Version: 2.4
2
+ Name: matrixrender
3
+ Version: 0.1.0
4
+ Summary: Turn a numpy matrix into a rendered screen via PyGame
5
+ License: MIT
6
+ License-File: LICENSE
7
+ Author: Sandu Bogdan
8
+ Author-email: bogdanelsandu@hotmail.com
9
+ Requires-Python: >=3.13
10
+ Classifier: License :: OSI Approved :: MIT License
11
+ Classifier: Programming Language :: Python :: 3
12
+ Classifier: Programming Language :: Python :: 3.13
13
+ Classifier: Programming Language :: Python :: 3.14
14
+ Requires-Dist: numpy (>=2.4.1,<3.0.0)
15
+ Requires-Dist: pygame (>=2.6.1,<3.0.0)
16
+ Description-Content-Type: text/markdown
17
+
18
+
File without changes
@@ -0,0 +1 @@
1
+ from . import render as render
@@ -0,0 +1,111 @@
1
+ import pygame
2
+ import numpy as np
3
+ import typing as ttypes
4
+
5
+ class WindowExitRequested(BaseException): ...
6
+
7
+ class App:
8
+ def __init__(self, screen: pygame.Surface, clock: pygame.time.Clock, zoom: int = 1, target_fps: int = 60):
9
+ self.screen = screen
10
+ self.clock = clock
11
+ self.zoom = zoom
12
+ self.target_fps = target_fps
13
+ self.__width = int(self.screen.get_width() / zoom)
14
+ self.__height = int(self.screen.get_height() / zoom)
15
+ self.__delta_time: float = 1.0 / target_fps
16
+
17
+ def tick(self):
18
+ self.__delta_time = float(self.clock.tick(self.target_fps)) / 1000
19
+
20
+ @property
21
+ def width(self): return self.__width
22
+
23
+ @property
24
+ def height(self): return self.__height
25
+
26
+ @property
27
+ def dt(self) -> float:
28
+ return min(self.__delta_time, 0.25) # Clamp at 4 FPS
29
+
30
+ def clear_events(self):
31
+ for event in pygame.event.get():
32
+ yield event
33
+
34
+ class State:
35
+ def __init__(self, width: int, height: int):
36
+ self.x_max = width
37
+ self.y_max = height
38
+
39
+ # State.area usage:
40
+ # State.area[x, y]
41
+ #
42
+ # or (not recommended as it is slower): State.area[x][y]
43
+ self.area = np.zeros((self.x_max, self.y_max), dtype=int)
44
+
45
+ def set_area(self, area: np.ndarray) -> None:
46
+ if area.shape != self.area.shape:
47
+ raise ValueError("Area shape mismatch")
48
+
49
+ self.area = area
50
+
51
+ def set_cell(self, x: int, y: int, value: int) -> None:
52
+ self.area[x, y] = value
53
+
54
+ def init_screen(width=800, height=600, title="Pygame Window", zoom: int = 1, target_fps: int = 60) -> App:
55
+ pygame.init()
56
+
57
+ screen = pygame.display.set_mode((width * zoom, height * zoom))
58
+ pygame.display.set_caption(title)
59
+
60
+ clock = pygame.time.Clock()
61
+
62
+ return App(screen, clock, zoom, target_fps)
63
+
64
+ def _draw_cell(
65
+ screen: pygame.Surface,
66
+ x: int,
67
+ y: int,
68
+ color: pygame.Color,
69
+ cell_size: int,
70
+ ) -> None:
71
+ rect = pygame.Rect(
72
+ x * cell_size,
73
+ y * cell_size,
74
+ cell_size,
75
+ cell_size,
76
+ )
77
+ pygame.draw.rect(screen, color, rect)
78
+
79
+ WHITE: pygame.Color = pygame.Color(255, 255, 255, 255)
80
+ BLACK: pygame.Color = pygame.Color( 0, 0, 0, 255)
81
+ _states = {0: WHITE}
82
+
83
+ def register_state(value: int, color: pygame.Color) -> None:
84
+ _states[value] = color
85
+
86
+ def update_screen(app: App, state: State) -> tuple[bool, ttypes.Optional[Exception]]:
87
+ # If types don't correspond, raise Error as the caller is possibly in an invalid state
88
+ #
89
+ # If drawing fails, simply return the Error so the caller can understand what went wrong
90
+ # and handle the error / do cleanup
91
+
92
+ if (not isinstance(app, App)):
93
+ raise TypeError("app is not an instance of type App().")
94
+ elif (not isinstance(state, State)):
95
+ raise TypeError("state is not an instance of type State().")
96
+
97
+ app.screen.fill(BLACK)
98
+
99
+ for i in range(0, state.x_max):
100
+ for j in range(0, state.y_max):
101
+ _draw_cell(app.screen, i, j, _states.get(state.area[i, j], BLACK), app.zoom)
102
+
103
+ if pygame.event.peek(pygame.QUIT):
104
+ raise WindowExitRequested("User closed the PyGame window.")
105
+
106
+ try:
107
+ pygame.display.flip()
108
+ app.tick()
109
+ return (True, None)
110
+ except Exception as e:
111
+ return (False, e)
@@ -0,0 +1,25 @@
1
+ [project]
2
+ name = "matrixrender"
3
+ version = "0.1.0"
4
+ description = "Turn a numpy matrix into a rendered screen via PyGame"
5
+ authors = [
6
+ {name = "Sandu Bogdan",email = "bogdanelsandu@hotmail.com"}
7
+ ]
8
+ license = {text = "MIT License"}
9
+ readme = "README.md"
10
+ requires-python = ">=3.13"
11
+ dependencies = [
12
+ "numpy (>=2.4.1,<3.0.0)",
13
+ "pygame (>=2.6.1,<3.0.0)"
14
+ ]
15
+
16
+ [dependency-groups]
17
+ dev = [
18
+ "ruff (>=0.14.14,<0.15.0)",
19
+ "mypy (>=1.19.1,<2.0.0)"
20
+ ]
21
+
22
+
23
+ [build-system]
24
+ requires = ["poetry-core>=2.0.0,<3.0.0"]
25
+ build-backend = "poetry.core.masonry.api"