要写出一个五子棋游戏,我们最先要解决的,就是如何下子,如何判断已经五子连珠,而不是如何绘制画面,因此我们先确定棋盘

五子棋采用15*15的棋盘,因此,我们可以使用二维列表来创建一个棋盘,不妨认为0表示未放置棋子,1表示放置白子,2表示放置黑子。

显而易见可以创建列表,注意不能使用*来复制列表
self.chess_board = [[0 for i in range(15)] for i in range(15)]
下棋的步骤十分好做,只需要找到对应的索引进行赋值即可,下一步应该解决如何判断五子连珠的问题。

每当我们落子结束后,应该判断是否已经完成五子连珠。对于刚放置的一颗棋子而言,可能的情况大致分为四种:

1.水平
2.斜向右下
3.竖直
4.斜向右上

要判断是否已经连珠成功,我们以刚放置的棋子为起点,先向前遍历4个棋子,并计算相同棋子的个数,一旦遇到不同的棋子,就停止,然后从起点向后遍历4个棋子,直到全部遍历完成或者棋子总数已经达到5个,就可以返回。我们只需要注意如何获得棋子的前后棋子以及棋盘的边界问题,棋子不可能超出棋盘,因此被遍历的棋子也不能超出棋盘。

以水平为例,可以得到代码
def judge_1(self,x:int,y:int) -> bool: count = 1 if self.chess_board[x][y] != 0: for i in range(1,5): if y - i >= 0: if self.chess_board[x][y] == self.chess_board[x][y-i]: print(x,y-i) count += 1 else: break else: break for i in range(1,5): if y + i <=14: if self.chess_board[x][y] == self.chess_board[x][y+i]: print(x,y+i) count += 1 else: break else: break if count == 5: return True return False
以相似的步骤完成其余三种判断,就已经完成了五子棋游戏的核心要素了,剩下的就需要交给PyQt5来完成游戏的绘制来完善游戏了。

我们创建一个类来继承QWidget类,创建一个窗口,之后我们需要创建几个属性来完成储存我们的数据信息
`#棋子的坐标
self.x = -1
self.y = -1

区分玩jia

开始标签

self.flag = False

储存已经下好的白子

self.white_chess = []

储存已经下好的黑子

self.black_chess = []`
我们已经可以开始绘制棋盘,在Qt5中,如果我们需要进行绘制,我们应该重写paintEvent方法,这个方法会由程序自动调用执行。创建一个QPainter对象,将需要绘制的内容用begin与end方法包裹起来,就可以完成绘制。

Python客栈送红bao、纸质书

我们用drawLine方法来绘制线条,用drawEllipse方法来绘制棋子,使用setPen来更改线条样式,setBrush来更改棋子样式。

得到代码(本段代码有参考他人代码,这是我第一次接触Qt的绘制)

——————————GUI中的x轴竖直向下,y轴水平向右,因此绘制棋子时的x与y需要颠倒———————-
`#绘制棋盘与棋子
def paintEvent(self, e) -> None:
qp = QPainter()
qp.begin(self)
qp.fillRect(self.rect(), QColor(“light blue”))
qp.drawRect(self.rect())
qp.setBackground(QColor(“yellow”))
qp.setPen(QPen(QColor(0, 0, 0), 2, Qt.SolidLine))
for i in range(15):
qp.drawLine(QPoint(30, 30 + 30 i), QPoint(450, 30 + 30 i))
for i in range(15):
qp.drawLine(QPoint(30 + 30 i, 30), QPoint(30 + 30 i, 450))
qp.setBrush(QColor(0, 0, 0))
key_points = [(3, 3), (11, 3), (3, 11), (11, 11), (7, 7)]
if len(self.black_chess) != 0:
for t in self.black_chess:

  1. #画黑子
  2. qp.drawEllipse(QPoint(30 + 30 * t[1], 30 + 30 * t[0]), 6, 6)
  3. for t in key_points:
  4. #棋盘的5个定点
  5. qp.drawEllipse(QPoint(30 + 30 * t[0], 30 + 30 * t[1]), 3, 3)
  6. qp.setBrush(QColor(255,255,255))
  7. if len(self.white_chess) != 0:
  8. for t in self.white_chess:
  9. #画白子
  10. qp.drawEllipse(QPoint(30 + 30 * t[1], 30 + 30 * t[0]), 6, 6)
  11. qp.end()`

另一个需要在GUI中解决的问题就是,如何获取要下的棋子的坐标?我们可以通过重写鼠标事件来解决,重写单机事件mousePressEvent,并修改棋子的x坐标与y坐标即可,另外,用户不可能每次都恰巧点到我们规定的坐标点上,因此需要给出一个大致范围判断,这里我的方式是先获取坐标,然后根据坐标找到距离最近的点
def mousePressEvent(self, e) -> None: if e.buttons() == QtCore.Qt.LeftButton: if e.x() > 15 and e.x() < 465 and e.y() > 15 and e.y() < 465: x = e.x()/30 - e.x()//30 y = e.y()/30 - e.y()//30 self.y = (e.x()-30)//30 if x < 0.5 else (e.x()-30)//30 + 1 self.x = (e.y()-30)//30 if y < 0.5 else (e.y()-30)//30 + 1 if self.flag: print(self.x,self.y) if self.player % 2 == 1: if goBang.put_white_chess(self.x,self.y): self.player += 1 print('黑子行动') else: print('白子行动') if goBang.judge(self.x,self.y): msg_box = QMessageBox(QMessageBox.Information, '提示', '白子获胜!') msg_box.exec_() else: if goBang.put_black_chess(self.x,self.y): self.player += 1 print('白子行动') else: print('黑子行动') if goBang.judge(self.x,self.y): msg_box = QMessageBox(QMessageBox.Information, '提示', '黑子获胜!') msg_box.exec_()
每当游戏完成,我们应该可以清空棋盘,也就是将所有储存数据的变量都重新初始化再重绘棋盘
#清除棋盘,重开游戏 def clear(self) -> None: self.x = -1 self.y = -1 self.player = 0 self.flag = False self.white_chess = [] self.black_chess = [] self.chess_board = [[0 for i in range(15)] for i in range(15)] self.update()
这样就大致结束了!!

下面是全部代码:
`from PyQt5 import
from PyQt5 import QtCore
from PyQt5.QtWidgets import

from PyQt5.QtGui import
from PyQt5.QtCore import

import sys

class GoBang(QWidget):

  1. #初始化棋盘
  2. def __init__(self):
  3. super().__init__()
  4. self.setWindowTitle('五子棋Hi~ o(* ̄▽ ̄*)ブ')
  5. self.x = -1
  6. self.y = -1
  7. #区分玩jia
  8. self.player = 0
  9. #开始标签
  10. self.flag = False
  11. #储存已经下好的白子
  12. self.white_chess = []
  13. #储存已经下好的黑子
  14. self.black_chess = []
  15. self.setFixedSize(800,600)
  16. self.chess_board = [[0 for i in range(15)] for i in range(15)]
  17. btn1 = QPushButton('开始',self)
  18. btn1.setGeometry(500,100,50,30)
  19. btn1.clicked.connect(self.setFlag)
  20. btn2 = QPushButton('重开',self)
  21. btn2.setGeometry(550,100,50,30)
  22. btn2.clicked.connect(self.clear)
  23. self.show()
  24. #绘制棋盘与棋子
  25. def paintEvent(self, e) -> None:
  26. qp = QPainter()
  27. qp.begin(self)
  28. qp.fillRect(self.rect(), QColor("light blue"))
  29. qp.drawRect(self.rect())
  30. qp.setBackground(QColor("yellow"))
  31. qp.setPen(QPen(QColor(0, 0, 0), 2, Qt.SolidLine))
  32. for i in range(15):
  33. qp.drawLine(QPoint(30, 30 + 30 * i), QPoint(450, 30 + 30 * i))
  34. for i in range(15):
  35. qp.drawLine(QPoint(30 + 30 * i, 30), QPoint(30 + 30 * i, 450))
  36. qp.setBrush(QColor(0, 0, 0))
  37. key_points = [(3, 3), (11, 3), (3, 11), (11, 11), (7, 7)]
  38. if len(self.black_chess) != 0:
  39. for t in self.black_chess:
  40. #画黑子
  41. qp.drawEllipse(QPoint(30 + 30 * t[1], 30 + 30 * t[0]), 6, 6)
  42. for t in key_points:
  43. #棋盘的5个定点
  44. qp.drawEllipse(QPoint(30 + 30 * t[0], 30 + 30 * t[1]), 3, 3)
  45. qp.setBrush(QColor(255,255,255))
  46. if len(self.white_chess) != 0:
  47. for t in self.white_chess:
  48. #画白子
  49. qp.drawEllipse(QPoint(30 + 30 * t[1], 30 + 30 * t[0]), 6, 6)
  50. qp.end()
  51. #更改标签,开始游戏
  52. def setFlag(self) -> None:
  53. self.flag = True
  54. def mousePressEvent(self, e) -> None:
  55. if e.buttons() == QtCore.Qt.LeftButton:
  56. if e.x() > 15 and e.x() < 465 and e.y() > 15 and e.y() < 465:
  57. x = e.x()/30 - e.x()//30
  58. y = e.y()/30 - e.y()//30
  59. self.y = (e.x()-30)//30 if x < 0.5 else (e.x()-30)//30 + 1
  60. self.x = (e.y()-30)//30 if y < 0.5 else (e.y()-30)//30 + 1
  61. if self.flag:
  62. print(self.x,self.y)
  63. if self.player % 2 == 1:
  64. if goBang.put_white_chess(self.x,self.y):
  65. self.player += 1
  66. print('黑子行动')
  67. else:
  68. print('白子行动')
  69. if goBang.judge(self.x,self.y):
  70. msg_box = QMessageBox(QMessageBox.Information, '提示', '白子获胜!')
  71. msg_box.exec_()
  72. else:
  73. if goBang.put_black_chess(self.x,self.y):
  74. self.player += 1
  75. print('白子行动')
  76. else:
  77. print('黑子行动')
  78. if goBang.judge(self.x,self.y):
  79. msg_box = QMessageBox(QMessageBox.Information, '提示', '黑子获胜!')
  80. msg_box.exec_()
  81. #下白子
  82. def put_white_chess(self,x:int,y:int) -> bool:
  83. if self.chess_board[x][y] != 0:
  84. msg_box = QMessageBox(QMessageBox.Information, '提示', '这个位置已经有棋子了!')
  85. msg_box.exec_()
  86. return False
  87. else:
  88. self.chess_board[x][y] = 1
  89. self.white_chess.append((x,y))
  90. self.update()
  91. return True
  92. #下黑子
  93. def put_black_chess(self,x:int,y:int) -> bool:
  94. if self.chess_board[x][y] != 0:
  95. msg_box = QMessageBox(QMessageBox.Information, '提示', '这个位置已经有棋子了!')
  96. msg_box.exec_()
  97. return False
  98. else:
  99. self.chess_board[x][y] = 2
  100. self.black_chess.append((x,y))
  101. self.update()
  102. return True
  103. #清除棋盘,重开游戏
  104. def clear(self) -> None:
  105. self.x = -1
  106. self.y = -1
  107. self.player = 0
  108. self.flag = False
  109. self.white_chess = []
  110. self.black_chess = []
  111. self.chess_board = [[0 for i in range(15)] for i in range(15)]
  112. self.update()
  113. #判断是否已经五子连珠
  114. def judge(self,x:int,y:int) -> bool:
  115. if self.judge_1(x,y) or self.judge_2(x,y) or self.judge_3(x,y) or self.judge_4(x,y):
  116. return True
  117. return False
  118. #判断横线
  119. def judge_1(self,x:int,y:int) -> bool:
  120. count = 1
  121. if self.chess_board[x][y] != 0:
  122. for i in range(1,5):
  123. if y - i >= 0:
  124. if self.chess_board[x][y] == self.chess_board[x][y-i]:
  125. print(x,y-i)
  126. count += 1
  127. else:
  128. break
  129. else:
  130. break
  131. for i in range(1,5):
  132. if y + i <=14:
  133. if self.chess_board[x][y] == self.chess_board[x][y+i]:
  134. print(x,y+i)
  135. count += 1
  136. else:
  137. break
  138. else:
  139. break
  140. if count == 5:
  141. return True
  142. return False
  143. #判断右下线
  144. def judge_2(self,x:int,y:int) -> bool:
  145. count = 1
  146. if self.chess_board[x][y] != 0:
  147. for i in range(1,5):
  148. if x-i >= 0 and y - i >= 0:
  149. if self.chess_board[x][y] == self.chess_board[x-i][y-i]:
  150. print(x-i,y-i)
  151. count += 1
  152. else:
  153. break
  154. else:
  155. break
  156. for i in range(1,5):
  157. if x + i <= 14 and y + i <= 14:
  158. if self.chess_board[x][y] == self.chess_board[x+i][y+i]:
  159. print(x+i,y+i)
  160. count += 1
  161. else:
  162. break
  163. else:
  164. break
  165. if count == 5:
  166. return True
  167. return False
  168. #判断竖线
  169. def judge_3(self,x:int,y:int) -> bool:
  170. count = 1
  171. if self.chess_board[x][y] != 0:
  172. for i in range(1,5):
  173. if x - i >= 0:
  174. if self.chess_board[x][y] == self.chess_board[x-i][y]:
  175. print(x-i,y)
  176. count += 1
  177. else:
  178. break
  179. else:
  180. break
  181. for i in range(1,5):
  182. if x + i <= 14:
  183. if self.chess_board[x][y] == self.chess_board[x+i][y]:
  184. print(x+i,y)
  185. count += 1
  186. else:
  187. break
  188. else:
  189. break
  190. if count == 5:
  191. return True
  192. return False
  193. #判断右上线
  194. def judge_4(self,x:int,y:int) -> bool:
  195. count = 1
  196. if self.chess_board[x][y] != 0:
  197. for i in range(1,5):
  198. if x - i >= 0 and y + i <= 14:
  199. if self.chess_board[x][y] == self.chess_board[x-i][y+i]:
  200. print(x-i,y+i)
  201. count += 1
  202. else:
  203. break
  204. else:
  205. break
  206. for i in range(1,5):
  207. if x + i <= 14 and y - i >= 0:
  208. if self.chess_board[x][y] == self.chess_board[x+i][y-i]:
  209. print(x+i,y-i)
  210. count += 1
  211. else:
  212. break
  213. else:
  214. break
  215. if count == 5:
  216. return True
  217. return False

程序入口

if name == ‘main‘:
app = QApplication(sys.argv)
goBang = GoBang()
sys.exit(app.exec_())`

更多相关文章

  1. java实现五子棋大战
  2. Android之自定义五子棋View
  3. Android开发-一个简单的五子棋游戏
  4. Android五子连珠
  5. Android(安卓)UI详解之动态布局
  6. Android开发-一个简单的五子棋游戏
  7. 学习android, 自己编的一个黑白棋游戏 (提供源代码下载)
  8. android ViewGroup中的LayoutParams的理解和说明
  9. Android开发:LayoutParams的用法

随机推荐

  1. Android 进度条
  2. How Android Draws Views
  3. android图片放大 缩小 旋转
  4. Layout1.9
  5. Android usb 驱动
  6. Android 查看本机外网IP
  7. AndroidMenifest.xml(Android清单文件)内
  8. Android调用摄像头闪退
  9. Android官方资料--A/B System Updates
  10. c#开发android时layout.axml没有智能提示