Skip to content

Instantly share code, notes, and snippets.

@samvarankashyap
Created January 8, 2023 18:07
Show Gist options
  • Save samvarankashyap/af844288503c5981cba7f65d6a50a3e7 to your computer and use it in GitHub Desktop.
Save samvarankashyap/af844288503c5981cba7f65d6a50a3e7 to your computer and use it in GitHub Desktop.
Program simulating pokemon battle
import random
class Pokemon:
def __init__(self, name, level, type, max_health, attacks):
self.name = name
self.level = level
self.type = type
self.max_health = max_health
self.health = max_health
self.attacks = attacks
self.is_knocked_out = False
def __repr__(self):
return f'{self.name} (Level {self.level})'
def lose_health(self, amount):
self.health -= amount
if self.health <= 0:
self.health = 0
self.is_knocked_out = True
print(f'{self.name} has been knocked out!')
def gain_health(self, amount):
self.health += amount
if self.health > self.max_health:
self.health = self.max_health
def attack(self, other_pokemon):
if self.is_knocked_out:
print(f'{self.name} is knocked out and cannot attack!')
return
print(f'{self.name} attacks {other_pokemon.name}!')
attack = random.choice(self.attacks)
print(f'{self.name} uses {attack.name}')
damage = attack.calculate_damage(self.level, self.type, other_pokemon.type)
other_pokemon.lose_health(damage)
class Attack:
def __init__(self, name, type, damage):
self.name = name
self.type = type
self.damage = damage
def calculate_damage(self, level, attacker_type, defender_type):
effectiveness = self.get_effectiveness(attacker_type, defender_type)
return level * self.damage * effectiveness
def get_effectiveness(self, attacker_type, defender_type):
if self.type == attacker_type:
return 1.5
elif self.type == defender_type:
return 0.5
else:
return 1
# Create some attacks
scratch = Attack('Scratch', 'normal', 10)
ember = Attack('Ember', 'fire', 15)
water_gun = Attack('Water Gun', 'water', 20)
# Create some Pokemon
charmander = Pokemon('Charmander', 5, 'fire', 50, [scratch, ember])
squirtle = Pokemon('Squirtle', 5, 'water', 50, [scratch, water_gun])
# Start the battle
charmander.attack(squirtle)
squirtle.attack(charmander)
charmander.attack(squirtle)
squirtle.attack(charmander)
@samvarankashyap
Copy link
Author

This program defines two classes: Pokemon and Attack. The Pokemon class represents a Pokemon, and the Attack class represents an attack that a Pokemon can use. The Pokemon class has a number of methods that allow it to attack another Pokemon, lose health, and gain health. The Attack class has a method that calculates the damage

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment