我想创建一个乌龟游戏。当我在游戏中点击乌龟形状时,它应该给我+1分,这样我就可以计算分数了。但当我执行命令并点击乌龟形状时,它会给我一个名称错误,即
NameError: name 'score_turtle' is not defined
即使它是在上级命令中定义的。请帮我修一下。
import turtle
import random
screen = turtle.Screen()
screen.bgcolor("light blue")
FONT = ('Arial', 25, 'normal')
skor = 0
turtle_list = []
#score turtle
def score_turtle_setup():
score_turtle = turtle.Turtle()
score_turtle.hideturtle()
score_turtle.penup()
score_turtle.color("dark blue")
top_screen = screen.window_height() / 2
y = top_screen * 0.85
score_turtle.setpos(-200,y)
score_turtle.write(arg=f"SCORE: 0", move=False, align="center", font=FONT)
def make_turtle(x, y):
point = turtle.Turtle()
def handle_click(x, y):
global skor
skor += 1
score_turtle.write(arg=f"SCORE: {skor}", move=False, align="center", font=FONT)
point.onclick(handle_click)
point.shape("turtle")
point.penup()
point.color("green")
point.shapesize(1.5)
point.goto(x * 12, y*12)
turtle_list.append(point)
x_coordinats = [-20,-10,0,10,20]
y_coordinats = [20,10,0,-10]
def setup_turtles():
for x in x_coordinats:
for y in y_coordinats:
make_turtle(x, y)
def hide_turtles():
for point in turtle_list:
point.hideturtle()
def show_turtles():
random.choice(turtle_list).showturtle()
turtle.tracer(0)
score_turtle_setup()
setup_turtles()
hide_turtles()
show_turtles()
turtle.tracer(1)
turtle.mainloop()
我试着调用函数
score_turtle_setup
来自
handle_click
功能,但不起作用。