iniwap

iniwap的博客

他的个人主页  他的博客

(pys60)手机端自动迷宫地图的生成算

iniwap  2009年08月11日 星期二 14:39 | 1571次浏览 | 0条评论

也是前几个月写的了,效果还不错,而且我还增加了岔路控制,可能地图大的效果会更好、

 

手机屏幕不大,所以无法生成更大的地图。

------------------------------------

# -*- coding: utf-8 -*-
# (c) iniwap
# wtx2zhm@126.com
# License: GPL3
import appuifw,graphics,e32,random,copy
running=1
pos_x=0
pos_y=0
step=0
Maze=[[[(i,j),[0,0,0,0],0]for i in range(21)]for j in range(21)]
def move(x,y):
    global pos_x,pos_y,Maze,step
    if x==1:
        if Maze[pos_y][pos_x][1][0]==1:
            step+=1
            pos_x+=1
    elif x==-1:
        if Maze[pos_y][pos_x][1][2]==1:
            step+=1
            pos_x+=-1
    elif y==1:
        if Maze[pos_y][pos_x][1][1]==1:
            step+=1
            pos_y+=1
    else:
        if Maze[pos_y][pos_x][1][3]==1:
            step+=1
            pos_y+=-1
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")
canvas=appuifw.Canvas()
appuifw.app.body=canvas
appuifw.app.screen="full"
w,h=canvas.size
canvas.bind(52,lambda:move(-1,0))
canvas.bind(54,lambda:move(1,0))
canvas.bind(56,lambda:move(0,1))
canvas.bind(50,lambda:move(0,-1))
img=graphics.Image.new((w,h))
appuifw.app.exit_key_handler=quit
def play():
  global pos_x,pos_y,step
  generate_Maze()
  while running:
    img.clear(0xaaaaaa)
    img.point((pos_x*8+8,pos_y*8+8),0x00ff00,width=6)
    for x in range(21):
        for y in range(21):
            img.rectangle((x*8+5,5+y*8,x*8+13,13+y*8),0x0000ff)
            if Maze[y][x][1][0]==1:
                img.line((x*8+12,6+y*8,x*8+12,y*8+10),0xaaaaaa,width=2)
            if Maze[y][x][1][1]==1:
                img.line((x*8+6,12+y*8,x*8+10,y*8+12),0xaaaaaa,width=2)
            if Maze[y][x][1][2]==1:
                img.line((x*8+4,6+y*8,x*8+4,y*8+10),0xaaaaaa,width=2)
            if Maze[y][x][1][3]==1:
                img.line((x*8+6,4+y*8,x*8+10,y*8+4),0xaaaaaa,width=2)
    img.rectangle((4,4,174,174),0x0000ff)
    img.line((4,4,4,11),0xaaaaaa,width=2)
    img.line((165,172,174,172),0xaaaaaa,width=2)
    img.text((40,190),chn('Steps:'),0x0000ff)
    img.text((100,190),chn(str(step)),0xff0000)
    canvas.blit(img)
    e32.ao_yield()
play()

 

评论

我的评论:

发表评论

请 登录 后发表评论。还没有在Zeuux哲思注册吗?现在 注册 !

暂时没有评论

Zeuux © 2024

京ICP备05028076号