Skip to content

Instantly share code, notes, and snippets.

@enzomtpYT
Last active April 19, 2024 10:49
Show Gist options
  • Save enzomtpYT/130d9edb00e0d1197e0130ed46613150 to your computer and use it in GitHub Desktop.
Save enzomtpYT/130d9edb00e0d1197e0130ed46613150 to your computer and use it in GitHub Desktop.
from dataclasses import dataclass
import random, os, time
from colorama import Fore, Back, Style
@dataclass
class Character():
name: str
__atq: int
__hp: int
__potions: int
__boosted: bool
__mp: int
__max_hp: int
def __str__(self):
if self.boosted:
return f"{Style.BRIGHT}{self.name}:\n{Fore.RED}Base Attack: {self.atq} {Fore.YELLOW}▲\n{Fore.GREEN}HP: {self.hp}\n{Fore.MAGENTA}Magic Points: {self.mp}\n{Fore.CYAN}Potions: {self.potions}{Style.RESET_ALL}"
else:
return f"{Style.BRIGHT}{self.name}:\n{Fore.RED}Base Attack: {self.atq}\n{Fore.GREEN}HP: {self.hp}\n{Fore.MAGENTA}Magic Points: {self.mp}\n{Fore.CYAN}Potions: {self.potions}{Style.RESET_ALL}"
def __init__(self, name="", atq=0, hp=0, potions=5, mp=0, max_hp=20):
if name == "":
name = input("Please enter your nickname: ")
if atq == 0:
atq = random.randint(1, 5)
if hp == 0:
hp = 20
if mp == 0:
mp = random.randint(5, 10)
self.atq = atq
self.hp = hp
self.name = name
self.potions = potions
self.mp = mp
self.boosted = False
self.max_hp = max_hp
def attack(self, target):
if random.randint(0, 25) == 1:
print(f"{Fore.BLACK}{Back.WHITE}You tripped and missed your attack!{Style.RESET_ALL}")
else:
if self.boosted:
target.hp -= self.atq+random.randint(0, 5)
self.boosted = False
else:
target.hp -= self.atq+random.randint(-1, 1)
return target.hp
def boost(self):
if self.mp < 2:
print(f"{Fore.BLACK}{Back.WHITE}Not enough magic points!{Style.RESET_ALL}")
return False
else:
self.mp -= 2
if self.boosted:
return False
else:
self.boosted = True
def use_pot(self):
if self.potions == 0 & self.hp > 20:
print(f"{Fore.BLACK}{Back.WHITE}No potions left!{Style.RESET_ALL}")
return False
else:
if random.randint(0, 25) == 1:
print(f"{Fore.BLACK}{Back.WHITE}You got lucky, potion unused!{Style.RESET_ALL}")
heal = random.randint(1, 3)
if self.hp + heal > self.max_hp:
self.hp = self.max_hp
else:
self.hp += heal
else:
r = random.randint(0, 50)
if r == 1:
print(f"{Fore.BLACK}{Back.WHITE}Ah looser you dropped your potion!{Style.RESET_ALL}")
self.potions -= 1
elif r == 2:
print(f"{Fore.BLACK}{Back.WHITE}You blind? You just drank a poison! You lost 3 HP!{Style.RESET_ALL}")
self.hp -= 3
else:
self.potions -= 1
heal = random.randint(1, 3)
if self.hp + heal > self.max_hp:
self.hp = self.max_hp
else:
self.hp += heal
return self.hp
def infos(self):
print(self)
def is_dead(self):
if self.hp <= 0:
return True
else:
return False
def get_pot(self):
return self.potions
def get_hp(self):
return self.hp
def ask_action(player):
action = input(f"\nAttack: a/A, \nUse a potion ({player.get_pot()} potions left): p/P, \nBoost: b/B\nChoose your action: ")
if action == "a" or action == "A":
return "attack"
elif action == "p" or action == "P":
return "potion"
elif action == "b" or action == "B":
return "boost"
else:
print("Invalid action")
return ask_action(player)
def game():
print("Welcome adventurer in this world of infamy and despair !\nYou are about to face the most dangerous creature of this world...\nA GOLUM !\nGood beating this rock !\n\n")
player = Character()
enemy = Character("Golum")
# clear the console
os.system("cls")
while not player.is_dead() and not enemy.is_dead():
# Player turn
os.system("cls")
player.infos()
print("\n")
enemy.infos()
# Choose actions
action = ask_action(player)
if action == "attack":
player.attack(enemy)
elif action == "potion":
player.use_pot()
elif action == "boost":
player.boost()
time.sleep(0.5)
# Enemy turn
if not enemy.is_dead():
os.system("cls")
enemy.infos()
# Choose actions
if enemy.get_pot() > 0 & enemy.get_hp() < 6:
action = random.choice(["attack", "potion", "boost"])
else:
action = random.choice(["attack", "boost"])
if action == "attack":
print(f"{Fore.BLACK}{Back.WHITE}Enemy attacked !{Style.RESET_ALL}")
enemy.attack(player)
elif action == "potion":
print(f"{Fore.BLACK}{Back.WHITE}Enemy used a potion !{Style.RESET_ALL}")
enemy.use_pot()
elif action == "boost":
print(f"{Fore.BLACK}{Back.WHITE}Enemy boosted !{Style.RESET_ALL}")
enemy.boost()
print("\n")
player.infos()
input("Press enter to continue")
if player.is_dead():
os.system("cls")
print("You lost ! Press F in chat")
else:
os.system("cls")
print("You won ! GG EZ 😎")
print("After all golum was just a rock😒...")
input("Press enter to exit")
def main():
os.system("cls")
game()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment