蟒蛇,乌龟,“ onkeypress”不起作用

时间:2020-05-13 16:46:10

标签: python onkeypress python-turtle

我正在尝试做些小游戏。但是我有一个问题。 我想做的是,每当我按空格键时,它会将1加到“ score”变量中。 为此,我使用了乌龟模块(mouse.onkeypress(Space, "space")的“ onkeypress”功能 据我所知,每次我按下空格键时,这应该运行“ Space”功能。 它可以与我所做的所有其他项目一起使用,但是这次却没有,我也不知道为什么。 我尝试使用“ onkey”代替“ onkeypress”,也不起作用。

那么,如果有人有想法?

下面是代码:(函数在其末尾)


setup = turtle.Turtle()
mouse = turtle.Screen()
up_score = turtle.Turtle()

score = 0
x = 5
sc_per_se = 0

E1 = 10
E2 = 10
E3 = 10
E4 = 10
E5 = 10

up_score.hideturtle()
up_score.penup()
up_score.speed(0)
setup.penup()
setup.speed(0)

up_score.goto(-300, -300)
up_score.write(score)

setup.right(90)
setup.goto(-300, 300)

for a in range(5):
    setup.write("[Upgrade]")
    setup.forward(50)

setup.right(90)
setup.forward(75)
setup.right(90)

for a in range(5):
    global x
    setup.forward(50)
    setup.write("Element")
    setup.right(90)
    setup.forward(40)
    setup.write(x)
    setup.left(180)
    setup.forward(40)
    setup.right(90)
    x -= 1

setup.hideturtle()

def whereClick(x, y):
    #print("x=", int(x) ,"/","y=", int(y))

    if -300<=int(x)<=-255 and 300<=int(y)<=312 :
        global E1
        global sc_per_se
        if E1 < 210:
            print("Element 1 upgraded !")
            setup.goto(-255+E1, 302)
            setup.pendown()
            setup.forward(10)
            setup.penup()
            E1 += 10
            sc_per_se += 0.001
        else:
            print("Max upgrade level reached !")

    if -300<=int(x)<=-255 and 250<=int(y)<=262 :
        global E2
        global sc_per_se
        if E2 < 210:
            print("Element 2 upgraded !")
            setup.goto(-255+E2, 252)
            setup.pendown()
            setup.forward(10)
            setup.penup()
            E2 += 10
        else:
            print("Max upgrade level reached !")

    if -300<=int(x)<=-255 and 200<=int(y)<=212 :
        global E3
        global sc_per_se
        if E3 < 210:
            print("Element 3 upgraded !")
            setup.goto(-255+E3, 202)
            setup.pendown()
            setup.forward(10)
            setup.penup()
            E3 += 10
        else:
            print("Max upgrade level reached !")

    if -300<=int(x)<=-255 and 150<=int(y)<=162 :
        global E4
        global sc_per_se
        if E4 < 210:
            print("Element 4 upgraded !")
            setup.goto(-255+E4, 152)
            setup.pendown()
            setup.forward(10)
            setup.penup()
            E4 += 10
        else:
            print("Max upgrade level reached !")

    if -300<=int(x)<=-255 and 100<=int(y)<=112 :
        global E5
        global sc_per_se
        if E5 < 210:
            print("Element 5 upgraded !")
            setup.goto(-255+E5, 102)
            setup.pendown()
            setup.forward(10)
            setup.penup()
            E5 += 10
        else:
            print("Max upgrade level reached !")

def Space():
    global score
    score += 100

mouse.listen
mouse.onclick(whereClick)
mouse.onkeypress(Space, "space")

while True:
    mouse.update()


    up_score.undo()
    up_score.write(int(score))

    score += sc_per_se

turtle.done()

1 个答案:

答案 0 :(得分:0)

function Layout({ content: Content, sidebar: Sidebar }) { return ( <> <div>{Content()}</div> <div>{Sidebar()}</div> </> ); } function Page() { const [value, setValue] = useState(""); return ( <> <Layout content={() => ( <input value={value} onChange={event => setValue(event.target.value)} /> )} sidebar={() => <p>Lorem ipsum...</p>} /> </> ); } 语句应为mouse.listen

此外,当我尝试运行您的代码时,它因抱怨mouse.listen()语句而失败,这些语句似乎被多余地使用,并且在不应使用的地方。以下是我对您的代码的修改,以解决这些问题和其他问题:

global