Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Python to implement question and answer Mini Game

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how to use Python to achieve question and answer Mini Game. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.

Read problem

As shown below, we wrote a question in the text and then read it out.

"the surging Yellow River comes from the clouds, and there are ten thousand mountains outside a lonely city." Who's the author?

Wang Zhixiang

Li Bai

Bai Juyi

Du Fu

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

After running, it is found that the error is reported, and after the query, it is found that the coding format is incorrect.

Set the encoding format of the read to find the content that can read the text

File = open ("1.txt", encoding='utf-8') draw the answer panel

To make it easy to read, create a new class to store the problems in the file

# question class class Question: # answer list answer_question = [] # correct answer answer_index = 1 "" question class "def _ _ init__ (self, question): self.question = question

Import questions and save the questions in the file in qustion

From question import Question

Because the format of the file is fixed, the number of lines with 6 as a problem.

Put the problem

QuestionList = [] for i in range (int (len (question_coll) / 6)): que_all = question_ [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)

Encapsulate the printing of text displayed on the screen

Def draw_text (window_screen, font_size, content, starting_x, starting_y, text_color=WHITE, bg_color=BLACK): # draw text # set font font = pygame.font.SysFont ("founder bold black Song simplified", font_size) text1 = font.render (content, True, text_color, bg_color) window_screen.blit (text1, (starting_x, starting_y))

Display problem

Draw_text (screen, 48, "knowledge contest", 180,20) while True: for event in pygame.event.get (): if event.type = = QUIT: pygame.quit () sys.exit () tips = "current total" + str (len (questionList)) + "problem, currently" + str (index) + "." Draw_text (screen, 18, tips, 20,140, bg_color=WHITE, text_color=BLACK) current_que = questionList [index-1] question_main = "problem" + str (index) + ". "+ current_que.question draw_text (screen, 16, question_main, 20200, bg_color=WHITE, text_color=BLACK) for i in range (len (current_que.answer_question)): option = str (I + 1) +". "+ current_que.answer_ [I] draw_text (screen, 16, option, 40,260 + I * 40, bg_color=WHITE, text_color=BLACK) pygame.display.update ()

In this way, the problem is displayed.

Question answering

First of all, we give a hint, in order to make it easy to confirm whether the question is answered and whether the answer is correct, we add variables to the question class.

# whether to answer answeredFlg = False # whether the answer is correct rightFlg = False

Set the text based on these variables.

If current_que.answeredFlg: if current_que.rightFlg: print ("correct answer, yes" + current_que.answer_ answer [current _ que.answer_index]) else: print ("wrong answer" The correct answer is "+ current_que.answer_ answer [current _ que.answer_index]) else: draw_text (screen, 16," Please press 1, 2, 3, 4 to answer ", 40460, bg_color=WHITE, text_color=RED)

If you press the button, you will be prompted to respond according to whether the answer is correct or not.

If current_que.answeredFlg: if current_que.rightFlg: str1 = "correct answer, yes" + current_que.answer_ answer [current _ que.answer_index-1] draw_text (screen, 16, str1, 40,460, bg_color=WHITE, text_color=GREEN) else: str1 = "wrong answer The correct answer is "+ current_que.answer_ answer [current _ que.answer_index-1] draw_text (screen, 16, str1, 40,460, bg_color=WHITE, text_color=RED) else: draw_text (screen, 16," Please press 1, 2, 3, 4 to answer ", 40460, bg_color=WHITE, text_color=RED)

Problem switching

To make it easier for users to switch questions, add corresponding buttons to the window.

Import pygame.fontclass Button: def _ _ init__ (self, screen, msg, start_x, start_y): # set the size and other properties of the button 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) # create the button's rect object And center it self.rect = pygame.Rect (0,0, self.width, self.height) self.rect.left = start_x self.rect.right = start_y # button label only need to create self._prep_msg (msg) def _ prep_msg (self, msg): "" render msg as an image And let the button top "" 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): # draw a button filled with color When drawing text 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", 300500) btn1.draw_button ()

Modify the corresponding button color and add the previous button.

By whether or not to answer and whether there is a display of the next or previous control button

If current_que.answeredFlg and index

< len(questionList): btn1 = Button(screen, "下一个", 300, 500) btn1.draw_button() if index >

1: btn2 = Button (screen, "previous", 50500) btn2.draw_button ()

Add an event to the 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

Complete code

Answer_question.py main program

Import pygame, sysfrom pygame.locals import * from question import Questionfrom button import Button# read problem 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_ collections [I * 6: I * 6 + 6] que = Question (que_all [0] .rs trip () 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) # color variable WHITE = (255,255,255) BLACK = (0,0,0) RED = (255,0,0) GREEN = (0,255,0) # initialization panel pygame.init () screen = pygame.display.set_mode Pygame.display.set_caption ("knowledge contest") # current problem index = 1def draw_text (window_screen, font_size, content, starting_x, starting_y, text_color=WHITE, bg_color=BLACK): # draw text # set font font = pygame.font.SysFont ("founder bold black Song simplified", font_size) text1 = font.render (content, True, text_color, bg_color) window_screen.blit (text1 (starting_x, starting_y)) # Button btn1 = Button (screen, "next", 300,500) btn2 = Button (screen, "previous", 50,500) while True: answer_index = 0 # fill white screen.fill (WHITE) draw_text (screen, 48, "knowledge contest", 180,20) tips = "there are current problems + str (len (questionList)) +" It is currently "+ 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 = = Kwon 1: answer_index = 1 if event.key = = Kwon 2: Answer_index = 2 if event.key = = Kany3: answer_index = 3 if event.key = = Kron4: 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 = "problem" + str (index) + ". "+ current_que.question draw_text (screen, 16, question_main, 20200, bg_color=WHITE, text_color=BLACK) for i in range (len (current_que.answer_question)): option = str (I + 1) +". "+ current_que.answer_ [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 =" correct answer It is "+ current_que.answer_ answer [current _ que.answer_index-1] draw_text (screen, 16, str1, 40,460, bg_color=WHITE, text_color=GREEN) else: str1 =" wrong answer. The correct answer is "+ current_que.answer_ answer [current _ que.answer_index-1] draw_text (screen, 16, str1, 40,460, bg_color=WHITE, text_color=RED) else: draw_text (screen, 16," Please press 1, 2, 3, 4 to answer ", 40460, 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 ()

Problem class qustion.py

# question class class Question: "" question class "" # answer list answer_question = [] # correct answer answer_index = 1 # whether to answer answeredFlg = False # whether the answer is correct or not rightFlg = False def _ _ init__ (self, question): self.question = question

Button type button.py

Import pygame.fontclass Button: def _ _ init__ (self, screen, msg, start_x, start_y): # set the size and other properties of the button 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) # create the button's rect object And center it self.rect = pygame.Rect (0,0, self.width, self.height) self.rect.left = start_x self.rect.top = start_y # button label only need to create self._prep_msg (msg) def _ prep_msg (self, msg): "" render msg as an image And let the button top "" 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): # draw a button filled with color When drawing text self.screen.fill (self.button_color, self.rect) self.screen.blit (self.msg_image, self.msg_image_rect)

Question text file 1.txt

"the surging Yellow River comes from the clouds, and there are ten thousand mountains outside a lonely city." Who's the author?

Wang Zhixiang

Li Bai

Bai Juyi

Du Fu

one

What's the next sentence of "Luoxia flies with loneliness"?

Bring wine to your lover

The autumn river shares a scenic hue with the vast sky

Baoqin open room

Lin Tang Hua Yue Xia

two

On how to use Python to achieve questions and answers Mini Game to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report