您好,登錄后才能下訂單哦!
這篇文章主要介紹“Python怎么實現雙人五子棋對局”,在日常操作中,相信很多人在Python怎么實現雙人五子棋對局問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Python怎么實現雙人五子棋對局”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
效果:
自己需要兩個棋子:
服務器玩家全部代碼:
# 案列使用TCP連接 # 這是服務器端 import socket import wx import threading import time from PIL import Image # 定義套接字 s s=socket.socket(socket.AF_INET,socket.SOCK_STREAM) Cell=40 # 定義窗口類 class MyFrame(wx.Frame): # 初始化這里就是生成界面,然后綁定了按鈕事件,其他沒了 def __init__(self): super().__init__(parent=None,size=(600,600),title="五子棋:服務器") self.Center() self.panel=wx.Panel(parent=self) #openButton = wx.Button(parent=map, id=1, label="開房間") #self.Bind(wx.EVT_BUTTON, self.StartGame) self.panel.Bind(wx.EVT_PAINT, self.PaintBackground) # 繪圖 self.panel.Bind(wx.EVT_LEFT_DOWN,self.GoChess) # 綁定鼠標左鍵消息 self.picture = [wx.Bitmap("黑.png"), wx.Bitmap("白.png")] # 創造二維數組用來判斷自己是否獲勝 self.map = [[" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "] for y in range(15)] # 定義一個變量來標志自己是否可以走棋 self.IsGoGame = True self.StartGame() # 畫背景函數 def PaintBackground(self,event): self.dc = wx.PaintDC(self.panel) # 創造背景 brush = wx.Brush("white") self.dc.SetBackground(brush) self.dc.Clear() # 畫方格線 pen = wx.Pen(wx.Colour(0, 0, 0), 1, wx.SOLID) self.dc.SetPen(pen) for i in range(15): self.dc.DrawLine(0, Cell*i, 600, Cell*i) self.dc.DrawLine(Cell * i, 0, Cell * i, 600) # 在x,y坐標繪制棋子 def PaintPiece(self,x,y): image = wx.StaticBitmap(self.panel, -1, self.picture[0],size=(40,40),pos=(x*Cell+Cell/2,y*Cell+Cell/2)) #image.SetPosition((x*Cell+Cell/2,y*Cell+Cell/2)) # 玩家自己走棋 def GoChess(self,event): #SetPosition(event.GetPosition()) if self.IsGoGame: pos = event.GetPosition() # 在x,y坐標繪制棋子 x = int((pos.x - Cell / 2) // Cell) y = int((pos.y - Cell / 2) // Cell) self.PaintPiece(x, y) # 下子后,向客戶端發送位置 msg = str(x) + "," + str(y) self.conn.send(msg.encode()) # 給客戶端發送信息 print("服務器發送:", msg) self.map[x][y]="a" # 判斷是否勝利 if self.win_lose("a"): self.one_Dialog("win") self.IsGoGame=False else: self.one_Dialog("notGo") # 開啟服務器端函數 def StartGame(self): self.otherNum=0 self.image=[] for item in range(50): self.image.append(wx.StaticBitmap(self.panel, -1, self.picture[1], size=(40, 40),pos=(-100,-100))) threadGame=threading.Thread(target=self.thread_body,name="Srever") threadGame.start() def thread_body(self): s.bind(("127.0.0.1", 8888)) # 綁定IP和端口(參數為二元組),就是尋址 s.listen(5) # 因為是TCP,所有要監聽端口 print("服務器啟動·····") self.conn, self.addess = s.accept() # 等待客戶端連接(參數為最大連接數),返回一個二元組(新的socket對象+客戶端地址) while True: data = self.conn.recv(1024) # 接受1024字節序列數據(這個函數阻塞,直到接受到數據) if len(data) != 0: msg = data.decode() print("服務器接收:",msg) msg = msg.split(",") #self.PaintPiece(int(msg[0]), int(msg[1])) #image = wx.StaticBitmap(self.panel, -1, self.picture[0], size=(40, 40)) # 設置原來實例化好的棋子的位置 self.image[self.otherNum].SetPosition((int(msg[0]) * Cell + Cell / 2, int(msg[1])* Cell + Cell / 2)) self.otherNum+=1 self.map[int(msg[0])][int(msg[1])] = "b" if self.win_lose("b"): # 判斷對方玩家是否勝利 self.one_Dialog("lose") self.IsGoGame = True # 接收消息后 玩家能夠走棋 #time.sleep(2) def one_Dialog(self,msg): if msg=="win": dlg = wx.MessageDialog(None, u"你勝利了!", u"恭喜", wx.YES_NO | wx.ICON_QUESTION) if dlg.ShowModal() == wx.ID_YES: self.Close(True) dlg.Destroy() elif msg=="lose": dlg = wx.MessageDialog(None, u"你輸了!", u"很遺憾", wx.YES_NO | wx.ICON_QUESTION) if dlg.ShowModal() == wx.ID_YES: self.Close(True) dlg.Destroy() elif msg == "notGo": dlg = wx.MessageDialog(None, u"等待對方下棋!", u"提示", wx.YES_NO | wx.ICON_QUESTION) if dlg.ShowModal() == wx.ID_YES: #self.Close(True) dlg.Destroy() def win_lose(self,msg): a = str(msg) print("a=", a) for i in range(0, 11): for j in range(0, 11): if self.map[i][j] == a and self.map[i + 1][j + 1] == a and self.map[i + 2][j + 2] == a and \ self.map[i + 3][ j + 3] == a and self.map[i + 4][j + 4] == a: print("x=y軸上形成五子連珠") return True for i in range(4, 15): for j in range(0, 11): if self.map[i][j] == a and self.map[i - 1][j + 1] == a and self.map[i - 2][j + 2] == a and \ self.map[i - 3][ j + 3] == a and self.map[i - 4][j + 4] == a: print("x=-y軸上形成五子連珠") return True for i in range(0, 15): for j in range(4, 15): if self.map[i][j] == a and self.map[i][j - 1] == a and self.map[i][j - 2] == a and self.map[i][ j - 2] == a and self.map[i][ j - 4] == a: print("Y軸上形成了五子連珠") return True for i in range(0, 11): for j in range(0, 15): if self.map[i][j] == a and self.map[i + 1][j] == a and self.map[i + 2][j] == a and \ self.map[i + 3][j] == a and \ self.map[i + 4][j] == a: print("X軸形成五子連珠") return True return False # 應用程序 class App(wx.App): def OnInit(self): frame=MyFrame() frame.Show() return True def OnExit(self): s.close() # 關閉socket對象 return 0 # 進入main函數運行:循環 if __name__=="__main__": app=App() app.MainLoop()
客戶端玩家全部代碼:
# 案列使用TCP連接 # 這是服務器端 import socket import wx import threading import time from PIL import Image # 定義套接字 s s=socket.socket(socket.AF_INET,socket.SOCK_STREAM) Cell=40 # 定義窗口類 class MyFrame(wx.Frame): # 初始化這里就是生成界面,然后綁定了按鈕事件,其他沒了 def __init__(self): super().__init__(parent=None,size=(600,600),title="五子棋:客戶端") self.Center() self.panel=wx.Panel(parent=self) #openButton = wx.Button(parent=map, id=1, label="開房間") #self.Bind(wx.EVT_BUTTON, self.StartGame) self.panel.Bind(wx.EVT_PAINT, self.PaintBackground) # 繪圖 self.panel.Bind(wx.EVT_LEFT_DOWN,self.GoChess) # 綁定鼠標左鍵消息 self.picture=[wx.Bitmap("白.png"),wx.Bitmap("黑.png")] # 創造二維數組用來判斷自己是否獲勝 self.map = [[" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "] for y in range(15)] # 定義一個變量來標志自己是否可以走棋 self.IsGoGame=False self.StartGame() # 畫背景函數 def PaintBackground(self,event): self.dc = wx.PaintDC(self.panel) # 創造背景 brush = wx.Brush("white") self.dc.SetBackground(brush) self.dc.Clear() # 畫方格線 pen = wx.Pen(wx.Colour(0, 0, 0), 1, wx.SOLID) self.dc.SetPen(pen) for i in range(15): self.dc.DrawLine(0, Cell*i, 600, Cell*i) self.dc.DrawLine(Cell * i, 0, Cell * i, 600) # 在x,y坐標繪制棋子 def PaintPiece(self,x,y): image = wx.StaticBitmap(self.panel, -1,self.picture[0] ,size=(40,40),pos=(x*Cell+Cell/2,y*Cell+Cell/2)) #image.SetPosition() def GoChess(self,event): #SetPosition(event.GetPosition()) if self.IsGoGame: # 輪到自己下棋 pos = event.GetPosition() # 在x,y坐標繪制棋子 x=int((pos.x-Cell/2)//Cell) y=int((pos.y-Cell/2)//Cell) self.PaintPiece(x, y) # 下子后,向客戶端發送位置 msg=str(x)+","+str(y) s.send(msg.encode()) # 給客戶端發送信息 print("客戶端發送:", msg) self.map[x][y] = "a" # 判斷是否勝利 if self.win_lose("a"): self.one_Dialog("win") self.IsGoGame=False else: self.one_Dialog("notGo") # 開啟服務器端函數 def StartGame(self): self.image=[] self.otherNum = 0 for item in range(50): self.image.append(wx.StaticBitmap(self.panel, -1, self.picture[1], size=(40, 40), pos=(-100, -100))) while True: try: s.connect(("127.0.0.1", 8888)) break except: print("等待服務器啟動~") threadGame = threading.Thread(target=self.thread_body, name="Client") threadGame.start() return def thread_body(self): while True: data = s.recv(1024) # 等待接收服務器端信息 if len(data) != 0: msg=data.decode() print("客戶端接收:", msg) msg = msg.split(",") #self.PaintPiece(int(msg[0]), int(msg[1])) #image = wx.StaticBitmap(self.panel, -1, self.picture[0], size=(40, 40)) self.image[self.otherNum].SetPosition((int(msg[0]) * Cell + Cell / 2, int(msg[1]) * Cell + Cell / 2)) self.otherNum += 1 self.map[int(msg[0])][int(msg[1])] = "b" if self.win_lose("b"): self.one_Dialog("lose") self.IsGoGame=True #time.sleep(2) def one_Dialog(self, msg): if msg == "win": dlg = wx.MessageDialog(None, u"你勝利了!", u"恭喜", wx.YES_NO | wx.ICON_QUESTION) if dlg.ShowModal() == wx.ID_YES: self.Close(True) dlg.Destroy() if msg == "lose": dlg = wx.MessageDialog(None, u"你輸了!", u"很遺憾", wx.YES_NO | wx.ICON_QUESTION) if dlg.ShowModal() == wx.ID_YES: self.Close(True) dlg.Destroy() if msg == "notGo": dlg = wx.MessageDialog(None, u"等待對方下棋!", u"提示", wx.YES_NO | wx.ICON_QUESTION) if dlg.ShowModal() == wx.ID_YES: #self.Close(True) dlg.Destroy() # 判斷整個棋盤的輸贏 def win_lose(self,msg): a = str(msg) print("a=", a) for i in range(0, 11): for j in range(0, 11): if self.map[i][j] == a and self.map[i + 1][j + 1] == a and self.map[i + 2][j + 2] == a and self.map[i + 3][ j + 3] == a and self.map[i + 4][j + 4] == a: print("x=y軸上形成五子連珠") return True for i in range(4, 15): for j in range(0, 11): if self.map[i][j] == a and self.map[i - 1][j + 1] == a and self.map[i - 2][j + 2] == a and self.map[i - 3][ j + 3] == a and self.map[i - 4][j + 4] == a: print("x=-y軸上形成五子連珠") return True for i in range(0, 15): for j in range(4, 15): if self.map[i][j] == a and self.map[i][j - 1] == a and self.map[i][j - 2] == a and self.map[i][j - 2] == a and self.map[i][ j - 4] == a: print("Y軸上形成了五子連珠") return True for i in range(0, 11): for j in range(0, 15): if self.map[i][j] == a and self.map[i + 1][j] == a and self.map[i + 2][j] == a and self.map[i + 3][j] == a and \ self.map[i + 4][j] == a: print("X軸形成五子連珠") return True return False # 應用程序 class App(wx.App): def OnInit(self): frame=MyFrame() frame.Show() return True def OnExit(self): s.close() # 關閉socket對象 return 0 # 進入main函數運行:循環 if __name__=="__main__": app=App() app.MainLoop()
到此,關于“Python怎么實現雙人五子棋對局”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。