def damage(self, damage: int): if damage > 0 and not self.shield_active and \ ml.time() - self.last_hit_time > self.invincibility_duration: self.health -= damage self.invincibility_active = True self.last_hit_time = ml.time() self.hit_toggle_time = ml.time() # Heal elif damage < 0: self.health -= damage if self.health > ml.get_upgrade_values('Max Health'): self.health = ml.get_upgrade_values('Max Health') ml.update_player_health()
def __init__(self) -> pygame.sprite.Sprite: pygame.sprite.Sprite.__init__(self) self.health = ml.get_upgrade_values('Max Health') self.hitbox_width = 5 self.move_speed = float(ml.get_upgrade_values('Movement Speed')) self.shift_speed = ml.normalize_target_fps(2) self.diagonal_move_speed = 0 self.invincibility_duration = 1 # Seconds of invulnerability after taking damage self.last_hit_time = -999 self.hit_toggle_time = ml.time() self.invincibility_active = False # Shooting self.firing_timer = ml.time() # Set up image, rect, and mask self.image_path = os.path.join('graphics', 'player.png') self.homing_image_path = os.path.join('graphics', 'player_homing.png') self.invisible_image_path = os.path.join('graphics', 'player_invisible.png') self.image = pygame.image.load(self.image_path).convert_alpha() self.homing_image = pygame.image.load( self.homing_image_path).convert_alpha() self.invisible_image = pygame.image.load( self.invisible_image_path).convert_alpha() self.image_original = self.image # Even width prevents proper centering of hitbox and mouse movement assert self.image.get_width() % 2, "Player image width must be odd." self.x = ml.window_width / 2 self.y = ml.window_height - 100 self.rect = pygame.Rect(0, 0, self.image.get_width(), self.image.get_height()) self.mask = pygame.mask.from_surface(self.image, 250) self.mask_original = self.mask self.hitbox = pygame.Rect(self.rect.centerx, self.rect.centerx, self.hitbox_width, self.hitbox_width) # Create mask for hitbox to deal with enemy collision self.hitbox_mask = pygame.mask.Mask(self.rect.size) self.rect.topleft = 0, 0 top_left = self.rect.centerx - self.hitbox_width // 2, \ self.rect.centery - self.hitbox_width // 2 for x in range(self.hitbox_width): current_x = top_left[0] + x for y in range(self.hitbox_width): current_y = top_left[1] + y self.hitbox_mask.set_at((current_x, current_y), 1) self.rect.center = self.x, self.y # Powerups self.shield_active = False self.shield = None self.heal_amount = 1 self.homing_shots_active = False self.homing_shots_activation_time = None self.toggle_time = None # Add sprite to group self.add(ml.player_group) ml.update_player_data(self) ml.update_player_health()