前几天我决定做一个非常简单的游戏 - " Cookie Clicker"。直到我偶然发现了一个getcreen错误,一切正常。也许你们知道这个问题的解决方案了吗?
回溯:
NameError: name 'getscreen' is not defined on line 41
我怀疑问题隐藏的部分代码:
def clicking():
if distance( cookie.pos() ) < 2:
COOKIE_COUNT = COOKIE_COUNT + 1
screen = getscreen()
screen.onclick( clicking )
整个代码:
import time
import turtle
screen = turtle.Screen()
image_COOKIEC = "COOKIEC.png"
image_COOKIEUN = "COOKIEUN.png"
image_UPGRADEBG = "UPGRADEBG.png"
cookie = turtle.Turtle()
COOKIE_COUNT = 0
COOKIE_COUNT1 = turtle.Turtle()
upgrade = turtle.Turtle()
upgrade1 = turtle.Turtle()
upgrade2 = turtle.Turtle()
upgrade3 = turtle.Turtle()
upgrade4 = turtle.Turtle()
upgrade5 = turtle.Turtle()
screen.addshape(image_COOKIEUN)
cookie.penup()
cookie.shape(image_COOKIEUN)
cookie.speed(0)
cookie.goto(0, 0)
COOKIE_COUNT1.speed(0)
COOKIE_COUNT1.penup()
COOKIE_COUNT1.hideturtle()
COOKIE_COUNT1.goto(-40, 170)
COOKIE_COUNT1.write("Cookies: %d" % COOKIE_COUNT, font=(None, 14, "bold"))
upgrade.speed(0)
upgrade.penup()
upgrade.hideturtle()
upgrade.goto(110, -190)
upgrade.write("Upgrades", font=(None, 12, "bold"))
def clicking():
if distance( cookie.pos() ) < 2:
COOKIE_COUNT = COOKIE_COUNT + 1
screen = getscreen()
screen.onclick( clicking )
答案 0 :(得分:2)
你需要做:
screen = turtle.getscreen()
或
from turtle import getscreen
screen = getscreen()
就个人而言,我推荐第一种方法,因为它显示了getscreen
的来源。