Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Player:
- def __init__(self, health, action_points, damage, x, y, name, marker):
- self.x = x
- self.y = y
- self.health = health
- self.action_points = action_points
- self.maximum_action_points = action_points
- self.name = name
- self.marker = marker
- self.damage = damage
- def hit(self, enemy):
- enemy.health -= self.damage
- self.action_points -= 1
- def move(self, x, y):
- self.x = x
- self.y = y
- self.action_points -= 1
- def is_alive(self):
- return self.health > 0
- def has_action_points(self):
- return self.action_points > 0
- def restore_action_points(self):
- self.action_points = self.maximum_action_points
- class Game:
- cells = {0: '#', 1: ' ', 2: '~', 3: '`'}
- cells_rev = {value: key for key, value in cells.items()}
- directions = {
- 'w': (-1, 0),
- 's': (1, 0),
- 'a': (0, -1),
- 'd': (0, 1)
- }
- def __init__(self, map, player_1: Player, player_2: Player, levers):
- self.map = map
- self.player_1 = player_1
- self.player_2 = player_2
- self.levers = levers
- self.portals = [] # хранит пары координат
- n = len(self.map)
- m = len(self.map[0])
- for x in range(n):
- for y in range(m):
- if map[x][y] == 2:
- self.portals.append((x, y))
- def print_map(self):
- n = len(self.map)
- m = len(self.map[0])
- for x in range(n):
- for y in range(m):
- if x == self.player_1.x and y == self.player_1.y:
- print(self.player_1.marker, end='')
- elif x == self.player_2.x and y == self.player_2.y:
- print(self.player_2.marker, end='')
- else:
- cell = self.map[x][y]
- print(self.cells[cell], end='')
- print()
- def next_cell(self, direction, player: Player):
- if direction not in self.directions:
- raise ValueError('Некорректное направление: ' + direction)
- # direction -- w, s, a или d
- dx, dy = self.directions[direction]
- return player.x + dx, player.y + dy
- def process_move(self, command, player: Player, other_player: Player):
- # w s a d
- # if command not in 'wsad':
- # if command not in ['w', 's', 'a', 'd']:
- x, y = self.next_cell(command, player)
- if self.map[x][y] == 0 or x == other_player.x and y == other_player.y:
- return
- player.move(x, y)
- def process_command(self, command, player: Player, other_player: Player):
- # command: 'w|s|a|d' -- process_move
- # command: 'h w|s|a|d' -- process_hit
- # command.split() = ['w|s|a|d']
- # command.split() = ['h', 'w|s|a|d']
- try:
- # write tour code here
- commands = command.split()
- if len(commands) == 0:
- return
- if commands[0] == 'u':
- self.process_use(player)
- elif commands[0] == 'h':
- self.process_hit(commands[1], player, other_player)
- else:
- self.process_move(command, player, other_player)
- except ValueError as e:
- print(e)
- def process_use(self, player: Player):
- cell = self.map[player.x][player.y]
- if cell == 2:
- for (x, y) in self.portals:
- if x != player.x or y != player.y:
- player.move(x, y)
- break
- elif cell == 3:
- pass
- def process_hit(self, command, player: Player, other_player: Player):
- x, y = self.next_cell(command, player)
- if x == other_player.x and y == other_player.y:
- player.hit(other_player)
- def print_info(self, player: Player):
- print('{} ({}) has {} HP and {} AP'.format(
- player.name,
- player.marker,
- player.health,
- player.action_points
- ))
- pass
- def one_step(self):
- # player 1
- while self.player_1.has_action_points():
- self.print_map()
- self.print_info(self.player_1)
- self.print_info(self.player_2)
- command = input()
- self.process_command(command, self.player_1, self.player_2)
- # player 2
- while self.player_2.has_action_points():
- self.print_map()
- self.print_info(self.player_2)
- self.print_info(self.player_1)
- command = input()
- self.process_command(command, self.player_2, self.player_1)
- self.player_1.restore_action_points()
- self.player_2.restore_action_points()
- def map_converter(string_map):
- return [[Game.cells_rev[cell] for cell in row] for row in string_map.split('\n')]
- string_map = '''
- ###################
- # # ~#
- #~ # #### #
- # # # #
- # ## # # #
- # # # #
- # # ## # # #
- #` # # #
- ###################
- '''.strip()
- game_map = map_converter(string_map)
- player_1 = Player(
- name='John',
- marker='@',
- damage=1,
- health=3,
- action_points=20,
- x=1,
- y=1
- )
- player_2 = Player(
- name='Peter',
- marker='$',
- damage=1,
- health=3,
- action_points=1,
- x=1,
- y=2
- )
- levers = {
- (7, 1): (6, 4)
- }
- game = Game(game_map, player_1, player_2, levers)
- game.print_map()
- while True:
- game.one_step()
- # https://pastebin.com/XYjXtp3K
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement