2D Maze Generator with a Save function by puzzler201824th Jul 2018 7:13
|
---|
Summary A 2D Maze Generation with a Saved output - will then be able to load in faster Description Code ` This code was downloaded from The Game Creators ` It is reproduced here with full permission ` http://www.thegamecreators.com [code] // Project: maze gen v3 // Created: 2018-07-06 // show all errors SetErrorMode(2) #constant swidth = 800 #constant sheight = 600 #constant white = MakeColor(255,255,255) #constant green = MakeColor(0,255,0) #constant blue = MakeColor(0,0,255) #constant black = MakeColor(0,0,0) // set window properties SetWindowTitle( "maze gen v3" ) SetWindowSize( swidth, sheight, 0 ) SetWindowAllowResize( 1 ) // allow the user to resize the window // set display properties SetVirtualResolution( swidth, sheight ) // doesn't have to match the window SetOrientationAllowed( 1, 1, 1, 1 ) // allow both portrait and landscape on mobile devices SetSyncRate( 30, 0 ) // 30fps instead of 60 to save battery SetScissor( 0,0,0,0 ) // use the maximum available screen space, no black borders UseNewDefaultFonts( 1 ) // since version 2.0.22 we can use nicer default fonts global cols,rows // Change this value - lower the larger the map - the higher to smaller the maze width = 15 // Change this value - lower the larger the map - the higher to smaller the maze type _cells x,y walls as integer[3] visited current direction endtype global cells as _cells[] global emptycell as _cells global stack as _cells[] global stackcount, completed=0 cols = floor(swidth / width)-5 rows = floor (sheight /width)-5 global current=0 initialisegrid() //currentcell.insert (cells[current]) saved=0 do for x=0 to cells.length xx = cells[x].x * width yy = cells[x].y * width if completed=0 and cells[x].visited = 1 then DrawBox(xx ,yy ,xx+width, yy+width,green,green,green,green,1) if cells[x].walls[0]=1 then DrawLine(xx ,yy ,xx+width, yy ,white,white) if cells[x].walls[1]=1 then DrawLine(xx+width,yy ,xx+width, yy+width,white,white) if cells[x].walls[2]=1 then DrawLine(xx+width,yy+width,xx , yy+width,white,white) if cells[x].walls[3]=1 then DrawLine(xx ,yy+width,xx , yy ,white,white) next DrawBox(cells[current].x * width+2 ,cells[current].y * width+2 ,cells[current].x * width+width-2 ,cells[current].y * width+width - 2,blue,blue,blue,blue,1) checkneighbours(cells[current]) cells[current].visited=1 if completed=1 // lets save the current maze out if saved=0 saved=1 savefile = OpenToWrite("level1.dat") WriteInteger(savefile,cells.length) WriteInteger(savefile,width) for loops = 0 to cells.length WriteInteger(savefile,cells[loops].x) WriteInteger(savefile,cells[loops].y) WriteInteger(savefile,cells[loops].walls[0]) WriteInteger(savefile,cells[loops].walls[1]) WriteInteger(savefile,cells[loops].walls[2]) WriteInteger(savefile,cells[loops].walls[3]) next CloseFile(savefile) print("Saved") sync() sleep(1000) endif endif Sync() sleep(0) loop function initialisegrid() i=0 for x=0 to cols for y=0 to rows emptycell.current = i emptycell.x = x emptycell.y = y emptycell.walls[0] = 1 //top emptycell.walls[1] = 1 //right emptycell.walls[2] = 1 //bottom emptycell.walls[3] = 1 // left emptycell.visited = 0 cells.insert(emptycell) inc i next next endfunction function checkneighbours(cell ref as _cells) neighbours as _cells[] top as _cells cright as _cells cleft as _cells cbottom as _cells r as _cells //top if index(cell.x, cell.y-1) <> -1 and cells[index(cell.x, cell.y-1)].visited = 0 top = cells[index(cell.x, cell.y - 1)] top.current = index(cell.x, cell.y - 1) top.visited=1 top.direction=0 neighbours.insert(top) endif //right if index(cell.x+1,cell.y) <> -1 and cells[index(cell.x+1, cell.y)].visited = 0 cright = cells[index(cell.x+1, cell.y)] cright.current = cells[index(cell.x+1, cell.y)].current cright.visited=1 cright.direction=1 neighbours.insert(cright) endif // bottom if index(cell.x, cell.y+1) <> -1 and cells[index(cell.x, cell.y+1)].visited = 0 cbottom = cells[index(cell.x, cell.y + 1)] cbottom.current = index(cell.x, cell.y + 1) cbottom.visited=1 cbottom.direction = 2 neighbours.insert(cbottom) endif //left if index(cell.x-1,cell.y) <> -1 and cells[index(cell.x-1, cell.y)].visited = 0 cleft = cells[index(cell.x-1, cell.y)] cleft.current = index(cell.x-1, cell.y) cleft.visited=1 cleft.direction = 3 neighbours.insert(cleft) endif oldc=current if neighbours.length>=0 r = neighbours[floor(random(0,neighbours.length))] // remove necessary walls that your heading towards select (r.direction) case 0: //up /top cells[index(cell.x,cell.y)].walls[0]=0 cells[index(cell.x,cell.y-1)].walls[2]=0 endcase case 1: // right cells[index(cell.x,cell.y)].walls[1]=0 cells[index(cell.x+1,cell.y)].walls[3]=0 endcase case 2: // bottom / down cells[index(cell.x,cell.y)].walls[2]=0 cells[index(cell.x,cell.y+1)].walls[0]=0 endcase case 3: // left cells[index(cell.x,cell.y)].walls[3]=0 cells[index(cell.x-1,cell.y)].walls[1]=0 endcase endselect current = r.current stack.insert (r) inc stackcount else if stackcount-1<0 print ("COMPLETED") completed = 1 else cells[index(r.x,r.y)] = stack[stackcount-1] current = index(r.x,r.y) stack.remove(stackcount-1) dec stackcount endif endif endfunction r function index ( i, j ) if (i <0 or j <0 or i > cols or j > rows ) else returnvalue = (i * (rows+1)) + j endif endfunction returnvalue [/code] |