Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # maze_gen_demo_revised.py
- # -*- coding: utf-8 -*-
- # (c) iniwap
- # wtx2zhm@126.com
- # License: GPL3
- import appuifw,graphics,e32,random,key_codes,time
- running=1
- pos_x=0
- pos_y=0
- step=0
- go=None
- Maze=[[[(i,j),[0,0,0,0],0]for i in range(20)]for j in range(20)]
- def mR(event):
- global pos_x,go
- if Maze[pos_y][pos_x][1][0]==1:
- pos_x+=1
- go=0
- def mD(event):
- global pos_y,go
- if Maze[pos_y][pos_x][1][1]==1:
- pos_y+=1
- go=1
- def mL(event):
- global pos_x,go
- if Maze[pos_y][pos_x][1][2]==1:
- pos_x-=1
- go=2
- def mU(event):
- global pos_y,go
- if Maze[pos_y][pos_x][1][3]==1:
- pos_y-=1
- go=3
- def get_neighbors(cell):
- neighbors=[]
- if cell[0][0]+1<len(Maze):
- if Maze[cell[0][1]][cell[0][0]+1][2]==0:
- neighbors.append(Maze[cell[0][1]][cell[0][0]+1])
- if cell[0][1]+1<len(Maze):
- if Maze[cell[0][1]+1][cell[0][0]][2]==0:
- neighbors.append(Maze[cell[0][1]+1][cell[0][0]])
- if cell[0][0]-1>=0:
- if Maze[cell[0][1]][cell[0][0]-1][2]==0:
- neighbors.append(Maze[cell[0][1]][cell[0][0]-1])
- if cell[0][1]-1>=0:
- if Maze[cell[0][1]-1][cell[0][0]][2]==0:
- neighbors.append(Maze[cell[0][1]-1][cell[0][0]])
- return neighbors
- def generate_Maze():
- depth=0
- MazeStack=[]
- current_cell=Maze[0][0]
- mark_visited(current_cell)
- MazeStack.append(current_cell)
- while len(MazeStack)!=0:
- depth+=1
- neighbor=get_neighbors(current_cell)
- if len(neighbor)!=0 and depth%16!=0:
- temp=random.randint(0,len(neighbor)-1)
- knock_wall(current_cell,neighbor[temp])
- current_cell=neighbor[temp]
- mark_visited(current_cell)
- MazeStack.append(current_cell)
- elif len(neighbor)!=0 and depth%16==0:
- temp=random.randint(0,len(MazeStack)-1)
- current_cell=MazeStack[temp]
- del(MazeStack[temp])
- else:
- current_cell=MazeStack.pop()
- def knock_wall(cell,next_cell):
- x=cell[0][0]
- y=cell[0][1]
- next_x=next_cell[0][0]
- next_y=next_cell[0][1]
- if x+1==next_x:
- Maze[y][x][1][0]=1
- Maze[next_y][next_x][1][2]=1
- if y+1==next_y:
- Maze[y][x][1][1]=1
- Maze[next_y][next_x][1][3]=1
- if x-1==next_x:
- Maze[y][x][1][2]=1
- Maze[next_y][next_x][1][0]=1
- if y-1==next_y:
- Maze[y][x][1][3]=1
- Maze[next_y][next_x][1][1]=1
- def mark_visited(cell):
- x=cell[0][0]
- y=cell[0][1]
- Maze[y][x][2]=1
- def quit():
- global running
- running=0
- def chn(x):
- return x.decode("utf8")
- def draw(rect):
- try:
- canvas.blit(img)
- except: pass
- appuifw.app.screen = 'large'
- appuifw.app.orientation = 'portrait'
- appuifw.app.directional_pad = False
- appuifw.app.body=canvas=appuifw.Canvas(redraw_callback=draw)
- w,h=canvas.size
- canvas.bind(key_codes.EButton1Down,mL,((70,420),(140,495))) # left
- canvas.bind(key_codes.EButton1Down,mR,((220,420),(290,495))) # right
- canvas.bind(key_codes.EButton1Down,mU,((140,350),(220,420))) # up
- canvas.bind(key_codes.EButton1Down,mD,((140,495),(220,565))) # down
- img=graphics.Image.new((w,h))
- appuifw.app.exit_key_handler=quit
- timer = e32.Ao_timer()
- def jpad():
- img.rectangle((70,420,290,495),fill=(220,220,220)) # w
- img.rectangle((140,350,220,565),fill=(220,220,220)) # h
- draw(())
- def play():
- global pos_x,pos_y,step,go
- generate_Maze()
- prev_xy=None
- a,b=16,32
- while running:
- if prev_xy <> (pos_x,pos_y):
- img.clear(0xffffff)
- jpad()
- img.rectangle((a-1,a-1,a*21+1,a*21+1),0x000000,width=2)
- for x in range(20):
- for y in range(20):
- if Maze[y][x][1][0] <> 1:
- img.line((x*a+b,y*a+a,x*a+b,y*a+b),0x000000,width=2)
- if Maze[y][x][1][1] <> 1:
- img.line((x*a+a,y*a+b,x*a+b,y*a+b),0x000000,width=2)
- img.rectangle((a*20+3,a*20+3,a*20+a-2,a*20+a-2),fill=0xff0000)
- img.text((20,360),chn('Steps:'),0x0000ff)
- img.text((75,360),chn(str(step)),0xff0000)
- img.point((pos_x*a+24,pos_y*a+24),0x00ff00,width=12)
- if go == 0:
- img.rectangle((220,420,290,495),fill=0xffff00) # R
- elif go == 1:
- img.rectangle((140,495,220,565),fill=0xffff00) # D
- elif go == 2:
- img.rectangle((70,420,140,495),fill=0xffff00) # L
- elif go == 3:
- img.rectangle((140,350,220,420),fill=0xffff00) # U
- prev_xy=pos_x,pos_y
- try: timer.cancel()
- except: pass
- timer.after(1,jpad)
- step+=1
- draw(())
- e32.ao_yield()
- play()
- timer.cancel()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement