2024年最新【围棋游戏——使用Python实现(纯tkinter gui)】

self.canvas_bottom.place(x=0,y=0)

几个功能按钮

self.startButton=Button(self,text=‘开始游戏’,command=self.start)

self.startButton.place(x=480self.size,y=200self.size)

self.passmeButton=Button(self,text=‘弃一手’,command=self.passme)

self.passmeButton.place(x=480self.size,y=225self.size)

self.regretButton=Button(self,text=‘悔棋’,command=self.regret)

self.regretButton.place(x=480self.size,y=250self.size)

初始悔棋按钮禁用

self.regretButton[‘state’]=DISABLED

self.replayButton=Button(self,text=‘重新开始’,command=self.reload)

self.replayButton.place(x=480self.size,y=275self.size)

self.newGameButton1=Button(self,text=(‘十三’ if self.mode_num==9 else ‘九’)+‘路棋’,command=self.newGame1)

self.newGameButton1.place(x=480self.size,y=300self.size)

self.newGameButton2=Button(self,text=(‘十三’ if self.mode_num==19 else ‘十九’)+‘路棋’,command=self.newGame2)

self.newGameButton2.place(x=480self.size,y=325self.size)

self.quitButton=Button(self,text=‘退出游戏’,command=self.quit)

self.quitButton.place(x=480self.size,y=350self.size)

画棋盘,填充颜色

self.canvas_bottom.create_rectangle(0self.size,0self.size,400self.size,400self.size,fill=‘#c51’)

刻画棋盘线及九个点

先画外框粗线

self.canvas_bottom.create_rectangle(20self.size,20self.size,380self.size,380self.size,width=3)

棋盘上的九个定位点,以中点为模型,移动位置,以作出其余八个点

for m in [-1,0,1]:

for n in [-1,0,1]:

self.oringinal=self.canvas_bottom.create_oval(200self.size-self.size2,200self.size-self.size2,

200self.size+self.size2,200self.size+self.size2,fill=‘#000’)

self.canvas_bottom.move(self.oringinal,mself.dd(2 if self.mode_num9 else (3 if self.mode_num13 else 6)),

nself.dd(2 if self.mode_num9 else (3 if self.mode_num13 else 6)))

画中间的线条

for i in range(1,self.mode_num-1):

self.canvas_bottom.create_line(20self.size,20self.size+iself.dd,380self.size,20self.size+iself.dd,width=2)

self.canvas_bottom.create_line(20self.size+iself.dd,20self.size,20self.size+iself.dd,380self.size,width=2)

放置右侧初始图片

self.pW=self.canvas_bottom.create_image(500self.size+11, 65self.size,image=self.photoW)

self.pB=self.canvas_bottom.create_image(500self.size-11, 65self.size,image=self.photoB)

每张图片都添加image标签,方便reload函数删除图片

self.canvas_bottom.addtag_withtag(‘image’,self.pW)

self.canvas_bottom.addtag_withtag(‘image’,self.pB)

鼠标移动时,调用shadow函数,显示随鼠标移动的棋子

self.canvas_bottom.bind(‘’,self.shadow)

鼠标左键单击时,调用getdown函数,放下棋子

self.canvas_bottom.bind(‘’,self.getDown)

设置退出快捷键+,快速退出游戏

self.bind(‘’,self.keyboardQuit)

开始游戏函数,点击“开始游戏”时调用

def start(self):

删除右侧太极图

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:

如果你也是看准了Python,想自学Python,在这里为大家准备了丰厚的免费学习大礼包,带大家一起学习,给大家剖析Python兼职、就业行情前景的这些事儿。

一、Python所有方向的学习路线

Python所有方向路线就是把Python常用的技术点做整理,形成各个领域的知识点汇总,它的用处就在于,你可以按照上面的知识点去找对应的学习资源,保证自己学得较为全面。

二、学习软件

工欲善其必先利其器。学习Python常用的开发软件都在这里了,给大家节省了很多时间。

三、全套PDF电子书

书籍的好处就在于权威和体系健全,刚开始学习的时候你可以只看视频或者听某个人讲课,但等你学完之后,你觉得你掌握了,这时候建议还是得去看一下书籍,看权威技术书籍也是每个程序员必经之路。

四、入门学习视频

我们在看视频学习的时候,不能光动眼动脑不动手,比较科学的学习方法是在理解之后运用它们,这时候练手项目就很适合了。

四、实战案例

光学理论是没用的,要学会跟着一起敲,要动手实操,才能将自己的所学运用到实际当中去,这时候可以搞点实战案例来学习。

五、面试资料

我们学习Python必然是为了找到高薪的工作,下面这些面试题是来自阿里、腾讯、字节等一线互联网大厂最新的面试资料,并且有阿里大佬给出了权威的解答,刷完这一套面试资料相信大家都能找到满意的工作。

成为一个Python程序员专家或许需要花费数年时间,但是打下坚实的基础只要几周就可以,如果你按照我提供的学习路线以及资料有意识地去实践,你就有很大可能成功!
最后祝你好运!!!

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化学习资料的朋友,可以戳这里无偿获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

  • 21
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,很高兴为您解答。以下是使用Python自带的Tkinter模块来实现石头剪刀布游戏的精美GUI界面的步骤: 1. 首先,导入Tkinter模块。 ```python import tkinter as tk ``` 2. 创建一个主窗口,并设置窗口的标题和大小。 ```python window = tk.Tk() window.title("石头剪刀布游戏") window.geometry("400x300") ``` 3. 创建游戏界面所需要的组件,包括标签、按钮和图片。 ```python # 创建标签组件 label = tk.Label(window, text="请选择:石头、剪刀、布") # 创建按钮组件 button_rock = tk.Button(window, text="石头") button_scissors = tk.Button(window, text="剪刀") button_paper = tk.Button(window, text="布") # 创建图片组件 image_player = tk.PhotoImage(file="player.png") image_computer = tk.PhotoImage(file="computer.png") label_player = tk.Label(window, image=image_player) label_computer = tk.Label(window, image=image_computer) ``` 4. 将组件添加到主窗口中。 ```python label.pack() button_rock.pack(side="left") button_scissors.pack(side="left") button_paper.pack(side="left") label_player.pack(side="left") label_computer.pack(side="right") ``` 5. 在按钮的点击事件中添加游戏逻辑代码,并更新图片。 ```python def play_rock(): # 游戏逻辑代码 # 更新图片 pass def play_scissors(): # 游戏逻辑代码 # 更新图片 pass def play_paper(): # 游戏逻辑代码 # 更新图片 pass button_rock.config(command=play_rock) button_scissors.config(command=play_scissors) button_paper.config(command=play_paper) ``` 6. 显示主窗口。 ```python window.mainloop() ``` 以上就是使用Python自带的Tkinter模块来实现石头剪刀布游戏的精美GUI界面的步骤。希望能帮到您!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值