Advertisement
here2share

# maze_gen_demo_revised.py

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