千家信息网

怎么用Python编写简易的成语接龙游戏

发表于:2025-01-20 作者:千家信息网编辑
千家信息网最后更新 2025年01月20日,本文小编为大家详细介绍"怎么用Python编写简易的成语接龙游戏",内容详细,步骤清晰,细节处理妥当,希望这篇"怎么用Python编写简易的成语接龙游戏"文章能帮助大家解决疑惑,下面跟着小编的思路慢慢
千家信息网最后更新 2025年01月20日怎么用Python编写简易的成语接龙游戏

本文小编为大家详细介绍"怎么用Python编写简易的成语接龙游戏",内容详细,步骤清晰,细节处理妥当,希望这篇"怎么用Python编写简易的成语接龙游戏"文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。

    1.游戏规则

    下面我简单说说「成语接龙」游戏的基本规则,你很熟悉可以跳过这段,不熟悉就仔细看看:

    ①用四字成语的最后一个字起头,接出下一句成语,由此不断延伸;

    ②成语尾首相接的字,可以是同一个字,也可以是同音的字;

    ③一场游戏中,不应出现重复成语。

    【举例】

    「海枯石烂」→「烂醉如泥」→「泥牛入海」

    ×不能接「海枯石烂」(出现重复)

    √可以接「海纳百川」

    2.正式敲代码

    2.1 模块导入

    import osimport sysimport randomfrom PyQt5.QtGui import *from PyQt5.QtWidgets import *

    2.2 读取txt数据

    self.idiom_data, self.valid_idioms = self.readData('data/data.txt') self.ai_answer = None

    2.3 界面设置

    self.setWindowTitle('成语接龙小程序-就差你了!')        self.setWindowIcon(QIcon('data/02.png'))        self.setFixedSize(600, 200)        self.user_input_label = QLabel('我方:')        self.user_input_edit = QLineEdit()        self.user_input_button = QPushButton('确定')        self.ai_input_label = QLabel('电脑方:')        self.ai_input_edit = QLineEdit()        self.restart_button = QPushButton('重新开始')        self.user_explain_label = QLabel('我方成语释义:')        self.user_explain_edit = QLineEdit()        self.ai_explain_label = QLabel('电脑方成语释义:')        self.ai_explain_edit = QLineEdit()        # 布局        self.grid = QGridLayout()        self.grid.setSpacing(12)        self.grid.addWidget(self.user_input_label, 0, 0)        self.grid.addWidget(self.user_input_edit, 0, 1)        self.grid.addWidget(self.user_input_button, 0, 2)        self.grid.addWidget(self.user_explain_label, 1, 0)        self.grid.addWidget(self.user_explain_edit, 1, 1, 1, 2)        self.grid.addWidget(self.ai_input_label, 2, 0)        self.grid.addWidget(self.ai_input_edit, 2, 1)        self.grid.addWidget(self.restart_button, 2, 2)        self.grid.addWidget(self.ai_explain_label, 3, 0)        self.grid.addWidget(self.ai_explain_edit, 3, 1, 1, 2)        self.setLayout(self.grid)        # 按键绑定        self.user_input_button.clicked.connect(self.airound)        self.restart_button.clicked.connect(self.restart)

    2.4 电脑接龙

      def airound(self):        idiom = self.user_input_edit.text()        idiom = idiom.strip()        if (not self.isvalid(idiom)) or (self.ai_answer and idiom[0] != self.ai_answer[0][-1]):            QMessageBox.warning(self, '成语输入错误', '你输入的成语不对哦, 不可以耍小聪明哒!', QMessageBox.Yes | QMessageBox.No)        else:            self.user_explain_edit.setText('读音: %s; 含义: %s' % (self.valid_idioms[idiom][0], self.valid_idioms[idiom][1]))            if idiom[-1] in self.idiom_data:                answers = self.idiom_data[idiom[-1]]                answer = random.choice(answers)                self.ai_answer = answer.copy()                self.ai_input_edit.setText(self.ai_answer[0])                self.ai_explain_edit.setText('读音: %s; 含义: %s' % (self.valid_idioms[answer[0]][0], self.valid_idioms[answer[0]][1]))            else:                QMessageBox.information(self, '你赢啦', '电脑都接不上你的成语, 你太厉害啦!', QMessageBox.Yes | QMessageBox.No)

    2.5 重新开始新游戏

     def restart(self):        self.ai_answer = None        self.ai_input_edit.clear()        self.ai_explain_edit.clear()        self.user_input_edit.clear()        self.user_explain_edit.clear()

    2.6 成语是否合法

     def isvalid(self, idiom):        return (idiom in self.valid_idioms)

    2.7 读取成语的数据

        def readData(self, filepath):        fp = open(filepath, 'r', encoding='utf-8')        idiom_data = {}        valid_idioms = {}        for line in fp.readlines():            line = line.strip()            if not line: continue            item = line.split('\t')            if len(item) != 3: continue            if item[0][0] not in idiom_data:                idiom_data[item[0][0]] = [item]            else:                idiom_data[item[0][0]].append(item)            valid_idioms[item[0]] = item[1:]        return idiom_data, valid_idioms

    2.8 附完整的项目源码

    '''小程序名:成语接龙'''import osimport sysimport randomfrom PyQt5.QtGui import *from PyQt5.QtWidgets import *  '''成语接龙'''class IdiomSolitaire(QWidget):    def __init__(self, parent=None, **kwargs):        super(IdiomSolitaire, self).__init__(parent)        # 读取数据        self.idiom_data, self.valid_idioms = self.readData('data/data.txt')        self.ai_answer = None        # 定义界面        self.setWindowTitle('成语接龙小程序-就差你了!')        self.setWindowIcon(QIcon('data/02.png'))        self.setFixedSize(600, 200)        self.user_input_label = QLabel('我方:')        self.user_input_edit = QLineEdit()        self.user_input_button = QPushButton('确定')        self.ai_input_label = QLabel('电脑方:')        self.ai_input_edit = QLineEdit()        self.restart_button = QPushButton('重新开始')        self.user_explain_label = QLabel('我方成语释义:')        self.user_explain_edit = QLineEdit()        self.ai_explain_label = QLabel('电脑方成语释义:')        self.ai_explain_edit = QLineEdit()        # 布局        self.grid = QGridLayout()        self.grid.setSpacing(12)        self.grid.addWidget(self.user_input_label, 0, 0)        self.grid.addWidget(self.user_input_edit, 0, 1)        self.grid.addWidget(self.user_input_button, 0, 2)        self.grid.addWidget(self.user_explain_label, 1, 0)        self.grid.addWidget(self.user_explain_edit, 1, 1, 1, 2)        self.grid.addWidget(self.ai_input_label, 2, 0)        self.grid.addWidget(self.ai_input_edit, 2, 1)        self.grid.addWidget(self.restart_button, 2, 2)        self.grid.addWidget(self.ai_explain_label, 3, 0)        self.grid.addWidget(self.ai_explain_edit, 3, 1, 1, 2)        self.setLayout(self.grid)        # 按键绑定        self.user_input_button.clicked.connect(self.airound)        self.restart_button.clicked.connect(self.restart)    '''电脑接龙'''    def airound(self):        idiom = self.user_input_edit.text()        idiom = idiom.strip()        if (not self.isvalid(idiom)) or (self.ai_answer and idiom[0] != self.ai_answer[0][-1]):            QMessageBox.warning(self, '成语输入错误', '你输入的成语不对哦, 不可以耍小聪明哒!', QMessageBox.Yes | QMessageBox.No)        else:            self.user_explain_edit.setText('读音: %s; 含义: %s' % (self.valid_idioms[idiom][0], self.valid_idioms[idiom][1]))            if idiom[-1] in self.idiom_data:                answers = self.idiom_data[idiom[-1]]                answer = random.choice(answers)                self.ai_answer = answer.copy()                self.ai_input_edit.setText(self.ai_answer[0])                self.ai_explain_edit.setText('读音: %s; 含义: %s' % (self.valid_idioms[answer[0]][0], self.valid_idioms[answer[0]][1]))            else:                QMessageBox.information(self, '你赢啦', '电脑都接不上你的成语, 你太厉害啦!', QMessageBox.Yes | QMessageBox.No)    '''重新开始'''    def restart(self):        self.ai_answer = None        self.ai_input_edit.clear()        self.ai_explain_edit.clear()        self.user_input_edit.clear()        self.user_explain_edit.clear()    '''检测成语是否合法'''    def isvalid(self, idiom):        return (idiom in self.valid_idioms)    '''读取成语数据'''    def readData(self, filepath):        fp = open(filepath, 'r', encoding='utf-8')        idiom_data = {}        valid_idioms = {}        for line in fp.readlines():            line = line.strip()            if not line: continue            item = line.split('\t')            if len(item) != 3: continue            if item[0][0] not in idiom_data:                idiom_data[item[0][0]] = [item]            else:                idiom_data[item[0][0]].append(item)            valid_idioms[item[0]] = item[1:]        return idiom_data, valid_idioms  '''run'''if __name__ == '__main__':    app = QApplication(sys.argv)    client = IdiomSolitaire()    client.show()    sys.exit(app.exec_())

    3.效果展示

    3.1 成语:龙腾虎跃 Part 1效果

    3.2 成语:山清水秀 Part 2效果

    读到这里,这篇"怎么用Python编写简易的成语接龙游戏"文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注行业资讯频道。

    0