importrandomimportosimportmsvcrtimporteasyguifromcoloramaimportinit,Foreinit(autoreset=False)classFangkuai:def__init__(self,x=0,y=0,neirong=0,zhuangtai=0):self.x=xself.y=yself.neirong=neirong#0,9,1,2,3,4,5,6,7,8分别为空,雷,数字1-8self.zhuangtai=zhuangtai#0,1,2,3,4分别为未点击,插旗,问号,被点击,错误defgetFangkuai(self):return[self.x,self.y,self.neirong,self.zhuangtai]classMymap:def__init__(self,xMax=10,yMax=10):self.xMax=xMaxself.yMax=yMaxself.fk=[]self.new_fk=Fangkuai()self.lei_xy=[]deffound_map(self):forxinrange(self.xMax):self.fk.append([])foryinrange(self.yMax):new_fk=Fangkuai(x,y)self.fk[x].append([new_fk.getFangkuai()[2],new_fk.getFangkuai()[3]])defbulei(self):foriinrange(lei_sum):whileTrue:self.x=random.randint(0,map_xy_max[0]-1)self.y=random.randint(0,map_xy_max[1]-1)if[self.x,self.y]notinself.lei_xy:self.fk[self.x][self.y][0]=9#这一位的9代表雷self.lei_xy.append([self.x,self.y])breakdefbushu(self):forjinself.lei_xy:#print('j=',j)#为雷周围布数字forlei_xin[j[0]-1,j[0],j[0]+1]:forlei_yin[j[1]-1,j[1],j[1]+1]:#超出地图不处理if0=lei_xmap_xy_max[0]and0=lei_ymap_xy_max[1]:#如果是雷不处理ifself.fk[lei_x][lei_y][0]==9:#print(lei_x,lei_y,'我是雷',end='')passelse:#print(lei_x,lei_y,'我加一',end='')self.fk[lei_x][lei_y][0]+=1else:#print(lei_x,lei_y,'我不在',end='')pass#print()defgetfk(self):returnself.fkclassState:def__init__(self,fk,x,y):self.fk=fkself.x=xself.y=y#点击defclick(self):ifself.fk[self.x][self.y][1]==0:self.fk[self.x][self.y][1]=3State.click_judge(self,self.x,self.y)over=State.game_over(self)returnover#判定点击效果defclick_judge(self,x1,y1):ifself.fk[x1][y1][0]==0:forlei_xin[x1-1,x1,x1+1]:forlei_yin[y1-1,y1,y1+1]:#超出地图不处理if0=lei_xmap_xy_max[0]and0=lei_ymap_xy_max[1]:#如果已被点击不处理ifself.fk[lei_x][lei_y][1]==3:passelse:self.fk[lei_x][lei_y][1]=3State.click_judge(self,lei_x,lei_y)else:passelse:pass#插旗defset_flag(self):ifself.fk[self.x][self.y][1]==3:passelifself.fk[self.x][self.y][1]==2:self.fk[self.x][self.y][1]=0elifself.fk[self.x][self.y][1]==0orself.fk[self.x][self.y][1]==1:self.fk[self.x][self.y][1]+=1else:pass#游戏结束判定defgame_over(self):#判定是否点到了雷ifself.fk[self.x][self.y][0]==9:forfk_xinrange(len(self.fk)):forfk_yinrange(len(self.fk[fk_x])):ifself.fk[fk_x][fk_y][1]==1:ifself.fk[fk_x][fk_y][0]!=9:self.fk[fk_x][fk_y][1]=4else:passelse:self.fk[fk_x][fk_y][1]=3return3#游戏获胜判定defgame_win(self):#判定所有的雷是不是都插了旗------------------------forfk_xinrange(len(self.fk)):forfk_yinrange(len(self.fk[fk_x])):#如果有雷没插旗,继续游戏ifself.fk[fk_x][fk_y][0]==9andself.fk[fk_x][fk_y][1]!=1:return1#如果有不是雷的插着旗,继续游戏elifself.fk[fk_x][fk_y][0]!=9andself.fk[fk_x][fk_y][1]==1:return1#此项是为防止通过插旗测试正确答案,必须点开所有的空地#如果有没点开的砖块,继续游戏elifself.fk[fk_x][fk_y][1]==0:return1else:passreturn2defsetfk(self):returnself.fkclassModel:def__init__(self,fk,start_x,start_y):self.fk=fkself.start_x=start_xself.start_y=start_yself.qishu=lei_sumdefdayin(self):os.system('cls')print()#打印开头空行print('x\\y',end='')fortopinrange(len(self.fk[0])):print('%2d'%top,end='')print('\n')forxinrange(len(self.fk)):print('%2d'%x,end='')foryinrange(len(self.fk[x])):#self.fk[x][y][0]0,9,1,2,3,4,5,6,7,8分别为空,雷,数字1-8#self.fk[x][y][1]0,1,2,3分别为未点击,插旗,问号,被点击ifx==self.start_xandy==self.start_y:print(Fore.GREEN,end='')ifself.fk[x][y][1]==0:print('■',end='')elifself.fk[x][y][1]==1:print(Fore.RED+'▶'+Fore.RESET,end='')self.qishu-=1elifself.fk[x][y][1]==2:print('?',end='')elifself.fk[x][y][1]==3:ifself.fk[x][y][0]==0:print('□',end='')elifself.fk[x][y][0]==9:print('¤',end='')elifself.fk[x][y][0]==1:print('①',end='')elifself.fk[x][y][0]==2:print('②',end='')elifself.fk[x][y][0]==3:print('③',end='')elifself.fk[x][y][0]==4:print('④',end='')elifself.fk[x][y][0]==5:print('⑤',end='')elifself.fk[x][y][0]==6:print('⑥',end='')elifself.fk[x][y][0]==7:print('⑦',end='')elifself.fk[x][y][0]==8:print('⑧',end='')#数字#else:#print('%2d'%self.fk[x][y][0],end='')elifself.fk[x][y][1]==4:print('×',end='')else:passifx==self.start_xandy==self.start_yandself.fk[x][y][1]!=1:print(Fore.RESET,end='')#autoreset=Trueprint()#打印一行后回车print()#打印结尾空行print('你还有%d面旗子'%self.qishu)classControl:def__init__(self,key,x=0,y=0):self.key=keyself.x=xself.y=y#Up,Down,Left,Rightdeffk_move(self):iford(self.key)==119:self.x-=1eliford(self.key)==115:self.x+=1eliford(self.key)==97:self.y-=1eliford(self.key)==100:self.y+=1eliford(self.key)==106:return'j'eliford(self.key)==107:return'k'eliford(self.key)==27:#keynr27isescapeifeasygui.ccbox('是否退出游戏','退出',choices=(['是','否'])):return'esc'else:pass#边缘判定ifself.x0:self.x=map_xy_max[0]-1ifself.x=map_xy_max[0]:self.x=0ifself.y0:self.y=map_xy_max[1]-1ifself.y=map_xy_max[1]:self.y=0defget_all(self):return[self.x,self.y]map_xy_max=[]lei_sum=0defcli():globalmap_xy_max,lei_sumchoices=['简单','普通','困难','自定义']xuanguan=easygui.buttonbox('选择您要玩的难度','选关',choices)ifxuanguan==choices[3]:fields=['雷区长度:','雷区宽度:','雷数:']xuan=easygui.multenterbox('请输入您要挑战的难度','自定义难度',fields)forxuan_keyinrange(len(xuan[:])):xuan[xuan_key]=int(xuan[xuan_key])map_xy_max=xuan[:2]lei_sum=xuan[2]else:guan={choices[0]:[10,10,10],choices[1]:[15,15,30],choices[2]:[20,20,60],}map_xy_max=guan[xuanguan][:2]#地图大小lei_s