问题描述
我对所有字母都这样做了,整个序列都在一个函数中。在变量“字母”的第一次迭代期间,它会正常工作,但在第二次迭代、第三次等期间,代码将为变量字母的过去迭代运行 fix_fall 函数。例如,如果第一次 letter == "a" fix_fall 只会在您单击 "a" 时运行,但是当您将 letter 更改为等于 "f" 时,它会在您单击 "a" 或 "f" 时运行 fix_fall。如何让“a”的按键停止激活,直到字母再次等于“a”。
lenny = 0
def draw_an_letter():
global letter
global lenny
apple_xcor = apple.xcor()
apple_ycor = apple.ycor()
drawer_xcor = apple_xcor - 14
drawer_ycor = apple_ycor - 32
if lenny > 0:
del letter
letter = random.choice(letters)
letter = letter.lower()
drawer.goto(drawer_xcor,drawer_ycor)
drawer.color("white")
drawer.write(letter,font=("Arial",35,"bold"))
lenny = lenny + 1
def fix_fall():
fall_apple(apple)
apple_newxcor = random.randint(-130,150)
apple.goto(apple_newxcor,40)
apple.showturtle()
draw_an_letter()
wn.listen()
if letter == "a":
wn.onkeypress(fix_fall,"a")
if letter == "b":
wn.onkeypress(fix_fall,"b")
if letter == "c":
wn.onkeypress(fix_fall,"c")
if letter == "d":
wn.onkeypress(fix_fall,"d")
if letter == "e":
wn.onkeypress(fix_fall,"e")
if letter == "f":
wn.onkeypress(fix_fall,"f")
if letter == "g":
wn.onkeypress(fix_fall,"g")
if letter == "h":
wn.onkeypress(fix_fall,"h")
if letter == "i":
wn.onkeypress(fix_fall,"i")
if letter == "j":
wn.onkeypress(fix_fall,"j")
if letter == "k":
wn.onkeypress(fix_fall,"k")
if letter == "l":
wn.onkeypress(fix_fall,"l")
if letter == "m":
wn.onkeypress(fix_fall,"m")
if letter == "n":
wn.onkeypress(fix_fall,"n")
if letter == "o":
wn.onkeypress(fix_fall,"o")
if letter == "p":
wn.onkeypress(fix_fall,"p")
if letter == "q":
wn.onkeypress(fix_fall,"q")
if letter == "r":
wn.onkeypress(fix_fall,"r")
if letter == "s":
wn.onkeypress(fix_fall,"s")
if letter == "t":
wn.onkeypress(fix_fall,"t")
if letter == "u":
wn.onkeypress(fix_fall,"u")
if letter == "v":
wn.onkeypress(fix_fall,"v")
if letter == "w":
wn.onkeypress(fix_fall,"w")
if letter == "x":
wn.onkeypress(fix_fall,"x")
if letter == "y":
wn.onkeypress(fix_fall,"y")
if letter == "z":
wn.onkeypress(fix_fall,"z")
解决方法
我不知道这是否解决了问题,因为我不明白问题是什么(当您添加更多信息时,我很乐意编辑答案),但与此同时,我对这个程序进行了一些有趣的编码:它会在屏幕上的随机 x 位置写下您按下的字母(这有点像您发布的片段中想要执行的操作)。
请注意,我避免使用一个不寻常的构造(无论如何对于 python)复制代码 26 次:closure。闭包究竟是什么超出了本答案的范围,但您可以将其(从极其广泛的意义上说)视为绑定到变量的函数。
函数 closure_writer
创建一个新函数,绑定到您调用 letter
的值 closure_writer
,然后返回 那个功能。
对于每个字母,函数 closure_writer
被调用,返回的函数被链接为该字母的回调。
import random
from string import ascii_lowercase
import turtle
def random_xpos():
"""Move the turtle to a random x position."""
newxcor = random.randint(-600,600)
t.setx(newxcor)
def write_letter(letter):
"""Write a letter in the current spot."""
t.write(letter,font=("Arial",35,"bold"))
def closure_writer(letter):
"""Return a function that writes the specified letter."""
def writer():
"""Write the specified letter."""
random_xpos()
write_letter(letter)
# note that we are NOT calling the function,but returning it
return writer
# setup the turtle
t = turtle.Turtle()
t.penup()
turtle.listen()
# register all the keypress
for letter in ascii_lowercase:
# closure_writer returns a new function that will be called at each keypress
# and each function is created with its own letter associated
turtle.onkeypress(closure_writer(letter),letter)
# start the loop
turtle.mainloop()
干杯!