/
ap1k
/
PyGame_Practice
Обзор
Документация
Войти
/
ap1k
/
PyGame_Practice
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/patterns/weapon_strategy.py
94 строки
3 KB
Roman
The all game
27 май 2025, 15:26
27 май 2025, 15:26
4ea7de9
Код
Авторство
О чём код?
""" Weapon Strategy pattern implementation Provides different weapon behaviors for the player """ from abc import ABC, abstractmethod from src.utils.resource_loader import ResourceLoader class Weapon(ABC): """ Abstract base class for all weapons Implements the Strategy pattern """ def __init__(self, player): """Initialize the weapon with a reference to the player""" self.player = player self.cooldown = 500 # Default cooldown in milliseconds self.damage = 10 # Default damage @abstractmethod def shoot(self): """Shoot a bullet""" pass class BasicWeapon(Weapon): """ Basic weapon with balanced cooldown and damage """ def __init__(self, player): super().__init__(player) self.cooldown = 500 self.damage = 10 def shoot(self): """Shoot a basic bullet""" from src.entities.bullet import Bullet # Create a bullet at the player's position bullet_x = self.player.x + self.player.width // 2 - 2 bullet_y = self.player.y bullet = Bullet(bullet_x, bullet_y, "basic", self.damage) # Play the shoot sound ResourceLoader().get_sound("shoot").play() return bullet class RapidWeapon(Weapon): """ Rapid fire weapon with low cooldown but less damage """ def __init__(self, player): super().__init__(player) self.cooldown = 300 self.damage = 5 def shoot(self): """Shoot a rapid bullet""" from src.entities.bullet import Bullet # Create a bullet at the player's position bullet_x = self.player.x + self.player.width // 2 - 2 bullet_y = self.player.y bullet = Bullet(bullet_x, bullet_y, "rapid", self.damage) # Play the shoot sound ResourceLoader().get_sound("shoot").play() return bullet class PowerWeapon(Weapon): """ Powerful weapon with high damage but long cooldown """ def __init__(self, player): super().__init__(player) self.cooldown = 1000 self.damage = 25 def shoot(self): """Shoot a powerful bullet""" from src.entities.bullet import Bullet # Create a bullet at the player's position bullet_x = self.player.x + self.player.width // 2 - 2 bullet_y = self.player.y bullet = Bullet(bullet_x, bullet_y, "power", self.damage) # Play the shoot sound ResourceLoader().get_sound("shoot").play() return bullet