-1

有人可以向我解释为什么无论您点击哪里,程序都会给出“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()
4

1 回答 1

2

你忘了减号,你必须<>in替换

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)
于 2020-04-14T01:42:06.660 回答