def remove_sprite(self, sprite: pygame.sprite.Sprite) -> None: """Remove sprite/actor from the map. Args: sprite (pygame.sprite.Sprite): sprite object to remove """ sprite.remove(self._map_group)
def activate(self, player: pygame.sprite.Sprite): if not self.activated: self.sound.play() self.activated = True player.add_money(self.price) self.spawn_particles(player) self.delete()
def spawn_particles(self, player: pygame.sprite.Sprite): color = random.choice(self.colors) direction = -1 if player.facing_right else 1 x = player.x + player.rect.w - (player.rect.w + 5) * player.facing_right + 8 y = player.y + 50 player.spawn_particles(x, y, color, amount=5, direction=direction) player.spawn_glowing_particles(x, y, direction=direction, lifespan=240)
def calculate_bullets_collisions(self, coll: pygame.sprite.Sprite): """метод для обработки столкновений пуль с объектом""" if (collisions := self.bullets.get_collisions(coll)): coll.take_damage(self.damage) for collision in collisions: self.spawn_explosion(*collision.pos, coll.blood_colors, radius=(2, 12)) collision.delete()
def activate(self, player: pygame.sprite.Sprite): self.delete() self.sound.play() for _ in range(20): color = random.choice(self.colors) player.spawn_particles(amount=2, momentum=random.randrange(0, 3), radius=random.randrange(4, 10), color=color)
def spawn_particles(self, player: pygame.sprite.Sprite): x = self.rect.center[0] - 3 + 6 * player.facing_right y = self.bottom - 5 for _ in range(20): player.spawn_glowing_particles(x, y, amount=2, momentum=random.randrange(2, 6), radius=random.randrange(4, 12))
def flip(sprite: pg.sprite.Sprite, orig_image, x_reflect: bool, y_reflect: bool): """Flips (reflects) the image for a sprite either horizontally, vertically, or both. :param sprite: The pygame Sprite whose image we're flipping. :param orig_image: A pygame Surface representing the image we will flip. :param x_reflect: A boolean indicating if the image should be flipped horizontally. :param y_reflect: A boolean indicating if the image should be flipped vertically. :return: None; operations done in-place. """ old_center = sprite.rect.center sprite.image = pg.transform.flip(orig_image, x_reflect, y_reflect) sprite.rect = sprite.image.get_rect() sprite.rect.center = old_center
def check_if_pawn_is_jumping_over(self, newPawnTile: pygame.sprite.Sprite): distanceX = abs(newPawnTile.get_tile_center_x() - self.get_tile_center_x()) distanceY = abs(newPawnTile.get_tile_center_y() - self.get_tile_center_y()) if (distanceX == (newPawnTile.image.get_width() * 2)) ^ (distanceY == ( newPawnTile.image.get_height() * 2)): if (distanceX == newPawnTile.image.get_width()) or ( distanceY == newPawnTile.image.get_height()): return False return True else: return False
def check_if_pawn_is_moving_to_the_nearest_tile( self, newPawnTile: pygame.sprite.Sprite): distanceX = abs(newPawnTile.get_tile_center_x() - self.get_tile_center_x()) distanceY = abs(newPawnTile.get_tile_center_y() - self.get_tile_center_y()) if (distanceX == newPawnTile.image.get_width()) ^ ( distanceY == newPawnTile.image.get_height()): if (distanceX > newPawnTile.image.get_width()) or ( distanceY > newPawnTile.image.get_height()): return False return True else: return False
def update(self, sprite: pygame.sprite.Sprite): if len(self.animation) > 1: self.count += 1 if self.count == self.speed: self.index += 1 if self.index == len(self.animation): self.index = 0 sprite.image = self.animation[self.index] self.count = 0
def double_jump(self, newPawnTile: pygame.sprite.Sprite, pawnsSprintTable: pygame.sprite.Group): distanceX = newPawnTile.get_tile_center_x() - self.get_tile_center_x() distanceY = newPawnTile.get_tile_center_y() - self.get_tile_center_y() if distanceX != 0: if distanceX > 0: pawnInBettwenX = newPawnTile.get_tile_center_x_for_drawing_pawn( self) - newPawnTile.image.get_width() for pawn in pawnsSprintTable: if pawn.rect.x == pawnInBettwenX and pawn.rect.y == self.rect.y: return True else: pawnInBettwenX = newPawnTile.get_tile_center_x_for_drawing_pawn( self) + newPawnTile.image.get_width() for pawn in pawnsSprintTable: if pawn.rect.x == pawnInBettwenX and pawn.rect.y == self.rect.y: return True else: if distanceY > 0: pawnInBettwenY = newPawnTile.get_tile_center_y_for_drawing_pawn( self) - newPawnTile.image.get_height() for pawn in pawnsSprintTable: if pawn.rect.y == pawnInBettwenY and pawn.rect.x == self.rect.x: return True else: pawnInBettwenY = newPawnTile.get_tile_center_y_for_drawing_pawn( self) + newPawnTile.image.get_height() for pawn in pawnsSprintTable: if pawn.rect.y == pawnInBettwenY and pawn.rect.x == self.rect.x: return True
def apply_powerup(self, character: pg.sprite.Sprite): """ Calls Character functions that handle the powerup effects :param character: pg.sprite.Sprite (Character) """ Timer(self.game, 5, 100, 100, DEFAULT_FONT_NAME, 25, True, _type=self.type) if self.type in ['purple', 'yellow', 'white']: Timer(self.game, POWERUP_EFFECT[self.type], 15, 20, DEFAULT_FONT_NAME, 25, _type=self.type) if self.type == 'red': character.heal(POWERUP_EFFECT[self.type]) character.image_code = 2 if self.type == 'pink': character.heal(POWERUP_EFFECT[self.type]) character.image_code = 3 if self.type == 'purple': character.double_shot(POWERUP_EFFECT[self.type]) character.image_code = 4 if self.type == 'blue': character.heal_shield() if self.type == 'yellow': character.immune(POWERUP_EFFECT[self.type]) character.image_code = 5 if self.type == 'white': character.fast_fire(POWERUP_EFFECT[self.type]) character.image_code = 6 if self.type == 'green': if character.armor >= 4: return character.armor += POWERUP_EFFECT[self.type] character.image_code = 7 if self.type == 'w_green': character.attack += POWERUP_EFFECT[self.type] character.image_code = 8
def activate(self, player: pygame.sprite.Sprite): if not self.activated: self.sound.play() self.activated = True player.set_momentum(-10) player.spawn_particles()
def spawn_particles(self, player: pygame.sprite.Sprite): color = random.choice(self.colors) player.spawn_particles(*self.rect.center, color, amount=1)
def activate(self, player: pygame.sprite.Sprite): """метод для обработки коллизии с игроком""" player.rect.bottom = self.top player.jump()
def activate(self, player: pygame.sprite.Sprite): if not self.activated: player.enable_gravity(False) player.set_flying_object(self) self.activated = True
def activate(self, player: pygame.sprite.Sprite): self.sound.play() player.set_momentum(-15) player.spawn_particles() player.rotate()
def on_delete(self, player: pygame.sprite.Sprite): if self.activated: self.sound.stop() player.enable_gravity(True) player.set_flying_object()
def activate(self, player: pygame.sprite.Sprite): if player.shield is None: player.game_over = True
def on_delete(self, player: pygame.sprite.Sprite): if self.activated: player.set_magnet()
def activate(self, player: pygame.sprite.Sprite): if not self.activated: player.set_magnet(self) self.activated = True
def spawn_particles(self, player: pygame.sprite.Sprite): player.spawn_explosion(*self.rect.center, self.colors, radius=(1, 10))