我刚刚开始一个练习,我应该完成一个基本的'愤怒的小鸟'克隆。 我被困在一个点,我想从列表中删除一个对象。该列表包含游戏中使用的所有障碍(框)。 所以如果我想在一个盒子被击中后移除它,我必须制定一个方法来做到这一点。不管我怎么做都失败了。你知道吗
class spel(object):
def __init__(self):
self.obstacles = [obstacle(50,pos=(200,90)),]
#defines all other stuff of the game
class obstacle(object):
def __init__(self,size,pos):
#defines how it looks like
def break(self):
#methode that defines what happens when the obstacles gets destroyed
spel.obstacles.remove(self)
我得到的错误是:
AttributeError: 'NoneType' object has no attribute 'obstacles'
在最后一行之后。 请原谅我的noob级别,但关键是我以后再也不需要编写代码了,所以没有必要解释一切。你知道吗
您还没有实例化spel类。你知道吗
如果你想使用这样的类,你必须对它进行初始化(创建一个实例)。你知道吗
在这样的课堂之外:
然后你可以这样调用它的方法:
或者在你的情况下,从另一个班级:
我提议如下:
返回
您已将“spel”定义为类,而不是对象。因此,您收到了一个错误,因为Python正在尝试查找spel类的成员“障碍物”,在运行单个spel对象的
__init__
方法之前,该成员不存在。你知道吗要将spel类的对象与创建的每个单独的障碍相关联,可以尝试为障碍类的对象指定一个引用其关联的spel对象的数据成员。数据成员可以在障碍类
__init__
函数中实例化。像这样:希望有帮助。你知道吗
相关问题 更多 >
编程相关推荐