python英文单词打字练习之单词爆破机

"""单词爆破机.py,这是没有定义类制作的一个英文单词打字练习小游戏。
单词会从上面的左边或边冒出来。正确输入后按回车键,它就会爆炸,
按空格键会在移动和暂停之间切换。"""

from random import randint,choice
from explosion import *
from words import *

def spawn_word():
    """产生一个单词对象,并且移动,使用screen的standard坐标系"""
          
    def init():
        """初始化字母,让它在左边或右边 随机出现。letter是一个字母对象"""
        nonlocal input_string                      # input_string是从键盘输入的字符序列
        wordobj.clear()
        input_string = ""
        wordobj.go_on = True                       # 用于移动和暂停的切换
        wordobj.hitted = False                     # 描述是否击中
        wordobj.word = choice(words_keys)          # 随机选择一个英文单词
        wordobj.translation = words[wordobj.word]  # 这是和上面的单词相对应的翻译      
        wordobj.setheading(randint(0,1) * 180)     # 设置方向0向右,180向左
        tmp = int(wordobj.heading()/180)
        x = ( 2 * tmp - 1 ) * screen_width//2      # 方向右,它会在左边出现
        y = randint(140,screen_height//2-150)
        wordobj.goto(x,y)                          # 定位到坐标
        wordobj.distance = 0                       # 注意这里重定义了distance
        screen.title(wordobj.word + " ," + wordobj.translation)
        
    def move():
        """移动字母,移动的距离超过屏幕宽度并且没有被击中则换个字母重来"""
        pass
    
    def eventfun(event):
       """按键事件执行函数,event如下所示,坐标是tkinter式坐标:
       pass
         
    def setspeed(x,y):
        """设置字母的移动速度"""
        wordobj.speed = screen.numinput("速度","请输入字母移动速度",default=5,maxval=30,minval=1)
        if wordobj.speed == None : wordobj.speed = 5 
        screen.listen()                    # 会失去焦点,需重新监听 

    input_string = ""                      # 通过键盘输入的一串字符(words.py中)    
    ziti = ("黑体",28,'normal')            # 定义字体   
    words = WORDS_DICT                     # 待练习的英语单词表
    words_values = list(words.values())    # 所有的英文单词
    words_keys = list(words.keys())        # 对应的翻译

    wordobj = Turtle(shape ='blank',visible=False) # 产生单词对象
    wordobj.color("blue")
    wordobj.penup()
    wordobj.speed(0)
    screen = wordobj.screen
    screen_width = screen.window_width()
    screen_height = screen.window_height()
    setspeed(0,0)                           # 设置速度        
    init()
    move()
    screen.cv.bind("<KeyPress>",eventfun)   # 绑定画布的按键事件
    screen.onclick(setspeed)
    screen.listen()    

if __name__ == "__main__":

    width,height = 800,600
    screen = Screen()
    screen.bgpic("bg.png")
    screen.setup(width,height)
    screen.delay(0)
    explosion_images = glob("explosion/*.gif")
    [screen.addshape(image) for image in explosion_images]
    spawn_word()
    screen.mainloop()

python英文打字游戏单词爆破机