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