简体   繁体   中英

Code beyond the Onclick and Listen doesn't get executed

this is my first time posting here. I'm trying to make a Title Screen before getting the user in my game. When I hit enter, I want to break out of the While loop into a the main game loop, or go into the next section of the While loop. However, when I hit enter and the onkey use my Enter function which sets the In_title to False, it doesn't seem to respond with anything. (this is in python with turtle, also I'm working using replit.)

I've created a variable named in_title = True. I've tried breaking out of the while loop using if in_title == false then break.

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()

no error message where given.

The way you have it, main_game() only is called once, right before screen's main loop begins. That means that this fragment

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

happens once and then control flow leaves main_game() forever, and the screen main loop begins.

Since the fragment only exists in main_game() , and main_game() isn't set to be called on any button press or called from anywhere else in the code, that fragment will never be called ever again. So, the if in_title == False: will never be encountered and print("got passed that") will never happen, even when in_title is False .

In this case, there is a place you already know is going to have control flow when in_title == False , and that is at the end of enter . So, you could put the code you have in mind there. This is the solution I suggested in the comments:

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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM