turtle 中的主菜单
A main menu in turtle
谁能给我解释一下,为什么无论您点击哪里,程序都会给出 "Else" 响应,以及为什么它会不断重复直到崩溃?我没有 for 循环或 while 循环,所以我不明白。可能是 onscreenclick() 或 listen() 函数吗?另外,我知道部分菜单不在屏幕上的问题。我打算在检查它的效果后修复它。
import turtle
pen = turtle.Turtle()
pen.speed(0)
#Creating Menu option buttons
def button(length):
for i in range(4):
pen.forward(length)
pen.left(90)
def column(n, length):
pen.left(270)
for i in range(n):
button(length)
pen.forward(length)
pen.penup()
pen.left(90)
pen.forward(n * length)
pen.left(180)
pen.pendown()
column(5, 100)
#Menu Options
pen.penup()
pen.goto(8, -46)
pen.write("START GAME!", font=("Arial",12,"normal"))
pen.penup()
pen.goto(6, -145)
pen.write("RULES", font=("Arial",12,"normal"))
pen.penup()
pen.goto(3, -248)
pen.write("HIGH SCORE", font=("Arial",12,"normal"))
pen.penup()
pen.goto(4, -343)
pen.write("FAQ", font=("Arial",12,"normal"))
pen.penup()
pen.goto(3, -450)
pen.write("QUIT GAME", font=("Arial",12,"normal"))
#Making options clickable
def btnclick(x,y):
if x > 0 and x < 101 and y > 0 and y < -101:
print("Start Game")
print(x, y)
turtle.clearscreen()
elif x > 0 and x < 101 and y > 101 and y < -201:
print("Rules")
print(x, y)
turtle.clearscreen()
elif x > 0 and x < 101 and y > 201 and y < -301:
print("Highscore")
print(x, y)
turtle.clearscreen()
elif x > 0 and x < 101 and y > 301 and y < -401:
print("Hi")
print(x, y)
turtle.clearscreen()
elif x > 0 and x < 101 and y > 401 and y < -501:
print("Hi")
print(x, y)
turtle.clearscreen()
elif x > 0 and x < 101 and y > 501 and y < -601:
print("Hi")
print(x, y)
turtle.clearscreen()
else:
print("Click One Of The Options!")
print(x, y)
btnclick(x, y)
turtle.onscreenclick(btnclick, 1)
turtle.listen()
turtle.done()
您忘记了减号,您必须将
中的 <
替换为 >
y > 0 and y < -101:
y > 101 and y < -201:
y > 201 and y < -301:
y > 301 and y < -401:
y > 401 and y < -501:
y > 501 and y < -601:
应该是。
y < 0 and y > -101
y < -101 and y > -201
y < -201 and y > -301
# etc.
或更短
0 > y > -101
-101 > y > -201
-201 > y > -301
# etc.
编辑: 也许你应该在某些地方使用 >=
,因为旧代码不适用于 y == -101
、y == -201
等.
0 > y >= -101
-101 > y >= -201
-201 > y >= -301
etc.
全功能
def btnclick(x,y):
if 0 < x < 101 and 0 > y > -101:
print("Start Game")
print(x, y)
turtle.clearscreen()
elif 0 < x < 101 and -101 > y > -201:
print("Rules")
print(x, y)
turtle.clearscreen()
elif 0 < x < 101 and -201 > y > -301:
print("Highscore")
print(x, y)
turtle.clearscreen()
elif 0 < x < 101 and -301 > y > -401:
print("Hi")
print(x, y)
turtle.clearscreen()
elif 0 < x < 101 and -401 > y > -501:
print("Hi")
print(x, y)
turtle.clearscreen()
elif 0 < x < 101 and -501 > y > -601:
print("Hi")
print(x, y)
turtle.clearscreen()
else:
print("Click One Of The Options!")
print(x, y)
btnclick(x, y)
谁能给我解释一下,为什么无论您点击哪里,程序都会给出 "Else" 响应,以及为什么它会不断重复直到崩溃?我没有 for 循环或 while 循环,所以我不明白。可能是 onscreenclick() 或 listen() 函数吗?另外,我知道部分菜单不在屏幕上的问题。我打算在检查它的效果后修复它。
import turtle
pen = turtle.Turtle()
pen.speed(0)
#Creating Menu option buttons
def button(length):
for i in range(4):
pen.forward(length)
pen.left(90)
def column(n, length):
pen.left(270)
for i in range(n):
button(length)
pen.forward(length)
pen.penup()
pen.left(90)
pen.forward(n * length)
pen.left(180)
pen.pendown()
column(5, 100)
#Menu Options
pen.penup()
pen.goto(8, -46)
pen.write("START GAME!", font=("Arial",12,"normal"))
pen.penup()
pen.goto(6, -145)
pen.write("RULES", font=("Arial",12,"normal"))
pen.penup()
pen.goto(3, -248)
pen.write("HIGH SCORE", font=("Arial",12,"normal"))
pen.penup()
pen.goto(4, -343)
pen.write("FAQ", font=("Arial",12,"normal"))
pen.penup()
pen.goto(3, -450)
pen.write("QUIT GAME", font=("Arial",12,"normal"))
#Making options clickable
def btnclick(x,y):
if x > 0 and x < 101 and y > 0 and y < -101:
print("Start Game")
print(x, y)
turtle.clearscreen()
elif x > 0 and x < 101 and y > 101 and y < -201:
print("Rules")
print(x, y)
turtle.clearscreen()
elif x > 0 and x < 101 and y > 201 and y < -301:
print("Highscore")
print(x, y)
turtle.clearscreen()
elif x > 0 and x < 101 and y > 301 and y < -401:
print("Hi")
print(x, y)
turtle.clearscreen()
elif x > 0 and x < 101 and y > 401 and y < -501:
print("Hi")
print(x, y)
turtle.clearscreen()
elif x > 0 and x < 101 and y > 501 and y < -601:
print("Hi")
print(x, y)
turtle.clearscreen()
else:
print("Click One Of The Options!")
print(x, y)
btnclick(x, y)
turtle.onscreenclick(btnclick, 1)
turtle.listen()
turtle.done()
您忘记了减号,您必须将
中的<
替换为 >
y > 0 and y < -101:
y > 101 and y < -201:
y > 201 and y < -301:
y > 301 and y < -401:
y > 401 and y < -501:
y > 501 and y < -601:
应该是。
y < 0 and y > -101
y < -101 and y > -201
y < -201 and y > -301
# etc.
或更短
0 > y > -101
-101 > y > -201
-201 > y > -301
# etc.
编辑: 也许你应该在某些地方使用 >=
,因为旧代码不适用于 y == -101
、y == -201
等.
0 > y >= -101
-101 > y >= -201
-201 > y >= -301
etc.
全功能
def btnclick(x,y):
if 0 < x < 101 and 0 > y > -101:
print("Start Game")
print(x, y)
turtle.clearscreen()
elif 0 < x < 101 and -101 > y > -201:
print("Rules")
print(x, y)
turtle.clearscreen()
elif 0 < x < 101 and -201 > y > -301:
print("Highscore")
print(x, y)
turtle.clearscreen()
elif 0 < x < 101 and -301 > y > -401:
print("Hi")
print(x, y)
turtle.clearscreen()
elif 0 < x < 101 and -401 > y > -501:
print("Hi")
print(x, y)
turtle.clearscreen()
elif 0 < x < 101 and -501 > y > -601:
print("Hi")
print(x, y)
turtle.clearscreen()
else:
print("Click One Of The Options!")
print(x, y)
btnclick(x, y)