Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- import copy
- import time
- class GameMap:
- def __init__(self, size_x, size_y):
- self.size_x = size_x
- self.size_y = size_y
- gamemap = []
- for x in range(self.size_y):
- gamemap.append(['.' for _ in range(size_x)])
- self.gamemap = gamemap
- self.techmap = copy.deepcopy(gamemap)
- # **** Just displays the map. ****
- def display(self):
- print('\n')
- for x in self.gamemap:
- print(' '.join(x))
- # **** Place the symbol on frontend map. ****
- def place(self, entity, y, x):
- if x < self.size_x and y < self.size_y:
- self.gamemap[y][x] = entity
- else:
- print('Coordinates out of index.')
- # **** Place the unit on techmap. ****
- def place_techmap(self, entity, y, x):
- if x < self.size_x and y < self.size_y:
- self.techmap[y][x] = entity
- else:
- print('Coordinates out of index.')
- # **** Make the tile empty in both "dimensions" - techmap and frontmap.
- def make_empty(self, y, x):
- self.gamemap[y][x] = '.'
- self.techmap[y][x] = '.'
- # **** Check for presence of entity on tile in both gamemap and techmap.
- def check_entity(self, y, x):
- if self.techmap[y][x] == '.' and self.gamemap[y][x] == '.':
- return False
- else:
- return True
- class Unit:
- def __init__(self, name, hp, damage, team, y, x, map_engine):
- self.name = name
- self.hp = hp
- self.y = y
- self.x = x
- self.damage = damage
- self.map_eng = map_engine
- self.team = team
- map_engine.place_techmap(self, y, x)
- map_engine.place(name[0].upper(), y, x)
- # **** Moves the unit on one coordinate. Also checks if there is any unit attackable. ****
- def move_right(self):
- if self.x < self.map_eng.size_x - 1:
- if self.map_eng.check_entity(self.y, self.x+1):
- self.attack(self.map_eng.techmap[self.y][self.x+1])
- self.map_eng.display()
- time.sleep(1)
- return
- self.map_eng.make_empty(self.y, self.x)
- self.x += 1
- self.map_eng.place(self.name[0].upper(), self.y, self.x)
- self.map_eng.place_techmap(self, self.y, self.x)
- self.map_eng.display()
- def move_left(self):
- if self.x > 0:
- if self.map_eng.check_entity(self.y, self.x-1):
- self.attack(self.map_eng.techmap[self.y][self.x-1])
- self.map_eng.display()
- time.sleep(1)
- return
- self.map_eng.make_empty(self.y, self.x)
- self.x -= 1
- self.map_eng.place(self.name[0].upper(), self.y, self.x)
- self.map_eng.place_techmap(self, self.y, self.x)
- self.map_eng.display()
- def move_up(self):
- if self.y > 0:
- if self.map_eng.check_entity(self.y - 1, self.x):
- self.attack(self.map_eng.techmap[self.y-1][self.x])
- self.map_eng.display()
- time.sleep(1)
- return
- self.map_eng.make_empty(self.y, self.x)
- self.y -= 1
- self.map_eng.place(self.name[0].upper(), self.y, self.x)
- self.map_eng.place_techmap(self, self.y, self.x)
- self.map_eng.display()
- def move_down(self):
- if self.y < self.map_eng.size_y - 1:
- if self.map_eng.check_entity(self.y + 1, self.x):
- self.attack(self.map_eng.techmap[self.y+1][self.x])
- self.map_eng.display()
- time.sleep(1)
- return
- self.map_eng.make_empty(self.y, self.x)
- self.y += 1
- self.map_eng.place(self.name[0].upper(), self.y, self.x)
- self.map_eng.place_techmap(self, self.y, self.x)
- self.map_eng.display()
- # **** Attack an entity. Entity must be Unit class, don't forget it! ****
- def attack(self, entity):
- print('{} attacks {} and takes {} HP!'.format(self.name, entity.name, self.damage))
- entity.hp -= self.damage
- entity.check_crit()
- # **** Deletes unit. On both map and whole program. ****
- def die(self):
- print('Oh no! {} dies!'.format(self.name))
- self.map_eng.make_empty(self.y, self.x)
- del(self)
- # **** Check X and Y coordinates for presence of unit with another team. ****
- def check_enemies(self):
- coords = None
- for x_check in self.map_eng.techmap[self.y]:
- if hasattr(x_check, 'team') and x_check.team != self.team:
- coords = [x_check.y, x_check.x]
- self.move_on_enemy(coords[0], coords[1])
- return
- for y_check in self.map_eng.techmap:
- if hasattr(y_check[self.x], 'team') and y_check[self.x].team != self.team:
- coords = [y_check[self.x].y, y_check[self.x].x]
- self.move_on_enemy(coords[0], coords[1])
- return
- if self.team == 'Blue':
- self.move_left()
- else:
- self.move_right()
- # **** Check if unit suffered critical damage. ****
- def check_crit(self):
- if self.hp <= 0:
- self.die()
- # **** Move to enemy unit. ****
- def move_on_enemy(self, enemy_y, enemy_x):
- if enemy_y < self.y:
- self.move_up()
- if enemy_y > self.y:
- self.move_down()
- if enemy_x < self.x:
- self.move_left()
- if enemy_x > self.x:
- self.move_right()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement