Python五子棋小游戏源代码,支持人机对战和局域网对战两模式,程序运行截图:
在这里插入图片描述
核心程序代码
WuZi.py

'''
Function:
	五子棋小游戏-支持人机和局域网对战
Author:
	Charles
微信公众号:
	Python代码大全
'''
import sys
import cfg
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from modules.misc.Buttons import *
from modules.ai.playWithAI import *
from modules.online.playOnline import *


'''游戏开始界面'''
class gameStartUI(QWidget):
	def __init__(self, parent=None, **kwargs):
		super(gameStartUI, self).__init__(parent)
		self.setFixedSize(760, 650)
		self.setWindowTitle('五子棋-微信公众号: Python代码大全')
		self.setWindowIcon(QIcon(cfg.ICON_FILEPATH))
		# 背景图片
		palette = QPalette()
		palette.setBrush(self.backgroundRole(), QBrush(QPixmap(cfg.BACKGROUND_IMAGEPATHS.get('bg_start'))))
		self.setPalette(palette)
		# 按钮
		# --人机对战
		self.ai_button = PushButton(cfg.BUTTON_IMAGEPATHS.get('ai'), self)
		self.ai_button.move(250, 200)
		self.ai_button.show()
		self.ai_button.click_signal.connect(self.playWithAI)
		# --联机对战
		self.online_button = PushButton(cfg.BUTTON_IMAGEPATHS.get('online'), self)
		self.online_button.move(250, 350)
		self.online_button.show()
		self.online_button.click_signal.connect(self.playOnline)
	'''人机对战'''
	def playWithAI(self):
		self.close()
		self.gaming_ui = playWithAIUI(cfg)
		self.gaming_ui.exit_signal.connect(lambda: sys.exit())
		self.gaming_ui.back_signal.connect(self.show)
		self.gaming_ui.show()
	'''联机对战'''
	def playOnline(self):
		self.close()
		self.gaming_ui = playOnlineUI(cfg, self)
		self.gaming_ui.show()


'''run'''
if __name__ == '__main__':
	app = QApplication(sys.argv)
	handle = gameStartUI()
	font = QFont()
	font.setPointSize(12)
	handle.setFont(font)
	handle.show()
	sys.exit(app.exec_())

cfg.py

'''config file'''
import os


# 图标路径
ICON_FILEPATH = os.path.join(os.getcwd(), 'resources/images/icon/icon.ico')
# 背景图片路径
BACKGROUND_IMAGEPATHS = {
                            'bg_game': os.path.join(os.getcwd(), 'resources/images/bg/bg_game.png'),
                            'bg_start': os.path.join(os.getcwd(), 'resources/images/bg/bg_start.png')
                        }
# 按钮图片路径
BUTTON_IMAGEPATHS = {
                        'online': [os.path.join(os.getcwd(), 'resources/images/buttons/online_0.png'),
                                   os.path.join(os.getcwd(), 'resources/images/buttons/online_1.png'),
                                   os.path.join(os.getcwd(), 'resources/images/buttons/online_2.png')],
                        'ai': [os.path.join(os.getcwd(), 'resources/images/buttons/ai_0.png'),
                               os.path.join(os.getcwd(), 'resources/images/buttons/ai_1.png'),
                               os.path.join(os.getcwd(), 'resources/images/buttons/ai_2.png')],
                        'home': [os.path.join(os.getcwd(), 'resources/images/buttons/home_0.png'),
                                 os.path.join(os.getcwd(), 'resources/images/buttons/home_1.png'),
                                 os.path.join(os.getcwd(), 'resources/images/buttons/home_2.png')],
                        'givein': [os.path.join(os.getcwd(), 'resources/images/buttons/givein_0.png'),
                                   os.path.join(os.getcwd(), 'resources/images/buttons/givein_1.png'),
                                   os.path.join(os.getcwd(), 'resources/images/buttons/givein_2.png')],
                        'regret': [os.path.join(os.getcwd(), 'resources/images/buttons/regret_0.png'),
                                   os.path.join(os.getcwd(), 'resources/images/buttons/regret_1.png'),
                                   os.path.join(os.getcwd(), 'resources/images/buttons/regret_2.png')],
                        'startgame': [os.path.join(os.getcwd(), 'resources/images/buttons/startgame_0.png'),
                                      os.path.join(os.getcwd(), 'resources/images/buttons/startgame_1.png'),
                                      os.path.join(os.getcwd(), 'resources/images/buttons/startgame_2.png')],
                        'urge': [os.path.join(os.getcwd(), 'resources/images/buttons/urge_0.png'),
                                 os.path.join(os.getcwd(), 'resources/images/buttons/urge_1.png'),
                                 os.path.join(os.getcwd(), 'resources/images/buttons/urge_2.png')]
                    }
# 显示胜利图片路径
WIN_IMAGEPATHS = {
                    'black': os.path.join(os.getcwd(), 'resources/images/win/black_win.png'),
                    'white': os.path.join(os.getcwd(), 'resources/images/win/white_win.png'),
                    'draw': os.path.join(os.getcwd(), 'resources/images/win/draw.png')
                }
# 棋子图片路径
CHESSMAN_IMAGEPATHS = {
                        'black': os.path.join(os.getcwd(), 'resources/images/chessman/black.png'),
                        'white': os.path.join(os.getcwd(), 'resources/images/chessman/white.png'),
                        'sign': os.path.join(os.getcwd(), 'resources/images/chessman/sign.png'),
                    }
# 音效
SOUNDS_PATHS = {
                    'drop': os.path.join(os.getcwd(), 'resources/audios/drop.wav'),
                    'urge': os.path.join(os.getcwd(), 'resources/audios/urge.wav')
                }
# 端口号(联机对战时使用)
PORT = 3333

PlayWithAI.py

'''
Function:
    定义人机对战
Author:
    Charles
微信公众号:
    Python代码大全
'''
import pygame
from PyQt5 import QtCore
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from itertools import product
from modules.misc.utils import *
from modules.misc.Buttons import *
from modules.misc.Chessman import *
from modules.ai.aiGobang import aiGobang


'''人机对战'''
class playWithAIUI(QWidget):
    back_signal = pyqtSignal()
    exit_signal = pyqtSignal()
    send_back_signal = False
    def __init__(self, cfg, parent=None, **kwargs):
        super(playWithAIUI, self).__init__(parent)
        self.cfg = cfg
        self.setFixedSize(760, 650)
        self.setWindowTitle('五子棋-微信公众号: Charles的皮卡丘')
        self.setWindowIcon(QIcon(cfg.ICON_FILEPATH))
        # 背景图片
        palette = QPalette()
        palette.setBrush(self.backgroundRole(), QBrush(QPixmap(cfg.BACKGROUND_IMAGEPATHS.get('bg_game'))))
        self.setPalette(palette)
        # 按钮
        self.home_button = PushButton(cfg.BUTTON_IMAGEPATHS.get('home'), self)
        self.home_button.click_signal.connect(self.goHome)
        self.home_button.move(680, 10)
        self.startgame_button = PushButton(cfg.BUTTON_IMAGEPATHS.get('startgame'), self)
        self.startgame_button.click_signal.connect(self.startgame)
        self.startgame_button.move(640, 240)
        self.regret_button = PushButton(cfg.BUTTON_IMAGEPATHS.get('regret'), self)
        self.regret_button.click_signal.connect(self.regret)
        self.regret_button.move(640, 310)
        self.givein_button = PushButton(cfg.BUTTON_IMAGEPATHS.get('givein'), self)
        self.givein_button.click_signal.connect(self.givein)
        self.givein_button.move(640, 380)
        # 落子标志
        self.chessman_sign = QLabel(self)
        sign = QPixmap(cfg.CHESSMAN_IMAGEPATHS.get('sign'))
        self.chessman_sign.setPixmap(sign)
        self.chessman_sign.setFixedSize(sign.size())
        self.chessman_sign.show()
        self.chessman_sign.hide()
        # 棋盘(19*19矩阵)
        self.chessboard = [[None for i in range(19)] for _ in range(19)]
        # 历史记录(悔棋用)
        self.history_record = []
        # 是否在游戏中
        self.is_gaming = True
        # 胜利方
        self.winner = None
        self.winner_info_label = None
        # 颜色分配and目前轮到谁落子
        self.player_color = 'white'
        self.ai_color = 'black'
        self.whoseround = self.player_color
        # 实例化ai
        self.ai_player = aiGobang(self.ai_color, self.player_color)
        # 落子声音加载
        pygame.mixer.init()
        self.drop_sound = pygame.mixer.Sound(cfg.SOUNDS_PATHS.get('drop'))
    '''鼠标左键点击事件-玩家回合'''
    def mousePressEvent(self, event):
        if (event.buttons() != QtCore.Qt.LeftButton) or (self.winner is not None) or (self.whoseround != self.player_color) or (not self.is_gaming):
            return
        # 保证只在棋盘范围内响应
        if event.x() >= 50 and event.x() <= 50 + 30 * 18 + 14 and event.y() >= 50 and event.y() <= 50 + 30 * 18 + 14:
            pos = Pixel2Chesspos(event)
            # 保证落子的地方本来没有人落子
            if self.chessboard[pos[0]][pos[1]]:
                return
            # 实例化一个棋子并显示
            c = Chessman(self.cfg.CHESSMAN_IMAGEPATHS.get(self.whoseround), self)
            c.move(event.pos())
            c.show()
            self.chessboard[pos[0]][pos[1]] = c
            # 落子声音响起
            self.drop_sound.play()
            # 最后落子位置标志对落子位置进行跟随
            self.chessman_sign.show()
            self.chessman_sign.move(c.pos())
            self.chessman_sign.raise_()
            # 记录这次落子
            self.history_record.append([*pos, self.whoseround])
            # 是否胜利了
            self.winner = checkWin(self.chessboard)
            if self.winner:
                self.showGameEndInfo()
                return
            # 切换回合方(其实就是改颜色)
            self.nextRound()
    '''鼠标左键释放操作-调用电脑回合'''
    def mouseReleaseEvent(self, event):
        if (self.winner is not None) or (self.whoseround != self.ai_color) or (not self.is_gaming):
            return
        self.aiAct()
    '''电脑自动下-AI回合'''
    def aiAct(self):
        if (self.winner is not None) or (self.whoseround == self.player_color) or (not self.is_gaming):
            return
        next_pos = self.ai_player.act(self.history_record)
        # 实例化一个棋子并显示
        c = Chessman(self.cfg.CHESSMAN_IMAGEPATHS.get(self.whoseround), self)
        c.move(QPoint(*Chesspos2Pixel(next_pos)))
        c.show()
        self.chessboard[next_pos[0]][next_pos[1]] = c
        # 落子声音响起
        self.drop_sound.play()
        # 最后落子位置标志对落子位置进行跟随
        self.chessman_sign.show()
        self.chessman_sign.move(c.pos())
        self.chessman_sign.raise_()
        # 记录这次落子
        self.history_record.append([*next_pos, self.whoseround])
        # 是否胜利了
        self.winner = checkWin(self.chessboard)
        if self.winner:
            self.showGameEndInfo()
            return
        # 切换回合方(其实就是改颜色)
        self.nextRound()
    '''改变落子方'''
    def nextRound(self):
        self.whoseround = self.player_color if self.whoseround == self.ai_color else self.ai_color
    '''显示游戏结束结果'''
    def showGameEndInfo(self):
        self.is_gaming = False
        info_img = QPixmap(self.cfg.WIN_IMAGEPATHS.get(self.winner))
        self.winner_info_label = QLabel(self)
        self.winner_info_label.setPixmap(info_img)
        self.winner_info_label.resize(info_img.size())
        self.winner_info_label.move(50, 50)
        self.winner_info_label.show()
    '''认输'''
    def givein(self):
        if self.is_gaming and (self.winner is None) and (self.whoseround == self.player_color):
            self.winner = self.ai_color
            self.showGameEndInfo()
    '''悔棋-只有我方回合的时候可以悔棋'''
    def regret(self):
        if (self.winner is not None) or (len(self.history_record) == 0) or (not self.is_gaming) and (self.whoseround != self.player_color):
            return
        for _ in range(2):
            pre_round = self.history_record.pop(-1)
            self.chessboard[pre_round[0]][pre_round[1]].close()
            self.chessboard[pre_round[0]][pre_round[1]] = None
        self.chessman_sign.hide()
    '''开始游戏-之前的对弈必须已经结束才行'''
    def startgame(self):
        if self.is_gaming:
            return
        self.is_gaming = True
        self.whoseround = self.player_color
        for i, j in product(range(19), range(19)):
            if self.chessboard[i][j]:
                self.chessboard[i][j].close()
                self.chessboard[i][j] = None
        self.winner = None
        self.winner_info_label.close()
        self.winner_info_label = None
        self.history_record.clear()
        self.chessman_sign.hide()
    '''关闭窗口事件'''
    def closeEvent(self, event):
        if not self.send_back_signal:
            self.exit_signal.emit()
    '''返回游戏主页面'''
    def goHome(self):
        self.send_back_signal = True
        self.close()
        self.back_signal.emit()

完整程序代码下载:Python五子棋小游戏源代码,支持人机对战和局域网对战两模式
更多Python源代码,请关注公众号:Python代码大全
在这里插入图片描述

Logo

华为云1024程序员节送福利,参与活动赢单人4000元礼包,更有热门技术干货免费学习

更多推荐