I have created a simple and fast random level generator for the game engine written in python
i hope some will find it useful :eyebrowlift:
blend file supplied.
Attachments
levelgen-v1.blend (6.78 MB)
I have created a simple and fast random level generator for the game engine written in python
i hope some will find it useful :eyebrowlift:
blend file supplied.
levelgen-v1.blend (6.78 MB)
i have made some code cleanup and bug fixes to the script
and i have also added a few things in the demo level, by pressing N while you run the demo a new level is generated on the fly.
bugfix:
should now work on windows to
i have made the script more generic to make it easy to import into other scripts
levelgen.py
import random
import time
class Cell:
id=0
def Build(scale=20):
cell = [Cell() for i in range(100)]
x=0
y=0
for i in range(0,100):
cell[i].x = x
cell[i].y = y
cell[i].set=0
cell[i].wall = [1,1,1,1]
x = x +1
if x>9:
x=0
y=y+1
if y>9:
y=0
cc=55
cell[cc].set=1
build=1
count=0
random.seed(time.time())
while build==1:
d=random.randrange(0,4)
if d==0:
if cc not in [90,91,92,93,94,95,96,97,98,99]:
cd = cc +10
cell[cc].wall[3]=0
cell[cd].wall[2]=0
cell[cc].set=1
cell[cd].set=1
cc=cc+10
count=count+1
if d==1:
if cc not in [0,1,2,3,4,5,6,7,8,9]:
cd = cc -10
cell[cc].wall[2]=0
cell[cd].wall[3]=0
cell[cc].set=1
cell[cd].set=1
cc=cc-10
count=count+1
if d==2:
if cc not in [9,19,29,39,49,59,69,79,89,99]:
cell[cc].wall[1]=0
cd = cc +1
cell[cd].wall[0]=0
cell[cc].set=1
cell[cd].set=1
cc=cc+1
count=count+1
if d==3:
if cc not in [0,10,20,30,40,50,60,70,80,90]:
cell[cc].wall[0]=0
cd = cc -1
cell[cd].wall[1]=0
cell[cc].set=1
cell[cd].set=1
cc=cc-1
count=count+1
if count>scale:
build=0
return cell
and here is an example how it can be used
level.py
import bge
import string
import random
import levelgen
scene = bge.logic.getCurrentScene()
#build level data
cell = levelgen.Build(100)
#loop thru list
for i in range(0,100):
# check if the cell is set ( if its set it got a room/area or whatever you want it to be)
if cell[i].set==1:
#place a empty based on cell position and scale it to desired room size
scene.objects["newplace"].position.x = cell[i].x *80
scene.objects["newplace"].position.y = cell[i].y *80
# put in the floor where the empty where placed
obj = scene.addObject("floor3Plane",scene.objects["newplace"],0)
# put in the walls
if cell[i].wall[0]==1:
obj = scene.addObject("wall3s",scene.objects["newplace"],0)
if cell[i].wall[1]==1:
obj = scene.addObject("wall3n",scene.objects["newplace"],0)
if cell[i].wall[2]==1:
obj = scene.addObject("wall3e",scene.objects["newplace"],0)
if cell[i].wall[3]==1:
obj = scene.addObject("wall3w",scene.objects["newplace"],0)
here is a Demo blend file
Impressive! Most impressive indeed!
Holy crap, this is really mind blowing. amazing work, dude! Mind if I use it in a game someday? XD
you are most welcome to do so :yes:
i have completely rewritten the levelgen code to remove the 10x10 grid limitation
import random
import time
class Cell:
x=0
y=0
z=0
walln=1
walls=1
walle=1
wallw=1
tile=""
minimapseen=0
def Build(steps=20):
cell = [Cell()]
if steps < 2:
steps=2
count=0
x=10
y=10
cell[0].x=x
cell[0].y=y
build=1
random.seed(time.time())
while(build==1):
direction=random.randrange(0,4)
if direction==0: # walk n
set=0
for i in range(0,len(cell)):
if cell[i].x==x and cell[i].y==y:
cell[i].walln=0
x=x+1
for i in range(0,len(cell)):
if (cell[i].x==x) and (cell[i].y==y):
cell[i].walls=0
set=1
if set==0:
cell.append(Cell())
cell[-1].x=x
cell[-1].y=y
cell[-1].walls=0
if direction==1 and x>0: # walk s
set=0
for i in range(0,len(cell)):
if cell[i].x==x and cell[i].y==y:
cell[i].walls=0
x=x-1
for i in range(0,len(cell)):
if cell[i].x==x and cell[i].y==y:
cell[i].walln=0
set=1
if set==0:
cell.append(Cell())
cell[-1].x=x
cell[-1].y=y
cell[-1].walln=0
if direction==2 and y>1: # walk e
set=0
for i in range(0,len(cell)):
if cell[i].x==x and cell[i].y==y:
cell[i].walle=0
y=y-1
for i in range(0,len(cell)):
if cell[i].x==x and cell[i].y==y:
cell[i].wallw=0
set=1
if set==0:
cell.append(Cell())
cell[-1].x=x
cell[-1].y=y
cell[-1].wallw=0
if direction==3: # walk w
set=0
for i in range(0,len(cell)):
if cell[i].x==x and cell[i].y==y:
cell[i].wallw=0
y=y+1
for i in range(0,len(cell)):
if cell[i].x==x and cell[i].y==y:
cell[i].walle=0
set=1
if set==0:
cell.append(Cell())
cell[-1].x=x
cell[-1].y=y
cell[-1].walle=0
if len(cell)>=steps:
build=0
return cell
here are some demos that uses the script
filesize: 122mb
http://15b.dk/greatoutdoors.blend
filesize: 21mb
http://15b.dk/dungeon-test.blend
enjoy
a little thing i made to test a solution to a little problem i had on how to store and connect random levels together.
filesize: 49mb
http://15b.dk/random-multilevel-test.blend