I'm learning Python by converting a BASIC (BlitzPlus) program I wrote based on article at http://gamedeveloper.texterity.com/gamedeveloper/201002?pg=42#pg43 for random dungeon generation.
I've basically used Google and trial and error to get this script to where it is now (which is a working program).
However, I'd like to know if it's "Pythonic" or just a hybrid BASIC to Python translation.
Thanks in advance for any advice.
# Random Dungeon - use TKInter to draw
from Tkinter import *
import random
master=Tk()
random.seed()
def notConnected(x,y):
uncon=False
if (cellArray[x,y]['u']==0) and (cellArray[x,y]['d']==0) and (cellArray[x,y]['l']==0) and (cellArray[x,y]['r']==0):
uncon=True
return uncon
def pickNeighbour(x,y,d):
done=0
tries=0
while done==0:
if d==1:
if y>0:
if notConnected(x,y-1):
cellArray[x,y]['u']=cellArray[x,y-1]['n']
cellArray[x,y-1]['d']=cellArray[x,y]['n']
newx=x
newy=y-1
done=1
else:
d+=1
if d==5:
d=1
tries+=1
if tries==4:
done=2
else:
d+=1
if d==5:
d=1
if d==2:
if x<cellsX-1:
if notConnected(x+1,y):
cellArray[x,y]['r']=cellArray[x+1,y]['n']
cellArray[x+1,y]['l']=cellArray[x,y]['n']
newx=x+1
newy=y
done=1
else:
d+=1
if d==5:
d=1
tries+=1
if tries==4:
done=2
else:
d+=1
if d==5:
d=1
if d==3:
if y<cellsY-1:
if notConnected(x,y+1):
cellArray[x,y]['d']=cellArray[x,y+1]['n']
cellArray[x,y+1]['u']=cellArray[x,y]['n']
newx=x
newy=y+1
done=1
else:
d+=1
if d==5:
d=1
tries+=1
if tries==4:
done=2
else:
d+=1
if d==5:
d=1
if d==4:
if x>0:
if notConnected(x-1,y):
cellArray[x,y]['l']=cellArray[x-1,y]['n']
cellArray[x-1,y]['r']=cellArray[x,y]['n']
newx=x-1
newy=y
done=1
else:
d+=1
if d==5:
d=1
tries+=1
if tries==4:
done=2
else:
d+=1
if d==5:
d=1
if done==1:
return newx,newy
elif done==2:
return -1,-1
def drawCells():
offsetX=10
offsetY=10
for y in range(cellsY):
for x in range(cellsX):
#draw cells grid
x0=(x*50)+offsetX
y0=(y*50)+offsetY
x1=x0+49
y1=y0+49
canvas.create_rectangle(x0,y0,x1,y1)
canvas.create_text(x0+23,y0+22,text=str(cellArray[x,y]['n']).zfill(2))
#draw rooms
if not(notConnected(x,y)):
rx0=x0+10
ry0=y0+10
rx1=rx0+29
ry1=ry0+29
canvas.create_rectangle(rx0,ry0,rx1,ry1,outline='red')
#draw connections
if cellArray[x,y]['r']!=0:
cx0=rx1
cy0=ry0+15
cx1=rx1+21
cy1=cy0
canvas.create_line(cx0,cy0,cx1,cy1,width=5)
if cellArray[x,y]['d']!=0:
cx0=rx0+15
cy0=ry1
cx1=cx0
cy1=ry1+21
canvas.create_line(cx0,cy0,cx1,cy1,width=5)
#main program
cellsX=10
cellsY=10
done=False
cellArray={}
fillPercent=80
gridWidth=(cellsX*50)
gridHeight=(cellsY*50)
count=-1
connected=0
canvas=Canvas(master,width=gridWidth+50,height=gridHeight+100)
canvas.pack()
for y in range(cellsY):
for x in range(cellsX):
count+=1
cellArray[x,y]={'x':x,'y':y,'u':0,'d':0,'l':0,'r':0,'n':count}
#pick random start cell and direction
rx=random.randint(0,cellsX-1)
ry=random.randint(0,cellsY-1)
rd=random.randint(1,4)
startCell='Start cell: '+str(cellArray[rx,ry]['n'])
connected+=1
while not(done):
nx,ny=pickNeighbour(rx,ry,rd)
if nx==-1:
#see what percent filled
pcf=int(float(connected)/float(cellsX*cellsY)*100)
if pcf>=fillPercent:
endCell='End cell: '+str(cellArray[rx,ry]['n'])
done=True
else:
found=False
while not(found):
rx=random.randint(0,cellsX-1)
ry=random.randint(0,cellsY-1)
if not(notConnected(rx,ry)):
found=True
else:
rx=cellArray[nx,ny]['x']
ry=cellArray[nx,ny]['y']
connected+=1
rd=random.randint(1,4)
drawCells()
canvas.create_text(10,gridHeight+30,text=startCell,justify='right',anchor=W)
canvas.create_text(10,gridHeight+60,text=endCell,justify='right',anchor=W)
master.mainloop()