[英]Code beyond the Onclick and Listen doesn't get executed
這是我第一次在這里發帖。 在讓用戶進入我的游戲之前,我正在嘗試制作一個標題屏幕。 當我按下回車鍵時,我想跳出 While 循環進入主游戲循環,或者 go 進入 While 循環的下一部分。 但是,當我按 Enter 鍵並使用我的 Enter function 將 In_title 設置為 False 時,它似乎沒有任何響應。 (這是在帶海龜的 python 中,我也在使用 repli。)
我創建了一個名為 in_title = True 的變量。 我嘗試使用 if in_title == false then 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()
沒有給出錯誤消息。
你擁有它的方式, 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")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.