Last active
April 12, 2020 21:37
-
-
Save Atorich/f95447f0a004acadfe6f0a91ebbea24d to your computer and use it in GitHub Desktop.
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 | |
import time | |
class Animal: | |
legs_count = None | |
eyes_count = None | |
def __init__(self, name): | |
self.name = name | |
self.health = 100 | |
def __str__(self): | |
return f'{self.name} (the {self.__class__.__name__}, {self.health})' | |
class Human(Animal): | |
legs_count = 2 | |
eyes_count = 2 | |
class Bear(Animal): | |
legs_count = 4 | |
eyes_count = 2 | |
class Spider(Animal): | |
legs_count = 4 | |
eyes_count = 4 | |
class Death(Exception): | |
def __init__(self, animal, *args): | |
self.animal = animal | |
super().__init__(*args) | |
body_parts = [ | |
('ноге', 1), | |
('голове', 4), | |
('туловищу', 2), | |
('руке', 1), | |
] | |
def handle_attack(subject, victim): | |
body_part, damage_factor = random.choice(body_parts) | |
damage = random.randrange(0, subject.legs_count) + damage_factor | |
avoid_chance = random.randrange(0, victim.eyes_count) | |
if avoid_chance >= 1: | |
print(f'{victim} увернулся.') | |
return victim.health | |
print(f'`{subject}` ебнул `{victim}` по {body_part} на {damage} урона.') | |
victim.health -= damage | |
return victim.health | |
def handle_round(a, b): | |
if random.random() > 0.5: | |
subject = a | |
victim = b | |
else: | |
subject = b | |
victim = a | |
victim_health = handle_attack(subject, victim) | |
if victim_health <= 0: | |
raise Death(victim) | |
def handle_fight(fighter_1, fighter_2): | |
print(f'Новый бой: {fighter_1} vs. {fighter_2}.') | |
while True: | |
handle_round(fighter_1, fighter_2) | |
time.sleep(0.33) | |
def main(): | |
fighters = [ | |
Human('Maxim'), | |
Human('Eugene'), | |
Bear('Misha'), | |
Spider('Peter Parker'), | |
] | |
while fighters: | |
random.shuffle(fighters) | |
fighter_1 = fighters[0] | |
fighter_2 = fighters[1] | |
try: | |
handle_fight(fighter_1, fighter_2) | |
except Death as exc: | |
print(f'Бой окончен, {exc.animal} умер.\n') | |
fighters.remove(exc.animal) | |
if len(fighters) < 2: | |
break | |
the_winner = fighters[0] | |
print(f'Победитель: {the_winner}') | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment