您好,登錄后才能下訂單哦!
利用python 編寫一個(gè)圍棋小游戲?很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。
1. 開始前
本程序基于tkinter生成GUI,使用前請(qǐng)確保已經(jīng)安裝好tkinter
對(duì)于windows用戶,您可能已經(jīng)安裝了tkinter,cmd中輸入python -c 'import tkinter',如果沒有出錯(cuò)則已安裝tkinter
對(duì)于linux用戶,您可能未安裝tkinter,使用包管理工具搜索tkinter,如:apt search tkinter,pacman -Ss tkinter,yum search tkinter,選擇符合要求的軟件包,然后安裝,如果未找到,請(qǐng)自行下載并編譯源代碼,再安裝。
2.完整代碼
#!/usr/bin/python3 # 使用Python內(nèi)置GUI模塊tkinter from tkinter import * # ttk覆蓋tkinter部分對(duì)象,ttk對(duì)tkinter進(jìn)行了優(yōu)化 from tkinter.ttk import * # 深拷貝時(shí)需要用到copy模塊 import copy import tkinter.messagebox # 圍棋應(yīng)用對(duì)象定義 class Application(Tk): # 初始化棋盤,默認(rèn)九路棋盤 def __init__(self,my_mode_num=9): Tk.__init__(self) # 模式,九路棋:9,十三路棋:13,十九路棋:19 self.mode_num=my_mode_num # 窗口尺寸設(shè)置,默認(rèn):1.8 self.size=1.8 # 棋盤每格的邊長(zhǎng) self.dd=360*self.size/(self.mode_num-1) # 相對(duì)九路棋盤的矯正比例 self.p=1 if self.mode_num==9 else (2/3 if self.mode_num==13 else 4/9) # 定義棋盤陣列,超過邊界:-1,無子:0,黑棋:1,白棋:2 self.positions=[[0 for i in range(self.mode_num+2)] for i in range(self.mode_num+2)] # 初始化棋盤,所有超過邊界的值置-1 for m in range(self.mode_num+2): for n in range(self.mode_num+2): if (m*n==0 or m==self.mode_num+1 or n==self.mode_num+1): self.positions[m][n]=-1 # 拷貝三份棋盤“快照”,悔棋和判斷“打劫”時(shí)需要作參考 self.last_3_positions=copy.deepcopy(self.positions) self.last_2_positions=copy.deepcopy(self.positions) self.last_1_positions=copy.deepcopy(self.positions) # 記錄鼠標(biāo)經(jīng)過的地方,用于顯示shadow時(shí) self.cross_last=None # 當(dāng)前輪到的玩家,黑:0,白:1,執(zhí)黑先行 self.present=0 # 初始停止運(yùn)行,點(diǎn)擊“開始游戲”運(yùn)行游戲 self.stop=True # 悔棋次數(shù),次數(shù)大于0才可悔棋,初始置0(初始不能悔棋),悔棋后置0,下棋或棄手時(shí)恢復(fù)為1,以禁止連續(xù)悔棋 self.regretchance=0 # 圖片資源,存放在當(dāng)前目錄下的/Pictures/中 self.photoW=PhotoImage(file = "./Pictures/W.png") self.photoB=PhotoImage(file = "./Pictures/B.png") self.photoBD=PhotoImage(file = "./Pictures/"+"BD"+"-"+str(self.mode_num)+".png") self.photoWD=PhotoImage(file = "./Pictures/"+"WD"+"-"+str(self.mode_num)+".png") self.photoBU=PhotoImage(file = "./Pictures/"+"BU"+"-"+str(self.mode_num)+".png") self.photoWU=PhotoImage(file = "./Pictures/"+"WU"+"-"+str(self.mode_num)+".png") # 用于黑白棋子圖片切換的列表 self.photoWBU_list=[self.photoBU,self.photoWU] self.photoWBD_list=[self.photoBD,self.photoWD] # 窗口大小 self.geometry(str(int(600*self.size))+'x'+str(int(400*self.size))) # 畫布控件,作為容器 self.canvas_bottom=Canvas(self,bg='#369',bd=0,width=600*self.size,height=400*self.size) self.canvas_bottom.place(x=0,y=0) # 幾個(gè)功能按鈕 self.startButton=Button(self,text='開始游戲',command=self.start) self.startButton.place(x=480*self.size,y=200*self.size) self.passmeButton=Button(self,text='棄一手',command=self.passme) self.passmeButton.place(x=480*self.size,y=225*self.size) self.regretButton=Button(self,text='悔棋',command=self.regret) self.regretButton.place(x=480*self.size,y=250*self.size) # 初始悔棋按鈕禁用 self.regretButton['state']=DISABLED self.replayButton=Button(self,text='重新開始',command=self.reload) self.replayButton.place(x=480*self.size,y=275*self.size) self.newGameButton1=Button(self,text=('十三' if self.mode_num==9 else '九')+'路棋',command=self.newGame1) self.newGameButton1.place(x=480*self.size,y=300*self.size) self.newGameButton2=Button(self,text=('十三' if self.mode_num==19 else '十九')+'路棋',command=self.newGame2) self.newGameButton2.place(x=480*self.size,y=325*self.size) self.quitButton=Button(self,text='退出游戲',command=self.quit) self.quitButton.place(x=480*self.size,y=350*self.size) # 畫棋盤,填充顏色 self.canvas_bottom.create_rectangle(0*self.size,0*self.size,400*self.size,400*self.size,fill='#c51') # 刻畫棋盤線及九個(gè)點(diǎn) # 先畫外框粗線 self.canvas_bottom.create_rectangle(20*self.size,20*self.size,380*self.size,380*self.size,width=3) # 棋盤上的九個(gè)定位點(diǎn),以中點(diǎn)為模型,移動(dòng)位置,以作出其余八個(gè)點(diǎn) for m in [-1,0,1]: for n in [-1,0,1]: self.oringinal=self.canvas_bottom.create_oval(200*self.size-self.size*2,200*self.size-self.size*2, 200*self.size+self.size*2,200*self.size+self.size*2,fill='#000') self.canvas_bottom.move(self.oringinal,m*self.dd*(2 if self.mode_num==9 else (3 if self.mode_num==13 else 6)), n*self.dd*(2 if self.mode_num==9 else (3 if self.mode_num==13 else 6))) # 畫中間的線條 for i in range(1,self.mode_num-1): self.canvas_bottom.create_line(20*self.size,20*self.size+i*self.dd,380*self.size,20*self.size+i*self.dd,width=2) self.canvas_bottom.create_line(20*self.size+i*self.dd,20*self.size,20*self.size+i*self.dd,380*self.size,width=2) # 放置右側(cè)初始圖片 self.pW=self.canvas_bottom.create_image(500*self.size+11, 65*self.size,image=self.photoW) self.pB=self.canvas_bottom.create_image(500*self.size-11, 65*self.size,image=self.photoB) # 每張圖片都添加image標(biāo)簽,方便reload函數(shù)刪除圖片 self.canvas_bottom.addtag_withtag('image',self.pW) self.canvas_bottom.addtag_withtag('image',self.pB) # 鼠標(biāo)移動(dòng)時(shí),調(diào)用shadow函數(shù),顯示隨鼠標(biāo)移動(dòng)的棋子 self.canvas_bottom.bind('<Motion>',self.shadow) # 鼠標(biāo)左鍵單擊時(shí),調(diào)用getdown函數(shù),放下棋子 self.canvas_bottom.bind('<Button-1>',self.getDown) # 設(shè)置退出快捷鍵<Ctrl>+<D>,快速退出游戲 self.bind('<Control-KeyPress-d>',self.keyboardQuit) # 開始游戲函數(shù),點(diǎn)擊“開始游戲”時(shí)調(diào)用 def start(self): # 刪除右側(cè)太極圖 self.canvas_bottom.delete(self.pW) self.canvas_bottom.delete(self.pB) # 利用右側(cè)圖案提示開始時(shí)誰先落子 if self.present==0: self.create_pB() self.del_pW() else: self.create_pW() self.del_pB() # 開始標(biāo)志,解除stop self.stop=None # 放棄一手函數(shù),跳過落子環(huán)節(jié) def passme(self): # 悔棋恢復(fù) if not self.regretchance==1: self.regretchance+=1 else: self.regretButton['state']=NORMAL # 拷貝棋盤狀態(tài),記錄前三次棋局 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 # 悔棋函數(shù),可悔棋一回合,下兩回合不可悔棋 def regret(self): # 判定是否可以悔棋,以前第三盤棋局復(fù)原棋盤 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 # 重新加載函數(shù),刪除圖片,序列歸零,設(shè)置一些初始參數(shù),點(diǎn)擊“重新開始”時(shí)調(diào)用 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 # 以下四個(gè)函數(shù)實(shí)現(xiàn)了右側(cè)太極圖的動(dòng)態(tài)創(chuàng)建與刪除 def create_pW(self): self.pW=self.canvas_bottom.create_image(500*self.size+11, 65*self.size,image=self.photoW) self.canvas_bottom.addtag_withtag('image',self.pW) def create_pB(self): self.pB=self.canvas_bottom.create_image(500*self.size-11, 65*self.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) # 顯示鼠標(biāo)移動(dòng)下棋子的移動(dòng) def shadow(self,event): if not self.stop: # 找到最近格點(diǎn),在當(dāng)前位置靠近的格點(diǎn)出顯示棋子圖片,并刪除上一位置的棋子圖片 if (20*self.size<event.x<380*self.size) and (20*self.size<event.y<380*self.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+22*self.p, event.y-dy+round(dy/self.dd)*self.dd-27*self.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 # 落子,并驅(qū)動(dòng)玩家的輪流下棋行為 def getDown(self,event): if not self.stop: # 先找到最近格點(diǎn) if (20*self.size-self.dd*0.4<event.x<self.dd*0.4+380*self.size) and (20*self.size-self.dd*0.4<event.y<self.dd*0.4+380*self.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) # 判斷位置是否已經(jīng)被占據(jù) if self.positions[y][x]==0: # 未被占據(jù),則嘗試占據(jù),獲得占據(jù)后能殺死的棋子列表 self.positions[y][x]=self.present+1 self.image_added=self.canvas_bottom.create_image(event.x-dx+round(dx/self.dd)*self.dd+4*self.p, event.y-dy+round(dy/self.dd)*self.dd-5*self.p,image=self.photoWBD_list[self.present]) self.canvas_bottom.addtag_withtag('image',self.image_added) # 棋子與位置標(biāo)簽綁定,方便“殺死” self.canvas_bottom.addtag_withtag('position'+str(x)+str(y),self.image_added) deadlist=self.get_deadlist(x,y) self.kill(deadlist) # 判斷是否重復(fù)棋局 if not self.last_2_positions==self.positions: # 判斷是否屬于有氣和殺死對(duì)方其中之一 if len(deadlist)>0 or self.if_dead([[x,y]],self.present+1,[x,y])==False: # 當(dāng)不重復(fù)棋局,且屬于有氣和殺死對(duì)方其中之一時(shí),落下棋子有效 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) # 刪除上次的標(biāo)記,重新創(chuàng)建標(biāo)記 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.5*self.dd, event.y-dy+round(dy/self.dd)*self.dd+0.5*self.dd,event.x-dx+round(dx/self.dd)*self.dd-0.5*self.dd, event.y-dy+round(dy/self.dd)*self.dd-0.5*self.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: # 不屬于殺死對(duì)方或有氣,則判斷為無氣,警告并彈出警告框 self.positions[y][x]=0 self.canvas_bottom.delete('position'+str(x)+str(y)) self.bell() self.showwarningbox('無氣',"你被包圍了!") else: # 重復(fù)棋局,警告打劫 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,無氣則返回?zé)o氣棋子的列表 # 本函數(shù)是游戲規(guī)則的關(guān)鍵,初始deadlist只包含了自己的位置,每次執(zhí)行時(shí),函數(shù)嘗試尋找yourPosition周圍有沒有空的位置,有則結(jié)束,返回False代表有氣; # 若找不到,則找自己四周的同類(不在deadlist中的)是否有氣,即調(diào)用本函數(shù),無氣,則把該同類加入到deadlist,然后找下一個(gè)鄰居,只要有一個(gè)有氣,返回False代表有氣; # 若四周沒有一個(gè)有氣的同類,返回deadlist,至此結(jié)束遞歸 # 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 # 警告消息框,接受標(biāo)題和警告信息 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.present==0 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.present==0 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 # 恢復(fù)位置列表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(20*self.size+(list_to_recover[i][0]-1)*self.dd+4*self.p, 20*self.size+(list_to_recover[i][1]-1)*self.dd-5*self.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): self.quit() # 以下兩個(gè)函數(shù)修改全局變量值,newApp使主函數(shù)循環(huán),以建立不同參數(shù)的對(duì)象 def newGame1(self): global mode_num,newApp mode_num=(13 if self.mode_num==9 else 9) newApp=True self.quit() def newGame2(self): global mode_num,newApp mode_num=(13 if self.mode_num==19 else 19) newApp=True self.quit() # 聲明全局變量,用于新建Application對(duì)象時(shí)切換成不同模式的游戲 global mode_num,newApp mode_num=9 newApp=False if __name__=='__main__': # 循環(huán),直到不切換游戲模式 while True: newApp=False app=Application(mode_num) app.title('圍棋') app.mainloop() if newApp: app.destroy() else: break
3.運(yùn)行效果
看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝您對(duì)億速云的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。