class Asteroid(Sprite): def __init__(self, image_url, image_width, image_height, inset = None): super(Asteroid, self).__init__() self.sheet = SpriteSheet(image_url) self.images = self.sheet.images_by(image_width, image_height, Color("black"), inset) self.current_frame = 0 self.image = self.images[self.current_frame] self.rect = self.image.get_rect( top = randint(-100, 0), left = randint(0, SCREEN_SIZE[0]), width = image_width, height = image_height ) self.frame_speed = 1.5 self.animation_counter = 0 def update(self): if self.animation_counter == (self.frame_speed - 1): self.current_frame = ( (self.current_frame + 1) if self.current_frame < len(self.images) else 0 ) self.animation_counter = (self.animation_counter + 1) % self.frame_speed self.image = self.images[self.current_frame - 1] if self.rect.y > SCREEN_SIZE[1]: self.rect.y = 0 self.rect.x = random.randint(0, SCREEN_SIZE[0]) else: self.rect.y += self.speed
class PlayerHitAnimation(Sprite): def __init__(self, player): super(PlayerHitAnimation, self).__init__() self.player = player self.sheet = SpriteSheet("assets/images/Sprite_FX_Explosion_0042.png") self.images = self.sheet.images_by(148, 148, (0, 0, 0)) self.current_frame = 0 self.frame_speed = 2 self.animation_counter = 0 self.image = self.images[self.current_frame] self.rect = self.image.get_rect( left = self.player.rect.x, top = self.player.rect.y ) def update(self): if self.animation_counter == (self.frame_speed - 1): self.current_frame = (self.current_frame + 1) % len(self.images) self.animation_counter = (self.animation_counter + 1) % self.frame_speed self.image = self.images[self.current_frame - 1] self.rect = self.image.get_rect( left = self.player.rect.x, top = self.player.rect.y ) if self.current_frame == len(self.images) - 1: self.kill()