python word back

In order to achieve the above functions, it is necessary to consider the following points:

What kind of a way to change the title and options displayed on the screen
how to change the color options of the player's answer
first, thinking about the first point of the following
as the title and content options are constantly changing, to every problem and options are set to a output statement unrealistic
in this case, it is more appropriate to use the file
read out after a readlines method of file saved to a list, each line in the file is a list, but the list using the print statement output found that each will bring back a list of line breaks
to delete this line break, use the following method
here would also like to note that, because the file has Chinese, it is necessary to add coding to open the file (for this reason should I added the fishes)

f=open(textname,encoding='utf-8') #transfer gbk(the way of chinese text) to utf-8
question_data=f.readlines()
f.close()
for question_line in question_data:
self.data.append(question_line.strip()) #load in data[] in lines without any strip
self.total+=1

def print_text(font,x,y,text,color=(255,255,255),shadow=True):
#font conversion
if shadow:
#create 2D font
img_text=font.render(text,True,(0,0,0))
screen.blit(img_text,(x-2,y-2))

img_text=font.render(text,True,color)
screen.blit(img_text,(x,y))


That is read line by line, item by item and then delete the newline character
will also need to file data into a certain format:
the first line is the problem, is the 2-5 line options, Line 6 is the answer
like this:

Close, method, via
Access
Acess
ACCES
eccess
. 1

Well, the problem's solved the exam, then we consider the problem of displaying
text in the window display of three steps:
(1) a new new Font object (java terminology used to it ha ha)
(2) to convert a bitmap font
( 3) maps
to note here is, no Chinese Font, I have used this method to display Chinese:
own a Chinese font package download, save the file together with py, the Font constructor of change font files None name
Further, the entire window display text with multiple needs, for convenience, directly to the package as a function of the operation, as follows:

def print_text(font,x,y,text,color=(255,255,255),shadow=True):
#font conversion
if shadow:
#create 2D font
img_text=font.render(text,True,(0,0,0))
screen.blit(img_text,(x-2,y-2))

= font.render img_text (text, True, Color)
screen.blit (img_text, (X, Y))
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
herein shadow set parameters, to decide whether to display a 2D font (font is common with the back a shadow Haha)

Next, we address how to change the color options based on user input issues:
The logic is simple: to receive user input -> determine whether the right -> to change the color of the corresponding option
to receive user input + True or false:

#handdle player's answer
def handdle_answer(self,player_input):
if not self.scored and not self.failed:
#in case to when the player have answered question without pressing enter to next
if player_input==self.correct:
self.scored=True
self.score+=1
else:
self.wronganswer=player_input
self.failed=True

#handdle player's answer
def handdle_answer(self,player_input):
if not self.scored and not self.failed:
#in case to when the player have answered question without pressing enter to next
if player_input==self.correct:
self.scored=True
self.score+=1
else:
self.wronganswer=player_input
self.failed=True


The correct answer for each question are stored in the "sixth line" (the exam file number on the last answer each question), to save with the correct
user input for comparison, the right to set scored attribute (below speaks for the what defined this variable) is True, and vice versa wronganswer user input is stored in a variable, because the wrong options need to turn red, and set failed property (below speaks) to True
in a recent study, I think in some cases under it is necessary to define the "flag" type of variable is marked, it has only two values, it can be placed in the appropriate action if statements do
this in some cases, I think it is when you need to make certain the program based on the results of certain reaction
here is scored and failed
when user input is correct, you need to correct option turns green when the input error, the user needs to select the red, green correct option

Change the color:
I think one of the magic of this program is to change the color of this operation
we know, the color of the text attached to a function parameter on the window of
each title four color options parameters are changing
us now can take advantage of that already know the correct selection of options and user options, and they are of this type 1,2,3,4
at this point you should think subscript list
we set up a list of four options put color, the determination result input by the user to change the list of items corresponding to
the last color parameters taken directly to do text corresponding item in the list on the ok

#handdle player's answer
#respond to correct answer
if self.scored:
self.colors=[white,white,white,white] #Is this line restless?
self.colors[self.correct-1]=green
print_text(font1,210,380,"CORRECT!",green)
print_text(font2,130,420,"Press Enter For Next Question",green)
elif self.failed:
self.colors=[white,white,white,white]
self.colors[self.correct-1]=green
self.colors[self.wronganswer-1]=red
print_text(font1,220,380,"INCORRECT!",red)
print_text(font2,170,420,"Press Enter For Next Question",green) #former is x,latter is y

Answers #display
print_text (font1,5,170, "ANSWERS")
print_text (font2,20,210, ". 1 -" + self.data [self.current +. 1], self.colors [0])
print_text (font2,20,240, "2 - "+ self.data [self.current + 2], self.colors [. 1])
print_text (font2,20,270,". 3 - "+ self.data [self.current. 3 +], self.colors [2])
print_text (font2,20,300, "4 -" + self.data [self.current + 4], self.colors [3])

well, I think the essence of this program is about all
oh yes, the game will put the main logic in the class, said earlier what failed, scored all classes of property
in fact, I had wanted to talk about the cycle of the game, but I did not understand thoroughly the while loop, the next time you come back -
also a need to explain that, this program is not quite what I want out, I refer to the preparation of Jonathan S.Harbour Trivia game

Next is the source code, font files, and you can download their own exam and written exam written in Notepad OK

import pygame
import sys
from pygame.locals import *
import time

#main logic
class Recite():
def __init__(self,textname):
self.data=[] #file list
self.current=0 #data current index
self.correct=0 #true answer
self.score=0 #player's socre
self.scored=False #flag
self.failed=False #flag
self.wronganswer=0 #player's wronganswer
self.total=0 #total of the file's lines
self.colors=[white,white,white,white] #options'color
self.question_number=1 #record question number
#load file to data list
f=open(textname,encoding='utf-8') #transfer gbk(the way of chinese text) to utf-8
question_data=f.readlines()
f.close()
for question_line in question_data:
self.data.append(question_line.strip()) #load in data[] in lines without any strip
self.total+=1

#show_question
def show_question(self):
print_text(font1,160,5,"CET4 WORDS TEST")
print_text(font2,150,500-30,"Press keys (1-4) To Answer",purple)
print_text(font2,530,5,"SCORE",purple)
print_text(font2,550,25,str(self.score),purple)

#get correct answer from data[]
self.correct=int(self.data[self.current+5]) #the lines are string

#display question
print_text(font1,5,80,"QUESTION "+str(self.question_number))
print_text(font2,20,120,self.data[self.current],yellow)

#respond to correct answer
if self.scored:
self.colors=[white,white,white,white] #Is this line restless? Yes
self.colors[self.correct-1]=green
print_text(font1,210,380,"CORRECT!",green)
print_text(font2,130,420,"Press Enter For Next Question",green)
elif self.failed:
self.colors=[white,white,white,white]
self.colors[self.correct-1]=green
self.colors[self.wronganswer-1]=red
print_text(font1,220,380,"INCORRECT!",red)
print_text(font2,170,420,"Press Enter For Next Question",green) #former is x,latter is y

#display answers
print_text(font1,5,170,"ANSWERS")
print_text(font2,20,210,"1-"+self.data[self.current+1],self.colors[0])
print_text(font2,20,240,"2-"+self.data[self.current+2],self.colors[1])
print_text(font2,20,270,"3-"+self.data[self.current+3],self.colors[2])
print_text(font2,20,300,"4-"+self.data[self.current+4],self.colors[3])

 

#next_question
def next_question(self):
self.scored=False
self.failed=False
self.current+=6
self.colors=[white,white,white,white]
self.correct=0 #reset arributes
if self.current>self.total:
self.current=0
self.question_number+=1

#handdle player's answer
def handdle_answer(self,player_input):
if not self.scored and not self.failed:
#in case to when the player have answered question without pressing enter to next
if player_input==self.correct:
self.scored=True
self.score+=1
else:
self.wronganswer=player_input
self.failed=True


def print_text(font,x,y,text,color=(255,255,255),shadow=True):
#font conversion
if shadow:
#create 2D font
img_text=font.render(text,True,(0,0,0))
screen.blit(img_text,(x-2,y-2))

img_text=font.render(text,True,color)
screen.blit(img_text,(x,y))

#program initialized
#create pygame windows,and make preparations to the game
pygame.init()
screen=pygame.display.set_mode((600,500))
pygame.display.set_caption("Cet-4 words dictation")
font1=pygame.font.Font('mnjyx.ttf',40) #Chinese font or it can't be identified
font2=pygame.font.Font('mnjyx.ttf',24)
white=255,255,255
purple=255,0,255
yellow=255,255,0
green=0,255,0
red=255,0,0


#new a object
recite=Recite("CET4 words.txt")

#game loop
while True:
for event in pygame.event.get():
if event.type== QUIT:
sys.exit()
elif event.type==KEYUP:
if event.key==pygame.K_ESCAPE:
sys.exit()
elif event.key==pygame.K_1:
recite.handdle_answer(1)
elif event.key==pygame.K_2:
recite.handdle_answer(2)
elif event.key==pygame.K_3:
recite.handdle_answer(3)
elif event.key==pygame.K_4:
recite.handdle_answer(4)
elif event.key==pygame.K_RETURN:
recite.next_question()
#clear screen
screen.fill((0,0,200))

#show_question
recite.show_question()

time.sleep(0.0005)

#update
pygame.display.update()

 

If my code still can be optimized and improved partially, welcome of all pointed out that the great God

Since that function to optimize future may include:
1. The exam is stored in a database, to adjust by the database
2. Extract the players got it wrong out of the question, send a letter to his micro regularly remind him recite
(as seen a couple of days ago the article says you can use python to write a letter to remind micro memo, feel hen6)
3. play online law within the prescribed time, to see who the most correct answers, and set up rankings

Guess you like

Origin www.cnblogs.com/guanghuili/p/11970253.html