Advertisement
Guest User

Untitled

a guest
Dec 8th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.57 KB | None | 0 0
  1. import random
  2. import copy
  3. import time
  4.  
  5.  
  6. class GameMap:
  7.  
  8. def __init__(self, size_x, size_y):
  9. self.size_x = size_x
  10. self.size_y = size_y
  11. gamemap = []
  12. for x in range(self.size_y):
  13. gamemap.append(['.' for _ in range(size_x)])
  14. self.gamemap = gamemap
  15. self.techmap = copy.deepcopy(gamemap)
  16.  
  17. # **** Just displays the map. ****
  18. def display(self):
  19. print('\n')
  20. for x in self.gamemap:
  21. print(' '.join(x))
  22.  
  23. # **** Place the symbol on frontend map. ****
  24. def place(self, entity, y, x):
  25. if x < self.size_x and y < self.size_y:
  26. self.gamemap[y][x] = entity
  27. else:
  28. print('Coordinates out of index.')
  29.  
  30. # **** Place the unit on techmap. ****
  31. def place_techmap(self, entity, y, x):
  32. if x < self.size_x and y < self.size_y:
  33. self.techmap[y][x] = entity
  34. else:
  35. print('Coordinates out of index.')
  36.  
  37. # **** Make the tile empty in both "dimensions" - techmap and frontmap.
  38. def make_empty(self, y, x):
  39. self.gamemap[y][x] = '.'
  40. self.techmap[y][x] = '.'
  41.  
  42. # **** Check for presence of entity on tile in both gamemap and techmap.
  43. def check_entity(self, y, x):
  44. if self.techmap[y][x] == '.' and self.gamemap[y][x] == '.':
  45. return False
  46. else:
  47. return True
  48.  
  49.  
  50. class Unit:
  51. def __init__(self, name, hp, damage, team, y, x, map_engine):
  52. self.name = name
  53. self.hp = hp
  54. self.y = y
  55. self.x = x
  56. self.damage = damage
  57. self.map_eng = map_engine
  58. self.team = team
  59. map_engine.place_techmap(self, y, x)
  60. map_engine.place(name[0].upper(), y, x)
  61.  
  62. # **** Moves the unit on one coordinate. Also checks if there is any unit attackable. ****
  63. def move_right(self):
  64. if self.x < self.map_eng.size_x - 1:
  65. if self.map_eng.check_entity(self.y, self.x+1):
  66. self.attack(self.map_eng.techmap[self.y][self.x+1])
  67. self.map_eng.display()
  68. time.sleep(1)
  69. return
  70. self.map_eng.make_empty(self.y, self.x)
  71. self.x += 1
  72. self.map_eng.place(self.name[0].upper(), self.y, self.x)
  73. self.map_eng.place_techmap(self, self.y, self.x)
  74. self.map_eng.display()
  75. def move_left(self):
  76. if self.x > 0:
  77. if self.map_eng.check_entity(self.y, self.x-1):
  78. self.attack(self.map_eng.techmap[self.y][self.x-1])
  79. self.map_eng.display()
  80. time.sleep(1)
  81. return
  82. self.map_eng.make_empty(self.y, self.x)
  83. self.x -= 1
  84. self.map_eng.place(self.name[0].upper(), self.y, self.x)
  85. self.map_eng.place_techmap(self, self.y, self.x)
  86. self.map_eng.display()
  87. def move_up(self):
  88. if self.y > 0:
  89. if self.map_eng.check_entity(self.y - 1, self.x):
  90. self.attack(self.map_eng.techmap[self.y-1][self.x])
  91. self.map_eng.display()
  92. time.sleep(1)
  93. return
  94. self.map_eng.make_empty(self.y, self.x)
  95. self.y -= 1
  96. self.map_eng.place(self.name[0].upper(), self.y, self.x)
  97. self.map_eng.place_techmap(self, self.y, self.x)
  98. self.map_eng.display()
  99. def move_down(self):
  100. if self.y < self.map_eng.size_y - 1:
  101. if self.map_eng.check_entity(self.y + 1, self.x):
  102. self.attack(self.map_eng.techmap[self.y+1][self.x])
  103. self.map_eng.display()
  104. time.sleep(1)
  105. return
  106. self.map_eng.make_empty(self.y, self.x)
  107. self.y += 1
  108. self.map_eng.place(self.name[0].upper(), self.y, self.x)
  109. self.map_eng.place_techmap(self, self.y, self.x)
  110. self.map_eng.display()
  111.  
  112. # **** Attack an entity. Entity must be Unit class, don't forget it! ****
  113. def attack(self, entity):
  114. print('{} attacks {} and takes {} HP!'.format(self.name, entity.name, self.damage))
  115. entity.hp -= self.damage
  116. entity.check_crit()
  117.  
  118. # **** Deletes unit. On both map and whole program. ****
  119. def die(self):
  120. print('Oh no! {} dies!'.format(self.name))
  121. self.map_eng.make_empty(self.y, self.x)
  122. del(self)
  123.  
  124. # **** Check X and Y coordinates for presence of unit with another team. ****
  125. def check_enemies(self):
  126. coords = None
  127. for x_check in self.map_eng.techmap[self.y]:
  128. if hasattr(x_check, 'team') and x_check.team != self.team:
  129. coords = [x_check.y, x_check.x]
  130. self.move_on_enemy(coords[0], coords[1])
  131. return
  132. for y_check in self.map_eng.techmap:
  133. if hasattr(y_check[self.x], 'team') and y_check[self.x].team != self.team:
  134. coords = [y_check[self.x].y, y_check[self.x].x]
  135. self.move_on_enemy(coords[0], coords[1])
  136. return
  137. if self.team == 'Blue':
  138. self.move_left()
  139. else:
  140. self.move_right()
  141.  
  142. # **** Check if unit suffered critical damage. ****
  143. def check_crit(self):
  144. if self.hp <= 0:
  145. self.die()
  146.  
  147. # **** Move to enemy unit. ****
  148. def move_on_enemy(self, enemy_y, enemy_x):
  149. if enemy_y < self.y:
  150. self.move_up()
  151. if enemy_y > self.y:
  152. self.move_down()
  153. if enemy_x < self.x:
  154. self.move_left()
  155. if enemy_x > self.x:
  156. self.move_right()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement