Advertisement
here2share

# Tk_hexagonal_maze.py

Mar 18th, 2021
1,207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.79 KB | None | 0 0
  1. # Tk_hexagonal_maze.py
  2.  
  3. import Tkinter
  4. import random
  5.  
  6. ww = 600
  7. hh = 600
  8.  
  9. tk = Tkinter.Tk()
  10. tk.title("hex maze")
  11. tk.geometry("%dx%d"%(ww,hh))
  12.  
  13. class Mogura:
  14.     x=0
  15.     y=0
  16.     live=1
  17.     age=0
  18.     def drop(self):
  19.         global w,h,mp
  20.         self.x = random.randint(0,int((w-1)/2))*2
  21.         self.y = random.randint(0,int((h-1)/2))*2
  22.         if self.y%4==2:self.x+=1
  23.         if mp[self.y][self.x]==1:self.live=0
  24.     def dig(self):
  25.         global mp,dx1,dy1,dx2,dy2,w,h
  26.         di=random.randint(0,5)
  27.         tx=self.x+dx2[di]
  28.         ty=self.y+dy2[di]
  29.         if tx<0 or ty<0 or tx>w-1 or ty>h-1:
  30.             self.live=0
  31.             return
  32.         if mp[ty][tx]==0:
  33.             self.live=0
  34.             return
  35.         mp[ty][tx]=0
  36.         mp[self.y+dy1[di]][self.x+dx1[di]]=0
  37.         self.x=tx
  38.         self.y=ty
  39.         self.age+=1
  40.  
  41. size=10  #hex size
  42. w=ww/size-3  #width
  43. h=hh/size-1  #height
  44. hsize=int(size/2)
  45.  
  46. #direction data
  47. dx1=[-1,-1, 0, 1, 0,-1]
  48. dy1=[ 0,-1,-1, 0, 1, 1]
  49. dx2=[-2,-1, 1, 2, 1,-1]
  50. dy2=[ 0,-2,-2, 0, 2, 2]
  51.  
  52. while 1:
  53.     #map init
  54.     mp=[[1 for x in range(w+1)] for y in range(h)]
  55.     mp[0][0]=0
  56.  
  57.     toukei=[0 for x in range(11)]
  58.  
  59.     mo=[Mogura() for i in range(50000)]
  60.  
  61.     #main
  62.     while len(mo)>0:
  63.         mo[0].drop()
  64.         for i in range(10):
  65.             if mo[0].live==0:break
  66.             mo[0].dig()
  67.         toukei[mo[0].age]+=1
  68.         del mo[0]
  69.  
  70.     print(toukei)
  71.  
  72.     #map write
  73.     cv = Tkinter.Canvas(tk, width = ww, height = hh)
  74.     cv.create_rectangle(0, 0, ww, hh, fill = 'gray')
  75.  
  76.     for y in range(h):
  77.         if y==h-1:continue
  78.         for x in range(w):
  79.             sx=x*size+size+(y%2)*hsize
  80.             sy=y*size+size
  81.             if mp[y][x]==1:
  82.                 cv.create_rectangle(sx, sy, sx+size, sy+size, fill = 'red')
  83.             else:
  84.                 cv.create_rectangle(sx, sy, sx+size, sy+size, fill = 'black')
  85.  
  86.     cv.place(x=0, y=0)
  87.     cv.update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement