用Python制作五子棋人机对弈(人工智障版和升级AI版)

您所在的位置:网站首页 人工智障对话截图 用Python制作五子棋人机对弈(人工智障版和升级AI版)

用Python制作五子棋人机对弈(人工智障版和升级AI版)

2024-07-13 06:02| 来源: 网络整理| 查看: 265

智障版截图:

在这里插入图片描述

智能版截图:

在这里插入图片描述

在这里插入图片描述

可能遇到的问题:

No module named ‘pyqt5‘解决办法

智障版源码: 背景:

muzm.jpg

在这里插入图片描述

window.py 1 from PyQt5.QtWidgets import QMainWindow, QMessageBox 2 from PyQt5.QtGui import QPainter, QPen, QColor, QPalette, QBrush, QPixmap, QRadialGradient 3 from PyQt5.QtCore import Qt, QPoint, QTimer 4 import traceback 5 from game import Gomoku 6 from corner_widget import CornerWidget 7 8 9 def run_with_exc(f): 10 """游戏运行出现错误时,用messagebox把错误信息显示出来""" 11 12 def call(window, *args, **kwargs): 13 try: 14 return f(window, *args, **kwargs) 15 except Exception: 16 exc_info = traceback.format_exc() 17 QMessageBox.about(window, '错误信息', exc_info) 18 return call 19 20 21 class GomokuWindow(QMainWindow): 22 23 def __init__(self): 24 super().__init__() 25 self.init_ui() # 初始化游戏界面 26 self.g = Gomoku() # 初始化游戏内容 27 28 self.last_pos = (-1, -1) 29 self.res = 0 # 记录那边获得了胜利 30 self.operate_status = 0 # 游戏操作状态。0为游戏中(可操作),1为游戏结束闪烁过程中(不可操作) 31 32 def init_ui(self): 33 """初始化游戏界面""" 34 # 1. 确定游戏界面的标题,大小和背景颜色 35 self.setObjectName('MainWindow') 36 self.setWindowTitle('五子棋') 37 self.setFixedSize(650, 650) 38 # self.setStyleSheet('#MainWindow{background-color: green}') 39 palette = QPalette() 40 palette.setBrush(QPalette.Window, QBrush(QPixmap('imgs/muzm.jpg'))) 41 self.setPalette(palette) 42 # 2. 开启鼠标位置的追踪。并在鼠标位置移动时,使用特殊符号标记当前的位置 43 self.setMouseTracking(True) 44 # 3. 鼠标位置移动时,对鼠标位置的特殊标记 45 self.corner_widget = CornerWidget(self) 46 self.corner_widget.repaint() 47 self.corner_widget.hide() 48 # 4. 游戏结束时闪烁的定时器 49 self.end_timer = QTimer(self) 50 self.end_timer.timeout.connect(self.end_flash) 51 self.flash_cnt = 0 # 游戏结束之前闪烁了多少次 52 self.flash_pieces = ((-1, -1), ) # 哪些棋子需要闪烁 53 # 5. 显示初始化的游戏界面 54 self.show() 55 56 @run_with_exc 57 def paintEvent(self, e): 58 """绘制游戏内容""" 59 60 def draw_map(): 61 """绘制棋盘""" 62 qp.setPen(QPen(QColor(0, 0, 0), 2, Qt.SolidLine)) # 棋盘的颜色为黑色 63 # 绘制横线 64 for x in range(15): 65 qp.drawLine(40 * (x + 1), 40, 40 * (x + 1), 600) 66 # 绘制竖线 67 for y in range(15): 68 qp.drawLine(40, 40 * (y + 1), 600, 40 * (y + 1)) 69 # 绘制棋盘中的黑点 70 qp.setBrush(QColor(0, 0, 0)) 71 key_points = [(4, 4), (12, 4), (4, 12), (12, 12), (8, 8)] 72 for t in key_points: 73 qp.drawEllipse(QPoint(40 * t[0], 40 * t[1]), 5, 5) 74 75 def draw_pieces(): 76 """绘制棋子""" 77 # 绘制黑棋子 78 qp.setPen(QPen(QColor(0, 0, 0), 1, Qt.SolidLine)) 79 # qp.setBrush(QColor(0, 0, 0)) 80 for x in range(15): 81 for y in range(15): 82 if self.g.g_map[x][y] == 1: 83 if self.flash_cnt % 2 == 1 and (x, y) in self.flash_pieces: 84 continue 85 radial = QRadialGradient(40 * (x + 1), 40 * (y + 1), 15, 40 * x + 35, 40 * y + 35) # 棋子的渐变效果 86 radial.setColorAt(0, QColor(96, 96, 96)) 87 radial.setColorAt(1, QColor(0, 0, 0)) 88 qp.setBrush(QBrush(radial)) 89 qp.drawEllipse(QPoint(40 * (x + 1), 40 * (y + 1)), 15, 15) 90 # 绘制白棋子 91 qp.setPen(QPen(QColor(160, 160, 160), 1, Qt.SolidLine)) 92 # qp.setBrush(QColor(255, 255, 255)) 93 for x in range(15): 94 for y in range(15): 95 if self.g.g_map[x][y] == 2: 96 if self.flash_cnt % 2 == 1 and (x, y) in self.flash_pieces: 97 continue 98 radial = QRadialGradient(40 * (x + 1), 40 * (y + 1), 15, 40 * x + 35, 40 * y + 35) # 棋子的渐变效果 99 radial.setColorAt(0, QColor(255, 255, 255)) 100 radial.setColorAt(1, QColor(160, 160, 160)) 101 qp.setBrush(QBrush(radial)) 102 qp.drawEllipse(QPoint(40 * (x + 1), 40 * (y + 1)), 15, 15) 103 104 if hasattr(self, 'g'): # 游戏还没开始的话,就不用画了 105 qp = QPainter() 106 qp.begin(self) 107 draw_map() # 绘制棋盘 108 draw_pieces() # 绘制棋子 109 qp.end() 110 111 @run_with_exc 112 def mouseMoveEvent(self, e): 113 # 1. 首先判断鼠标位置对应棋盘中的哪一个格子 114 mouse_x = e.windowPos().x() 115 mouse_y = e.windowPos().y() 116 if 25


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3