python背單詞


為了實現上述功能,需要考慮以下幾個點:

用什么樣的方式將題目和選項顯示在屏幕上
如何根據玩家的回答變選項的顏色
首先,關於第一點的思路如下
因為題目和選項的內容在不斷變化,給每一個問題和選項都設置一個輸出語句不現實
在這種情況下,用文件就比較合適
用readlines方法讀出來的文件會存到一個列表中,文件中的每一行都是列表中的一項,但是用print語句輸出這個列表后發現,列表中的每一項后面都會帶一個換行符
為了刪除這個換行符,使用了如下方法
此處還要注意,因為文件中有中文,所以打開文件時必須加上編碼方式(應該就是這個原因,我加了才對了)

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))


也就是逐行讀入,再逐項刪除換行字符
將數據存入文件還需要一定的格式:
第一行是問題,第2-5行是選項,第6行是答案
就像這樣:

接近,方法,通路
access
acess
acces
eccess
1

好了,題庫的問題解決了,接下來我們考慮顯示的問題
將文字顯示在窗口有3步:
(1)new一個Font對象(java的術語習慣了哈哈)
(2)將字體轉換為位圖
(3)貼圖
這里需要特別注意的是,Font沒有中文,我用了這個方法顯示中文:
自己下載一個中文字體的包,與py文件保存在一起,把Font構造方法中的None改成字體文件的文件名
另外,整個窗口有多處需要顯示文字,為了方便,直接把上述操作打包成一個函數,代碼如下:

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))
1
2
3
4
5
6
7
8
9
這里設置了shadow參數,決定是否顯示2D字體(就是普通字體后面帶一塊陰影哈哈)

接下來,我們來解決如何根據用戶的輸入來改變選項顏色的問題:
邏輯很簡單:接收用戶輸入->判斷是否正確->改變相應選項的顏色
接收用戶輸入+判斷正誤:

#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


每一題的正確答案都保存在“第六行”(題庫文件把答案序號放在了每一題的最后),用correct來保存
與用戶輸入作對比,正確就置scored屬性(下面會講為啥定義這個變量)為True,反之把用戶輸入保存在wronganswer變量中,因為錯誤的選項需要變成紅色,並置failed屬性(下面會講)為True
在近期的學習中,我覺得在某些情況下很有必要定義“flag”類型變量,就是標記,它只有兩個值,可以放在if語句中做相應操作
這個某些情況,我覺得就是當你的程序需要根據某些結果做出某些反應
這里的scored和failed就是
用戶輸入正確時,需要將正確選項變成綠色,輸入錯誤時,需要將用戶的選擇變紅,正確的選項變綠

變換顏色:
我覺得這個程序的神奇之處之一就是變顏色這個操作
我們知道,顏色就在將文本貼在窗口上的函數中的一個參數
四個選項的顏色參數每一題都在變化
我們現在能利用的就是已經知道了用戶的選擇的選項和正確選項,而且它們都是1,2,3,4這種類型
此時就應該想到列表的下標
我們設置一個放四個選項顏色的列表,通過判斷用戶輸入的結果來改變列表的對應項
最后直接取出列表中的對應項來做文本的顏色參數就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

#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])

好了,我覺得這個程序的精華大概就是這些
哦對,游戲的主邏輯都放在了類里,前面說的什么failed,scored 都是類的屬性
其實我本來還想說一下游戲的循環,但是我一直沒有把while循環理解透,下次再來吧~
另外還需說明的是,這個程序並不完全是我想出來的,我參照了Jonathan S.Harbour編寫的Trivia游戲

接下來是源代碼,字體文件和題庫大家可以自行下載和編寫,題庫用記事本寫就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()

 

如果我的代碼還有可以優化改進的部分,歡迎各路大神指出

自認為今后功能優化可以有:
1.題庫存放到數據庫里,通過數據庫來調
2.將玩家答錯的題提取出來,定期發送到他的微信上,提醒他背誦
(因為前兩天看到一個文章說可以用python寫一個微信提醒備忘錄,覺得hen6)
3. 聯機玩兒法,在規定的時間,看誰答對的最多,並設置排行榜


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM