我在 python 编程方面遇到问题,当我尝试编写游戏时(由书本介绍:Coding Games Python DK 3),它说:
名称“Actor”未定义。
这是我的代码:
import pgzrun
from random import randint
WIDTH = 400
HEIGHT = 400
dots = []
lines = []
next_dot = 0
for dot in range(0, 10):
actor = Actor("dot")
actor.pos = randint(20, WIDTH -20), randint (20, HEIGHT - 20)
dots.append(actor)
def draw():
screen.fill("black")
number = 1
for dot in dots:
screen.draw.text(str(number), (dot.pos[0], dot.pos[1] + 12))
dot.draw()
number = number + 1
for line in lines:
screen.draw.line(line[0], line[1], (100, 0, 0))
pgzrun.go()
您正在使用 Python 库pgzero
(间接通过导入pgzrun
).
我已将游戏代码重构为多个文件(导入到主文件中),并且也观察到同样奇怪的情况
NameError:名称“Actor”未定义
错误信息。
The Actor
类似乎是“私有”的,但可以使用以下简单的代码行导入:
from pgzero.builtins import Actor, animate, keyboard
背景参见:
https://github.com/lordmauve/pgzero/issues/61 https://github.com/lordmauve/pgzero/issues/61
2019 年 8 月 18 日更新:无法导入屏幕对象,因为它是在运行时创建为全局变量(对象 = 的实例)Screen
class) 并且 IDE 支持的代码完成是不可能的。查看源代码:https://github.com/lordmauve/pgzero/blob/master/pgzero/game.py https://github.com/lordmauve/pgzero/blob/master/pgzero/game.py(特别是def reinit_screen
part)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)