Skip to content

Commit 9d3228b

Browse files
committed
add png output
1 parent c009233 commit 9d3228b

File tree

6 files changed

+30
-1
lines changed

6 files changed

+30
-1
lines changed

__pycache__/grid.cpython-36.pyc

788 Bytes
Binary file not shown.

grid.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from cell import Cell
22
import random
33
import svgwrite
4+
from PIL import Image, ImageDraw
45

56
class Grid:
67
def __init__(self, rows, columns):
@@ -77,7 +78,7 @@ def to_svg(self, cell_size = 10):
7778
left_offset = 20
7879
img_width = cell_size * self.columns
7980
img_height = cell_size * self.rows
80-
dwg = svgwrite.Drawing('maze.svg')
81+
dwg = svgwrite.Drawing('maze-' + '10' + '.svg')
8182

8283
for cell in self.each_cell():
8384
x1 = cell.column * cell_size + top_offset
@@ -99,3 +100,28 @@ def to_svg(self, cell_size = 10):
99100
if not cell.linked(cell.south):
100101
dwg.add(dwg.line((x1, y2), (x2, y2), stroke=svgwrite.rgb(10, 10, 16, '%')))
101102
dwg.save()
103+
104+
def to_png(self, cell_size=20):
105+
top_offset = 20
106+
left_offset = 20
107+
img_width = cell_size * self.columns
108+
img_height = cell_size * self.rows
109+
im = Image.new('RGB', (640,800),(255,255,255))
110+
111+
draw = ImageDraw.Draw(im)
112+
113+
for cell in self.each_cell():
114+
x1 = cell.column * cell_size + top_offset
115+
y1 = cell.row * cell_size + left_offset
116+
x2 = (cell.column + 1) * cell_size + top_offset
117+
y2 = (cell.row + 1) * cell_size + left_offset
118+
119+
if not cell.north:
120+
draw.line((x1, y1, x2, y1), (0,0,0))
121+
if not cell.west:
122+
draw.line((x1, y1, x1, y2), (0,0,0))
123+
if not cell.linked(cell.east):
124+
draw.line((x2, y1, x2, y2), (0,0,0))
125+
if not cell.linked(cell.south):
126+
draw.line((x1, y2, x2, y2), (0,0,0))
127+
im.save("maze.png", "PNG")

hunt_and_kill_demo.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@
66

77
grid.to_string()
88
grid.to_svg()
9+
grid.to_png()

maze-10.svg

Lines changed: 2 additions & 0 deletions
Loading

maze.png

4.09 KB
Loading

mazepng

4.68 KB
Binary file not shown.

0 commit comments

Comments
 (0)