0

In my program, my 'missilepack' object is being mistaken for the asteroid object and returns the error 'Missilepack object has no attribute 'handle_caught', and I don't understand why, any help with fixing this error would be great. EDIT: It appears the missile is being mistaken for an Asteroid if that helps someone help me.

class MissilePack(games.Sprite):
speed = 1.7
image = games.load_image('MissilePack.bmp')
global inventory

def __init__(self, x,y = 10):
    """ Initialize a missilepack object. """
    super(MissilePack, self).__init__(image = MissilePack.image,
                                x = x, y = y,
                                dy = MissilePack.speed)


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

def handle_caughtpack(self):
    inventory.append('missile')
    self.destroy.()

The part were the error occurs is in the 'asteroid.handlecaught() bit'

   def check_collison(self):
    """ Check if catch pizzas. """
    global lives
    for asteroid in self.overlapping_sprites:
        asteroid.handle_caught()
        if lives.value <=0:
            self.ship_destroy()

def check_pack(self):
    for missilepack in self.overlapping_sprites:
        missilepack.handle_caughtpack()

This is the handle_caught() method in my Asteroid class:

def handle_caught(self):
    if lives.value>0:
        lives.value-=1
        self.destroy_asteroid()

    if lives.value <= 0:
        self.destroy_asteroid()
        self.end_game()

And this is the part of the spawner which spawns to missile pack and asteroid

def check_drop(self):
    """ Decrease countdown or drop asteroid and reset countdown. """
    if self.time_til_drop > 0:
        self.time_til_drop -= 0.7
    else:
        asteroid_size = random.choice(images)
        new_asteroid = Asteroid(x = self.x,image = asteroid_size)
        games.screen.add(new_asteroid)

        # makes it so the asteroid spawns slightly below the spawner   
        self.time_til_drop = int(new_asteroid.height * 1.3 / Asteroid.speed) + 1

def drop_missile(self):
    """Randomely drops a missile pack for the player to use"""
    dropmissile = random.randrange(0, 100)
    if dropmissile == 5:
        missilepack = MissilePack(x = self.x)
        games.screen.add(missilepack)
Callum Houghton
  • 25
  • 2
  • 13
  • Anyone? I'm pretty much stumped. – Callum Houghton Oct 09 '13 at 16:23
  • Are you learning from the Python Programming for the absolute beginner by Mark Dawson because that code looks similar to one of the programs in the book. If it is from that book, then just read the whole chapter on that program and you might find your answer –  Oct 09 '13 at 17:26
  • Show the ENTIRE code instead of these segments, it might make it easier for to locate this bug –  Oct 09 '13 at 17:31
  • Yes I was, I figured it out eventually but thanks for the help :) – Callum Houghton Oct 09 '13 at 21:13
  • possible duplicate of [Object not behaving correctly](http://stackoverflow.com/questions/19278613/object-not-behaving-correctly) – sloth Oct 10 '13 at 07:39
  • please don't post questions twice – sloth Oct 10 '13 at 07:40

1 Answers1

0

It would really be useful to see the code where you put sprites in a group, but I found a problem: When a missile checks for collisions in that way, it will also return that it is colliding with itself. It will try to call the handle_caught method on itself. It is easy to fix this, all you need to do is this:

for asteroid in self.overlapping_sprites:
    if asteroid != self:
        asteroid.handle_caught()
        if lives.value <=0:
            self.ship_destroy()

this just makes it ignore collisions with itself.

trevorKirkby
  • 1,886
  • 20
  • 47