Skip to content

Commit a1ea1eb

Browse files
committed
add animation for coloring
1 parent 75e6856 commit a1ea1eb

10 files changed

+36
-12
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,4 @@ python3 coloring.py
5252

5353
![coloring]
5454

55-
[coloring]: ./images/coloring.png
55+
[coloring]: ./images/colored_and_animated.gif

__pycache__/grid.cpython-36.pyc

18 Bytes
Binary file not shown.
119 Bytes
Binary file not shown.
30 Bytes
Binary file not shown.

coloring_animation.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from colored_grid import ColoredGrid
2+
from sidewinder import Sidewinder
3+
from hunt_and_kill import HuntAndKill
4+
5+
height, width = [30, 30]
6+
7+
grid = ColoredGrid(height,width)
8+
HuntAndKill.mutate(grid, True, 10, 30)
9+
10+
start = grid.grid[int(height/2)][int(width/2)]
11+
12+
grid.distances = start.distances()
13+
14+
grid.maximum = grid.distances.max()[1]
15+
16+
grid.to_svg()

exports/maze.svg

+1-1
Loading

grid.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -116,18 +116,22 @@ def to_png(self, cell_size=20, index = "0"):
116116
y1 = cell.row * cell_size + left_offset
117117
x2 = (cell.column + 1) * cell_size + top_offset
118118
y2 = (cell.row + 1) * cell_size + left_offset
119+
color = "#fff"
119120
if len(cell.get_links()) > 0:
120-
draw.rectangle((x1 + 1, y1+1 , x2-1, y2-1), fill="#fff")
121+
if self.background_color_for(cell) is not None:
122+
color=self.background_color_for(cell)
123+
124+
draw.rectangle((x1 + 1, y1+1 , x2-1, y2-1), fill=color)
121125
if not cell.north:
122126
draw.line((x1, y1, x2, y1), (0,0,0))
123127
if not cell.west:
124128
draw.line((x1, y1, x1, y2), (0,0,0))
125129
if not cell.linked(cell.east):
126130
draw.line((x2, y1, x2, y2), (0,0,0))
127131
elif cell.east:
128-
draw.line((x2, y1 + 1, x2, y2 -1), (255,255,255))
132+
draw.line((x2, y1 + 1, x2, y2 -1), fill=color)
129133
if not cell.linked(cell.south):
130134
draw.line((x1, y2, x2, y2), (0,0,0))
131135
elif cell.south:
132-
draw.line((x1 + 1, y2, x2 - 1, y2), (255,255,255))
136+
draw.line((x1 + 1, y2, x2 - 1, y2), fill=color)
133137
im.save("./exports/maze"+index+".png", "PNG")

hunt_and_kill.py

+9-5
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44
class HuntAndKill:
55

6-
def mutate(grid, animation = False):
7-
current = grid.random_cell()
6+
def mutate(grid, animation = False, cell_size=20, fps=10):
7+
start = current = grid.random_cell()
88
index = 0
99
filenames = []
1010

@@ -17,7 +17,9 @@ def mutate(grid, animation = False):
1717
current = neighbor
1818
index += 1
1919
if animation:
20-
grid.to_png(20, str(index))
20+
grid.distances = start.distances()
21+
grid.maximum = grid.distances.max()[1]
22+
grid.to_png(cell_size, str(index))
2123
filenames.append("./exports/maze"+str(index)+".png")
2224
else:
2325
current = None
@@ -31,8 +33,10 @@ def mutate(grid, animation = False):
3133
current.link(neighbor)
3234
index += 1
3335
if animation:
34-
grid.to_png(20, str(index))
36+
grid.distances = start.distances()
37+
grid.maximum = grid.distances.max()[1]
38+
grid.to_png(cell_size, str(index))
3539
filenames.append("./exports/maze"+str(index)+".png")
3640
break
3741
if animation:
38-
MakeAnimation(filenames, 'hunt_and_kill.gif')
42+
MakeAnimation(filenames, 'hunt_and_kill.gif', fps)

images/colored_and_animated.gif

43.7 MB
Loading

make_animation.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import imageio
22
import os
33

4-
def MakeAnimation(filenames, moviename):
4+
def MakeAnimation(filenames, moviename, fps=10):
55
images = []
66
for filename in filenames:
77
images.append(imageio.imread(filename))
8-
imageio.mimsave('./exports/' + moviename, images)
8+
imageio.mimsave('./exports/' + moviename, images, fps=fps)
99
for filename in filenames:
1010
os.remove(filename)

0 commit comments

Comments
 (0)