您好,登錄后才能下訂單哦!
本篇內容主要講解“基于Python如何實現圍棋游戲”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“基于Python如何實現圍棋游戲”吧!
tkinter:ttk覆蓋tkinter部分對象,ttk對tkinter進行了優化
copy:深拷貝時需要用到copy模塊
tkinter.messagebox:圍棋應用對象定義
如沒有以上模塊,在pycharm終端輸入以下指令:
pip install 相應模塊 -i https://pypi.douban.com/simple
from tkinter import * from tkinter.ttk import * import copy import tkinter.messagebox
對棋盤進行初始化和棋盤右側的按鈕設置,以及對棋子的控制。
class Application(Tk): # 初始化棋盤,默認九路棋盤 def __init__(self,my_mode_num=9): Tk.__init__(self) # 模式,九路棋:9,十三路棋:13,十九路棋:19 self.mode_num=my_mode_num # 窗口尺寸設置,默認:1.8 self.size=1.8 # 棋盤每格的邊長 self.dd=360*self.size/(self.mode_num-1) # 相對九路棋盤的矯正比例 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 # 拷貝三份棋盤“快照”,悔棋和判斷“打劫”時需要作參考 self.last_3_positions=copy.deepcopy(self.positions) self.last_2_positions=copy.deepcopy(self.positions) self.last_1_positions=copy.deepcopy(self.positions) # 記錄鼠標經過的地方,用于顯示shadow時 self.cross_last=None # 當前輪到的玩家,黑:0,白:1,執黑先行 self.present=0 # 初始停止運行,點擊“開始游戲”運行游戲 self.stop=True # 悔棋次數,次數大于0才可悔棋,初始置0(初始不能悔棋),悔棋后置0,下棋或棄手時恢復為1,以禁止連續悔棋 self.regretchance=0 # 圖片資源,存放在當前目錄下的/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) # 幾個功能按鈕 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') # 刻畫棋盤線及九個點 # 先畫外框粗線 self.canvas_bottom.create_rectangle(20*self.size,20*self.size,380*self.size,380*self.size,width=3) # 棋盤上的九個定位點,以中點為模型,移動位置,以作出其余八個點 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) # 放置右側初始圖片 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標簽,方便reload函數刪除圖片 self.canvas_bottom.addtag_withtag('image',self.pW) self.canvas_bottom.addtag_withtag('image',self.pB) # 鼠標移動時,調用shadow函數,顯示隨鼠標移動的棋子 self.canvas_bottom.bind('<Motion>',self.shadow) # 鼠標左鍵單擊時,調用getdown函數,放下棋子 self.canvas_bottom.bind('<Button-1>',self.getDown) # 設置退出快捷鍵<Ctrl>+<D>,快速退出游戲 self.bind('<Control-KeyPress-d>',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(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)
def shadow(self,event): if not self.stop: # 找到最近格點,在當前位置靠近的格點出顯示棋子圖片,并刪除上一位置的棋子圖片 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 # 落子,并驅動玩家的輪流下棋行為 def getDown(self,event): if not self.stop: # 先找到最近格點 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) # 判斷位置是否已經被占據 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+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) # 棋子與位置標簽綁定,方便“殺死” 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.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: # 不屬于殺死對方或有氣,則判斷為無氣,警告并彈出警告框 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()
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.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 # 恢復位置列表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() # 以下兩個函數修改全局變量值,newApp使主函數循環,以建立不同參數的對象 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對象時切換成不同模式的游戲 global mode_num,newApp mode_num=9 newApp=False
if __name__=='__main__': # 循環,直到不切換游戲模式 while True: newApp=False app=Application(mode_num) app.title('圍棋') app.mainloop() if newApp: app.destroy() else: break
到此,相信大家對“基于Python如何實現圍棋游戲”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。