超出Onclick和Listen的代码不会执行

时间:2019-10-08 21:20:39

标签: python python-3.x

这是我第一次在这里发布。 我正在尝试制作标题屏幕,然后再吸引用户进入我的游戏。当我按下Enter键时,我想从While循环中跳出到主游戏循环中,或者进入While循环的下一部分。但是,当我按Enter键并且onkey使用将In_title设置为False的Enter函数时,它似乎没有任何响应。 (这是在python中用turtle编写的,也是我在使用replit。)

我创建了一个名为in_title = True的变量。 我试过使用if in_title == false然后break打破while循环。

import turtle
import random 
import math
import time
#set up for the screen title. 
screen_size = [500,500]  
screen_color = "#1a2645"
t = turtle.Turtle()
screen = t.getscreen()
screen.setup(screen_size[0],screen_size[1])
screen.bgcolor(screen_color)
screen.tracer(0)
t.ht()
game_state = 1 #to see if the game is still runninng. #0, 1 are no, yes.
in_title = True #to see if the game is in title or not.

select = turtle.Turtle()
select.color("yellow")
select.speed(0)
select.up()
select.setx(-80) #putting it in the middle of the text.
select.sety(30)
#all of the entity here:
player = {"p": turtle.Turtle(), "lives": 3, "color": "white", "rad": 15}
player["p"].ht()
harm = {"color": "red", "rad": 10}
num_harm = 10
good = {"g": turtle.Turtle(), "color": "white", "rad": 8}
good["g"].ht()
#universal movement function.
def moving_harm(h,rad): #function to move the harm objects.
  if h.ycor() == (250 + rad/2):
    h.up()
    h.setx(random.randrange(-250,250))
    h.sety(h.ycor() - 5)
    screen.update()
  if (h.ycor() != 250 + rad/2) and (h.ycor() != -250 - rad/2):
    h.sety(h.ycor() - 5)
  if (h.ycor() == (-250 - rad/2)):
    h.sety(250 + rad/2)
    screen.update()
def up(): 
  if game_state == 1:
    select.sety(30) #hard coded the number so it's in the middle. 
    print(select.ycor())
    screen.update()
# def down(): 
# def left():
# def right():
def enter():
  global in_title
  if (game_state == 1):
    if(select.ycor() == 30):
      select.down()
      select.forward(150)
      select.setheading(180)
      select.color("white")
      select.forward(150)
      screen.update()
      time.sleep(0.2)
      screen.clear()
      screen.update()
      time.sleep(0.1)
      screen.setup(screen_size[0],screen_size[1])
      screen.bgcolor(screen_color)

      in_title = False


#main menu operation:
def main_game():
  global game_state
  #writing out the tittle screen.
  menu = turtle.Turtle()
  menu.up()
  menu.sety(150)
  menu.color("white")
  menu.write("Sorta a Side Scroller Game", align="center", font=("Arial", 22, "normal"))
  menu.sety(100)
  menu.write("By Silver", align="center", font=("Arial", 15, "normal"))
  menu.ht()
  menu.sety(25)
  menu.write("Start game", align="center", font=("Arial", 15, "normal"))
  #create all the harm turtles:
  for i in range(num_harm):
    h = "h"+str(i+1)
    harm[str(h)] = turtle.Turtle()
    harm[str(h)].up()
    harm[str(h)].ht()
  #handle control at the title screen. 
  screen.onkey(enter, "Enter")
  if in_title == False: 
    print("got passed that")


main_game()
screen.listen()
screen.update()
screen.mainloop()

在没有错误消息的地方。

1 个答案:

答案 0 :(得分:0)

您拥有它的方式main_game()仅在屏幕主循环开始之前被调用一次。这意味着该片段

if in_title == False: 
    print("got passed that")

发生一次,然后控制流永远离开main_game(),并且屏幕主循环开始。

由于该片段仅存在于main_game()中,并且没有将main_game()设置为可在任何按钮按下时调用或在代码中的任何其他位置调用,因此该片段将永远不会再被调用。因此,即使if in_title == False:print("got passed that"),也不会遇到in_title,也不会发生False

在这种情况下,in_title == False时有一个您已经知道有控制流的位置,该位置位于enter的末尾。因此,您可以在其中考虑一下代码。这是我在评论中建议的解决方案:

def enter():
    global in_title
    if (game_state == 1):
        if(select.ycor() == 30):
            # ...
            in_title = False
            print("got passed that")


#main menu operation:
def main_game():
    # ... 
    screen.onkey(enter, "Enter")