So, i am trying to have an explosion when the asteroid and house collides. And i think i have the code written down, but something might me be missing. Please help me figure this out. Thanks a lot.

# Fresh Start

# Classes:  House; Asteroid; Explosion

from livewires import games

games.init(screen_width = 960, screen_height = 540, fps = 50)




#Class # 1

class Collider():
    """ will this make it explode. """
    def update(self):
        """ Check for overlapping sprites. """
        super(Collider, self).update()
        
        if self.overlapping_sprites:
            for sprite in self.overlapping_sprites:
                sprite.die()
            self.die()               

    def die(self):
        """ Destroy self and leave explosion behind. """
        new_explosion = Explosion(x = self.x, y = self.y)
        games.screen.add(new_explosion)
        self.destroy()

class Asteroid(games.Sprite):
    """ The Asteroid """
    image = games.load_image("asteroid_med.bmp")

#Create the Asteroid and Make it to move

the_asteroid = Asteroid(image = Asteroid.image,
                        x = 1,
                        y = 1,
                        dx = 1,
                        dy = 1)
games.screen.add(the_asteroid)

def update(self):
    if self.ovelapping_sprites:
        for sprite in self.overlapping_sprites:
            sprite.die()
        self.die()

def die(self):
    """ Destroy Asteroid """
    self.destroy()


#Class # 2

class House(games.Sprite):
    """ The House """
    image = games.load_image("2 house.jpg")

# Create the House

the_house = House(image = House.image,
                  x = 480,
                  y = 440)
games.screen.add(the_house)

def update(self):
    # Check if House overlaps any other objects.
    if self.ovelapping_sprites:
        for sprite in self.overlapping_sprites:
            sprite.die()
        self.die()

def die(self):
    """ Destroy House """
    self.destroy()

#Class # 3

class Explosion(games.Animation):
    """ Explosion animation. """
    sound = games.load_sound("explosion.wav")
    images = ["explosion1.bmp",
              "explosion2.bmp",
              "explosion3.bmp",
              "explosion4.bmp",
              "explosion5.bmp",
              "explosion6.bmp",
              "explosion7.bmp",
              "explosion8.bmp",
              "explosion9.bmp"]

    def __init__(self, x, y):
        super(Explosion, self).__init__(images = Explosion.images,
                                        x = x, y = y,
                                        repeat_interval = 4, n_repeats = 1,
                                        is_collideable = False)
        Explosion.sound.play()




# Time to set it in place

def main():
    # establish background
    wall_image = games.load_image("field2.jpg", transparent = False)
    games.screen.background = wall_image

    games.screen.mainloop()

# Kick it Off!
main()

Recommended Answers

All 3 Replies

Difficult to test without the pictures (you can attach them from Advanced Editor)

How do use the Advanced Editor.

See the attached clip, then in Advanced Editor, push Manage Attachments from Attach Files.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.