ADD(mlx path animation)

This commit is contained in:
2026-03-27 19:47:21 +01:00
parent 6ec617848f
commit cb19cf1413
3 changed files with 45 additions and 29 deletions
+38 -22
View File
@@ -1,9 +1,10 @@
from typing import Any from typing import Any, Generator
from src.AMazeIng import AMazeIng from src.AMazeIng import AMazeIng
from src.parsing import Parsing from src.parsing import Parsing
from mlx.mlx import Mlx from mlx.mlx import Mlx
import numpy as np import numpy as np
import math import math
import time
class MazeMLX: class MazeMLX:
@@ -12,7 +13,6 @@ class MazeMLX:
self.height = height self.height = height
self.width = width self.width = width
self.mlx_ptr = self.mlx.mlx_init() self.mlx_ptr = self.mlx.mlx_init()
self.generator = None
self.win_ptr = self.mlx.mlx_new_window( self.win_ptr = self.mlx.mlx_new_window(
self.mlx_ptr, width, height, "amazing" self.mlx_ptr, width, height, "amazing"
) )
@@ -80,13 +80,14 @@ class MazeMLX:
) )
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]) -> None:
for y in range(ul[1], dr[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))
def put_path(self, amazing: AMazeIng) -> None: def put_path(self, amazing: AMazeIng):
path = amazing.solve_path() path = amazing.solve_path()
print(path) print(path)
actual = amazing.entry actual = amazing.entry
actual = (actual[0] - 1, actual[1] - 1)
maze = amazing.maze.get_maze() maze = amazing.maze.get_maze()
if maze is None: if maze is None:
return return
@@ -101,48 +102,59 @@ class MazeMLX:
else (self.width - margin) // len(maze[0]) else (self.width - margin) // len(maze[0])
) )
) )
self.update_maze(maze)
for i in range(len(path)): for i in range(len(path)):
self.mlx.mlx_clear_window(self.mlx_ptr, self.win_ptr)
ul = ( ul = (
(actual[0]) * cell_size + margin + 6, (actual[0]) * cell_size + margin + 12,
(actual[1]) * cell_size + 6 + margin, (actual[1]) * cell_size + 12 + margin,
) )
dr = ( dr = (
(actual[0]) * cell_size + cell_size + margin - 12, (actual[0]) * cell_size + cell_size + margin - 12,
(actual[1]) * cell_size + cell_size - 12 + margin, (actual[1]) * cell_size + cell_size - 12 + margin,
) )
self.put_block(ul, dr) self.put_block(ul, dr)
x0 = actual[0] * cell_size + margin + 3 self.mlx.mlx_put_image_to_window(
y0 = actual[1] * cell_size + margin + 3 self.mlx_ptr, self.win_ptr, self.img_ptr, 0, 0
x1 = actual[0] * cell_size + cell_size + margin - 6 )
y1 = actual[1] * cell_size + cell_size + margin - 6 x0 = actual[0] * cell_size + margin + 12
if i == len(path) - 1: y0 = actual[1] * cell_size + margin + 12
continue x1 = actual[0] * cell_size + cell_size + margin - 12
y1 = actual[1] * cell_size + cell_size + margin - 12
yield
match path[i]: match path[i]:
case "N": case "N":
self.put_line((x0, y0), (x1, y0)) self.put_block((x0, y0), (x1, y0 - 24))
actual = (actual[0], actual[1] - 1) actual = (actual[0], actual[1] - 1)
case "E": case "E":
self.put_line((x1, y0), (x1, y1)) self.put_block((x1, y0), (x1 + 24, y1))
actual = (actual[0] + 1, actual[1]) actual = (actual[0] + 1, actual[1])
case "S": case "S":
self.put_line((x0, y1), (x1, y1)) self.put_block((x0, y1), (x1, y1 + 24))
actual = (actual[0], actual[1] + 1) actual = (actual[0], actual[1] + 1)
case "W": case "W":
self.put_line((x0, y0), (x0, y1)) self.put_block((x0, y0), (x0 - 24, y1))
actual = (actual[0] - 1, actual[1]) actual = (actual[0] - 1, actual[1])
ul = (
(actual[0]) * cell_size + margin + 12,
(actual[1]) * cell_size + 12 + margin,
)
dr = (
(actual[0]) * cell_size + cell_size + margin - 12,
(actual[1]) * cell_size + cell_size - 12 + margin,
)
self.put_block(ul, dr)
self.mlx.mlx_put_image_to_window( self.mlx.mlx_put_image_to_window(
self.mlx_ptr, self.win_ptr, self.img_ptr, 0, 0 self.mlx_ptr, self.win_ptr, self.img_ptr, 0, 0
) )
return
def close_loop(self, _: Any): def close_loop(self, _: Any):
self.mlx.mlx_loop_exit(self.mlx_ptr) self.mlx.mlx_loop_exit(self.mlx_ptr)
def gen_maze(self, amazing: AMazeIng) -> None:
self.generator = amazing.generate()
def start(self, amazing: AMazeIng) -> None: def start(self, amazing: AMazeIng) -> None:
self.gen_maze(amazing) self.path_printer = self.put_path(amazing)
self.generator = amazing.generate()
self.mlx.mlx_loop_hook(self.mlx_ptr, self.render, amazing) self.mlx.mlx_loop_hook(self.mlx_ptr, self.render, 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_loop(self.mlx_ptr) self.mlx.mlx_loop(self.mlx_ptr)
@@ -153,7 +165,11 @@ class MazeMLX:
self.update_maze(amazing.maze.get_maze()) self.update_maze(amazing.maze.get_maze())
# time.sleep(0.01) # time.sleep(0.01)
except StopIteration: except StopIteration:
self.put_path(amazing) try:
next(self.path_printer)
time.sleep(0.03)
except StopIteration:
pass
def main() -> None: def main() -> None:
+5 -5
View File
@@ -1,8 +1,8 @@
WIDTH=10 WIDTH=20
HEIGHT=10 HEIGHT=20
ENTRY=1,1 ENTRY=1,1
EXIT=9,9 EXIT=20,20
OUTPUT_FILE=maze.txt OUTPUT_FILE=maze.txt
PERFECT=True PERFECT=False
GENERATOR=Kruskal GENERATOR=DFS
SOLVER=DFS SOLVER=DFS
+2 -2
View File
@@ -20,9 +20,9 @@ class AMazeIng(BaseModel):
@model_validator(mode="after") @model_validator(mode="after")
def check_entry_exit(self) -> Self: def check_entry_exit(self) -> Self:
if self.entry[0] >= self.width or self.entry[1] >= self.height: if self.entry[0] > self.width or self.entry[1] > self.height:
raise ValueError("Entry coordinates exceed the maze size") raise ValueError("Entry coordinates exceed the maze size")
if self.exit[0] >= self.width or self.exit[1] >= self.height: if self.exit[0] > self.width or self.exit[1] > self.height:
raise ValueError("Exit coordinates exceed the maze size") raise ValueError("Exit coordinates exceed the maze size")
return self return self