Spaces:
Runtime error
Runtime error
File size: 7,324 Bytes
a8c7059 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 | import random
class Box:
"""Box character class"""
def __init__(self, name="Unnamed Box"):
self.name = name
self.attack = 10
self.defense = 8
self.health = 50
self.max_health = 50
self.speed = 5
self.level = 1
self.xp = 0
self.xp_to_next_level = 100
self.ability = None
def gain_xp(self, amount):
"""Add XP and check for level up"""
self.xp += amount
while self.xp >= self.xp_to_next_level:
self.level_up()
def level_up(self):
"""Level up the box"""
self.level += 1
self.xp -= self.xp_to_next_level
self.xp_to_next_level = int(self.xp_to_next_level * 1.5)
# Increase stats
self.attack += 2
self.defense += 2
self.max_health += 10
self.health = self.max_health # Full heal on level up
self.speed += 1
# Upgrade ability
if self.ability and "level" in self.ability:
self.ability["level"] += 1
def heal(self, amount):
"""Heal the box"""
self.health = min(self.max_health, self.health + amount)
def take_damage(self, damage):
"""Apply damage to the box"""
self.health = max(0, self.health - damage)
def is_alive(self):
"""Check if box is alive"""
return self.health > 0
def get_status_string(self):
"""Get formatted status string"""
return f"""
📦 {self.name} (Level {self.level})
⚔️ Attack: {self.attack}
🛡️ Defense: {self.defense}
❤️ Health: {self.health}/{self.max_health}
⚡ Speed: {self.speed}
✨ XP: {self.xp}/{self.xp_to_next_level}
🎯 Ability: {self.ability['name'] if self.ability else 'None'}
""".strip()
class Battle:
"""Battle class for managing fights"""
def __init__(self, attacker, defender):
self.attacker = attacker
self.defender = defender
self.rounds = 0
self.battle_log = []
self.winner = None
def start_battle(self):
"""Start the battle"""
self.rounds = 0
self.battle_log = []
# Determine who goes first
attacker_first = self.attacker.speed >= self.defender.speed
self.battle_log.append(f"⚔️ Battle Start: {self.attacker.name} vs {self.defender.name}!")
self.battle_log.append(f"{self.attacker.name} goes first!" if attacker_first else f"{self.defender.name} goes first!")
max_rounds = 20
while self.attacker.is_alive() and self.defender.is_alive() and self.rounds < max_rounds:
self.rounds += 1
self.battle_log.append(f"\n--- Round {self.rounds} ---")
if attacker_first:
self.execute_round(self.attacker, self.defender)
if self.defender.is_alive():
self.execute_round(self.defender, self.attacker)
else:
self.execute_round(self.defender, self.attacker)
if self.attacker.is_alive():
self.execute_round(self.attacker, self.defender)
# Determine winner
if self.attacker.is_alive() and not self.defender.is_alive():
self.winner = self.attacker
self.battle_log.append(f"\n🎉 {self.attacker.name} WINS!")
elif self.defender.is_alive() and not self.attacker.is_alive():
self.winner = self.defender
self.battle_log.append(f"\n💀 {self.defender.name} WINS!")
else:
# Draw or max rounds
if self.attacker.health > self.defender.health:
self.winner = self.attacker
self.battle_log.append(f"\n⏰ Time's up! {self.attacker.name} wins by health!")
elif self.defender.health > self.attacker.health:
self.winner = self.defender
self.battle_log.append(f"\n⏰ Time's up! {self.defender.name} wins by health!")
else:
self.winner = None
self.battle_log.append(f"\n🤝 It's a DRAW!")
return {
"winner": self.winner,
"rounds": self.rounds,
"log": "\n".join(self.battle_log)
}
def execute_round(self, attacker, defender):
"""Execute a single round"""
damage = max(1, attacker.attack - defender.defense + random.randint(-2, 3))
# Apply ability effects
if attacker.ability:
if "damage_bonus" in attacker.ability:
damage += attacker.ability["damage_bonus"]
self.battle_log.append(f"{attacker.name} uses {attacker.ability['name']}!")
if "crit_chance" in attacker.ability and random.random() < attacker.ability["crit_chance"]:
damage *= 2
self.battle_log.append(f"💥 CRITICAL HIT!")
defender.take_damage(damage)
self.battle_log.append(f"{attacker.name} deals {damage} damage to {defender.name}")
self.battle_log.append(f"{defender.name} health: {defender.health}/{defender.max_health}")
class Shop:
"""Shop class for managing items"""
def __init__(self):
self.items = self.generate_shop_items()
def generate_shop_items(self):
"""Generate shop items"""
return [
{
"id": 1,
"name": "Power Boost",
"cost": 50,
"description": "Increase attack by 3",
"type": "stat_boost",
"stat": "attack",
"value": 3
},
{
"id": 2,
"name": "Defense Shield",
"cost": 40,
"description": "Increase defense by 3",
"type": "stat_boost",
"stat": "defense",
"value": 3
},
{
"id": 3,
"name": "Health Potion",
"cost": 30,
"description": "Restore 20 health",
"type": "heal",
"value": 20
},
{
"id": 4,
"name": "Speed Boots",
"cost": 60,
"description": "Increase speed by 2",
"type": "stat_boost",
"stat": "speed",
"value": 2
},
{
"id": 5,
"name": "Mystery Box",
"cost": 100,
"description": "Random stat boost",
"type": "random"
}
]
def get_item(self, item_id):
"""Get item by ID"""
for item in self.items:
if item["id"] == item_id:
return item
return None
def purchase_item(self, user_coins, item_id):
"""Purchase an item"""
item = self.get_item(item_id)
if not item:
return {"success": False, "message": "Item not found"}
if user_coins < item["cost"]:
return {"success": False, "message": "Not enough coins"}
return {"success": True, "item": item, "message": f"Purchased {item['name']}"} |