没有合适的资源?快使用搜索试试~ 我知道了~
首页python实现人机五子棋
资源详情
资源推荐
python实现人机五子棋实现人机五子棋
主要为大家详细介绍了python实现人机五子棋,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣
的小伙伴们可以参考一下
本文实例为大家分享了python实现人机五子棋的具体代码,供大家参考,具体内容如下
图形界面引用PyQt5,还有socket通信。可以局域网对战,可以人机对战,应该存在一些小的bug,但是还没有找出来。希望
读者可以找到
下面附几张运行的截图:
五子棋.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("我的五子棋")
#设置窗口图标
self.setWindowIcon(QIcon("source/icon.ico"))
#设置背景图片
p = QPalette(self.palette())#获得当前的调色板
brush = QBrush(QImage("source/五子棋界面.png"))
p.setBrush(QPalette.Background,brush)#设置调色版
self.setPalette(p)#给窗口设置调色板
self.singlePlayerBtn = MyButton.MyButton('source/人机对战_hover.png',
'source/人机对战_normal.png',
'source/人机对战_press.png',
parent=self)
self.singlePlayerBtn.move(300,300)
self.dancelePlayerBtn = MyButton.MyButton('source/双人对战_hover.png',
'source/双人对战_normal.png',
'source/双人对战_press.png',
parent=self)
self.dancelePlayerBtn.move(300,400)
#self.dancelePlayerBtn.clicked.connect(DoublePlayerGame)
self.drawlePlayerBtn = MyButton.MyButton('source/联机对战_hover.png',
'source/联机对战_normal.png',
'source/联机对战_press.png',
parent=self)
self.drawlePlayerBtn.move(300,500)
#绑定开始双人游戏信号和槽函数
self.dancelePlayerBtn.clicked.connect(self.startDoubliGame)
self.singlePlayerBtn.clicked.connect(self.startSingleGame)
self.drawlePlayerBtn.clicked.connect(self.startNetGame)
def startDoubliGame(self):
print("in")
#构建双人对战界面
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):
'''
接收网络配置信息
'''
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/人机大战_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_())
doubleplayergame.py代码:
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5 import *
import sys
class Chessman(QLabel):
def __init__(self, color = "black",parent = None):
super().__init__(parent)
self.color = color
self.pic = None
if self.color == "black":
self.pic = QPixmap("source/黑子.png")
else:
self.pic = QPixmap("source/白子.png")
self.setPixmap(self.pic)
self.setFixedSize(self.pic.size())#设置棋子大小
self.show()
self.x = 0
self.y = 0
def move(self,a0:QtCore.QPoint):
super().move(a0.x()-15,a0.y()-15)
def setIndex(self,x,y):
self.x = x
self.y = y
import MyButton
class DoublePlayGame(QWidget):
backSignal = pyqtSignal()#返回信号
def __init__(self,parent = None):
super().__init__(parent=parent)
#左上角chessboard[0][0]
#右上角chessboard[0][18]
#左下角chessboard[18][0]
#右下角chessboard[18][18]
#chessboard[行下标][列下标]
self.chessboard = [[None for i in range(19)] for i in range(19)]
#落子棋子颜色
self.turnChessColor = "black"
self.history = []
self.history2 = []
self.is_over = False
#配置背景图
p = QPalette(self.palette())#获得当前的调色板
brush = QBrush(QImage("source/游戏界面.png"))
p.setBrush(QPalette.Background,brush)#设置调色版
self.setPalette(p)#给窗口设置调色板
#设置标题
#self.resize(760,650)
self.setWindowTitle("双人联机")
#设置窗口图标
self.setWindowIcon(QIcon("source/icon.ico"))
#设置窗口大小
self.setFixedSize(QImage("source/游戏界面.png").size())
self.backBtn = MyButton.MyButton('source/返回按钮_hover.png',
'source/返回按钮_normal.png',
'source/返回按钮_press.png',
parent=self)
self.backBtn.move(650,50)
self.startBtn = MyButton.MyButton('source/开始按钮_hover.png',
'source/开始按钮_normal.png',
'source/开始按钮_press.png',
parent=self)
self.startBtn.move(650,300)
self.returnBtn = MyButton.MyButton('source/悔棋按钮_hover.png',
'source/悔棋按钮_normal.png',
'source/悔棋按钮_press.png',
parent=self)
self.returnBtn.move(650,400)
self.loseBtn = MyButton.MyButton('source/认输按钮_hover.png',
'source/认输按钮_normal.png',
'source/认输按钮_press.png',
parent=self)
self.loseBtn.move(650,500)
#绑定返回按钮
self.backBtn.clicked.connect(self.goBack)
self.startBtn.clicked.connect(self.restar)
self.loseBtn.clicked.connect(self.lose)
self.returnBtn.clicked.connect(self.huiback)
self.gameStatu = []
self.focusPoint = QLabel(self)
self.focusPoint.setPixmap(QPixmap("source/标识.png"))
def goBack(self):
self.backSignal.emit()
self.close()
def closeEvent(self, a0: QtGui.QCloseEvent):
剩余16页未读,继续阅读
weixin_38735790
- 粉丝: 4
- 资源: 899
上传资源 快速赚钱
- 我的内容管理 收起
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
会员权益专享
最新资源
- zigbee-cluster-library-specification
- JSBSim Reference Manual
- c++校园超市商品信息管理系统课程设计说明书(含源代码) (2).pdf
- 建筑供配电系统相关课件.pptx
- 企业管理规章制度及管理模式.doc
- vb打开摄像头.doc
- 云计算-可信计算中认证协议改进方案.pdf
- [详细完整版]单片机编程4.ppt
- c语言常用算法.pdf
- c++经典程序代码大全.pdf
- 单片机数字时钟资料.doc
- 11项目管理前沿1.0.pptx
- 基于ssm的“魅力”繁峙宣传网站的设计与实现论文.doc
- 智慧交通综合解决方案.pptx
- 建筑防潮设计-PowerPointPresentati.pptx
- SPC统计过程控制程序.pptx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功