mirror of
https://github.com/maoakeEnterprise/amazing.git
synced 2026-04-28 16:04:35 +02:00
trying to get the blink on the 42
This commit is contained in:
+44
-15
@@ -42,16 +42,23 @@ class MazeMLX:
|
|||||||
"1: regen; 2: path; 3: color; 4: quit;",
|
"1: regen; 2: path; 3: color; 4: quit;",
|
||||||
)
|
)
|
||||||
|
|
||||||
def put_pixel(self, x, y) -> None:
|
def put_pixel(self, x, y, color: list | None = None) -> None:
|
||||||
if x < 0 or y < 0 or x >= self.width or y >= self.height:
|
if x < 0 or y < 0 or x >= self.width or y >= self.height:
|
||||||
return
|
return
|
||||||
offset = y * self.size_line + x * (self.bpp // 8)
|
offset = y * self.size_line + x * (self.bpp // 8)
|
||||||
|
|
||||||
self.buf[offset + 0] = self.color[0]
|
if color:
|
||||||
self.buf[offset + 1] = self.color[1]
|
self.buf[offset + 0] = color[0]
|
||||||
self.buf[offset + 2] = self.color[2]
|
self.buf[offset + 1] = color[1]
|
||||||
if self.bpp >= 32:
|
self.buf[offset + 2] = color[2]
|
||||||
self.buf[offset + 3] = self.color[3]
|
if self.bpp >= 32:
|
||||||
|
self.buf[offset + 3] = color[3]
|
||||||
|
else:
|
||||||
|
self.buf[offset + 0] = self.color[0]
|
||||||
|
self.buf[offset + 1] = self.color[1]
|
||||||
|
self.buf[offset + 2] = self.color[2]
|
||||||
|
if self.bpp >= 32:
|
||||||
|
self.buf[offset + 3] = self.color[3]
|
||||||
|
|
||||||
def clear_image(self) -> None:
|
def clear_image(self) -> None:
|
||||||
self.buf[:] = b"\x00" * len(self.buf)
|
self.buf[:] = b"\x00" * len(self.buf)
|
||||||
@@ -59,26 +66,38 @@ class MazeMLX:
|
|||||||
@staticmethod
|
@staticmethod
|
||||||
def random_color() -> Any:
|
def random_color() -> Any:
|
||||||
colors = [
|
colors = [
|
||||||
[0x00, 0x00, 0xFF, 0xFF], # red
|
|
||||||
[0x00, 0xFF, 0xFF, 0xFF], # yellow
|
[0x00, 0xFF, 0xFF, 0xFF], # yellow
|
||||||
[0x00, 0xFF, 0x40, 0xFF], # green
|
[0x00, 0xFF, 0x40, 0xFF], # green
|
||||||
[0xFF, 0xBF, 0x00, 0xFF], # blue
|
[0xFF, 0xBF, 0x00, 0xFF], # blue
|
||||||
[0xFF, 0x00, 0x80, 0xFF], # purple
|
[0xFF, 0x00, 0x80, 0xFF], # purple
|
||||||
[0xFF, 0x00, 0xFF, 0xFF], # rose
|
[0xFF, 0x00, 0xFF, 0xFF], # rose
|
||||||
|
[0x00, 0x00, 0xFF, 0xFF], # red
|
||||||
|
]
|
||||||
|
while True:
|
||||||
|
for color in colors:
|
||||||
|
yield color
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def random_color_ft() -> Any:
|
||||||
|
colors = [
|
||||||
|
[0xFF, 0xBF, 0x00, 0xFF], # blue
|
||||||
|
[0xFF, 0x00, 0x80, 0xFF], # purple
|
||||||
|
[0xFF, 0x00, 0xFF, 0xFF], # rose
|
||||||
]
|
]
|
||||||
while True:
|
while True:
|
||||||
for color in colors:
|
for color in colors:
|
||||||
yield color
|
yield color
|
||||||
|
|
||||||
def put_line(self, start: tuple[int, int], end: tuple[int, int]) -> None:
|
def put_line(self, start: tuple[int, int], end: tuple[int, int],
|
||||||
|
color: list | None = None) -> None:
|
||||||
sx, sy = start
|
sx, sy = start
|
||||||
ex, ey = end
|
ex, ey = end
|
||||||
if sy == ey:
|
if sy == ey:
|
||||||
for x in range(min(sx, ex), max(sx, ex) + 1):
|
for x in range(min(sx, ex), max(sx, ex) + 1):
|
||||||
self.put_pixel(x, sy)
|
self.put_pixel(x, sy, color)
|
||||||
if sx == ex:
|
if sx == ex:
|
||||||
for y in range(min(sy, ey), max(sy, ey) + 1):
|
for y in range(min(sy, ey), max(sy, ey) + 1):
|
||||||
self.put_pixel(sx, y)
|
self.put_pixel(sx, y, color)
|
||||||
|
|
||||||
def update_maze(self, maze: np.ndarray) -> None:
|
def update_maze(self, maze: np.ndarray) -> None:
|
||||||
self.clear_image()
|
self.clear_image()
|
||||||
@@ -109,9 +128,11 @@ class MazeMLX:
|
|||||||
if maze[y][x].get_west():
|
if maze[y][x].get_west():
|
||||||
self.put_line((x0, y0), (x0, y1))
|
self.put_line((x0, y0), (x0, y1))
|
||||||
|
|
||||||
def put_block(self, ul: tuple[int, int], dr: tuple[int, int]) -> None:
|
def put_block(self, ul: tuple[int, int], dr: tuple[int, int],
|
||||||
|
color: list | None = None) -> None:
|
||||||
for y in range(min(ul[1], dr[1]), max(dr[1], ul[1])):
|
for y in range(min(ul[1], dr[1]), max(dr[1], ul[1])):
|
||||||
self.put_line((min(ul[0], dr[0]), y), (max(ul[0], dr[0]), y))
|
self.put_line((min(ul[0], dr[0]), y), (max(ul[0], dr[0]), y),
|
||||||
|
color)
|
||||||
|
|
||||||
def put_path(self, amazing: AMazeIng) -> Any:
|
def put_path(self, amazing: AMazeIng) -> Any:
|
||||||
path = amazing.solve_path()
|
path = amazing.solve_path()
|
||||||
@@ -172,7 +193,7 @@ class MazeMLX:
|
|||||||
self.put_block(ul, dr)
|
self.put_block(ul, dr)
|
||||||
return
|
return
|
||||||
|
|
||||||
def draw_ft(self, maze: np.ndarray):
|
def draw_ft(self, maze: np.ndarray, color: list | None = None):
|
||||||
self.clear_image()
|
self.clear_image()
|
||||||
margin = math.trunc(
|
margin = math.trunc(
|
||||||
math.sqrt(self.width if self.width > self.height else self.height)
|
math.sqrt(self.width if self.width > self.height else self.height)
|
||||||
@@ -222,7 +243,9 @@ class MazeMLX:
|
|||||||
def draw_image(self, amazing: AMazeIng) -> None:
|
def draw_image(self, amazing: AMazeIng) -> None:
|
||||||
if self.render_maze(amazing):
|
if self.render_maze(amazing):
|
||||||
if self.path_printer and self.print_path:
|
if self.path_printer and self.print_path:
|
||||||
self.render_path()
|
if self.render_path():
|
||||||
|
color = next(self.color_gen_ft)
|
||||||
|
color
|
||||||
else:
|
else:
|
||||||
self.draw_ft(amazing.maze.get_maze())
|
self.draw_ft(amazing.maze.get_maze())
|
||||||
self.redraw_image()
|
self.redraw_image()
|
||||||
@@ -230,6 +253,7 @@ class MazeMLX:
|
|||||||
def start(self, amazing: AMazeIng) -> None:
|
def start(self, amazing: AMazeIng) -> None:
|
||||||
self.restart_maze(amazing)
|
self.restart_maze(amazing)
|
||||||
self.shift_color()
|
self.shift_color()
|
||||||
|
self.shift_color_ft()
|
||||||
self.mlx.mlx_loop_hook(self.mlx_ptr, self.draw_image, amazing)
|
self.mlx.mlx_loop_hook(self.mlx_ptr, self.draw_image, amazing)
|
||||||
self.mlx.mlx_hook(self.win_ptr, 33, 0, self.close_loop, None)
|
self.mlx.mlx_hook(self.win_ptr, 33, 0, self.close_loop, None)
|
||||||
self.mlx.mlx_hook(
|
self.mlx.mlx_hook(
|
||||||
@@ -246,12 +270,17 @@ class MazeMLX:
|
|||||||
def shift_color(self):
|
def shift_color(self):
|
||||||
self.color_gen = self.random_color()
|
self.color_gen = self.random_color()
|
||||||
|
|
||||||
def render_path(self):
|
def shift_color_ft(self):
|
||||||
|
self.color_gen_ft = self.random_color_ft()
|
||||||
|
|
||||||
|
def render_path(self) -> bool:
|
||||||
try:
|
try:
|
||||||
next(self.path_printer)
|
next(self.path_printer)
|
||||||
time.sleep(0.03)
|
time.sleep(0.03)
|
||||||
|
return False
|
||||||
except StopIteration:
|
except StopIteration:
|
||||||
pass
|
pass
|
||||||
|
return True
|
||||||
|
|
||||||
def render_maze(self, amazing: AMazeIng) -> bool:
|
def render_maze(self, amazing: AMazeIng) -> bool:
|
||||||
try:
|
try:
|
||||||
|
|||||||
+3
-3
@@ -1,7 +1,7 @@
|
|||||||
WIDTH=15
|
WIDTH=11
|
||||||
HEIGHT=15
|
HEIGHT=11
|
||||||
ENTRY=1,1
|
ENTRY=1,1
|
||||||
EXIT=13,13
|
EXIT=11,11
|
||||||
OUTPUT_FILE=maze.txt
|
OUTPUT_FILE=maze.txt
|
||||||
PERFECT=False
|
PERFECT=False
|
||||||
GENERATOR=DFS
|
GENERATOR=DFS
|
||||||
|
|||||||
Reference in New Issue
Block a user