当前位置: 首页 > 后端技术 > Python

BasicGamesPython源码分析02Amazing

时间:2023-03-26 14:30:25 Python

阅读全文:apachecn/python-code-anal本游戏会接收用户输入的长宽,动态生成迷宫。从FrankPalazzolo的版本改进而来。importimportrandomimportosfromtimeimportsleepconstant#定义移动方向常数GO_LEFT,GO_UP,GO_RIGHT,GO_DOWN=[0,1,2,3]#定义连接方向常数CONN_DOWN=1CONN_RIGHT=2get_width_length()#从中获取迷宫的长度用户输入和宽度defget_width_length():whileTrue:try:width,length=input('Whatareyourwidthandlength?').split(',')width=int(width)length=int(length)if宽度!=1和长度!=1:return(width,length)print('Meaninglessdimensions.Tryagain.')exceptValueError:print('Meaninglessdimensions.Tryagain.')get_possible_dirs()#ReturnpossibledirsfromagridDirectiondefget_possible_dirs(row,col,width,length,used):possible_dirs=[GO_LEFT,GO_UP,GO_RIGHT,GO_DOWN]#如果不是最左边,并且没有访问到左边#那么左边是可以访问的,依此类推如果col==0或使用[row][col-1]!=0:possible_dirs.remove(GO_LEFT)如果row==0或使用[row-1][col]!=0:possible_dirs.remove(GO_UP)如果col==width-1或used[row][col+1]!=0:possible_dirs.remove(GO_RIGHT)ifrow==length-1orused[row+1][col]!=0:possible_dirs.remove(GO_DOWN)returnpossible_dirsget_next_used()#获取某个位置的下一个访问的griddefget_next_used(row,col,used):length,width=len(used),len(used[0])whileTrue:如果col!=width-1:col+=1elifrow!=length-1:row,col=row+1,0else:row,col=0,0ifused[row][col]!=0:breakreturnrow,colprint_maze()#printmazedefprint_maze(walls,used,enter_col):#每次清除打印前的屏幕os.system('cls')length,width=len(walls),len(walls[0])#Printthetopwallforcolinrange(width):#如果是入口,就放打开ifcol==enter_col:print('*',end='')else:print('*--',end='')print('*')forrowinrange(length):#打印网格print('|',end='')forcolinrange(width):#区分已访问和未访问的cell_ch=''ifused[row][col]!=0else'><'#区分网格右侧是否有墙ifwalls[row][col]|CONN_RIGHT==walls[row][col]:print(cell_ch+'',end='')else:print(cell_ch+'|',end='')print()#打印列网格下方的墙inrange(width):#区分网格下方是否有墙ifwalls[row][col]|CONN_DOWN==walls[row][col]:print('*',end='')else:print('*--',end='')print('*')#短暂停留以避免闪光Sleep(0.1)main()#程序主要逻辑defmain():print(''*28+'AMAZINGPROGRAM')print(''*15+'CREATIVECOMPUTINGMORRISTOWN,NEWJERSEY')print()print()print()#获取用户输入的长宽width,length=get_width_length()#used数组保存网格是否被访问过#0表示未访问过,其他数字表示访问过哪个网格used=[[0]*widthfor_inrange(length)]#wall保存右边和下面是否相连#0:不相连,1:下面相连,2:右边相连,3:连接在右侧和下侧壁=[[0]*widthfor_inrange(length)]#随机选择条目enter_col=random.randint(0,width-1)#定义起始位置为entryrow,col=0,enter_col#定义访问的格子数count=1#设置访问的entryused[row][col]=count#格式化每个time任何变化都会打印迷宫,下同print_maze(walls,used,enter_col)#在所有格子都被访问完之前执行循环whilecount!=width*length:#获取位置的移动方向possible_dirs=get_possible_dirs(row,col,width,length,used)iflen(possible_dirs)!=0:#如果可以移动,则随机选择一个方向移动#并移除墙direction=random.choice(possible_dirs)ifdirection==GO_LEFT:col=col-1walls[row][col]|=CONN_RIGHTelifdirection==GO_UP:row=row-1walls[row][col]|=CONN_DOWNelifdirection==GO_RIGHT:walls[row][col]]|=CONN_RIGHTcol=col+1elifdirection==GO_DOWN:walls[row][col]|=CONN_DOWNrow=row+1#updatecount,setvisitedcount=count+1used[row][col]=countprint_maze(walls,used,enter_col)else:#否则选择下一个访问过的格子,重复这一步#因为外面已经检查过是否可以移动了,#这里只检查访问过的行,col=get_next_used(row,col,used)#最后随机选择出口col=random.randint(0,width-1)row=length-1walls[row][col]=walls[row][col]+1print_maze(walls,used,enter_col)if__name__=='__main__':main()