您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關如何用Python實現問題回答小游戲,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。
如下所示,我們在文本中寫了一個問題,然后將其讀取出來。
“黃河遠上白云間,一片孤城萬仞山。”的作者是誰?
王之渙
李白
白居易
杜甫
file = open("1.txt", "r") question_coll = file.readlines() file.close() print(file)
運行之后發現報錯,查詢之后發現編碼格式不正確。
設置了讀取的編碼格式發現可以讀取文本的內容
file = open("1.txt", encoding='utf-8')
為了方便讀取,新建一個類來儲存文件中的問題
# 問題類 class Question: # 回答列表 answer_question = [] # 正確答案 answer_index = 1 """問題類""" def __init__(self, question): self.question = question
導入問題,將文件中的問題保存在qustion中
from question import Question
因為文件的格式是固定的以6為一個問題的所有行數。
將問題
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): # 繪制文字 # 設置字體 font = pygame.font.SysFont("方正粗黑宋簡體", font_size) text1 = font.render(content, True, text_color, bg_color) window_screen.blit(text1, (starting_x, starting_y))
顯示問題
draw_text(screen, 48, "知識競賽", 180, 20) while True: for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() tips = "當前一共有" + str(len(questionList)) + "個問題,目前是第" + str(index) + "個。" 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()
這樣就實現了問題的顯示
首先我們給出提示,為了方便確認問題是否回答,答案正確與否,我們在問題類中添加變量
# 是否回答 answeredFlg = False # 回答是否正確 rightFlg = False
根據這些變量設置文字。
if current_que.answeredFlg: if current_que.rightFlg: print("回答正確,是" + current_que.answer_question[current_que.answer_index]) else: print("回答錯誤,正確答案是" + current_que.answer_question[current_que.answer_index]) else: draw_text(screen, 16, "請按下1、2、3、4來回答答案", 40, 460, bg_color=WHITE, text_color=RED)
如果按下按鍵,根據答案的正確與否給與響應的提示。
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 = "回答錯誤,正確答案是" + 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, "請按下1、2、3、4來回答答案", 40, 460, bg_color=WHITE, text_color=RED)
為了方便用戶切換問題,在窗口上添加對應的按鈕。
import pygame.font class Button: def __init__(self, screen, msg, start_x, start_y): # 設置按鈕的尺寸和其他屬性 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("方正粗黑宋簡體", 20) # 創建按鈕的rect對象,并使其居中 self.rect = pygame.Rect(0, 0, self.width, self.height) self.rect.left = start_x self.rect.right = start_y # 按鈕的標簽只需創建一次 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): # 繪制一個用顏色填充的按鈕,在繪制文本 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()
修改對應的按鈕顏色,并添加上一個按鈕。
通過是否回答和是否有下一個或者上一個控制按鈕的顯示
if current_que.answeredFlg and index < len(questionList): btn1 = Button(screen, "下一個", 300, 500) btn1.draw_button() if index > 1: btn2 = Button(screen, "上一個", 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("知識競賽") # 當前問題 index = 1 def draw_text(window_screen, font_size, content, starting_x, starting_y, text_color=WHITE, bg_color=BLACK): # 繪制文字 # 設置字體 font = pygame.font.SysFont("方正粗黑宋簡體", font_size) text1 = font.render(content, True, text_color, bg_color) window_screen.blit(text1, (starting_x, starting_y)) # 按鈕 btn1 = Button(screen, "下一個", 300, 500) btn2 = Button(screen, "上一個", 50, 500) while True: answer_index = 0 # 填充白色 screen.fill(WHITE) draw_text(screen, 48, "知識競賽", 180, 20) tips = "當前一共有" + str(len(questionList)) + "個問題,目前是第" + str(index) + "個。" 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 = "回答錯誤,正確答案是" + 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, "請按下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): # 設置按鈕的尺寸和其他屬性 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("方正粗黑宋簡體", 20) # 創建按鈕的rect對象,并使其居中 self.rect = pygame.Rect(0, 0, self.width, self.height) self.rect.left = start_x self.rect.top = start_y # 按鈕的標簽只需創建一次 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): # 繪制一個用顏色填充的按鈕,在繪制文本 self.screen.fill(self.button_color, self.rect) self.screen.blit(self.msg_image, self.msg_image_rect)
問題文本文件 1.txt
“黃河遠上白云間,一片孤城萬仞山。”的作者是誰?
王之渙
李白
白居易
杜甫
1
“落霞與孤鶩齊飛”的下一句是?
攜酒對情人
秋水共長天一色
抱琴開野室
林塘花月下
2
關于如何用Python實現問題回答小游戲就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。