import random
# --- Data Structures ---
class Pokemon:
def __init__(self, pokemon_id, name, type1, type2, base_stats, moves):
self.pokemon_id = pokemon_id
self.name = name
self.type1 = type1
self.type2 = type2
self.base_stats = base_stats # Dictionary: HP, Attack, Defense, SpAttack, SpDefense, Speed
self.moves = moves # List of Move objects
self.current_hp = base_stats['HP']
self.status = None # e.g., 'Poisoned', 'Paralyzed', None
def __str__(self):
return f"{self.name} (Type: {self.type1}, {self.type2 if self.type2 else 'None'}, HP: {self.current_hp}/{self.base_stats['HP']})"
class Move:
def __init__(self, move_id, name, type, category, power, accuracy, pp, effect, effect_chance):
self.move_id = move_id
self.name = name
self.type = type
self.category = category # Physical, Special, Status
self.power = power
self.accuracy = accuracy
self.pp = pp
self.effect = effect # e.g., 'Poison', 'Paralyze', 'Burn', None
self.effect_chance = effect_chance
def __str__(self):
return f"{self.name} (Type: {self.type}, Power: {self.power}, Accuracy: {self.accuracy}, PP: {self.pp})"
# --- Type Effectiveness Chart ---
# Simplified example; expand this for a full chart
TYPE_EFFECTIVENESS = {
'Fire': {'Fire': 0.5, 'Water': 0.5, 'Grass': 2.0, 'Ground': 0.5},
'Water': {'Fire': 2.0, 'Water': 0.5, 'Grass': 0.5, 'Ground': 2.0},
'Grass': {'Fire': 0.5, 'Water': 2.0, 'Grass': 0.5, 'Ground': 2.0},
'Normal': {} #Normal type has no super effectiveness
}
# --- Helper Functions ---
def calculate_damage(attacker, defender, move):
"""Calculates damage based on attacker, defender, and move."""
level = 50 # Example level
attack_stat = attacker.base_stats['Attack'] if move.category == 'Physical' else attacker.base_stats['SpAttack']
defense_stat = defender.base_stats['Defense'] if move.category == 'Physical' else defender.base_stats['SpDefense']
#Type Effectiveness
type_effectiveness = get_type_effectiveness(move.type, defender.type1, defender.type2)
#STAB
stab = 1.5 if attacker.type1 == move.type or attacker.type2 == move.type else 1
damage = (((((2 * level) / 5) + 2) * move.power * (attack_stat / defense_stat)) / 50 + 2) * type_effectiveness * stab
damage *= random.uniform(0.85, 1.0) # Random variance
return int(damage) # Round down to an integer
def get_type_effectiveness(move_type, defender_type1, defender_type2):
"""Calculates the type effectiveness multiplier."""
effectiveness = 1.0
if move_type in TYPE_EFFECTIVENESS:
effectiveness *= TYPE_EFFECTIVENESS[move_type].get(defender_type1, 1.0)
if defender_type2:
effectiveness *= TYPE_EFFECTIVENESS[move_type].get(defender_type2, 1.0)
return effectiveness
def apply_status_effect(target, move):
"""Applies a status effect if the move has one and the chance succeeds."""
if move.effect and random.random() < move.effect_chance / 100: #Chance needs to be a float
target.status = move.effect
print(f"{target.name} was {move.effect}!")
def handle_status_damage(pokemon):
"""Handles damage from status effects like poison or burn."""
if pokemon.status == 'Poisoned':
damage = pokemon.base_stats['HP'] // 8 # 1/8th of max HP
pokemon.current_hp -= damage
print(f"{pokemon.name} is hurt by poison! (HP: {pokemon.current_hp})")
# Add more status effects as needed
def check_fainted(pokemon):
"""Checks if a Pokemon has fainted."""
return pokemon.current_hp <= 0
# --- Battle Logic ---
def battle(pokemon1, pokemon2):
"""Simulates a battle between two Pokemon."""
print(f"\n--- Battle Start: {pokemon1.name} vs. {pokemon2.name} ---")
turn = 1
while True:
print(f"\n--- Turn {turn} ---")
# Player 1's Turn (Pokemon1)
move1 = choose_move(pokemon1) # Replace with AI or player input later
print(f"{pokemon1.name} used {move1.name}!")
execute_move(pokemon1, pokemon2, move1)
if check_fainted(pokemon2):
print(f"{pokemon2.name} fainted! {pokemon1.name} wins!")
break
# Apply Status damage after pokemon1's turn
handle_status_damage(pokemon2)
if check_fainted(pokemon2):
print(f"{pokemon2.name} fainted! {pokemon1.name} wins!")
break
# Player 2's Turn (Pokemon2)
move2 = choose_move(pokemon2) # Replace with AI or player input
print(f"{pokemon2.name} used {move2.name}!")
execute_move(pokemon2, pokemon1, move2)
if check_fainted(pokemon1):
print(f"{pokemon1.name} fainted! {pokemon2.name} wins!")
break
# Apply Status damage after pokemon2's turn
handle_status_damage(pokemon1)
if check_fainted(pokemon1):
print(f"{pokemon1.name} fainted! {pokemon2.name} wins!")
break
turn += 1
def choose_move(pokemon):
"""Chooses a move for the Pokemon. Replace with AI or player input."""
# Simple: Choose a random move
return random.choice(pokemon.moves)
def execute_move(attacker, defender, move):
"""Executes a move, calculating damage and applying effects."""
if random.random() < move.accuracy / 100: #Check move accuaracy
damage = calculate_damage(attacker, defender, move)
defender.current_hp -= damage
print(f"{defender.name} took {damage} damage! (HP: {defender.current_hp})")
apply_status_effect(defender, move) #Apply Status Effects
else:
print(f"{attacker.name}'s attack missed!")
# --- Example Pokemon and Moves ---
#Bulbasaur moves
tackle = Move(1, "Tackle", "Normal", "Physical", 40, 100, 35, None, 0)
vine_whip = Move(2, "Vine Whip", "Grass", "Physical", 45, 100, 25, None, 0)
#Charmander moves
ember = Move(3, "Ember", "Fire", "Special", 40, 100, 25, "Burn", 10) #Effect chance set to 10
scratch = Move(4, "Scratch", "Normal", "Physical", 40, 100, 35, None, 0)
bulbasaur = Pokemon(1, "Bulbasaur", "Grass", "Poison",
{'HP': 45, 'Attack': 49, 'Defense': 49, 'SpAttack': 65, 'SpDefense': 65, 'Speed': 45},
[tackle, vine_whip])
charmander = Pokemon(4, "Charmander", "Fire", None,
{'HP': 39, 'Attack': 52, 'Defense': 43, 'SpAttack': 60, 'SpDefense': 50, 'Speed': 65},
[ember, scratch])
# --- Main ---
if __name__ == "__main__":
battle(bulbasaur, charmander)