2

I'm following the book, 'Python Programming For The Absolute Beginner' and decided to test some of my skills by making my own game. The game is basically "don't get hit by the flying spikes" and I have come across a problem with it. When running it with this code:

class Player(games.Sprite):
    """The player that must dodge the spikes."""

    def update(self):
        """Move to the mouse."""
        self.x = games.mouse.x
        self.y = games.mouse.y
        self.check_collide()

    def check_collide(self):
        """Check for a collision with the spikes."""
        for spike in self.overlapping_sprites:
            spike.handle_collide()


def main():


    pig_image = games.load_image("Mr_Pig.png")
    the_pig = Player(image = pig_image,
                     x = games.mouse.x,
                     y = games.mouse.y)
    games.screen.add(the_pig)
    games.mouse.is_visible = False
    games.screen.event_grab = True

    games.screen.mainloop()

main()

I get no problem. But when I want to use an 'init', like in this code:

class Player(games.Sprite):
    """The player that must dodge the spikes."""

    def update(self):
        """Move to the mouse."""
        self.x = games.mouse.x
        self.y = games.mouse.y
        self.check_collide()

    def check_collide(self):
        """Check for a collision with the spikes."""
        for spike in self.overlapping_sprites:
            spike.handle_collide()

    def __init__(self):
        """A test!"""
        print("Test.")


def main():

    pig_image = games.load_image("Mr_Pig.png")
    the_pig = Player(image = pig_image,
                     x = games.mouse.x,
                     y = games.mouse.y)
    games.screen.add(the_pig)
    games.mouse.is_visible = False
    games.screen.event_grab = True

    games.screen.mainloop()

main()

I get this error when running the game:

File "run.py", line 85, in main
    y = games.mouse.y)
TypeError: __init__() got an unexpected keyword argument 'y'.
matsjoyce
  • 5,277
  • 6
  • 28
  • 36
Lixerman99
  • 141
  • 1
  • 2
  • 11
  • Please post only relevant code. None would go through all this code and debug it for you, Please paste only relevant code which would be enough to reproduce the problem. – ZdaR May 16 '15 at 10:13

1 Answers1

6

This line:

the_pig = Player(image = pig_image,
                 x = games.mouse.x,
                 y = games.mouse.y)

Is equivalent to:

the_pig = Player.__init__(image = pig_image,
                 x = games.mouse.x,
                 y = games.mouse.y)

This means your __init__ should accept the arguments image, x and y, but you have defined it as:

def __init__(self):
    """A test!"""
    print("Test.")

If you want to simple pass on all the arguments, you can do it like this:

def __init__(self, *args, **kwargs):
    super().__init__(*args, **kwargs)
    """A test!"""
    print("Test.")

This uses the * and ** syntax to get all the arguments and keyword arguments and then uses super to call the super-classes __init__ with those arguments.

The alternative (more work) is:

def __init__(self, image, x, y):
    super().__init__(image=image, x=x, y=y)
    """A test!"""
    print("Test.")
Community
  • 1
  • 1
matsjoyce
  • 5,277
  • 6
  • 28
  • 36