import gradio as gr
import random
import time
# --- Character Classes ---
class Character:
"""Base class for all characters in the game."""
def __init__(self, name, max_hp, attack, defense, character_type="neutral"):
self.name = name
self.max_hp = max_hp
self.current_hp = max_hp
self.attack = attack
self.defense = defense
self.character_type = character_type # 'player', 'enemy'
def is_alive(self):
"""Checks if the character is still alive."""
return self.current_hp > 0
def take_damage(self, damage):
"""Calculates and applies damage taken."""
actual_damage = max(0, damage - self.defense) # Defense reduces damage
self.current_hp = max(0, self.current_hp - actual_damage)
return f"{self.name} took {actual_damage} damage!"
def attack_target(self, target):
"""Performs an attack on a target."""
# Add randomness, critical hits, and misses!
miss_chance = 0.05 # 5% chance to miss
if random.random() < miss_chance:
return f"{self.name} swings and MISSES {target.name}!"
damage = random.randint(self.attack - 2, self.attack + 2)
critical_hit = random.random() < 0.1 # 10% chance for a crit
if critical_hit:
damage = int(damage * 1.5)
crit_text = " CRITICAL HIT!"
else:
crit_text = ""
message = f"{self.name} attacks {target.name} for {damage} damage!{crit_text}"
message += "\n" + target.take_damage(damage)
return message
class PlayerCharacter(Character):
"""Base class for player characters (Scott or Ramona)."""
def __init__(self, name, max_hp, attack, defense):
super().__init__(name, max_hp, attack, defense, character_type="player")
self.xp = 0
self.level = 1
# Start players with a little spending cash
self.money = 25
def gain_xp(self, amount):
"""Awards experience points and checks for level up."""
self.xp += amount
message = f"You gained {amount} XP!"
# Simple level up system: gain a level every 50 XP
while self.xp >= self.level * 50:
message += "\n" + self.level_up()
return message
def level_up(self):
"""Increases player stats upon leveling up."""
self.level += 1
self.max_hp += 20
self.current_hp = self.max_hp # Fully heal on level up
self.attack += 3
self.defense += 1
return f"\n--- {self.name} Leveled Up to Level {self.level}! ---\nNew Stats: HP: {self.max_hp}, Attack: {self.attack}, Defense: {self.defense}"
class ScottPilgrim(PlayerCharacter):
"""Scott Pilgrim character with his specific stats."""
def __init__(self):
super().__init__("Scott Pilgrim", 100, 15, 5)
class RamonaFlowers(PlayerCharacter):
"""Ramona Flowers character with her specific stats."""
def __init__(self):
super().__init__("Ramona Flowers", 110, 17, 4)
class EvilEx(Character):
"""Base class for enemies (Evil Exes)."""
def __init__(self, name, max_hp, attack, defense, xp_reward, money_drop):
super().__init__(name, max_hp, attack, defense, character_type="enemy")
self.xp_reward = xp_reward
self.money_drop = money_drop
# --- Game State Management ---
class ScottPilgrimGame:
"""Manages the entire game state and logic."""
def __init__(self):
# Initial game state attributes are set to None or default values
self.player = None
self.current_enemy = None
self.game_active = False
self.message_log = []
self.ex_index = 0
self.current_ex_list = []
self.current_game_phase = "start" # "start", "combat", "game_over", "win"
# Define Evil Exes for Scott's storyline
self.scott_exes = [
("The Chaos Theater", EvilEx("Matthew Patel", 50, 10, 2, 30, 10)),
("Casa Loma (outside)", EvilEx("The Twins & Roxy Richter", 70, 12, 3, 50, 20)),
("Vegan restaurant", EvilEx("Todd Ingram", 90, 15, 4, 70, 30)),
("The Score at Lee's Palace", EvilEx("Envy Adams", 110, 17, 5, 90, 40)),
("Final Boss Arena", EvilEx("Gideon Graves", 150, 20, 7, 150, 70))
]
# Define Evil Exes for Ramona's storyline (a creative twist!)
self.ramona_exes = [
("Scott's Band Practice", EvilEx("Kim Pine", 55, 11, 3, 35, 12)),
("A Bar on Bloor", EvilEx("Knives Chau", 75, 13, 4, 55, 22)),
("Scott's Apartment", EvilEx("Lisa Miller", 95, 16, 5, 75, 32)),
("The Club Scene", EvilEx("Stacey Pilgrim", 115, 18, 6, 95, 42)), # Fictional boss
("The Subspace Highway", EvilEx("Young Neil", 160, 21, 8, 160, 75)) # Fictional final boss
]
# --- In-game shop items ---
self.shop_items = {
"Energy Drink": {"cost": 10, "type": "heal", "amount": 20, "desc": "Restore 20 HP"},
"Protein Bar": {"cost": 15, "type": "attack", "amount": 2, "desc": "+2 Attack"},
"Armor Patch": {"cost": 15, "type": "defense", "amount": 1, "desc": "+1 Defense"},
"Max Potion": {"cost": 20, "type": "heal_full", "amount": 0, "desc": "Fully heal"},
}
def reset_game(self):
"""Resets the game state for a new playthrough."""
self.__init__() # Re-initialize the game object to its default state
def start_new_game(self, character_choice):
"""Initializes a new game based on character choice."""
self.reset_game() # Ensure a clean slate
if character_choice == "Play as Scott Pilgrim":
self.player = ScottPilgrim()
self.current_ex_list = list(self.scott_exes) # Use a copy of the list
self.add_message("You chose to play as Scott Pilgrim! Get ready to face Ramona's Evil Exes!")
# Image assets for Scott
self.image_assets = {
"idle": ["characters/Scott_idle.webp"],
"attack": ["characters/Scott_Attack.webp", "characters/Scott_angry.webp"],
"run": ["characters/Scott_angry.webp"]
}
self.player_image_path = self.image_assets["idle"][0]
# Prepare cycling list
self.image_cycle = self.image_assets["idle"] + self.image_assets["attack"] + self.image_assets["run"]
self.image_cycle_index = 0
elif character_choice == "Play as Ramona Flowers":
self.player = RamonaFlowers()
self.current_ex_list = list(self.ramona_exes) # Use a copy of the list
self.add_message("You chose to play as Ramona Flowers! It's time to deal with Scott's complicated past!")
# Image assets for Ramona
self.image_assets = {
"idle": ["characters/Ramona_Angry.webp"],
"attack": ["characters/Ramona_attack.webp", "characters/Ramona_attack2.webp"],
"run": ["characters/Ramona_attack2.webp"]
}
self.player_image_path = self.image_assets["idle"][0]
# Prepare cycling list
self.image_cycle = self.image_assets["idle"] + self.image_assets["attack"] + self.image_assets["run"]
self.image_cycle_index = 0
else:
self.add_message("Invalid character choice. Please select Scott or Ramona.")
return
self.game_active = True
self.current_game_phase = "combat"
self._set_next_enemy()
def _set_next_enemy(self):
"""Sets the next enemy from the list for the current battle."""
if self.ex_index < len(self.current_ex_list):
location, enemy_data = self.current_ex_list[self.ex_index]
# Create a new instance of the enemy so their HP is fresh for each battle
self.current_enemy = EvilEx(enemy_data.name, enemy_data.max_hp, enemy_data.attack, enemy_data.defense, enemy_data.xp_reward, enemy_data.money_drop)
self.add_message(f"\n--- Confronting {self.current_enemy.name} at {location}! ---")
else:
self.current_enemy = None # No more enemies
def add_message(self, message):
"""Adds a message to the game log for display."""
self.message_log.append(message)
# Keep the log from getting too long for the display
if len(self.message_log) > 20:
self.message_log = self.message_log[-20:]
def get_status_text(self):
"""Returns a formatted string of current player and enemy status."""
if not self.player:
return ""
status = f"{self.player.name} (Lvl {self.player.level}) | HP: {self.player.current_hp}/{self.player.max_hp} | XP: {self.player.xp}/{self.player.level * 50}"
status += f" | $: {self.player.money}"
if self.current_enemy:
status += f"\n{self.current_enemy.name} | HP: {self.current_enemy.current_hp}/{self.current_enemy.max_hp}"
return status
def _check_game_over(self):
"""Checks for and handles the player's defeat."""
if not self.player.is_alive():
self.game_active = False
self.current_game_phase = "game_over"
self.add_message(f"\n{self.player.name} has been defeated by {self.current_enemy.name}!")
self.add_message("Game Over!")
def _handle_victory(self):
"""Handles the logic after defeating an enemy."""
self.add_message(f"\n{self.current_enemy.name} has been defeated!")
self.add_message(self.player.gain_xp(self.current_enemy.xp_reward))
self.player.money += self.current_enemy.money_drop
self.add_message(f"You found ${self.current_enemy.money_drop}!")
self.ex_index += 1
# Check for game completion
if self.ex_index >= len(self.current_ex_list):
self.game_active = False
self.current_game_phase = "win"
self.add_message("\nCongratulations! You have defeated all Evil Exes!")
self.add_message("You have truly earned the power of self-respect!")
else:
# Set up the next fight
self._set_next_enemy()
def player_attack_turn(self):
"""Handles the full sequence of a player's attack turn."""
if not self.game_active or not self.player.is_alive() or not self.current_enemy or not self.current_enemy.is_alive():
return
# Switch to attack animation
self.set_player_image("attack")
# Player attacks the enemy
self.add_message(self.player.attack_target(self.current_enemy))
# Check if enemy was defeated
if not self.current_enemy.is_alive():
self._handle_victory()
return # Stop the turn here since the battle is over or a new one is starting
# If the enemy survived, it's their turn to attack
self.enemy_attack_turn()
def enemy_attack_turn(self):
"""Handles the enemy's attack turn."""
if not self.game_active or not self.player.is_alive() or not self.current_enemy or not self.current_enemy.is_alive():
return
# Enemy attacks the player
self.add_message(self.current_enemy.attack_target(self.player))
# Check if the player was defeated
self._check_game_over()
def attempt_run(self):
"""Handles the player's attempt to run away from a battle."""
if not self.game_active:
return
# Switch to run/escape animation
self.set_player_image("run")
if random.random() < 0.2: # 20% chance to successfully run
self.add_message("You managed to escape from the battle!")
self.ex_index += 1 # Advance to the next foe after running
if self.ex_index < len(self.current_ex_list):
self._set_next_enemy()
else: # Running from the last boss is still a win condition
self.game_active = False
self.current_game_phase = "win"
self.add_message("\nCongratulations! You ran away from all your problems!")
else:
self.add_message("You couldn't get away!")
# Enemy gets a free hit as a penalty for a failed escape
self.enemy_attack_turn()
# --------- Shop Mechanics ---------
def get_shop_choices(self):
"""Return list of item names with price for UI dropdown."""
return [f"{name} - ${data['cost']}" for name, data in self.shop_items.items()]
def get_shop_description_html(self):
"""Return an HTML string listing shop items with cost and effect."""
lines = ["Shop Items:"]
for name, data in self.shop_items.items():
lines.append(f"{name} (${data['cost']}) – {data['desc']}")
return "
".join(lines)
def buy_item(self, item_display_name):
"""Process purchasing an item given the dropdown display string."""
if not self.player:
return
# Extract item key (before ' - $')
item_name = item_display_name.split(" - $")[0]
if item_name not in self.shop_items:
self.add_message("That item doesn't exist!")
return
item = self.shop_items[item_name]
cost = item["cost"]
if self.player.money < cost:
self.add_message("Not enough money!")
return
# Deduct money
self.player.money -= cost
# Apply item effect
if item["type"] == "heal":
self.player.current_hp = min(self.player.max_hp, self.player.current_hp + item["amount"])
self.add_message(f"You used {item_name} and healed {item['amount']} HP!")
elif item["type"] == "heal_full":
healed = self.player.max_hp - self.player.current_hp
self.player.current_hp = self.player.max_hp
self.add_message(f"{item_name} fully restored your HP (+{healed})!")
elif item["type"] == "attack":
self.player.attack += item["amount"]
self.add_message(f"{item_name} consumed! Attack increased by {item['amount']}.")
elif item["type"] == "defense":
self.player.defense += item["amount"]
self.add_message(f"{item_name} equipped! Defense increased by {item['amount']}.")
# Confirm purchase
self.add_message(f"You bought {item_name} for ${cost}. Remaining money: ${self.player.money}.")
def get_player_image(self):
"""Returns current player image path if set."""
return getattr(self, "player_image_path", None)
# --------- Image switching ----------
def set_player_image(self, action="idle"):
"""Set player image based on action type ('idle', 'attack', 'run')."""
if hasattr(self, "image_assets") and action in self.image_assets:
choices = self.image_assets[action]
self.player_image_path = random.choice(choices)
def advance_player_image(self):
"""Cycle through all available images sequentially."""
if hasattr(self, "image_cycle") and self.image_cycle:
self.image_cycle_index = (self.image_cycle_index + 1) % len(self.image_cycle)
self.player_image_path = self.image_cycle[self.image_cycle_index]
# --- Gradio Interface Logic ---
# Create a single game instance to be managed by gr.State
game_instance = ScottPilgrimGame()
# New helper to format the message log into color-coded HTML
def _format_log_html(game_state):
"""Returns the message log as HTML with basic color-coding."""
# Early return for fresh page load
if not game_state.message_log:
return ""
player_name = game_state.player.name if game_state.player else ""
enemy_name = game_state.current_enemy.name if game_state.current_enemy else ""
# Show only the last 2 log messages: latest action and its outcome
recent_msgs = game_state.message_log[-2:]
html_lines = []
for msg in recent_msgs:
color = "#ECF0F1" # default light text for dark mode
if player_name and (msg.startswith(player_name) or msg.startswith("You")):
color = "#2ECC71" # green for player actions
elif enemy_name and msg.startswith(enemy_name):
color = "#E74C3C" # red for enemy actions
elif msg.startswith("---") or "Congratulations" in msg or "GAME OVER" in msg:
color = "#F1C40F" # yellow/gold for system or banner messages
html_lines.append(f"