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

Python實(shí)現(xiàn)問題回答小游戲

 更新時(shí)間:2021年12月10日 11:12:00   作者:我的天才女友  
這篇文章主要介紹了利用Python制作一個(gè)簡(jiǎn)單的知識(shí)競(jìng)賽小游戲,可以實(shí)現(xiàn)回答問題功能,文中的示例代碼介紹詳細(xì),感興趣的同學(xué)快跟隨小編一起學(xué)習(xí)吧

讀取問題

如下所示,我們?cè)谖谋局袑懥艘粋€(gè)問題,然后將其讀取出來。

“黃河遠(yuǎn)上白云間,一片孤城萬仞山。”的作者是誰?

王之渙

李白

白居易

杜甫

file = open("1.txt", "r")
question_coll = file.readlines()
file.close()
print(file)

運(yùn)行之后發(fā)現(xiàn)報(bào)錯(cuò),查詢之后發(fā)現(xiàn)編碼格式不正確。

設(shè)置了讀取的編碼格式發(fā)現(xiàn)可以讀取文本的內(nèi)容

file = open("1.txt", encoding='utf-8')

繪制回答面板

為了方便讀取,新建一個(gè)類來儲(chǔ)存文件中的問題

# 問題類
class Question:

    # 回答列表
    answer_question = []

    # 正確答案
    answer_index = 1

    """問題類"""
    def __init__(self, question):
        self.question = question

導(dǎo)入問題,將文件中的問題保存在qustion中

from question import Question

因?yàn)槲募母袷绞枪潭ǖ囊?為一個(gè)問題的所有行數(shù)。

將問題

questionList = []
for i in range(int(len(question_coll) / 6)):
    que_all = question_coll[i * 6: i * 6 + 6]
    que = Question(que_all[0].rstrip())
    que.answer_question = [que_all[1].rstrip(), que_all[2].rstrip(), que_all[3].rstrip(), que_all[4].rstrip()]
    que.answer_index = int(que_all[5].rstrip())
    questionList.append(que)

封裝屏幕上顯示文字的打印

def draw_text(window_screen, font_size, content, starting_x, starting_y, text_color=WHITE, bg_color=BLACK):
    # 繪制文字
    # 設(shè)置字體
    font = pygame.font.SysFont("方正粗黑宋簡(jiǎn)體", font_size)
    text1 = font.render(content, True, text_color, bg_color)
    window_screen.blit(text1, (starting_x, starting_y))

顯示問題

draw_text(screen, 48, "知識(shí)競(jìng)賽", 180, 20)
while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
    tips = "當(dāng)前一共有" + str(len(questionList)) + "個(gè)問題,目前是第" + str(index) + "個(gè)。"
    draw_text(screen, 18, tips, 20, 140, bg_color=WHITE, text_color=BLACK)
    current_que = questionList[index - 1]
    question_main = "問題" + str(index) + ". " + current_que.question
    draw_text(screen, 16, question_main, 20, 200, bg_color=WHITE, text_color=BLACK)
    for i in range(len(current_que.answer_question)):
        option = str(i + 1) + ". " + current_que.answer_question[i]
        draw_text(screen, 16, option, 40, 260 + i * 40, bg_color=WHITE, text_color=BLACK)

    pygame.display.update()

這樣就實(shí)現(xiàn)了問題的顯示

回答問題

首先我們給出提示,為了方便確認(rèn)問題是否回答,答案正確與否,我們?cè)趩栴}類中添加變量

    # 是否回答
    answeredFlg = False

    # 回答是否正確
    rightFlg = False

根據(jù)這些變量設(shè)置文字。

    if current_que.answeredFlg:
        if current_que.rightFlg:
            print("回答正確,是" + current_que.answer_question[current_que.answer_index])
        else:
            print("回答錯(cuò)誤,正確答案是" + current_que.answer_question[current_que.answer_index])
    else:
        draw_text(screen, 16, "請(qǐng)按下1、2、3、4來回答答案", 40, 460, bg_color=WHITE, text_color=RED)

