2
\$\begingroup\$

Would appreciate any suggestions or improvements, I'm sure there are many. Especially so on the way I've avoided multiple hits by the missiles; shifting the asteroid into a separate group to enable the animation to continue without registering more hits and hence addition to the score.

P.S As it stands my game has a ship at the bottom of the screen which can move horizontally but not vertically. It can fire missiles at a horizontally moving asteroid sprite at the top of the screen. If the asteroid is hit by a missile sprite, it triggers an explosion animation and the sprite is removed. A new asteroid sprite is then spawned.

Sprite:

class MyAsteroid(pygame.sprite.Sprite):
    """ My sprite"""

    def __init__(self):
        super().__init__()
        self.height = 0
        self.width = 0
        self.vel_x = 5
        self.velocity_y = 0
        self.hit = False
        self.files = []
        self.images = []
        self.index = 0
        self.asteroid_explosion = pygame.mixer.Sound("explosion.wav")

        for i in range(1,10):
            file = "explosion0.bmp"
            new_file = file.replace("0", str(i))
            self.files.append(new_file)
        for file in self.files:
            self.images.append(load_image(file))

    def ast_hit(self):
        self.hit = True


    def update(self, missile_group, the_screen):
        super().update()
        self.X = self.X + self.vel_x
        if self.X < 0 or self.X > the_screen.width - self.width:
            self.vel_x = -(self.vel_x)
        if self.hit == True:
            self.vel_x = 0
            self.asteroid_explosion.play()
            self.image = self.images[self.index]
            self.index += 1
            if self.index >= len(self.images):
                missile_group.remove(self)


    def draw(self):
        super().draw()

    def load(self, image):
        self.image = image
        self.rect = self.image.get_rect()
        self.width, self.height = image.get_size()

    # X property
    def _get_x(self): return self.rect.x
    def _set_x(self, value): self.rect.x = value
    X = property(_get_x, _set_x)

    # Y property
    def _get_y(self): return self.rect.y
    def _set_y(self, value): self.rect.y = value
    Y = property(_get_y, _set_y)

    # position property
    def _get_pos(self): return self.rect.topleft
    def _set_pos(self, value): self.rect.topleft = value
    position = property(_get_pos, _set_pos)

Hit detection:

for asteroid in asteroid_group:
            asteroid_hit = False
            asteroid_hit = pygame.sprite.spritecollide(asteroid, missile_group,\
                                                       True)
            if asteroid_hit:
                asteroid.ast_hit()
                asteroid_group.remove(asteroid)
                hit_group.add(asteroid)
                #asteroid.hit = True
##                for missile in asteroid_hit:
##                    missile_group.remove(missile)
                score += 10
\$\endgroup\$
  • \$\begingroup\$ The purpose of the code I posted is to avoid multiple hits to the Asteroid Sprite, which it does. Hence 'Avoiding multiple hits to Sprite..." title. I'm asking if there is a better way as this is my first pygame and I want to establish good habits. What I am missing regarding the purpose of this site? If I post this type of question on stackexchange, I get sent here :/ \$\endgroup\$ – Michael Johnson Sep 16 '16 at 14:36
  • \$\begingroup\$ Ah, sorry. At first glance to me your title read like a request to improve your code in some way. Sorry for the confusion! \$\endgroup\$ – Ethan Bierlein Sep 16 '16 at 15:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.