Okorosso

Untitled

Sep 13th, 2019
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.00 KB | None | 0 0
  1. import random
  2. from random import randint
  3.  
  4.  
  5. class Field:
  6.     # initialisation
  7.     def __init__(self, pos_hero):
  8.         self.playing_field = ['*', '*', '*', '*', '*'], ['*', '*', '*', '*', '*'], ['*', '*', '*', '*', '*'], ['*', '*', '*', '*', '*'], ['*', '*', '*', '*', '*']
  9.         self.count_of_stone = 7
  10.         self.stones = [randint(0, 4) for j in range(self.count_of_stone * 2)]
  11.         for i in range(7):
  12.             self.playing_field[self.stones[i]][self.stones[i + 1]] = 'S'
  13.         self.playing_field[pos_hero[0]][pos_hero[1]] = 'H'
  14.     # add things on the field
  15.  
  16.     # respawn stones?
  17.  
  18.     def print(self):
  19.         for i in range(5):
  20.    
  21.             for j in range(5):
  22.                 if j != 4:
  23.                     print(self.playing_field[i][j], end='')
  24.                 else:
  25.                     print(self.playing_field[i][j])
  26.  
  27.  
  28. class Hero:
  29.  
  30.     def __init__(self):
  31.         self.name = 'Artur'
  32.         self.hp = 10
  33.         self.backpack_count = 0
  34.         self.backpack = []
  35.         self.pos = [randint(0, 4), randint(0, 4)]
  36.         self.last_button = ''
  37.         self.field = None
  38.  
  39.     # def __init__(self):
  40.     #     pass
  41.  
  42.     # action
  43.     def print_inventory(self):
  44.         for i in range(len(self.backpack)):
  45.             print(self.backpack[i], end='')
  46.         print()
  47.  
  48.     def change_pos(self, button):
  49.         self.last_button = button
  50.         if (button == 'S' or button == 's') and self.pos[0] + 1 < 6:
  51.             if self.field.playing_field[self.pos[0] + 1][self.pos[1]] == 'S':
  52.                 print('If U want take stone, pls press F')
  53.                 a = input()
  54.                 if a.lower() == 'f':
  55.                     hero.backpack_count += 1
  56.                     hero.backpack.append('Stone')
  57.             self.field.playing_field[self.pos[0]][self.pos[1]] = '*'
  58.             self.pos[0] += 1
  59.             self.field.playing_field[self.pos[0]][self.pos[1]] = 'H'
  60.         if (button == 'W' or button == 'w') and self.pos[0] - 1 > -1:
  61.             if self.field.playing_field[self.pos[0] - 1][self.pos[1]] == 'S':
  62.                 print('If U want take stone, pls press F')
  63.                 a = input()
  64.                 if a.lower() == 'f':
  65.                     hero.backpack_count += 1
  66.                     hero.backpack.append('Stone')
  67.             self.field.playing_field[self.pos[0]][self.pos[1]] = '*'
  68.             self.pos[0] -= 1
  69.             self.field.playing_field[self.pos[0]][self.pos[1]] = 'H'
  70.         if (button == 'D' or button == 'd') and self.pos[1] + 1 < 6:
  71.             if self.field.playing_field[self.pos[0]][self.pos[1] + 1] == 'S':
  72.                 print('If U want take stone, pls press F')
  73.                 a = input()
  74.                 if a.lower() == 'f':
  75.                     hero.backpack_count += 1
  76.                     hero.backpack.append('Stone')
  77.             self.field.playing_field[self.pos[0]][self.pos[1]] = '*'
  78.             self.pos[1] += 1
  79.             self.field.playing_field[self.pos[0]][self.pos[1]] = 'H'
  80.         if (button == 'A' or button == 'a') and self.pos[1] - 1 > -1:
  81.             if self.field.playing_field[self.pos[0]][self.pos[1] - 1] == 'S':
  82.                 print('If U want take stone, pls press F')
  83.                 a = input()
  84.                 if a.lower() == 'f':
  85.                     hero.backpack_count += 1
  86.                     hero.backpack.append('Stone')
  87.             self.field.playing_field[self.pos[0]][self.pos[1]] = '*'
  88.             self.pos[1] -= 1
  89.             self.field.playing_field[self.pos[0]][self.pos[1]] = 'H'
  90.  
  91.     def drop_stone(self):
  92.         if self.last_button.lower() == 'w' and self.pos[0] - 1 > -1:
  93.             field.playing_field[self.pos[0] - 1][self.pos[1]] = 'S'
  94.             hero.backpack.pop()
  95.             hero.backpack_count -= 1
  96.         elif self.last_button.lower() == 's' and self.pos[0] + 1 < 6:
  97.             field.playing_field[self.pos[0] + 1][self.pos[1]] = 'S'
  98.             hero.backpack.pop()
  99.             hero.backpack_count -= 1
  100.         elif self.last_button.lower() == 'a' and self.pos[1] - 1 > -1:
  101.             field.playing_field[self.pos[0]][self.pos[1] - 1] = 'S'
  102.             hero.backpack.pop()
  103.             hero.backpack_count -= 1
  104.         elif self.last_button.lower() == 'd' and self.pos[1] + 1 < 6:
  105.             field.playing_field[self.pos[0]][self.pos[1] + 1] = 'S'
  106.             hero.backpack.pop()
  107.             hero.backpack_count -= 1
  108.  
  109.  
  110. hero = Hero()
  111. field = Field(hero.pos)
  112. field.print()
  113. hero.field = field
  114.  
  115. while hero.hp > 0:
  116.     print('press one of keys WASD, Q to drop stone, I to check inventory')
  117.     button = str(input())
  118.     if button.lower() == 'i':
  119.         print(hero.backpack)
  120.     if button.lower() == 'q':
  121.         hero.drop_stone()
  122.  
  123.     if button.lower() == 'w' or button.lower() == 's' or button.lower() == 'a' or button.lower() == 'd':
  124.         hero.change_pos(button)
  125.  
  126.     if hero.backpack_count > 0 and button.lower() != 'i':
  127.         print('count items in backpack =', hero.backpack_count)
  128.     if button.lower() != 'i':
  129.         field.print()
  130.         print(hero.pos)
Add Comment
Please, Sign In to add comment