self.canvas_bottom.delete(self.pW)
self.canvas_bottom.delete(self.pB)
利用右侧图案提示开始时谁先落子
if self.present==0:
self.create_pB()
self.del_pW()
else:
self.create_pW()
self.del_pB()
开始标志,解除stop
self.stop=None
放弃一手函数,跳过落子环节
def passme(self):
悔棋恢复
if not self.regretchance==1:
self.regretchance+=1
else:
self.regretButton[‘state’]=NORMAL
拷贝棋盘状态,记录前三次棋局
self.last_3_positions=copy.deepcopy(self.last_2_positions)
self.last_2_positions=copy.deepcopy(self.last_1_positions)
self.last_1_positions=copy.deepcopy(self.positions)
self.canvas_bottom.delete(‘image_added_sign’)
轮到下一玩家
if self.present==0:
self.create_pW()
self.del_pB()
self.present=1
else:
self.create_pB()
self.del_pW()
self.present=0
悔棋函数,可悔棋一回合,下两回合不可悔棋
def regret(self):
判定是否可以悔棋,以前第三盘棋局复原棋盘
if self.regretchance==1:
self.regretchance=0
self.regretButton[‘state’]=DISABLED
list_of_b=[]
list_of_w=[]
self.canvas_bottom.delete(‘image’)
if self.present==0:
self.create_pB()
else:
self.create_pW()
for m in range(1,self.mode_num+1):
for n in range(1,self.mode_num+1):
self.positions[m][n]=0
for m in range(len(self.last_3_positions)):
for n in range(len(self.last_3_positions[m])):
if self.last_3_positions[m][n]==1:
list_of_b+=[[n,m]]
elif self.last_3_positions[m][n]==2:
list_of_w+=[[n,m]]
self.recover(list_of_b,0)
self.recover(list_of_w,1)
self.last_1_positions=copy.deepcopy(self.last_3_positions)
for m in range(1,self.mode_num+1):
for n in range(1,self.mode_num+1):
self.last_2_positions[m][n]=0
self.last_3_positions[m][n]=0
重新加载函数,删除图片,序列归零,设置一些初始参数,点击“重新开始”时调用
def reload(self):
if self.stop==1:
self.stop=0
self.canvas_bottom.delete(‘image’)
self.regretchance=0
self.present=0
self.create_pB()
for m in range(1,self.mode_num+1):
for n in range(1,self.mode_num+1):
self.positions[m][n]=0
self.last_3_positions[m][n]=0
self.last_2_positions[m][n]=0
self.last_1_positions[m][n]=0
以下四个函数实现了右侧太极图的动态创建与删除
def create_pW(self):
self.pW=self.canvas_bottom.create_image(500self.size+11, 65self.size,image=self.photoW)
self.canvas_bottom.addtag_withtag(‘image’,self.pW)
def create_pB(self):
self.pB=self.canvas_bottom.create_image(500self.size-11, 65self.size,image=self.photoB)
self.canvas_bottom.addtag_withtag(‘image’,self.pB)
def del_pW(self):
self.canvas_bottom.delete(self.pW)
def del_pB(self):
self.canvas_bottom.delete(self.pB)
显示鼠标移动下棋子的移动
def shadow(self,event):
if not self.stop:
找到最近格点,在当前位置靠近的格点出显示棋子图片,并删除上一位置的棋子图片
if (20self.size<event.x<380self.size) and (20self.size<event.y<380self.size):
dx=(event.x-20*self.size)%self.dd
dy=(event.y-20*self.size)%self.dd
self.cross=self.canvas_bottom.create_image(event.x-dx+round(dx/self.dd)self.dd+22self.p, event.y-dy+round(dy/self.dd)self.dd-27self.p,image=self.photoWBU_list[self.present])
self.canvas_bottom.addtag_withtag(‘image’,self.cross)
if self.cross_last!=None:
self.canvas_bottom.delete(self.cross_last)
self.cross_last=self.cross
落子,并驱动玩家的轮流下棋行为
def getDown(self,event):
if not self.stop:
先找到最近格点
if (20self.size-self.dd0.4<event.x<self.dd0.4+380self.size) and (20self.size-self.dd0.4<event.y<self.dd0.4+380self.size):
dx=(event.x-20*self.size)%self.dd
dy=(event.y-20*self.size)%self.dd
x=int((event.x-20*self.size-dx)/self.dd+round(dx/self.dd)+1)
y=int((event.y-20*self.size-dy)/self.dd+round(dy/self.dd)+1)
判断位置是否已经被占据
if self.positions[y][x]==0:
未被占据,则尝试占据,获得占据后能杀死的棋子列表
self.positions[y][x]=self.present+1
self.image_added=self.canvas_bottom.create_image(event.x-dx+round(dx/self.dd)self.dd+4self.p, event.y-dy+round(dy/self.dd)self.dd-5self.p,image=self.photoWBD_list[self.present])
self.canvas_bottom.addtag_withtag(‘image’,self.image_added)
棋子与位置标签绑定,方便“杀死”
self.canvas_bottom.addtag_withtag(‘position’+str(x)+str(y),self.image_added)
deadlist=self.get_deadlist(x,y)
self.kill(deadlist)
判断是否重复棋局
if not self.last_2_positions==self.positions:
判断是否属于有气和杀死对方其中之一
if len(deadlist)>0 or self.if_dead([[x,y]],self.present+1,[x,y])==False:
当不重复棋局,且属于有气和杀死对方其中之一时,落下棋子有效
if not self.regretchance==1:
self.regretchance+=1
else:
self.regretButton[‘state’]=NORMAL
self.last_3_positions=copy.deepcopy(self.last_2_positions)
self.last_2_positions=copy.deepcopy(self.last_1_positions)
self.last_1_positions=copy.deepcopy(self.positions)
删除上次的标记,重新创建标记
self.canvas_bottom.delete(‘image_added_sign’)
self.image_added_sign=self.canvas_bottom.create_oval(event.x-dx+round(dx/self.dd)self.dd+0.5self.dd, event.y-dy+round(dy/self.dd)self.dd+0.5self.dd,event.x-dx+round(dx/self.dd)self.dd-0.5self.dd, event.y-dy+round(dy/self.dd)self.dd-0.5self.dd,width=3,outline=‘#3ae’)
self.canvas_bottom.addtag_withtag(‘image’,self.image_added_sign)
self.canvas_bottom.addtag_withtag(‘image_added_sign’,self.image_added_sign)
if self.present==0:
self.create_pW()
self.del_pB()
self.present=1
else:
self.create_pB()
self.del_pW()
self.present=0
else:
不属于杀死对方或有气,则判断为无气,警告并弹出警告框
self.positions[y][x]=0
self.canvas_bottom.delete(‘position’+str(x)+str(y))
self.bell()
self.showwarningbox(‘无气’,“你被包围了!”)
else:
重复棋局,警告打劫
self.positions[y][x]=0
self.canvas_bottom.delete(‘position’+str(x)+str(y))
self.recover(deadlist,(1 if self.present==0 else 0))
self.bell()
self.showwarningbox(“打劫”,“此路不通!”)
else:
覆盖,声音警告
self.bell()
else:
超出边界,声音警告
self.bell()
判断棋子(种类为yourChessman,位置为yourPosition)是否无气(死亡),有气则返回False,无气则返回无气棋子的列表
本函数是游戏规则的关键,初始deadlist只包含了自己的位置,每次执行时,函数尝试寻找yourPosition周围有没有空的位置,有则结束,返回False代表有气;
若找不到,则找自己四周的同类(不在deadlist中的)是否有气,即调用本函数,无气,则把该同类加入到deadlist,然后找下一个邻居,只要有一个有气,返回False代表有气;
若四周没有一个有气的同类,返回deadlist,至此结束递归
def if_dead(self,deadlist,yourChessman,yourPosition):
def if_dead(self,deadList,yourChessman,yourPosition):
for i in [-1,1]:
if [yourPosition[0]+i,yourPosition[1]] not in deadList:
if self.positions[yourPosition[1]][yourPosition[0]+i]==0:
return False
if [yourPosition[0],yourPosition[1]+i] not in deadList:
if self.positions[yourPosition[1]+i][yourPosition[0]]==0:
return False
if ([yourPosition[0]+1,yourPosition[1]] not in deadList) and (self.positions[yourPosition[1]][yourPosition[0]+1]==yourChessman):
midvar=self.if_dead(deadList+[[yourPosition[0]+1,yourPosition[1]]],yourChessman,[yourPosition[0]+1,yourPosition[1]])
if not midvar:
return False
else:
deadList+=copy.deepcopy(midvar)
if ([yourPosition[0]-1,yourPosition[1]] not in deadList) and (self.positions[yourPosition[1]][yourPosition[0]-1]==yourChessman):
midvar=self.if_dead(deadList+[[yourPosition[0]-1,yourPosition[1]]],yourChessman,[yourPosition[0]-1,yourPosition[1]])
if not midvar:
return False
else:
deadList+=copy.deepcopy(midvar)
if ([yourPosition[0],yourPosition[1]+1] not in deadList) and (self.positions[yourPosition[1]+1][yourPosition[0]]==yourChessman):
midvar=self.if_dead(deadList+[[yourPosition[0],yourPosition[1]+1]],yourChessman,[yourPosition[0],yourPosition[1]+1])
if not midvar:
return False
else:
deadList+=copy.deepcopy(midvar)
if ([yourPosition[0],yourPosition[1]-1] not in deadList) and (self.positions[yourPosition[1]-1][yourPosition[0]]==yourChessman):
midvar=self.if_dead(deadList+[[yourPosition[0],yourPosition[1]-1]],yourChessman,[yourPosition[0],yourPosition[1]-1])
if not midvar:
return False
else:
deadList+=copy.deepcopy(midvar)
return deadList
警告消息框,接受标题和警告信息
def showwarningbox(self,title,message):
self.canvas_bottom.delete(self.cross)
tkinter.messagebox.showwarning(title,message)
落子后,依次判断四周是否有棋子被杀死,并返回死棋位置列表
def get_deadlist(self,x,y):
deadlist=[]
for i in [-1,1]:
if self.positions[y][x+i](2 if self.present0 else 1) and ([x+i,y] not in deadlist):
killList=self.if_dead([[x+i,y]],(2 if self.present==0 else 1),[x+i,y])
if not killList==False:
deadlist+=copy.deepcopy(killList)
if self.positions[y+i][x](2 if self.present0 else 1) and ([x,y+i] not in deadlist):
killList=self.if_dead([[x,y+i]],(2 if self.present==0 else 1),[x,y+i])
if not killList==False:
deadlist+=copy.deepcopy(killList)
return deadlist
恢复位置列表list_to_recover为b_or_w指定的棋子
def recover(self,list_to_recover,b_or_w):
if len(list_to_recover)>0:
for i in range(len(list_to_recover)):
self.positions[list_to_recover[i][1]][list_to_recover[i][0]]=b_or_w+1
self.image_added=self.canvas_bottom.create_image(20self.size+(list_to_recover[i][0]-1)self.dd+4self.p, 20self.size+(list_to_recover[i][1]-1)self.dd-5self.p,image=self.photoWBD_list[b_or_w])
self.canvas_bottom.addtag_withtag(‘image’,self.image_added)
self.canvas_bottom.addtag_withtag(‘position’+str(list_to_recover[i][0])+str(list_to_recover[i][1]),self.image_added)
杀死位置列表killList中的棋子,即删除图片,位置值置0
def kill(self,killList):
if len(killList)>0:
for i in range(len(killList)):
self.positions[killList[i][1]][killList[i][0]]=0
self.canvas_bottom.delete(‘position’+str(killList[i][0])+str(killList[i][1]))
键盘快捷键退出游戏
def keyboardQuit(self,event):
文末有福利领取哦~
👉一、Python所有方向的学习路线
Python所有方向的技术点做的整理,形成各个领域的知识点汇总,它的用处就在于,你可以按照上面的知识点去找对应的学习资源,保证自己学得较为全面。
👉二、Python必备开发工具
👉三、Python视频合集
观看零基础学习视频,看视频学习是最快捷也是最有效果的方式,跟着视频中老师的思路,从基础到深入,还是很容易入门的。
👉 四、实战案例
光学理论是没用的,要学会跟着一起敲,要动手实操,才能将自己的所学运用到实际当中去,这时候可以搞点实战案例来学习。(文末领读者福利)
👉五、Python练习题
检查学习结果。
👉六、面试资料
我们学习Python必然是为了找到高薪的工作,下面这些面试题是来自阿里、腾讯、字节等一线互联网大厂最新的面试资料,并且有阿里大佬给出了权威的解答,刷完这一套面试资料相信大家都能找到满意的工作。
👉因篇幅有限,仅展示部分资料,这份完整版的Python全套学习资料已经上传
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!