diff --git a/be5/algorithms/wave_function_collapse.py b/be5/algorithms/wave_function_collapse.py index a0976fc..ac0a985 100644 --- a/be5/algorithms/wave_function_collapse.py +++ b/be5/algorithms/wave_function_collapse.py @@ -59,6 +59,8 @@ def rotate(self, counter): edges.rotate(counter) return Tile(image=new_image, edges=edges) + + @dataclass class Cell: i: int @@ -91,6 +93,7 @@ def update_options(self, collapsed_cell): cond = lambda tile: tile.right == ref_tile.left self.options = list(filter(cond, self.options)) + @dataclass class WaveFunctionCollapseGrid: tiles: list @@ -113,7 +116,7 @@ def h(self): def cells(self): return [ Cell(i=i, j=j, options=self.tiles[:]) - for i, j in product(range(0, py5.width // self.w), range(0, py5.height // self.h)) + for i, j in product(range(0, py5.width // self.w), range(0, py5.height // self.h)) ] def start(self): @@ -125,6 +128,7 @@ def collapse_cell(self, cell): self.pending_cells.remove(cell) for neighbor_cell in self.get_neighbors(cell): neighbor_cell.update_options(collapsed_cell=cell) + def get_neighbors(self, cell): i, j = cell.i, cell.j positions = [ @@ -135,9 +139,7 @@ def get_neighbors(self, cell): ] return [ - c - for c in self.pending_cells - if all(((c.i == i or c.j == j), (c.i, c.j) in positions)) + c for c in self.pending_cells if all(((c.i == i or c.j == j), (c.i, c.j) in positions)) ] @property @@ -157,7 +159,10 @@ def draw(self): py5.stroke(100) py5.rect(cell.i * self.w, cell.j * self.h, self.w, self.h) + grid = None + + def setup(): global grid py5.size(800, 800) @@ -179,6 +184,7 @@ def setup(): grid = WaveFunctionCollapseGrid(dim=40, tiles=tiles) grid.start() + def draw(): grid.collapse() grid.draw() @@ -187,4 +193,5 @@ def draw(): print("finished!") py5.no_loop() + py5.run_sketch()