当我在标题屏幕上时,如何才能让我按 Enter 键只将我发送到角色选择?

How do I make it so that pressing Enter only sends me to the character selection when I am on the title screen?

当我开始游戏时,它首先显示标题屏幕,当我按 Enter 时,它会进入角色选择屏幕。这是有意的,但是在任何时候,如果我按回车键,无论是否在标题屏幕之后,它都会进入角色选择。

有什么方法可以让 onkeypress 只有在我进入标题画面时才有效?

我试图将 onkeypress 移动到我打开标题屏幕的函数中,我还尝试制作一个 if 声明,说“如果它在标题屏幕上,那么我的onkeypress”,但两种解决方案均无效。

我怎样才能正确地做到这一点?我在replit工作。

import turtle as trtl
wn = trtl.Screen()
wn.setup(1280,720)

#the turtles
why = trtl.Turtle()
why2 = trtl.Turtle()
why3 = trtl.Turtle()

wn.bgcolor("dim grey")

#characters
closed = "mouthclosed.gif"
opened = "mouthopen.gif"
thumb = "thumb.gif"
cclosed = "mouthclosedc.gif"
copened = "mouthopenc.gif"
thumbc = "thumbc.gif"
#backgrounds
bikini = "bikini.gif"


wn.addshape(closed)
wn.addshape(opened)
wn.addshape(cclosed)
wn.addshape(copened)
wn.addshape(thumb)
wn.addshape(thumbc)


def title():
  why.pu()
  why2.pu()
  why3.pu()
  why.goto(0,300)
  why.color("rebecca purple")
  why.write("mouth pop game !", align="center",font=("Courier", 40, "bold"))
  why.goto(0,250)
  why.color("royal blue")
  why.write("Press enter to start", align="center",font=("Courier", 40, "bold"))
title()



def character_select():
  why.clear()
  why.goto(0,300)
  why.color("royal blue")
  why.write("Choose your Character", align="center",font=("Courier", 40, "bold"))
  why.goto(-200,0)
  why.shape(thumb)
  why2.goto(200,0)
  why2.shape(thumbc)


def godspeed():
  why.shape(opened)
def ohdear():
  why.shape(closed)
def bikinimc():
  wn.bgpic(bikini)

wn.onkeypress(character_select,"Return")

wn.onkeypress(godspeed,"a")
wn.onkeypress(ohdear, "s")
wn.onkeypress(bikinimc,"c")
wn.listen()
wn.mainloop()

我试图通过添加一个名为 a 的变量来解决它,该变量在开头等于 1,并在 character_select 末尾向 a 加一:

if a == 1:
  wn.onkeypress(character_select, "Return")

每次 Enter 键被按下时需要检查a是否等于1。

你的尝试

if a == 1:
  wn.onkeypress(character_select, "Return")

没有生效,因为在程序中只执行了一次

相反,将检查移到 character_select 函数中。作为 positive 检查:

def character_select():
    global a
    if a == 1:
        # ... code for going to character selection screen ...
        a += 1

或作为 否定 检查“早期 return”:

def character_select():
    global a
    if a != 1:
        return  # do NOT go to character selection screen

    # ... code for going to character selection screen ...
    a += 1

请注意,在这种情况下,进入角色选择屏幕的代码不得缩进 if 语句内。

另请注意,为了从 character_select 内部分配给 a 变量,您需要将其标记为 global