-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
106 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
|
73288c4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make me a game