Advertisement
here2share

# maze_gen_demo_revised.py

Jan 6th, 2015
435
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # maze_gen_demo_revised.py
  2.  
  3. # -*- coding: utf-8 -*-
  4. # (c) iniwap
  5. # wtx2zhm@126.com
  6. # License: GPL3
  7. import appuifw,graphics,e32,random,key_codes,time
  8. running=1
  9. pos_x=0
  10. pos_y=0
  11. step=0
  12. go=None
  13. Maze=[[[(i,j),[0,0,0,0],0]for i in range(20)]for j in range(20)]
  14.  
  15. def mR(event):
  16.     global pos_x,go
  17.     if Maze[pos_y][pos_x][1][0]==1:
  18.         pos_x+=1
  19.         go=0
  20. def mD(event):
  21.     global pos_y,go
  22.     if Maze[pos_y][pos_x][1][1]==1:
  23.         pos_y+=1
  24.         go=1
  25. def mL(event):
  26.     global pos_x,go
  27.     if Maze[pos_y][pos_x][1][2]==1:
  28.         pos_x-=1
  29.         go=2
  30. def mU(event):
  31.     global pos_y,go
  32.     if Maze[pos_y][pos_x][1][3]==1:
  33.         pos_y-=1
  34.         go=3
  35.  
  36. def get_neighbors(cell):
  37.     neighbors=[]
  38.     if cell[0][0]+1<len(Maze):
  39.         if Maze[cell[0][1]][cell[0][0]+1][2]==0:
  40.             neighbors.append(Maze[cell[0][1]][cell[0][0]+1])
  41.     if cell[0][1]+1<len(Maze):
  42.         if Maze[cell[0][1]+1][cell[0][0]][2]==0:
  43.             neighbors.append(Maze[cell[0][1]+1][cell[0][0]])
  44.     if cell[0][0]-1>=0:
  45.         if Maze[cell[0][1]][cell[0][0]-1][2]==0:
  46.             neighbors.append(Maze[cell[0][1]][cell[0][0]-1])
  47.     if cell[0][1]-1>=0:
  48.         if Maze[cell[0][1]-1][cell[0][0]][2]==0:
  49.             neighbors.append(Maze[cell[0][1]-1][cell[0][0]])
  50.     return neighbors
  51. def generate_Maze():
  52.     depth=0
  53.     MazeStack=[]
  54.     current_cell=Maze[0][0]
  55.     mark_visited(current_cell)
  56.     MazeStack.append(current_cell)
  57.     while len(MazeStack)!=0:
  58.         depth+=1
  59.         neighbor=get_neighbors(current_cell)
  60.         if len(neighbor)!=0 and depth%16!=0:
  61.             temp=random.randint(0,len(neighbor)-1)
  62.             knock_wall(current_cell,neighbor[temp])
  63.             current_cell=neighbor[temp]
  64.             mark_visited(current_cell)
  65.             MazeStack.append(current_cell)
  66.         elif len(neighbor)!=0 and depth%16==0:
  67.             temp=random.randint(0,len(MazeStack)-1)
  68.             current_cell=MazeStack[temp]
  69.             del(MazeStack[temp])
  70.         else:
  71.             current_cell=MazeStack.pop()
  72. def knock_wall(cell,next_cell):
  73.     x=cell[0][0]
  74.     y=cell[0][1]
  75.     next_x=next_cell[0][0]
  76.     next_y=next_cell[0][1]
  77.     if x+1==next_x:
  78.         Maze[y][x][1][0]=1
  79.         Maze[next_y][next_x][1][2]=1
  80.     if y+1==next_y:
  81.         Maze[y][x][1][1]=1
  82.         Maze[next_y][next_x][1][3]=1
  83.     if x-1==next_x:
  84.         Maze[y][x][1][2]=1
  85.         Maze[next_y][next_x][1][0]=1
  86.     if y-1==next_y:
  87.         Maze[y][x][1][3]=1
  88.         Maze[next_y][next_x][1][1]=1
  89. def mark_visited(cell):
  90.     x=cell[0][0]
  91.     y=cell[0][1]
  92.     Maze[y][x][2]=1
  93. def quit():
  94.     global running
  95.     running=0
  96. def chn(x):
  97.     return x.decode("utf8")
  98. def draw(rect):
  99.     try:
  100.         canvas.blit(img)
  101.     except: pass
  102.  
  103. appuifw.app.screen = 'large'
  104. appuifw.app.orientation = 'portrait'
  105. appuifw.app.directional_pad = False
  106. appuifw.app.body=canvas=appuifw.Canvas(redraw_callback=draw)
  107. w,h=canvas.size
  108.  
  109. canvas.bind(key_codes.EButton1Down,mL,((70,420),(140,495)))     # left
  110. canvas.bind(key_codes.EButton1Down,mR,((220,420),(290,495)))     # right
  111. canvas.bind(key_codes.EButton1Down,mU,((140,350),(220,420)))     # up
  112. canvas.bind(key_codes.EButton1Down,mD,((140,495),(220,565)))     # down
  113.  
  114. img=graphics.Image.new((w,h))
  115. appuifw.app.exit_key_handler=quit
  116. timer = e32.Ao_timer()
  117.  
  118. def jpad():
  119.     img.rectangle((70,420,290,495),fill=(220,220,220)) # w
  120.     img.rectangle((140,350,220,565),fill=(220,220,220)) # h
  121.     draw(())
  122.  
  123. def play():
  124.   global pos_x,pos_y,step,go
  125.   generate_Maze()
  126.   prev_xy=None
  127.   a,b=16,32
  128.   while running:
  129.     if prev_xy <> (pos_x,pos_y):
  130.         img.clear(0xffffff)
  131.         jpad()
  132.         img.rectangle((a-1,a-1,a*21+1,a*21+1),0x000000,width=2)
  133.         for x in range(20):
  134.             for y in range(20):
  135.                 if Maze[y][x][1][0] <> 1:
  136.                     img.line((x*a+b,y*a+a,x*a+b,y*a+b),0x000000,width=2)
  137.                 if Maze[y][x][1][1] <> 1:
  138.                     img.line((x*a+a,y*a+b,x*a+b,y*a+b),0x000000,width=2)
  139.         img.rectangle((a*20+3,a*20+3,a*20+a-2,a*20+a-2),fill=0xff0000)
  140.         img.text((20,360),chn('Steps:'),0x0000ff)
  141.         img.text((75,360),chn(str(step)),0xff0000)
  142.         img.point((pos_x*a+24,pos_y*a+24),0x00ff00,width=12)
  143.         if   go == 0:
  144.             img.rectangle((220,420,290,495),fill=0xffff00) # R
  145.         elif go == 1:
  146.             img.rectangle((140,495,220,565),fill=0xffff00) # D
  147.         elif go == 2:
  148.             img.rectangle((70,420,140,495),fill=0xffff00) # L
  149.         elif go == 3:
  150.             img.rectangle((140,350,220,420),fill=0xffff00) # U
  151.         prev_xy=pos_x,pos_y
  152.         try: timer.cancel()
  153.         except: pass
  154.         timer.after(1,jpad)
  155.         step+=1
  156.         draw(())
  157.     e32.ao_yield()
  158. play()
  159.  
  160. timer.cancel()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement