Last active
July 25, 2024 06:37
-
-
Save horstjens/9d26fd51f7ab44d2b03f96a9114fc725 to your computer and use it in GitHub Desktop.
pokemonfight
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
# battle between 2 pokemons | |
import random | |
class Pokemon: | |
def __init__(self, name): | |
self.name = name | |
self.hp_full = 100 | |
self.hp = 100 | |
self.speed = 10 | |
self.attack = 7 | |
self.defense = 5 | |
self.attack_magic = 3 | |
self.defense_magic = 2 | |
def display_stats(self): | |
print(f"---- stats of {self.name} -----") | |
for k,v in self.__dict__.items(): | |
if not k.startswith("__"): | |
print(f"{k:>18}:{v:>5}") | |
def fight(a,b): | |
print(f"========== {a.name} vs. {b.name} =========") | |
# ---- faster monster attacks first ---- | |
speed_a = a.speed + random.randint(1,6) | |
speed_b = b.speed + random.randint(1,6) | |
print("speed rolls:", speed_a, speed_b) | |
if speed_a == speed_b: | |
speed_a += random.choice((1,-1)) | |
if speed_a > speed_b: | |
print(a.name, "attacks first") | |
attack(a,b) | |
# counterstrike | |
if b.hp > 0: | |
print(b.name, "strikes back") | |
attack(b,a) | |
else: | |
print(b.name, "attacks first") | |
attack(b,a) | |
if a.hp >0: | |
print(a.name, "strikes back") | |
attack(a,b) | |
print("------- end result: -------------") | |
print(f"{a.name}: {a.hp} hp left of {a.hp_full} ({a.hp/a.hp_full:.2f}%)") | |
print(f"{b.name}: {b.hp} hp left of {b.hp_full} ({b.hp/b.hp_full:.2f}%)") | |
def physical_attack(attacker, defender): | |
attack_value = attacker.attack + random.randint(1,6) | |
defense_value = defender.defense + random.randint(1,6) | |
print("physical attack:", attack_value, "vs.", defense_value) | |
if attack_value > defense_value: | |
damage = attack_value - defense_value | |
# print as % of full hp | |
print("damage:", damage, "% of full hp:", damage/defender.hp_full*100) | |
defender.hp -= damage | |
if defender.hp <= 0: | |
print("defender dies!") | |
else: | |
print("attack fails") | |
def magical_attack(attacker, defender): | |
attack_value = attacker.magical_attack + random.randint(1,6) | |
defense_value = defender.magical_defense + random.randint(1,6) | |
print("magical attack:", attack_value, "vs.", defense_value) | |
if attack_value > defense_value: | |
damage = attack_value - defense_value | |
# print as % of full hp | |
print("damage:", damage, "% of full hp:", damage/defender.full_hp*100) | |
defender.hp -= damage | |
if defender.hp <= 0: | |
print("defender dies!") | |
else: | |
print("attack fails") | |
def attack(attacker,defender): | |
# physical or magic ? | |
diff_physic = attacker.attack - defender.defense | |
diff_magic = attacker.attack_magic - defender.defense_magic | |
if diff_physic == diff_magic: | |
diff_physic += random.choice((-1,1)) | |
if ((diff_physic > 0) and (diff_magic > 0)) or ( | |
(diff_physic < 0) and (diff_magic < 0)): | |
if diff_physic > diff_magic: | |
physical_attack(attacker,defender) | |
else: | |
magic_attack(attacker,defender) | |
elif diff_physic > 0: | |
physical_attack(attacker,defender) | |
else: | |
magic_attack(attacker,defender) | |
ignivor = Pokemon("igni") | |
# change here stats of ignivor | |
# ignivor.attack_magic = 22 | |
ignivor.display_stats() | |
hydropy = Pokemon("hydro") | |
# change here stats of hydropy | |
# hydropy.defense_magic = 0 | |
hydropy.display_stats() | |
#put this into a loop | |
fight(ignivor, hydropy) | |
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
import random | |
# d.... normaler Würfel | |
# D.....nochmal würfeln bei (höchster Zahl), ergebnis addieren | |
""" | |
Beispiel | |
1D6....6+4 = 6-1 + 4 | |
1D6 6+1 = 6-1+1 | |
1D6....6+6+6+2 = (6-1)+(6-1)+(6-1)+2 | |
= 5+5+5+2 = 17 | |
""" | |
def roll(dicestring="1d6"): | |
# guardians, checking if dicestring is valid | |
if "d" not in dicestring.lower(): | |
raise ValueError("neiter 'd' nor 'D' found in dicestring", dicestring) | |
if dicestring.lower().count("d") > 1: | |
raise ValueError("more than one 'd'(or 'D') found inside dicestring", dicestring) | |
dice, sides = dicestring.lower().split("d") | |
if not (dice.isnumeric() and sides.isnumeric()): | |
raise ValueError("non-numeric char (except 'd'/'D') found in dicestring", dicestring) | |
# repeat ? | |
if "d" in dicestring: | |
repeat = False | |
elif "D" in dicestring: | |
repeat = True | |
dice = int(dice) | |
sides = int(sides) | |
#print("number of dice:", dice) | |
#print("number of sides per die:", sides) | |
resultstring = "" | |
result = 0 | |
for i in range(dice): | |
x = random.randint(1,sides) | |
while repeat and (x == sides): | |
result += (x-1) | |
resultstring += f"({x}-1)+" | |
x = random.randint(1,sides) | |
resultstring += f"{x}" | |
if i < dice-1: | |
resultstring += "+" | |
result += x | |
resultstring += f"={result}" | |
return result, resultstring | |
print(roll("8D20")) | |
#print(roll("2x5")) | |
#roll("4d2 ") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment