zl程序教程

您现在的位置是:首页 >  后端

当前栏目

情人节——与她对弈五子棋(Python实现)

Python 实现 五子棋 情人节
2023-09-14 09:05:23 时间

目录

1 珍惜今夕

2 我的天使

3 结果展现——与卿对弈一局五子棋

4 Python实现


1 珍惜今夕

情人节到了,愿岁月可回首,情深不负共白头。琢磨着给女朋友送一份礼物,送什么好呢,和她下象棋我让得太明显了。所以还是送她一副五子棋。用Python实现。

2 我的天使

最好的那个天使,我最熟悉的字是你的名字,我们会有大大的房子,你会送我一首小诗。”为你亲手搭建一所房子,大概是最美的誓言了。把所有的细节都照顾到,任谁收到这样的一份礼物,都会感动的吧。送她一所梦的房子,陪伴,是最长情的告白,承诺,不是说说而已。

3 结果展现——与卿对弈一局五子棋

胜利! 

4 Python实现


#===========导入相关包===================
import sys
import cfg
from modules import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *


#=============游戏开始界面===============
class gameStartUI(QWidget):
    def __init__(self, parent=None, **kwargs):
        super(gameStartUI, self).__init__(parent)
        self.setFixedSize(760, 650)
        self.setWindowTitle('五子棋 —— 与女朋友对弈')
        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()


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

 cfg库:

'''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