欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

基于Python編寫簡易的成語接龍游戲

 更新時間:2022年03月04日 08:42:59   作者:嗨!栗子同學(xué)  
成語接龍是中華民族傳統(tǒng)的文字游戲。它歷史悠久,是傳統(tǒng)文字、文化、文明的一個縮影,也是老少皆宜的民間文化娛樂活動。本文將用Python制作一個簡單的成語接龍游戲,需要的可以參考一下

前言

"胸藏文墨懷如谷,腹有詩書氣自華"。      ——《和董傳留別》

成語接龍是中華民族傳統(tǒng)的文字游戲。

它歷史悠久,是傳統(tǒng)文字、文化、文明的一個縮影,也是老少皆宜的民間文化娛樂活動。

成語接龍:"龍騰虎躍,該你了!"                        什么?你立刻接上了「躍馬彎弓」?

確認(rèn)過眼神,是高手沒錯了。    Hi~我是栗子,又見面了哇! 今天我?guī)砹诵录寄芙怄i,沒錯,就是:「成語接龍」

據(jù)說,跟我玩游戲的朋友偷偷作弊,拿了本《現(xiàn)代漢語詞典》要跟我大戰(zhàn)三百回合。

最后發(fā)現(xiàn),我說的每個成語她都忍不住要去查一下啊哈哈哈!?。⌒【巵y入:我的天吶,奇怪的知識成倍增長了!

認(rèn)慫? 我會認(rèn)慫嘛?!   不存在的.

看我寫一款成語接龍小程序,分分鐘秒殺你~快來和我「成語接龍」吧~

在這款成語接龍小游戲中,你會學(xué)到一些「新成語」,能邊玩兒邊學(xué)成語,最重要的還是能接觸一款編程,在玩兒中進步學(xué)習(xí),成就感滿滿,又能學(xué)知識,你有閑暇時,跟隔壁老王可以一直玩一直玩哈哈哈~

1.游戲規(guī)則

下面我簡單說說「成語接龍」游戲的基本規(guī)則,你很熟悉可以跳過這段,不熟悉就仔細(xì)看看:

①用四字成語的最后一個字起頭,接出下一句成語,由此不斷延伸;

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

③一場游戲中,不應(yīng)出現(xiàn)重復(fù)成語。

【舉例】

「??菔癄€」→「爛醉如泥」→「泥牛入海」

×不能接「海枯石爛」(出現(xiàn)重復(fù))

√可以接「海納百川」

是不是超級簡單?考驗詞匯儲備量的時候到了~

2.正式敲代碼

2.1 模塊導(dǎo)入

import os
import sys
import random
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

2.2 讀取txt數(shù)據(jù)

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

2.3 界面設(shè)置

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 讀取成語的數(shù)據(jù)

    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 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)
        # 讀取數(shù)據(jù)
        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)
    '''讀取成語數(shù)據(jù)'''
    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效果?

到此這篇關(guān)于基于Python編寫簡易的成語接龍游戲的文章就介紹到這了,更多相關(guān)Python成語接龍游戲內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • python 生成圖形驗證碼的方法示例

    python 生成圖形驗證碼的方法示例

    日常在網(wǎng)站使用過程中經(jīng)常遇到圖形驗證,這篇文章主要介紹了python 生成圖形驗證碼的方法示例,非常具有實用價值,需要的朋友可以參考下
    2018-11-11
  • 如何在pycharm中配置pyqt5設(shè)計GUI操作教程

    如何在pycharm中配置pyqt5設(shè)計GUI操作教程

    這篇文章主要介紹了如何在pycharm中配置pyqt5設(shè)計GUI的操作教程,有需要的朋友可以借鑒參考下,希望大家可以多多交流,討論相關(guān)問題共同提升
    2021-08-08
  • python pandas dataframe 去重函數(shù)的具體使用

    python pandas dataframe 去重函數(shù)的具體使用

    這篇文章主要介紹了python pandas dataframe 去重函數(shù)的具體使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • Python函數(shù)參數(shù)基礎(chǔ)介紹及示例

    Python函數(shù)參數(shù)基礎(chǔ)介紹及示例

    在聲明函數(shù)的時候,一般會根據(jù)函數(shù)所要實現(xiàn)的功能來決定函數(shù)是否需要參數(shù)。在多數(shù)情況下,我們聲明的函數(shù)都會使用到參數(shù),這篇文章主要介紹了Python函數(shù)參數(shù)
    2022-08-08
  • python_matplotlib改變橫坐標(biāo)和縱坐標(biāo)上的刻度(ticks)方式

    python_matplotlib改變橫坐標(biāo)和縱坐標(biāo)上的刻度(ticks)方式

    這篇文章主要介紹了python_matplotlib改變橫坐標(biāo)和縱坐標(biāo)上的刻度(ticks)方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-05-05
  • win7安裝python生成隨機數(shù)代碼分享

    win7安裝python生成隨機數(shù)代碼分享

    python3生成隨機數(shù)代碼分享,在win7上測試通過
    2013-12-12
  • python使用正則表達式(Regular Expression)方法超詳細(xì)

    python使用正則表達式(Regular Expression)方法超詳細(xì)

    這篇文章主要介紹了python使用正則表達式(Regular Expression)方法超詳細(xì),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-12-12
  • Python實現(xiàn)監(jiān)控遠程主機實時數(shù)據(jù)的示例詳解

    Python實現(xiàn)監(jiān)控遠程主機實時數(shù)據(jù)的示例詳解

    這篇文章主要為大家詳細(xì)介紹了Python如何使用Socket庫和相應(yīng)的第三方庫來監(jiān)控遠程主機的實時數(shù)據(jù),比如CPU使用率、內(nèi)存使用率、網(wǎng)絡(luò)帶寬等,感興趣的可以了解一下
    2023-04-04
  • 詳解python使用turtle庫來畫一朵花

    詳解python使用turtle庫來畫一朵花

    這篇文章主要介紹了python使用turtle庫來畫一朵花,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • python fabric實現(xiàn)遠程操作和部署示例

    python fabric實現(xiàn)遠程操作和部署示例

    這篇文章主要介紹了python使用fabric實現(xiàn)遠程操作和部署示例,需要的朋友可以參考下
    2014-03-03

最新評論