如果按下按鍵,根據(jù)答案的正確與否給與響應(yīng)的提示。

 if current_que.answeredFlg:
        if current_que.rightFlg:
            str1 = "回答正確,是" + current_que.answer_question[current_que.answer_index - 1]
            draw_text(screen, 16, str1, 40, 460, bg_color=WHITE, text_color=GREEN)
        else:
            str1 = "回答錯(cuò)誤,正確答案是" + current_que.answer_question[current_que.answer_index - 1]
            draw_text(screen, 16, str1, 40, 460, bg_color=WHITE, text_color=RED)
    else:
        draw_text(screen, 16, "請(qǐng)按下1、2、3、4來回答答案", 40, 460, bg_color=WHITE, text_color=RED)

問題切換

為了方便用戶切換問題,在窗口上添加對(duì)應(yīng)的按鈕。

import pygame.font


class Button:

    def __init__(self, screen, msg, start_x, start_y):
        # 設(shè)置按鈕的尺寸和其他屬性
        self.screen = screen
        self.width, self.height = 200, 50
        self.button_color = (0, 255, 0)
        self.text_color = (255, 255, 255)
        self.font = pygame.font.SysFont("方正粗黑宋簡(jiǎn)體", 20)

        # 創(chuàng)建按鈕的rect對(duì)象,并使其居中
        self.rect = pygame.Rect(0, 0, self.width, self.height)
        self.rect.left = start_x
        self.rect.right = start_y
        # 按鈕的標(biāo)簽只需創(chuàng)建一次
        self._prep_msg(msg)

    def _prep_msg(self, msg):
        """將msg渲染為圖像,并讓按鈕居上"""
        self.msg_image = self.font.render(msg, True, self.text_color, self.button_color)
        self.msg_image_rect = self.msg_image.get_rect()
        self.msg_image_rect.center = self.rect.center

    def draw_button(self):
        # 繪制一個(gè)用顏色填充的按鈕,在繪制文本
        self.screen.fill(self.button_color, self.rect)
        self.screen.blit(self.msg_image, self.msg_image_rect)

answer_question.py

	btn1 = Button(screen, "next", 300, 500)
    btn1.draw_button()

修改對(duì)應(yīng)的按鈕顏色,并添加上一個(gè)按鈕。

通過是否回答和是否有下一個(gè)或者上一個(gè)控制按鈕的顯示

    if current_que.answeredFlg and index < len(questionList):
        btn1 = Button(screen, "下一個(gè)", 300, 500)
        btn1.draw_button()
    if index > 1:
        btn2 = Button(screen, "上一個(gè)", 50, 500)
        btn2.draw_button()

給按鈕添加事件

        if event.type == pygame.MOUSEBUTTONDOWN:
            mouse_pos = pygame.mouse.get_pos()
            if btn1.rect.collidepoint(mouse_pos):
                if current_que.answeredFlg and index < len(questionList):
                    index += 1
            if btn2.rect.collidepoint(mouse_pos) and index > 1:
                index -= 1

完整代碼

answer_question.py 主程序

import pygame, sys
from pygame.locals import *
from question import Question
from button import Button

# 讀取問題
file = open("1.txt", encoding='utf-8')
question_coll = file.readlines()
file.close()
questionList = []
for i in range(int(len(question_coll) / 6)):
    que_all = question_coll[i * 6: i * 6 + 6]
    que = Question(que_all[0].rstrip())
    que.answer_question = [que_all[1].rstrip(), que_all[2].rstrip(), que_all[3].rstrip(), que_all[4].rstrip()]
    que.answer_index = int(que_all[5].rstrip())
    questionList.append(que)


# 顏色變量
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
# 初始化面板
pygame.init()
screen = pygame.display.set_mode((600, 600))
pygame.display.set_caption("知識(shí)競(jìng)賽")

# 當(dāng)前問題
index = 1


def draw_text(window_screen, font_size, content, starting_x, starting_y, text_color=WHITE, bg_color=BLACK):
    # 繪制文字
    # 設(shè)置字體
    font = pygame.font.SysFont("方正粗黑宋簡(jiǎn)體", font_size)
    text1 = font.render(content, True, text_color, bg_color)
    window_screen.blit(text1, (starting_x, starting_y))


