屏幕 = getscreen() 错误

screen = getscreen() error

前几天我决定制作一个非常简单的游戏 - "Cookie Clicker"。在我偶然发现 getscreen 错误之前一切都很好。也许你们知道问题的解决方案?

回溯:

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 )

您需要:

screen = turtle.getscreen()

from turtle import getscreen
screen = getscreen()

就我个人而言,我推荐第一种方法,因为它显示了 getscreen 的来源。