“必须以实例作为第一个参数调用未绑定方法(改为获取其他参数)”

2024-09-30 10:30:30 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我一直在做一个小行星游戏,我遇到了以下问题:

在我的主文件中,我创建了一个实例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

提前谢谢大家!在


Tags: selfindexinitdefupdateglobaldoclass
1条回答
网友
1楼 · 发布于 2024-09-30 10:30:30

几个问题:

  • 在main中,您希望在参数中传递ship(变量),而不是Ship(类)
  • 您忘记了GameObject.self中的第一个参数self
  • ship在这里不应该是全局的,它是无用的(至少在这部分代码中)。尽可能避免使用全局变量

相关问题 更多 >

    热门问题