如何修复TypeError:必须使用Dropper实例作为第一个参数来调用未绑定方法

乔希

我正在写一个游戏,其中一块岩石掉下来,您用鼠标使厨师不被压碎。当岩石从屏幕上掉落时,又有两个岩石产生并掉落。这一直持续到角色被弄脏为止。

但是我得到了错误:

TypeError: unbound method additonal_drop() must be called with Dropper instance as first argument (got nothing instead)

我不确定应该放入什么()有人可以向我解释这个错误吗?

另外,如何使Dropper子画面不可见?

这是我的代码:

from livewires import games, color
import random

games.init(screen_width = 640, screen_height = 480, fps = 50)


class Chef(games.Sprite):

    image = games.load_image("chef.bmp")

    def __init__(self):
        super(Chef, self).__init__(image = Chef.image,
                                  x = games.mouse.x,
                                  bottom = games.screen.height)
    def update(self):
        """ Move to mouse x position. """
        self.x = games.mouse.x

        if self.left < 0:
            self.left = 0

        if self.right > games.screen.width:
            self.right = games.screen.width

        self.check()

    def check(self):
        """ Check if hit by rocks. """
        for rock in self.overlapping_sprites:
            rock.end_game()

class Rock(games.Sprite):
    """
    A rock which falls to the ground.
    """ 
    image = games.load_image("rock.bmp")
    speed = 1

    def __init__(self, x = 320, y = 90):
        """ Initialize a rock object. """
        super(Rock, self).__init__(image = Rock.image,
                                    x = x, y = y,
                                    dy = Rock.speed)

    def end_game(self):
        """ End the game. """
        end_message = games.Message(value = "Game Over",
                                    size = 90,
                                    color = color.red,
                                    x = games.screen.width/2,
                                    y = games.screen.height/2,
                                    lifetime = 2 * games.screen.fps,
                                    after_death = games.screen.quit)
        games.screen.add(end_message)

    def update(self):
        """ Check if bottom edge has reached screen bottom. """
        if self.bottom > games.screen.height:
            self.destroy()
            Dropper.additonal_drop()


class Dropper(games.Sprite):
    """
    A invisible sprite that drops the rocks.
    """
    image = games.load_image("rock.bmp")
    def __init__(self, y = 55, speed = 2, odds_change = 200):
        """ Initialize the dropper object. """
        super(Dropper, self).__init__(image = Dropper.image,
                                      x = games.screen.width / 2, y = y,
                                      dx = speed)

        self.odds_change = odds_change
        self.time_til_drop = 0


    def update(self):
        """ Determine if direction needs to be reversed. """
        if self.left < 0 or self.right > games.screen.width:
            self.dx = -self.dx
        elif random.randrange(self.odds_change) == 0:
           self.dx = -self.dx

    def additonal_drop(self):
        new_rock = Rock(x = self.x)
        games.screen.add(new_rock)

        new_rock = Rock(x = self.x)
        games.screen.add(new_rock)


def main():
    """ Play the game. """
    wall_image = games.load_image("wall.jpg", transparent = False)
    games.screen.background = wall_image

    the_chef = Chef()
    games.screen.add(the_chef)

    the_rock = Rock()
    games.screen.add(the_rock)

    the_dropper = Dropper()
    games.screen.add(the_dropper)

    games.mouse.is_visible = False

    games.screen.event_grab = True
    games.screen.mainloop()

# start it up!
main()
虎鹰T3

尝试使用其他参数定义Rock类dropper

def __init__(self, x = 320, y = 90, dropper=Dropper()):

dropper将是Dropper实例。然后Rock从内部创建实例,Dropper如下所示:

Rock(x=self.x, dropper=self)

这会将Dropper实例传递self实例创建的每个Rock实例DropperRock__init__(),保存到一个参考Dropper实例:

self.dropper = dropper

致电additional_drop()

self.dropper.additional_drop()

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

未绑定的方法f()必须以fibo_实例作为第一个参数调用(取而代之的是class classobj实例)

必须以实例作为第一个参数调用未绑定方法-python

TypeError:第一个参数必须是可调用的

如何使用嵌套的闭包作为List :: Util :: reduce的第一个参数?

必须以实例作为第一个参数调用未绑定方法

来自节点JS的HTTPS REST API调用TypeError:第一个参数必须是字符串或Buffer

TypeError-未绑定方法ToTransition()必须以FSM实例作为第一个参数来调用(取而代之的是str实例)

必须以MyModel实例作为第一个参数来调用未绑定方法save()

熊猫样式为excel“ TypeError:第一个参数必须可调用”

TypeError:必须以LoginPage实例作为第一个参数来调用未绑定方法test_logn()(改为使用CompanyManagement实例)

TypeError:未绑定方法parse()必须以ElementTree实例作为第一个参数调用(改为使用str str实例)

TypeError:未绑定方法strftime()必须以Form1的第38行的日期时间实例作为第一个参数(而不是got str实例)来调用

如何根据提供给第一个函数的参数来推断参数?

TypeError:第一个参数必须是可调用的或None-错误不是第一次出现而是稍后出现

TypeError:第一个参数必须可在调度程序库中调用

如何使用Vim的escape()和<CR>作为其第一个参数?

std :: vector :: insert,可以将end作为第一个参数来调用吗?

错误:必须使用“类名”实例作为第一个参数来调用未绑定方法“方法名”(取而代之的是classobj实例)

未绑定的方法必须以实例作为第一个参数来调用

未绑定的方法equaldigits()必须以sut实例作为第一个参数来调用(取而代之的是int实例)

必须使用流处理程序实例作为fistr参数调用python typeerror未绑定方法发出(改为使用filehandler实例)

TypeError:未绑定方法move_to_element()必须以ActionChains实例作为第一个参数来调用(取而代之的是获取列表实例)

必须使用GetPostView实例作为第一个参数来调用未绑定的方法comment()(改为使用WSGIRequest实例)

类型错误:必须使用 Link 实例作为第一个参数调用未绑定的方法 set_rank()(什么都没有)

必须使用 Tk 实例作为第一个参数调用未绑定的方法 mainloop()(什么都没有)

类型错误:必须使用 Vars 实例作为第一个参数调用未绑定的方法 SendVars()(改为使用 bool 实例)

必须使用 UserLoginForm 实例作为第一个参数调用未绑定的方法 is_valid() (什么都没有)

Python 错误:必须使用 Point 实例作为第一个参数调用未绑定的方法 distance()(改为使用 classobj 实例)

这是什么错误?:未绑定方法需要 vtkRenderingCorePython.vtkAbstractMapper 作为第一个参数