Advertisement
Guest User

Untitled

a guest
Dec 16th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.55 KB | None | 0 0
  1. import random
  2. import time
  3. from colorama import init, Fore, Back, Style
  4. init(convert=True)
  5. print(Fore.RED + 'some red text' + Fore.RESET)
  6. class Menu:
  7. def __init__(self, engine):
  8. self.engine = engine
  9. self.menu()
  10.  
  11. def sandbox_mode(self):
  12. user_place = None
  13. while True:
  14. user_place = input('Where to place unit? Example: 5,8\n\nwhere 5 is X and 8 is Y, or just type "start" to start the game: ')
  15. if user_place.lower() == 'start':
  16. break
  17. coords = user_place.split(',')
  18. print(user_place)
  19. unit = Unit(name='Red', hp=1, y=int(coords[1]), x=int(coords[0]), damage=1, map_engine=gmap,
  20. team='Red')
  21. self.engine.display()
  22. print('Done!')
  23.  
  24. def menu(self):
  25. print(Fore.LIGHTYELLOW_EX + '''Hello there, friend! Welcome to TABSlike 0.1! I hope you'll like this game :)\n
  26. Right now there's two modes - sandbox and random encounter. Which one would you like to see?\n
  27. 1) Sandbox
  28. 2) Random encounter''')
  29. choice = input()
  30. if choice == '1':
  31. pass
  32. if choice == '2':
  33. self.sandbox_mode()
  34. else:
  35. print('Not an option.')
  36. print(choice)
  37. self.menu()
  38.  
  39.  
  40. class GameMap:
  41.  
  42. def __init__(self, size_x, size_y):
  43. self.size_x = size_x
  44. self.size_y = size_y
  45. self.gamemap = self.generate_map()
  46. self.techmap = self.generate_map()
  47. self.alive_list = []
  48.  
  49. def something(self, units_number):
  50. units_num = 0
  51. while units_num < units_number:
  52. b_coords = [random.randint(round(self.size_y / 2), self.size_y - 1),
  53. random.randint(round(self.size_x / 2), self.size_x - 1)]
  54. if not self.check_entity(b_coords[0], b_coords[1]):
  55. unit_blue = Unit(name='Blue' + str(units_num), hp=1, y=b_coords[0], x=b_coords[1],
  56. damage=1, map_engine=gmap, team='Blue')
  57. units_num += 1
  58. else:
  59. continue
  60.  
  61. def generate_map(self):
  62. gamemap = []
  63. for each_y in range(self.size_y):
  64. gamemap.append(['.' for _ in range(self.size_x)])
  65. return gamemap
  66.  
  67. def display(self):
  68. """**** Display the map itself. ****"""
  69. print('\n' * 18)
  70. for x in self.gamemap:
  71. print(' '.join(x))
  72. time.sleep(1)
  73.  
  74. def place(self, entity, y, x):
  75. """**** Place the symbol on frontend map. ****"""
  76. if -1 < x < self.size_x and -1 < y < self.size_y:
  77. self.gamemap[y][x] = entity
  78. else:
  79. print('Coordinates out of index.')
  80.  
  81. # **** Place the unit on techmap. ****
  82. def place_techmap(self, entity, y, x):
  83. if -1 < x < self.size_x and -1 < y < self.size_y:
  84. self.techmap[y][x] = entity
  85. else:
  86. print('Coordinates out of index.')
  87.  
  88. # **** Make the tile empty in both "dimensions" - techmap and frontmap.
  89. def make_empty(self, y, x):
  90. self.gamemap[y][x] = '.'
  91. self.techmap[y][x] = '.'
  92.  
  93. def place_both(self, y, x, tech_place, game_place):
  94. self.place(game_place, y, x)
  95. self.place_techmap(tech_place, y, x)
  96.  
  97. # **** Check for presence of entity on tile in both gamemap and techmap.
  98. def check_entity(self, y, x):
  99. if self.techmap[y][x] == '.' and self.gamemap[y][x] == '.':
  100. return False
  101. else:
  102. return True
  103.  
  104.  
  105. class Unit:
  106. def __init__(self, name, hp, damage, team, y, x, map_engine):
  107. self.name = name
  108. print(self.name)
  109. self.hp = hp
  110. self.y = y
  111. self.x = x
  112. self.damage = damage
  113. self.map_eng = map_engine
  114. self.team = team
  115. self.symbol = {'blue' : Fore.LIGHTCYAN_EX + "{}".format(name[0].upper()) + Fore.RESET,
  116. 'red' : Fore.LIGHTRED_EX + '{}'.format(name[0].upper()) + Fore.RESET}.get(self.team.lower())
  117. map_engine.alive_list.append(self)
  118. map_engine.place_both(y, x, self, self.symbol)
  119.  
  120. # **** Moves the unit on one coordinate. Also checks if there is any unit attackable. ****
  121. def check_inbounds(self, y, x):
  122. if y > self.map_eng.size_y - 1 or y < 0 or x > self.map_eng.size_x - 1 or x < 0:
  123. return False
  124. else:
  125. return True
  126.  
  127. def move(self, side):
  128. if self.check_attackable():
  129. return
  130. sides = {'right': [0, 1],
  131. 'left': [0, -1],
  132. 'up': [-1, 0],
  133. 'down': [1, 0]}
  134. move_side = sides.get(side)
  135. if self.check_inbounds(self.y + move_side[0], self.x + move_side[1]) \
  136. and not self.map_eng.check_entity(self.y + move_side[0], self.x + move_side[1]):
  137.  
  138. self.map_eng.make_empty(self.y, self.x)
  139. self.y += move_side[0]
  140. self.x += move_side[1]
  141. self.map_eng.place_both(self.y, self.x, tech_place=self, game_place=self.symbol)
  142. self.map_eng.display()
  143.  
  144. # **** Attack an entity. Entity must be Unit class, don't forget it! ****
  145. def attack(self, entity):
  146. entity.hp -= self.damage
  147. self.map_eng.display()
  148. print('{} attacks {}! Damage: {}, HP of {} left: {}!'.format(
  149. self.name, entity.name, self.damage, entity.name, entity.hp))
  150. time.sleep(1)
  151. if entity.hp <= 0:
  152. entity.die()
  153.  
  154. # **** Deletes unit. On both map and whole program. ****
  155. def die(self):
  156. print('Oh no! {} dies!'.format(self.name))
  157. self.map_eng.make_empty(self.y, self.x)
  158. self.map_eng.alive_list.remove(self)
  159.  
  160. # **** Check X and Y coordinates for presence of unit with another team. ****
  161. def check_enemies(self):
  162. for y in self.map_eng.techmap:
  163. for x in y:
  164. if hasattr(x, 'team') and x.team != self.team:
  165. coords = [x.y, x.x]
  166. self.move_on_enemy(coords[0], coords[1])
  167. return
  168. print('\n\n' + self.team + ' team wins!')
  169. self.map_eng.alive_list.clear()
  170. input('Press any key to quit...')
  171.  
  172. def check_attackable(self):
  173. coord_list = [[self.y, self.x+1 if self.check_inbounds(self.y, self.x+1) else self.x],
  174. [self.y, self.x-1 if self.check_inbounds(self.y, self.x-1) else self.x],
  175. [self.y+1 if self.check_inbounds(self.y+1, self.x) else self.y, self.x],
  176. [self.y-1 if self.check_inbounds(self.y-1, self.x) else self.y, self.x]]
  177. for coord in coord_list:
  178. if hasattr(self.map_eng.techmap[coord[0]][coord[1]], 'team') \
  179. and self.map_eng.techmap[coord[0]][coord[1]].team != self.team:
  180.  
  181. self.attack(self.map_eng.techmap[coord[0]][coord[1]])
  182. return True
  183.  
  184. return False
  185.  
  186. # **** Move to enemy unit. ****
  187. def move_on_enemy(self, enemy_y, enemy_x):
  188. if enemy_y < self.y:
  189. self.move('up')
  190. if enemy_y > self.y:
  191. self.move('down')
  192. if enemy_x < self.x:
  193. self.move('left')
  194. if enemy_x > self.x:
  195. self.move('right')
  196.  
  197. gmap = GameMap(10, 10)
  198. unit2 = Unit(name='Blue', hp=1, y=9, x=9, damage=1, map_engine=gmap, team='Blue')
  199. unit = Unit(name='Red', hp=1, y=0, x=0, damage=1, map_engine=gmap,
  200. team='Red')
  201. menu = Menu(gmap)
  202. while gmap.alive_list:
  203. for unit in gmap.alive_list:
  204. unit.check_enemies()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement