您好,登錄后才能下訂單哦!
本文小編為大家詳細介紹“怎么用Python編寫簡易的成語接龍游戲”,內容詳細,步驟清晰,細節處理妥當,希望這篇“怎么用Python編寫簡易的成語接龍游戲”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學習新知識吧。
下面我簡單說說「成語接龍」游戲的基本規則,你很熟悉可以跳過這段,不熟悉就仔細看看:
①用四字成語的最后一個字起頭,接出下一句成語,由此不斷延伸;
②成語尾首相接的字,可以是同一個字,也可以是同音的字;
③一場游戲中,不應出現重復成語。
【舉例】
「海枯石爛」→「爛醉如泥」→「泥牛入海」
×不能接「海枯石爛」(出現重復)
√可以接「海納百川」
import os import sys import random from PyQt5.QtGui import * from PyQt5.QtWidgets import *
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
''' 小程序名: 成語接龍 ''' import os import sys import random from 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.1 成語:龍騰虎躍 Part 1效果
3.2 成語:山清水秀 Part 2效果
讀到這里,這篇“怎么用Python編寫簡易的成語接龍游戲”文章已經介紹完畢,想要掌握這篇文章的知識點還需要大家自己動手實踐使用過才能領會,如果想了解更多相關內容的文章,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。