Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- import time
- from colorama import init, Fore, Back, Style
- init(convert=True)
- print(Fore.RED + 'some red text' + Fore.RESET)
- class Menu:
- def __init__(self, engine):
- self.engine = engine
- self.menu()
- def sandbox_mode(self):
- user_place = None
- while True:
- 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: ')
- if user_place.lower() == 'start':
- break
- coords = user_place.split(',')
- print(user_place)
- unit = Unit(name='Red', hp=1, y=int(coords[1]), x=int(coords[0]), damage=1, map_engine=gmap,
- team='Red')
- self.engine.display()
- print('Done!')
- def menu(self):
- print(Fore.LIGHTYELLOW_EX + '''Hello there, friend! Welcome to TABSlike 0.1! I hope you'll like this game :)\n
- Right now there's two modes - sandbox and random encounter. Which one would you like to see?\n
- 1) Sandbox
- 2) Random encounter''')
- choice = input()
- if choice == '1':
- pass
- if choice == '2':
- self.sandbox_mode()
- else:
- print('Not an option.')
- print(choice)
- self.menu()
- class GameMap:
- def __init__(self, size_x, size_y):
- self.size_x = size_x
- self.size_y = size_y
- self.gamemap = self.generate_map()
- self.techmap = self.generate_map()
- self.alive_list = []
- def something(self, units_number):
- units_num = 0
- while units_num < units_number:
- b_coords = [random.randint(round(self.size_y / 2), self.size_y - 1),
- random.randint(round(self.size_x / 2), self.size_x - 1)]
- if not self.check_entity(b_coords[0], b_coords[1]):
- unit_blue = Unit(name='Blue' + str(units_num), hp=1, y=b_coords[0], x=b_coords[1],
- damage=1, map_engine=gmap, team='Blue')
- units_num += 1
- else:
- continue
- def generate_map(self):
- gamemap = []
- for each_y in range(self.size_y):
- gamemap.append(['.' for _ in range(self.size_x)])
- return gamemap
- def display(self):
- """**** Display the map itself. ****"""
- print('\n' * 18)
- for x in self.gamemap:
- print(' '.join(x))
- time.sleep(1)
- def place(self, entity, y, x):
- """**** Place the symbol on frontend map. ****"""
- if -1 < x < self.size_x and -1 < 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 -1 < x < self.size_x and -1 < 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] = '.'
- def place_both(self, y, x, tech_place, game_place):
- self.place(game_place, y, x)
- self.place_techmap(tech_place, 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
- print(self.name)
- self.hp = hp
- self.y = y
- self.x = x
- self.damage = damage
- self.map_eng = map_engine
- self.team = team
- self.symbol = {'blue' : Fore.LIGHTCYAN_EX + "{}".format(name[0].upper()) + Fore.RESET,
- 'red' : Fore.LIGHTRED_EX + '{}'.format(name[0].upper()) + Fore.RESET}.get(self.team.lower())
- map_engine.alive_list.append(self)
- map_engine.place_both(y, x, self, self.symbol)
- # **** Moves the unit on one coordinate. Also checks if there is any unit attackable. ****
- def check_inbounds(self, y, x):
- if y > self.map_eng.size_y - 1 or y < 0 or x > self.map_eng.size_x - 1 or x < 0:
- return False
- else:
- return True
- def move(self, side):
- if self.check_attackable():
- return
- sides = {'right': [0, 1],
- 'left': [0, -1],
- 'up': [-1, 0],
- 'down': [1, 0]}
- move_side = sides.get(side)
- if self.check_inbounds(self.y + move_side[0], self.x + move_side[1]) \
- and not self.map_eng.check_entity(self.y + move_side[0], self.x + move_side[1]):
- self.map_eng.make_empty(self.y, self.x)
- self.y += move_side[0]
- self.x += move_side[1]
- self.map_eng.place_both(self.y, self.x, tech_place=self, game_place=self.symbol)
- self.map_eng.display()
- # **** Attack an entity. Entity must be Unit class, don't forget it! ****
- def attack(self, entity):
- entity.hp -= self.damage
- self.map_eng.display()
- print('{} attacks {}! Damage: {}, HP of {} left: {}!'.format(
- self.name, entity.name, self.damage, entity.name, entity.hp))
- time.sleep(1)
- if entity.hp <= 0:
- entity.die()
- # **** 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)
- self.map_eng.alive_list.remove(self)
- # **** Check X and Y coordinates for presence of unit with another team. ****
- def check_enemies(self):
- for y in self.map_eng.techmap:
- for x in y:
- if hasattr(x, 'team') and x.team != self.team:
- coords = [x.y, x.x]
- self.move_on_enemy(coords[0], coords[1])
- return
- print('\n\n' + self.team + ' team wins!')
- self.map_eng.alive_list.clear()
- input('Press any key to quit...')
- def check_attackable(self):
- coord_list = [[self.y, self.x+1 if self.check_inbounds(self.y, self.x+1) else self.x],
- [self.y, self.x-1 if self.check_inbounds(self.y, self.x-1) else self.x],
- [self.y+1 if self.check_inbounds(self.y+1, self.x) else self.y, self.x],
- [self.y-1 if self.check_inbounds(self.y-1, self.x) else self.y, self.x]]
- for coord in coord_list:
- if hasattr(self.map_eng.techmap[coord[0]][coord[1]], 'team') \
- and self.map_eng.techmap[coord[0]][coord[1]].team != self.team:
- self.attack(self.map_eng.techmap[coord[0]][coord[1]])
- return True
- return False
- # **** 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')
- gmap = GameMap(10, 10)
- unit2 = Unit(name='Blue', hp=1, y=9, x=9, damage=1, map_engine=gmap, team='Blue')
- unit = Unit(name='Red', hp=1, y=0, x=0, damage=1, map_engine=gmap,
- team='Red')
- menu = Menu(gmap)
- while gmap.alive_list:
- for unit in gmap.alive_list:
- unit.check_enemies()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement