1

我正在尝试做一个小游戏。但我有一个问题。我想要做的是,每次我按下空格键时,它都会将 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()
4

1 回答 1

0

声明mouse.listen应该是。mouse.listen()

此外,当我尝试运行您的代码时,它失败了,抱怨说global似乎被冗余使用的语句以及在不应该使用的地方。以下是我对您的代码的修改,以解决这些问题和其他问题:

from turtle import Screen, Turtle

score = 0
sc_per_se = 0

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

def whereClick(x, y):
    global sc_per_se
    global E1, E2, E3, E4, E5

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

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

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

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

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

def Space():
    global score
    score += 100

screen = Screen()

up_score = Turtle()
up_score.hideturtle()
up_score.speed('fastest')
up_score.penup()
up_score.goto(-300, -300)
up_score.write(score)

setup = Turtle()
setup.hideturtle()
setup.speed('fastest')
setup.penup()
setup.goto(-300, 300)
setup.right(90)

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

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

for n in range(5, 0, -1):
    setup.forward(50)
    setup.write("Element")
    setup.right(90)
    setup.forward(40)
    setup.write(n)
    setup.left(180)
    setup.forward(40)
    setup.right(90)

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

while True:
    screen.update()

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

    score += sc_per_se

screen.mainloop()  # never reached
于 2020-05-13T17:14:27.410 回答