Skip to content

Commit

Permalink
Create Battlefield
Browse files Browse the repository at this point in the history
  • Loading branch information
Binni213 authored Apr 5, 2023
1 parent cfa331f commit 73288c4
Showing 1 changed file with 106 additions and 0 deletions.
106 changes: 106 additions & 0 deletions Battlefield
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import random

# Define player and enemy classes

class Character:

def __init__(self, name, max_health, attack):

self.name = name

self.max_health = max_health

self.current_health = max_health

self.attack = attack

def take_damage(self, damage):

self.current_health -= damage

if self.current_health < 0:

self.current_health = 0

def is_alive(self):

return self.current_health > 0

def attack_target(self, target):

damage = random.randint(1, self.attack)

target.take_damage(damage)

print(f"{self.name} attacks {target.name} for {damage} damage.")

class Player(Character):

def __init__(self, name, max_health, attack, potions):

super().__init__(name, max_health, attack)

self.potions = potions

def use_potion(self):

if self.potions > 0:

self.current_health += 20

self.potions -= 1

print(f"{self.name} drinks a health potion and restores 20 health.")

else:

print("You don't have any potions left!")

class Enemy(Character):

pass

# Define the game loop

def game_loop():

player = Player("Player", 100, 20, 3)

enemy = Enemy("Enemy", 50, 10)

print("You have encountered an enemy! Prepare to battle!")

while player.is_alive() and enemy.is_alive():

action = input("What do you want to do? (attack, potion) ")

if action == "attack":

player.attack_target(enemy)

if enemy.is_alive():

enemy.attack_target(player)

elif action == "potion":

player.use_potion()

enemy.attack_target(player)

else:

print("Invalid action! Try again.")

if player.is_alive():

print("Congratulations! You have defeated the enemy!")

else:

print("You have been defeated. Game over.")

# Start the game

game_loop()

1 comment on commit 73288c4

@Binni213
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make me a game

Please sign in to comment.