def get_game_variables(constant_variables): fighter_component = Fighter(hp=100, armor_class=3, strength=10, intelligence=10) inventory_component = Inventory(26) grimoire_component = Grimoire(5) equipment_component = Equipment() player = Entity(0, 0, '@', libtcod.red, 'Player', blocks=True, render_order=RenderOrder.ACTOR, fighter=fighter_component, inventory=inventory_component, grimoire=grimoire_component, equipment=equipment_component) spell = Spell(name='Fireball', cast_function=fireball, damage=10, targeting=True, targeting_message=Message('click to target'), radius=3, cost=10) player.grimoire.add_spell(spell) entities = [player] # Initializes map game_map = Map(constant_variables['map_width'], constant_variables['map_height']) game_map.make_BSP_map(player, entities, constant_variables['map_width'], constant_variables['map_height']) # Initializes other game variables game_state = GameStates.PLAYERS_TURN message_log = MessageLog(constant_variables['message_x'], constant_variables['message_width'], constant_variables['message_height']) return player, entities, game_map, game_state, message_log
def test_create_v_tunnel(): m1 = Map(10, 10) expected_tunnel_y1 = 2 expected_tunnel_y2 = 7 expected_tunnel_x = 5 m1.create_v_tunnel(expected_tunnel_y1, expected_tunnel_y2, expected_tunnel_x) for x_count, x in enumerate(m1.tiles): for y_count, y in enumerate(x): if expected_tunnel_y1 <= y_count <= expected_tunnel_y2: if x_count == expected_tunnel_x: assert y.blocked is False assert y.block_sight is False else: assert y.blocked is True assert y.block_sight is True else: assert y.blocked is True assert y.block_sight is True
def test_create_h_tunnel(): m1 = Map(10, 10) expected_tunnel_x1 = 2 expected_tunnel_x2 = 7 expected_tunnel_y = 5 m1.create_h_tunnel(expected_tunnel_x1, expected_tunnel_x2, expected_tunnel_y) for x_count, x in enumerate(m1.tiles): for y_count, y in enumerate(x): if expected_tunnel_x1 <= x_count <= expected_tunnel_x2: if y_count == expected_tunnel_y: assert y.blocked is False assert y.block_sight is False else: assert y.blocked is True assert y.block_sight is True else: assert y.blocked is True assert y.block_sight is True
def test_create_room(): m1 = Map(10, 10) room_x = 1 room_y = 1 room_w = 5 room_h = 5 test_room = Rect(room_x, room_y, room_w, room_h) m1.create_room(test_room) for x_count, x in enumerate(m1.tiles): for y_count, y in enumerate(x): if room_x < x_count < (room_x + room_w): if room_y < y_count < (room_y + room_h): assert y.blocked is False assert y.block_sight is False else: assert y.blocked is True assert y.block_sight is True else: assert y.blocked is True assert y.block_sight is True
def test_is_blocked(): m1 = Map(10, 10) assert m1.is_blocked(1, 1) is True room = Rect(0, 0, 10, 10) m1.create_room(room) assert m1.is_blocked(1, 1) is False
def test_class_initialization(): expected_width = 5 expected_height = 4 m1 = Map(expected_width, expected_height) assert m1.width == expected_width assert m1.height == expected_height assert len(m1.tiles) == expected_width for x_row in m1.tiles: assert len(x_row) == expected_height for tile in x_row: expected_blocked = True expected_block_sight = True expected_explored = False assert tile.blocked == expected_blocked assert tile.block_sight == expected_block_sight assert tile.explored == expected_explored
def main(): # initialise screen dimensions screen_width = 80 screen_height = 50 # initialise map dimensions map_width = 80 map_height = 45 # room limits and dimensions max_room_size = 10 min_room_size = 6 total_room_limit = 30 colours = { 'dark_wall': libtcod.Color(60, 49, 32), 'dark_ground': libtcod.Color(96, 128, 56), 'npc_colour': libtcod.Color(80, 80, 80) } # declare player and npcs, put them into a list player = Entity(int(screen_width / 2), int(screen_height / 2), '@', libtcod.white) npc = Entity(int(screen_width - 10 / 2), int(screen_height / 2), '$', colours.get('npc_colour')) entities = [player, npc] # initialise the map game_map = Map(map_width, map_height) game_map.create_map(total_room_limit, min_room_size, max_room_size, map_width, map_height, player) # choose font for rendering libtcod.console_set_custom_font( 'arial10x10.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD) # initialise console libtcod.console_init_root(screen_width, screen_height, 'Omars Roguelike', False) con = libtcod.console_new(screen_width, screen_height) # key and mouse presses key = libtcod.Key() mouse = libtcod.Mouse() while not libtcod.console_is_window_closed(): # checks for mouse or key interrupt libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS, key, mouse) # renders all entities render_all(con, entities, screen_width, screen_height, colours, game_map) libtcod.console_flush() clear_all(con, entities) # checks what action has to be done action = handle_keys(key) move = action.get('move') exit = action.get('exit') fullscreen = action.get('fullscreen') if move: dx, dy = move if not game_map.is_blocked(player.x + dx, player.y + dy) and map_width-1 > player.x + dx > 0 and map_height-1 > \ player.y + dy > 0: player.move(dx, dy) if exit: return True if fullscreen: libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen())