Python单词记忆小程序海龟画图版.py

"""单词记忆小程序海龟画图版.py"""

from time import sleep
from turtle import *
from tkinter import messagebox
from random import choice

game_title = "单词记忆小程序"

words = []                              #定义英语单词表
translate = []                          #定义翻译表
f = open("words.txt")
for line in f:
    if  ":" in line:
        s = line.split(":")             #用:辟开line
        words.append(s[0])              #索引为0的字符串为英文单词
        translate.append(s[1])          #索引为1的字符串为翻译

amounts = len(words)
score = 0
fontstyle1 = ("宋体",20,"normal")        #字体风格1
fontstyle2 = ("黑体",50,"bold")          #字体风格2

screen = Screen()                       #新建屏幕
screen.title(game_title)                #屏幕标题
screen.setup(800,600)                   #设定屏幕长和高
screen.bgpic("背景.png")                #设定背景
screen.delay(0)                         #设定绘画延时为0

t = Turtle(visible = False)       #用于写字的海龟对象
t.penup()
t.color("yellow")                 #画笔颜色为黄
t.goto(-200,300)                  #坐标定位

messagebox.showinfo(game_title, "Hello,我是计算机,欢迎来到风火轮少儿编程。")
messagebox.showinfo(game_title, "接下来显示英语单词与其对应翻译,请尽快记忆。")

for  i in range(amounts):
    info = words[i] + ":"  +  translate[i]
    t.write(info,font = fontstyle1)
    t.sety(t.ycor() - 50)

counter = Turtle(visible = False)  #用于倒计时的海龟对象
counter.penup()
counter.color("cyan")              #倒计时的颜色为青色
counter.goto(100,0)                #坐标定位
for i in range(11,0,-1):
    counter.clear()
    counter.write(str(i),font = fontstyle2)
    sleep(1)

counter.clear()
messagebox.showinfo(game_title, "记住了吗?练习马上就要开始了。")
t.clear()
 
while True:
    word  = choice(words)                                  #出题,随机选择一个单词
    index = words.index(word)                              #取这个单词的索引号,以便对应
    answer = screen.textinput(game_title,"请写出'" + word + "'的汉语翻译:")     #提示输入答案

    if answer =="exit" or answer == "quit":                 #输eixt或quit退出循环
        break
    
    if answer == translate[index]:                         #如果答案和translate表中同样索引的字符串相等
        score = score + 10
        messagebox.showinfo(game_title,"回答正确,加10分!当前得分:"  + str(score))  #打印‘回答正确......
        
    if answer=="" or answer == None:                       #没有输入或取消,表示忽略         
        #messagebox.showinfo(game_title,"你选择了忽略...")
        continue
        
    if answer != translate[index]:  
        score = score - 10
        messagebox.showinfo(game_title,"回答错误,减10分!当前得分:" + str(score))  #否则就是输入错误了


messagebox.showinfo(game_title,"你的总得分:" + str(score))
screen.bye()