所以我一直在做一个小行星游戏,我遇到了以下问题:
在我的主文件中,我创建了一个实例Ship(从GameObject继承)并将其传递给我的EntitySystem。但是当我试图更新EntitySystem时,我得到了一个错误“unbound method必须以ship instance作为第一个参数(改为使用float变量)”——网上关于类似问题的答案都没有真正帮助我:|有什么想法吗?在
简化代码:
class Game:
def main(self):
global ship
ship = Ship(100,100)
entities = EntitySystem()
entities.addEntity("ship", Ship)
# MAIN LOOP_________________________________
while True:
#do shomething
#update engine
entities.update()
class GameObject:
def __init__(self,x,y):
self.pos=[x, y]
def update():
pass
class Ship(GameObject):
def __init__(self, x, y):
GameObject.__init__(self,x,y)
def update(self, time):
# do something
class EntitySystem():
def __init__(self):
self.index = 0
self.currentEntities = []
def update(self):
for cEnt in self.currentEntities:
cEnt.update(global_variables.DELTA_TIME)
def addEntity(self, name, newEntity):
self.currentEntities.insert(self.index,newEntity)
self.index = self.index + 1
提前谢谢大家!在
几个问题:
ship
(变量),而不是Ship
(类)GameObject.self
中的第一个参数self
ship
在这里不应该是全局的,它是无用的(至少在这部分代码中)。尽可能避免使用全局变量相关问题 更多 >
编程相关推荐