Example output
import random
# Character creation
class Character:
def __init__(self, name, health, attack, defense):
self.name = name
self.health = health
self.attack = attack
self.defense = defense
# Calculate damage dealt to enemy
def calc_damage(self, enemy_defense):
damage = max(self.attack - enemy_defense, 1)
return damage
# Enemy creation
class Enemy:
def __init__(self, name, health, attack, defense):
self.name = name
self.health = health
self.attack = attack
self.defense = defense
# Calculate damage dealt to player
def calc_damage(self, player_defense):
damage = max(self.attack - player_defense, 1)
return damage
# Combat function
def combat(player, enemy):
while player.health > 0 and enemy.health > 0:
# Player's turn
enemy.health -= player.calc_damage(enemy.defense)
if enemy.health <= 0:
print(f"{player.name} defeated {enemy.name}")
break
# Enemy's turn
player.health -= enemy.calc_damage(player.defense)
if player.health <= 0:
print(f"{enemy.name} defeated {player.name}")
break
# Main game function
def main():
player_name = input("Enter your character's name: ")
player = Character(player_name, 100, 10, 5)
enemy_name = "Goblin"
enemy = Enemy(enemy_name, 50, 8, 3)
combat(player, enemy)
# Start the game
if __name__ == "__main__":
main()