本文實(shí)例為大家分享了python實(shí)現(xiàn)人機(jī)五子棋的具體代碼,供大家參考,具體內(nèi)容如下
成都創(chuàng)新互聯(lián)公司專業(yè)為企業(yè)提供化州網(wǎng)站建設(shè)、化州做網(wǎng)站、化州網(wǎng)站設(shè)計(jì)、化州網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)與制作、化州企業(yè)網(wǎng)站模板建站服務(wù),十余年化州做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。圖形界面引用PyQt5,還有socket通信??梢跃钟蚓W(wǎng)對戰(zhàn),可以人機(jī)對戰(zhàn),應(yīng)該存在一些小的bug,但是還沒有找出來。希望讀者可以找到
下面附幾張運(yùn)行的截圖:
五子棋.py代碼:
from PyQt5.QtWidgets import * from PyQt5.QtGui import * import sys import MyButton import DoublePlayerGame import SinglePlayerGame from NetConfig import * import NetPlayerGame class Mainwindow(QWidget): def __init__(self,parent = None): super().__init__(parent) self.resize(760,650) self.setWindowTitle("我的五子棋") #設(shè)置窗口圖標(biāo) self.setWindowIcon(QIcon("source/icon.ico")) #設(shè)置背景圖片 p = QPalette(self.palette())#獲得當(dāng)前的調(diào)色板 brush = QBrush(QImage("source/五子棋界面.png")) p.setBrush(QPalette.Background,brush)#設(shè)置調(diào)色版 self.setPalette(p)#給窗口設(shè)置調(diào)色板 self.singlePlayerBtn = MyButton.MyButton('source/人機(jī)對戰(zhàn)_hover.png', 'source/人機(jī)對戰(zhàn)_normal.png', 'source/人機(jī)對戰(zhàn)_press.png', parent=self) self.singlePlayerBtn.move(300,300) self.dancelePlayerBtn = MyButton.MyButton('source/雙人對戰(zhàn)_hover.png', 'source/雙人對戰(zhàn)_normal.png', 'source/雙人對戰(zhàn)_press.png', parent=self) self.dancelePlayerBtn.move(300,400) #self.dancelePlayerBtn.clicked.connect(DoublePlayerGame) self.drawlePlayerBtn = MyButton.MyButton('source/聯(lián)機(jī)對戰(zhàn)_hover.png', 'source/聯(lián)機(jī)對戰(zhàn)_normal.png', 'source/聯(lián)機(jī)對戰(zhàn)_press.png', parent=self) self.drawlePlayerBtn.move(300,500) #綁定開始雙人游戲信號和槽函數(shù) self.dancelePlayerBtn.clicked.connect(self.startDoubliGame) self.singlePlayerBtn.clicked.connect(self.startSingleGame) self.drawlePlayerBtn.clicked.connect(self.startNetGame) def startDoubliGame(self): print("in") #構(gòu)建雙人對戰(zhàn)界面 self.doublePlayerGame = DoublePlayerGame.DoublePlayGame() #綁定返回界面 self.doublePlayerGame.backSignal.connect(self.showStartGame) self.doublePlayerGame.show()#顯示游戲界面 self.close() def startSingleGame(self): self.SingleGame = SinglePlayerGame.SinglePlayerGame() self.SingleGame.backSignal.connect(self.showStartGame2) self.SingleGame.show() self.close() def startNetGame(self): self.netConfig = NetConfigWidget() self.netConfig.exit_signal.connect(self.show) self.netConfig.show() self.netConfig.config_signal.connect(self.receiveNetConfig) self.close() def receiveNetConfig(self,nettype,name,ip,port): ''' 接收網(wǎng)絡(luò)配置信息 ''' print("net config:",nettype,name,ip,port) if nettype == "client": net_object = NetClient(name,ip,port) elif nettype == "server": net_object = NetServer(name,ip,port) else: return self.netPlayerGame = NetPlayerGame.NetPlayerGame(net_object=net_object) self.netPlayerGame.backSignal.connect(self.show) self.close() self.netPlayerGame.show() self.netConfig.hide() '''lbl = QLabel(self) pix = QPixmap("source/人機(jī)大戰(zhàn)_norma.")''' #顯示開始界面 def showStartGame(self): self.show() self.doublePlayerGame.close() def showStartGame2(self): self.show() self.SingleGame.close() if __name__ == "__main__": import cgitb cgitb.enable("text") a = QApplication(sys.argv) m = Mainwindow() m.show() sys.exit(a.exec_())