# 按鈕
btn1 = Button(screen, "下一個(gè)", 300, 500)
btn2 = Button(screen, "上一個(gè)", 50, 500)
while True:
    answer_index = 0
    # 填充白色
    screen.fill(WHITE)
    draw_text(screen, 48, "知識(shí)競(jìng)賽", 180, 20)
    tips = "當(dāng)前一共有" + str(len(questionList)) + "個(gè)問題,目前是第" + str(index) + "個(gè)。"
    draw_text(screen, 18, tips, 20, 140, bg_color=WHITE, text_color=BLACK)
    current_que = questionList[index - 1]
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        if event.type == KEYDOWN:
            if event.key == K_1:
                answer_index = 1
            if event.key == K_2:
                answer_index = 2
            if event.key == K_3:
                answer_index = 3
            if event.key == K_4:
                answer_index = 4
        if event.type == pygame.MOUSEBUTTONDOWN:
            mouse_pos = pygame.mouse.get_pos()
            if btn1.rect.collidepoint(mouse_pos):
                if current_que.answeredFlg and index < len(questionList):
                    index += 1
            if btn2.rect.collidepoint(mouse_pos) and index > 1:
                index -= 1

    question_main = "問題" + str(index) + ". " + current_que.question
    draw_text(screen, 16, question_main, 20, 200, bg_color=WHITE, text_color=BLACK)
    for i in range(len(current_que.answer_question)):
        option = str(i + 1) + ". " + current_que.answer_question[i]
        draw_text(screen, 16, option, 40, 260 + i * 40, bg_color=WHITE, text_color=BLACK)
    if answer_index != 0:
        current_que.answeredFlg = True
        current_que.rightFlg = answer_index == current_que.answer_index
    if current_que.answeredFlg:
        if current_que.rightFlg:
            str1 = "回答正確,是" + current_que.answer_question[current_que.answer_index - 1]
            draw_text(screen, 16, str1, 40, 460, bg_color=WHITE, text_color=GREEN)
        else:
            str1 = "回答錯(cuò)誤,正確答案是" + current_que.answer_question[current_que.answer_index - 1]
            draw_text(screen, 16, str1, 40, 460, bg_color=WHITE, text_color=RED)
    else:
        draw_text(screen, 16, "請(qǐng)按下1、2、3、4來回答答案", 40, 460, bg_color=WHITE, text_color=RED)
    if current_que.answeredFlg and index < len(questionList):
        btn1.draw_button()
    if index > 1:
        btn2.draw_button()
    pygame.display.update()

問題類 qustion.py

# 問題類
class Question:
    """問題類"""

    # 回答列表
    answer_question = []

    # 正確答案
    answer_index = 1

    # 是否回答
    answeredFlg = False

    # 回答是否正確
    rightFlg = False

    def __init__(self, question):
        self.question = question


按鈕類 button.py

import pygame.font


class Button:

    def __init__(self, screen, msg, start_x, start_y):
        # 設(shè)置按鈕的尺寸和其他屬性
        self.screen = screen
        self.width, self.height = 200, 50
        self.button_color = (255, 192, 128)
        self.text_color = (255, 255, 255)
        self.font = pygame.font.SysFont("方正粗黑宋簡(jiǎn)體", 20)

        # 創(chuàng)建按鈕的rect對(duì)象,并使其居中
        self.rect = pygame.Rect(0, 0, self.width, self.height)
        self.rect.left = start_x
        self.rect.top = start_y
        # 按鈕的標(biāo)簽只需創(chuàng)建一次
        self._prep_msg(msg)

    def _prep_msg(self, msg):
        """將msg渲染為圖像,并讓按鈕居上"""
        self.msg_image = self.font.render(msg, True, self.text_color, self.button_color)
        self.msg_image_rect = self.msg_image.get_rect()
        self.msg_image_rect.center = self.rect.center

    def draw_button(self):
        # 繪制一個(gè)用顏色填充的按鈕,在繪制文本
        self.screen.fill(self.button_color, self.rect)
        self.screen.blit(self.msg_image, self.msg_image_rect)

問題文本文件 1.txt

“黃河遠(yuǎn)上白云間,一片孤城萬仞山?!钡淖髡呤钦l?

王之渙

李白

白居易

杜甫

1

“落霞與孤鶩齊飛”的下一句是?

攜酒對(duì)情人

秋水共長(zhǎng)天一色

抱琴開野室

林塘花月下

