Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- # The main class that controls the game.
- # Other classes initializing in him
- # and most of them are communicating there.
- class game:
- def __init__(self):
- self.show = interface()
- self.stats = statistics()
- self.human = player('human', input("Введите имя: "))
- self.pc = playerAi('pc')
- # A function that resets the values of the game
- # and prints a start message. It needs for restarting
- # with saving player name variable.
- def reset(self):
- self.human.health.set()
- self.pc.health.set()
- self.stats.set()
- self.pc.name = self.pc.choose_name()
- self.whose_move = random.choice([0, 1])
- self.move_count = 1
- self.show.start_game(self.pc.name, self.human.name)
- # A main cycle of the game.
- # Here it is determined whose move is now,
- # using the corresponding variable, on even
- # numbers of which -- pc goes, and on odd -- people.
- #
- # It operates with functions from other classes,
- # determining what action the character will perform
- # and how much damage it will cause,
- # displaying the corresponding messages.
- #
- # After the health value of one of the characters,
- # it displays a results message.
- def moves(self):
- self.show.next_move(self.move_count)
- if self.whose_move % 2 == 0:
- moves = {
- 'hit': self.human.health.decrease,
- 'hard_hit': self.human.health.decrease,
- 'heal': self.pc.health.increase
- }
- action = self.pc.think()
- value = self.pc.act(action)
- self.stats.add('pc', action, value)
- characters = [self.pc, self.human]
- else:
- moves = {
- 'hit': self.pc.health.decrease,
- 'hard_hit': self.pc.health.decrease,
- 'heal': self.human.health.increase
- }
- while True:
- self.show.move_hint()
- actions_map = {
- 'q': 'hit',
- 'w': 'hard_hit',
- 'e': 'heal'
- }
- action = actions_map.get(input().lower())
- if action in ['hit', 'hard_hit', 'heal']:
- break
- value = self.human.act(action)
- self.stats.add('human', action, value)
- characters = [self.human, self.pc]
- moves.get(action)(value)
- self.show.move(action, value, characters[0], characters[1])
- if self.human.health.value == 0:
- self.show.total(self.pc, self.stats)
- elif self.pc.health.value == 0:
- self.show.total(self.human, self.stats)
- self.move_count += 1
- self.whose_move += 1
- # A cycle, that runs the game, and ends when
- # one of the characters goes out of health.
- def run(self):
- self.reset()
- while self.human.health.value > 0 and self.pc.health.value > 0:
- self.moves()
- # Class, that collects the stats of the characters.
- # It creates a Nested Dictionary and put the stats in there.
- class statistics:
- # Create or reset the stats dictionary.
- def set(self):
- self.data = {'human': {
- 'damaged': 0,
- 'healed': 0,
- 'hits': 0
- },
- 'pc': {
- 'damaged': 0,
- 'healed': 0,
- 'hits': 0
- }
- }
- # Adds values to the keys in the stats dictionary
- # and counts a number of hits.
- def add(self, who, action, value):
- mapping = {
- 'hit': 'damaged',
- 'hard_hit': 'damaged',
- 'heal': 'healed'
- }
- self.data[who]['hits'] += 1
- self.data[who][mapping.get(action)] += value
- # Class, that helps to display the messages.
- class interface:
- def start_game(self, pc_name, human_name):
- print('\n========== Игра началась =========='
- f'\n {pc_name}'
- '\n VS\n'
- f' {human_name.title()}'
- '\n===================================')
- def next_move(self, move_number):
- print(f'\n --== Ход #{move_number} ==--')
- def move_hint(self):
- print('\n ---- Выберите ход: ----'
- '\n[q]Удар [w]Сильный удар [e]Лечение')
- def move(self, action, value, active_player, player):
- actions = {
- 'hit': ['ударил', 'отняв при этом'],
- 'hard_hit': ['замахнулся и ударил', 'нанеся урон'],
- 'heal': ['не тронул', 'восстановив в это время']
- }
- arrows = {
- 'hit': ['==', '>>'],
- 'hard_hit': ['>>', '>>'],
- 'heal': ['<<', ' ']
- }
- print(f'\n {active_player.name} {actions.get(action)[0]} '
- f'{player.name},\n {actions.get(action)[1]} {value}HP')
- print(f'\n {active_player.name}[{active_player.health.value}] '
- f'{arrows.get(action)[0]}[{value}]{arrows.get(action)[1]} '
- f'{player.name}[{player.health.value}]')
- def total(self, winner, statistics):
- stats = statistics.data[winner.health.identity]
- print("\n\n==================================="
- f"\n Победил {winner.name},"
- f"\n он нанес {stats['damaged']} урона "
- f"за {stats['hits']} ударов"
- f"\n и восстановив {stats['healed']} HP"
- "\n===================================")
- # A class for human characters.
- # Providing functions, that makes the character act.
- class player:
- def __init__(self, identification, name_input=''):
- self.name = name_input
- self.health = self.health()
- self.health.identity = identification
- # Returns a value of the damage.
- def act(self, action):
- range_map = {
- 'hit': (18, 25),
- 'hard_hit': (10, 35),
- 'heal': (18, 25)
- }
- return random.randrange(range_map.get(action)[0],
- range_map.get(action)[1])
- # An inner class of the 'player' class.
- # Creates and performs operations with 'health' variable.
- class health():
- def set(self):
- self.value = 100
- def increase(self, damage):
- self.value = self.value + damage
- self.normalize()
- def decrease(self, damage):
- self.value = self.value - damage
- self.normalize()
- def normalize(self):
- if self.value > 100: self.value = 100
- elif self.value < 0: self.value = 0
- # Child class to the player.
- # Inherits properties of the parent class and adds the ai.
- class playerAi(player):
- # Randomly chooses one of three options,
- # increasing the weight of the 'heal' option
- # when the health value is below or equal to 35.
- def think(self):
- if self.health.value <= 35:
- return random.choices([
- 'hit',
- 'hard_hit',
- 'heal'
- ], weights=[1, 1, 2])[0]
- else:
- return random.choices([
- 'hit',
- 'hard_hit',
- 'heal'
- ], weights=[1, 1, 1])[0]
- # Randomely choose a name from the list.
- def choose_name(self):
- names = [
- 'Stan',
- 'Francine',
- 'Hayley',
- 'Steve',
- 'Roger',
- 'Klaus',
- 'Jeff'
- ]
- return random.choice(names)
- if __name__ == '__main__':
- game = game()
- while True:
- game.run()
- if input("\nСыграть еще или выйти?[r, any]: ").lower() == 'r':
- continue
- break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement