Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_hexagonal_maze.py
- import Tkinter
- import random
- ww = 600
- hh = 600
- tk = Tkinter.Tk()
- tk.title("hex maze")
- tk.geometry("%dx%d"%(ww,hh))
- class Mogura:
- x=0
- y=0
- live=1
- age=0
- def drop(self):
- global w,h,mp
- self.x = random.randint(0,int((w-1)/2))*2
- self.y = random.randint(0,int((h-1)/2))*2
- if self.y%4==2:self.x+=1
- if mp[self.y][self.x]==1:self.live=0
- def dig(self):
- global mp,dx1,dy1,dx2,dy2,w,h
- di=random.randint(0,5)
- tx=self.x+dx2[di]
- ty=self.y+dy2[di]
- if tx<0 or ty<0 or tx>w-1 or ty>h-1:
- self.live=0
- return
- if mp[ty][tx]==0:
- self.live=0
- return
- mp[ty][tx]=0
- mp[self.y+dy1[di]][self.x+dx1[di]]=0
- self.x=tx
- self.y=ty
- self.age+=1
- size=10 #hex size
- w=ww/size-3 #width
- h=hh/size-1 #height
- hsize=int(size/2)
- #direction data
- dx1=[-1,-1, 0, 1, 0,-1]
- dy1=[ 0,-1,-1, 0, 1, 1]
- dx2=[-2,-1, 1, 2, 1,-1]
- dy2=[ 0,-2,-2, 0, 2, 2]
- while 1:
- #map init
- mp=[[1 for x in range(w+1)] for y in range(h)]
- mp[0][0]=0
- toukei=[0 for x in range(11)]
- mo=[Mogura() for i in range(50000)]
- #main
- while len(mo)>0:
- mo[0].drop()
- for i in range(10):
- if mo[0].live==0:break
- mo[0].dig()
- toukei[mo[0].age]+=1
- del mo[0]
- print(toukei)
- #map write
- cv = Tkinter.Canvas(tk, width = ww, height = hh)
- cv.create_rectangle(0, 0, ww, hh, fill = 'gray')
- for y in range(h):
- if y==h-1:continue
- for x in range(w):
- sx=x*size+size+(y%2)*hsize
- sy=y*size+size
- if mp[y][x]==1:
- cv.create_rectangle(sx, sy, sx+size, sy+size, fill = 'red')
- else:
- cv.create_rectangle(sx, sy, sx+size, sy+size, fill = 'black')
- cv.place(x=0, y=0)
- cv.update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement