Files
wicked-quest/src/powerup.py

124 lines
4.7 KiB
Python

# -*- coding: utf-8 -*-
import pygame
from libstormgames import *
from src.object import Object
from src.weapon import Weapon
class PowerUp(Object):
def __init__(self, x, y, item_type, sounds, direction, left_boundary=1, right_boundary=100):
super().__init__(
x, y, item_type,
isStatic=False,
isCollectible=True,
isHazard=False
)
self.sounds = sounds
self.direction = direction
self.speed = 0.049 # Base movement speed
self.item_type = item_type
self.channel = None
self._currentX = x # Initialize the current x position
self.left_boundary = left_boundary
self.right_boundary = right_boundary
def update(self, current_time, player_pos):
"""Update item position"""
if not self.isActive:
return False
# Update position
new_x = self._currentX + self.direction * self.speed
# Check boundaries and bounce if needed
if new_x < self.left_boundary:
self._currentX = self.left_boundary
self.direction = 1 # Start moving right
elif new_x > self.right_boundary:
self._currentX = self.right_boundary
self.direction = -1 # Start moving left
else:
self._currentX = new_x
# Update positional audio
if self.channel is None or not self.channel.get_busy():
self.channel = obj_play(self.sounds, "item_bounce", player_pos, self._currentX)
else:
self.channel = obj_update(self.channel, player_pos, self._currentX)
# Check if item is too far from player (12 tiles)
if abs(self._currentX - player_pos) > 12:
self.isActive = False
if self.channel:
self.channel.stop()
self.channel = None
return False
return True
def apply_effect(self, player):
"""Apply the item's effect when collected"""
if self.item_type == 'hand_of_glory':
player.start_invincibility()
elif self.item_type == 'cauldron':
player.restore_health()
elif self.item_type == 'guts':
player.add_guts()
player.collectedItems.append('guts')
self.check_for_nunchucks(player)
elif self.item_type == 'jack_o_lantern':
player.add_jack_o_lantern()
elif self.item_type == 'extra_life':
player.extra_life()
elif self.item_type == 'shin_bone': # Add shin bone handling
player.shinBoneCount += 1
player._coins += 5
if player.get_health() < player.get_max_health():
player.set_health(min(
player.get_health() + 1,
player.get_max_health()
))
self.check_for_nunchucks(player)
elif self.item_type == 'witch_broom':
broomWeapon = Weapon.create_witch_broom()
player.add_weapon(broomWeapon)
player.equip_weapon(broomWeapon)
elif self.item_type == 'spiderweb':
# Bounce player back (happens even if invincible)
player.xPos -= 3 if player.xPos > self.xPos else -3
# Only apply debuffs if not invincible
if not player.isInvincible:
# Half speed and double attack time for 15 seconds
player.moveSpeed *= 0.5
if player.currentWeapon:
player.currentWeapon.attackDuration *= 2
# Set timer for penalty removal
player.webPenaltyEndTime = pygame.time.get_ticks() + 15000
# Tell level to spawn a spider
if hasattr(self, 'level'):
self.level.spawn_spider(self.xPos, self.yPos)
# Stop movement sound when collected
if self.channel:
self.channel.stop()
self.channel = None
# Item tracking
player.stats.update_stat('Items collected', 1)
def check_for_nunchucks(self, player):
"""Check if player has materials for nunchucks and create if conditions are met"""
if (player.shinBoneCount >= 2 and
'guts' in player.collectedItems and
not any(weapon.name == "nunchucks" for weapon in player.weapons)):
nunchucksWeapon = Weapon.create_nunchucks()
player.add_weapon(nunchucksWeapon)
player.equip_weapon(nunchucksWeapon)
basePoints = nunchucksWeapon.damage * 1000
rangeModifier = nunchucksWeapon.range * 500
player.scoreboard.increase_score(basePoints + rangeModifier)
play_sound(self.sounds['get_nunchucks'])
player.stats.update_stat('Items collected', 1)