def shoot(self):
     """ Shoots a projectile """
     sound.play_laser_shot()
     bullet = projectile.Projectile(self.x + self.width//2 - 5,
                                    self.y - self.height,
                                    Player,
                                    10,
                                    window=self._window)
     game.add_gameobject(bullet)
Esempio n. 2
0
 def shoot(self):
     """ Shoots a projectile """
     sound.play_laser_shot(0.6)
     bullet = Projectile(self.x + self.width // 2 - 9,
                         self.y + self.height,
                         Enemy,
                         speed=-10,
                         window=self._window)
     self.change_sprite()
     game.add_gameobject(bullet)
 def receive_damage(self, damage: int):
     """
     Receives damage.
     :param damage: damage to substract from health.
     """
     num = random.randint(1, 2)
     game.add_gameobject(BlockDamage(self.damage_x,
                                     self.damage_y,
                                     self,
                                     sprite_path=damage_sprites[str(num)],
                                     window=self._window))
     self.health -= damage
    def update(self):
        """ Called every tick """
        if not self._is_destroyed:
            self.draw(self._window)

            self.y -= self.speed

            if self.y < 0 or self.y > self._window.get_height():
                self._is_destroyed = True

            if self.is_collided(game.active_gameobjects):
                if isinstance(self._collided_gameobject, DamageReceiver):
                    if not isinstance(self._collided_gameobject,
                                      self._shoot_class):
                        if isinstance(self._collided_gameobject, Block):
                            self._collided_gameobject.damage_x = self.x
                            self._collided_gameobject.damage_y = self.y
                        self._collided_gameobject.receive_damage(self.damage)
                        explosion = Explosion(self.x - 3,
                                              self.y - 7,
                                              window=self._window)
                        game.add_gameobject(explosion)
                        self._is_destroyed = True
Esempio n. 5
0
def load_level(level_name: str, window):
    """
    Load a level from levels.
    :param level_name: name of the level.
    :param window: window to render in.
    """
    game.clean_gameobjects()
    screen_width = window.get_width()
    screen_height = window.get_height()
    game.enemy_count = 0
    level = levels[level_name]
    level_str = level.level_str
    row = 1
    column = 1
    for i in range(2):
        game.add_gameobject(
            Block(screen_width // 2 - 140 * (i + 1) - 30,
                  screen_height - 130,
                  window=window))
        game.add_gameobject(
            Block(screen_width // 2 + 140 * (i + 1) - 30,
                  screen_height - 130,
                  window=window))
    for char in level_str:
        if char in enemies_sprite_paths:
            column += 1
            path = enemies_sprite_paths[char]
            game.enemy_count += 1
            game.add_gameobject(
                enemy.Enemy(level.shift_x + screen_width // 5 + 60 * column,
                            level.shift_y + screen_height // 5 + 60 * row,
                            50,
                            50,
                            window=window,
                            sprite_path=path))
        elif char == 'n':
            row += 1
            column = 1
        elif char == '0':
            column += 1
    game.player = Player(screen_width // 2 - 30,
                         screen_height - 40,
                         60,
                         40,
                         window=window)
    game.spawn_player()