2?

以上就是Python實(shí)現(xiàn)問題回答小游戲的詳細(xì)內(nèi)容,更多關(guān)于Python問題回答游戲的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 分享8點(diǎn)超級(jí)有用的Python編程建議(推薦)

    分享8點(diǎn)超級(jí)有用的Python編程建議(推薦)

    這篇文章主要介紹了分享8點(diǎn)超級(jí)有用的Python編程建議(推薦),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2019-10-10
  • 人工智能Text Generation文本生成原理示例詳解

    人工智能Text Generation文本生成原理示例詳解

    這篇文章主要為大家介紹了Text Generation文本生成原理示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-01-01
  • Python?的counter()函數(shù)解析與示例詳解

    Python?的counter()函數(shù)解析與示例詳解

    在?Python?中,?collections?模塊提供了?Counter?類,用于計(jì)算可迭代對(duì)象中元素的數(shù)量,?Counter?是一個(gè)字典的子類,它以元素作為鍵,以元素出現(xiàn)的次數(shù)作為值進(jìn)行計(jì)數(shù),本文給大家介紹Python?的counter()函數(shù),感興趣的朋友一起看看吧
    2023-08-08
  • ?Python使用Mediapipe對(duì)圖像進(jìn)行手部地標(biāo)檢測(cè)

    ?Python使用Mediapipe對(duì)圖像進(jìn)行手部地標(biāo)檢測(cè)

    本文將以深度庫即Mediapipe為基礎(chǔ)庫,以及其他計(jì)算機(jī)視覺預(yù)處理的CV2庫來制作手部地標(biāo)檢測(cè)模型,文中的示例代碼講解詳細(xì),感興趣的可以了解一下
    2022-03-03
  • Python中tqdm的使用和例子

    Python中tqdm的使用和例子

    Tqdm是一個(gè)快速,可擴(kuò)展的Python進(jìn)度條,可以在 Python 長(zhǎng)循環(huán)中添加一個(gè)進(jìn)度提示信息,用戶只需要封裝任意的迭代器tqdm(iterator),下面這篇文章主要給大家介紹了關(guān)于Python中tqdm的使用和例子的相關(guān)資料,需要的朋友可以參考下
    2022-09-09
  • python中用ctypes模擬點(diǎn)擊的實(shí)例講解

    python中用ctypes模擬點(diǎn)擊的實(shí)例講解

    在本篇文章里小編給各位整理了一篇關(guān)于python中用ctypes模擬點(diǎn)擊的實(shí)例講解內(nèi)容,需要的朋友可以參考學(xué)習(xí)下。
    2020-11-11
  • Python下載網(wǎng)絡(luò)文本數(shù)據(jù)到本地內(nèi)存的四種實(shí)現(xiàn)方法示例

    Python下載網(wǎng)絡(luò)文本數(shù)據(jù)到本地內(nèi)存的四種實(shí)現(xiàn)方法示例

    這篇文章主要介紹了Python下載網(wǎng)絡(luò)文本數(shù)據(jù)到本地內(nèi)存的四種實(shí)現(xiàn)方法,涉及Python網(wǎng)絡(luò)傳輸、文本讀寫、內(nèi)存I/O、矩陣運(yùn)算等相關(guān)操作技巧,代碼中包含了較為詳盡的注釋說明便于理解,需要的朋友可以參考下
    2018-02-02
  • 利用python打開攝像頭及顏色檢測(cè)方法

    利用python打開攝像頭及顏色檢測(cè)方法

    今天小編就為大家分享一篇利用python打開攝像頭及顏色檢測(cè)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-08-08
  • 基于Python制作GIF表情包生成工具

    基于Python制作GIF表情包生成工具

    在當(dāng)前無表情包不會(huì)聊天的時(shí)代,怎么也不能輸在表情包數(shù)量不足上啊,今天咱們就來基于Python制作一個(gè)?gif?生成工具,用來制作表情包也太好用啦
    2023-07-07
  • Python里disconnect UDP套接字的方法

    Python里disconnect UDP套接字的方法

    這篇文章主要介紹了Python里disconnect UDP套接字的方法,本文使用的是ctypes繞過的方法,需要的朋友可以參考下
    2015-04-04

最新評(píng)論