11from cell import Cell
22import random
33import svgwrite
4+ from PIL import Image , ImageDraw
45
56class 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" )
0 commit comments