Last active
May 27, 2025 15:12
-
-
Save horstjens/bd3e53e6901bf56d60ba1627cc7177da to your computer and use it in GitHub Desktop.
oop001
This file contains hidden or 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 | |
class Monster: | |
# class variables | |
number = 0 | |
zoo = {} | |
def __init__(self, **kwargs): | |
self.number = Monster.number | |
Monster.number += 1 | |
Monster.zoo[self.number] = self | |
self.attack_value = max(0,int(random.gauss(10,3))) | |
self.defense_value = max(0,int(random.gauss(5,2))) | |
self.hitpoints = int(random.gauss(20,2)) | |
self.damage = int(random.gauss(5,1)) | |
# überschreiben oder sonstige attribute | |
for k,v in kwargs.items(): | |
self.__dict__[k] = v | |
class Skelett(Monster): | |
def __init__(self, **kwargs): | |
super().__init__(**kwargs) | |
self.attack_value = max(0,int(random.gauss(6,3))) | |
self.defense_value = max(0,int(random.gauss(2,1))) | |
self.hitpoints = int(random.gauss(20,2)) | |
self.damage = max(0,int(random.gauss(7,1))) | |
class Zombie(Monster): | |
def __init__(self, **kwargs): | |
super().__init__(**kwargs) | |
self.attack_value = max(0,int(random.gauss(7,2))) | |
self.defense_value = max(0,int(random.gauss(3,1))) | |
self.hitpoints = int(random.gauss(15,1)) | |
self.damage = max(0,int(random.gauss(4,1))) | |
for _ in range(10): | |
print(Skelett(farbe="rot").__dict__) | |
for _ in range(10): | |
print(Zombie(farbe="blau").__dict__) | |
def strike(a,b, counterstrike=False): | |
if a.hitpoints <= 0 or b.hitpoints <= 0: | |
return "" | |
if counterstrike: | |
text= "-----counterstrike!-----\n" | |
else: | |
text = "-------strike!-------\n" | |
text += f"{a.__class__.__name__}" | |
text += f" #{a.number} attacks {b.__class__.__name__}" | |
text += f" #{b.number}\n" | |
w1 = random.randint(1,6) | |
w2 = random.randint(1,6) | |
attack = w1+w2+a.attack_value | |
w3 = random.randint(1,6) | |
w4 = random.randint(1,6) | |
defense = w3+w4+b.defense_value | |
#a_string = f"attack: 2d6+{a.attack_value} vs. 2d6 + {b.defense_value}\n" | |
text += f"2d6 +attack: {w1}+{w2}+{a.attack_value} vs. 2d6+defense: {w3}+{w4}+{b.defense_value}\n" | |
# attack fails | |
if defense >= attack: | |
text += f"{attack} < {defense} attack fails\n" | |
return text | |
# hit! | |
w5 = random.randint(1,6) | |
w6 = random.randint(1,6) | |
damage = w5+w6 + a.damage | |
text += f"hit! 2d6 + damage: {w5}+{w6}+{a.damage} = {damage}\n" | |
b.hitpoints -= damage | |
if b.hitpoints <= 0: | |
text += f"defender dies! ({b.hitpoints} hp left)" | |
else: | |
text += f"defender has {b.hitpoints} hp left" | |
return text | |
clash(a,b): | |
# who strikes first? | |
if random.random() < 0.5: | |
a,b = b,a # swap positions | |
strike(a,b,counterstrike=False) | |
strike(b,a,counterstrike=True) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment