def handle_keys(): #key = libtcod.console_check_for_keypress() #real-time key = libtcod.console_wait_for_keypress(True) #turn-based if key.vk == libtcod.KEY_ENTER and key.lalt: #Alt+Enter: toggle fullscreen libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen()) elif key.vk == libtcod.KEY_ESCAPE: return 'exit' #exit game if game_state == 'playing': #movement keys if libtcod.console_is_key_pressed(libtcod.KEY_UP): player_move_or_attack(0, -1) elif libtcod.console_is_key_pressed(libtcod.KEY_DOWN): player_move_or_attack(0, 1) elif libtcod.console_is_key_pressed(libtcod.KEY_LEFT): player_move_or_attack(-1, 0) elif libtcod.console_is_key_pressed(libtcod.KEY_RIGHT): player_move_or_attack(1, 0) else: return 'didnt-take-turn'
def handle_keys(): global playerx, playery #key = libtcod.console_check_for_keypress() #real-time key = libtcod.console_wait_for_keypress(True) #turn-based if key.vk == libtcod.KEY_ENTER and key.lalt: #Alt+Enter: toggle fullscreen libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen()) elif key.vk == libtcod.KEY_ESCAPE: return True #exit game #movement keys if libtcod.console_is_key_pressed(libtcod.KEY_UP): playery -= 1 elif libtcod.console_is_key_pressed(libtcod.KEY_DOWN): playery += 1 elif libtcod.console_is_key_pressed(libtcod.KEY_LEFT): playerx -= 1 elif libtcod.console_is_key_pressed(libtcod.KEY_RIGHT): playerx += 1
def handle_keys(): global key; if key.vk == libtcod.KEY_ENTER and key.lalt: #Alt+Enter: toggle fullscreen libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen()) elif key.vk == libtcod.KEY_ESCAPE: return 'exit' #exit game if game_state == 'playing': #movement keys if key.vk == libtcod.KEY_UP: player_move_or_attack(0, -1) elif key.vk == libtcod.KEY_DOWN: player_move_or_attack(0, 1) elif key.vk == libtcod.KEY_LEFT: player_move_or_attack(-1, 0) elif key.vk == libtcod.KEY_RIGHT: player_move_or_attack(1, 0) else: return 'didnt-take-turn'
def handle_keys(): global fov_recompute #key = libtcod.console_check_for_keypress() #real-time key = libtcod.console_wait_for_keypress(True) #turn-based if key.vk == libtcod.KEY_ENTER and key.lalt: #Alt+Enter: toggle fullscreen libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen()) elif key.vk == libtcod.KEY_ESCAPE: return True #exit game #movement keys if libtcod.console_is_key_pressed(libtcod.KEY_UP): player.move(0, -1) fov_recompute = True elif libtcod.console_is_key_pressed(libtcod.KEY_DOWN): player.move(0, 1) fov_recompute = True elif libtcod.console_is_key_pressed(libtcod.KEY_LEFT): player.move(-1, 0) fov_recompute = True elif libtcod.console_is_key_pressed(libtcod.KEY_RIGHT): player.move(1, 0) fov_recompute = True
def handle_keys(): global key; if key.vk == libtcod.KEY_ENTER and key.lalt: #Alt+Enter: toggle fullscreen libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen()) elif key.vk == libtcod.KEY_ESCAPE: return 'exit' #exit game if game_state == 'playing': #movement keys if key.vk == libtcod.KEY_UP: player_move_or_attack(0, -1) elif key.vk == libtcod.KEY_DOWN: player_move_or_attack(0, 1) elif key.vk == libtcod.KEY_LEFT: player_move_or_attack(-1, 0) elif key.vk == libtcod.KEY_RIGHT: player_move_or_attack(1, 0) else: #test for other keys key_char = chr(key.c) if key_char == 'g': #pick up an item for object in objects: #look for an item in the player's tile if object.x == player.x and object.y == player.y and object.item: object.item.pick_up() break if key_char == 'i': #show the inventory; if an item is selected, use it chosen_item = inventory_menu('Press the key next to an item to use it, or any other to cancel.\n') if chosen_item is not None: chosen_item.use() if key_char == 'd': #show the inventory; if an item is selected, drop it chosen_item = inventory_menu('Press the key next to an item to drop it, or any other to cancel.\n') if chosen_item is not None: chosen_item.drop() return 'didnt-take-turn'
def menu(header, options, width): if len(options) > 26: raise ValueError('Cannot have a menu with more than 26 options.') #calculate total height for the header (after auto-wrap) and one line per option header_height = libtcod.console_get_height_rect(con, 0, 0, width, SCREEN_HEIGHT, header) if header == '': header_height = 0 height = len(options) + header_height #create an off-screen console that represents the menu's window window = libtcod.console_new(width, height) #print the header, with auto-wrap libtcod.console_set_default_foreground(window, libtcod.white) libtcod.console_print_rect_ex(window, 0, 0, width, height, libtcod.BKGND_NONE, libtcod.LEFT, header) #print all the options y = header_height letter_index = ord('a') for option_text in options: text = '(' + chr(letter_index) + ') ' + option_text libtcod.console_print_ex(window, 0, y, libtcod.BKGND_NONE, libtcod.LEFT, text) y += 1 letter_index += 1 #blit the contents of "window" to the root console x = SCREEN_WIDTH//2 - width//2 y = SCREEN_HEIGHT//2 - height//2 libtcod.console_blit(window, 0, 0, width, height, 0, x, y, 1.0, 0.7) #present the root console to the player and wait for a key-press libtcod.console_flush() key = libtcod.console_wait_for_keypress(True) if key.vk == libtcod.KEY_ENTER and key.lalt: #(special case) Alt+Enter: toggle fullscreen libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen()) #convert the ASCII code to an index; if it corresponds to an option, return it index = key.c - ord('a') if index >= 0 and index < len(options): return index return None
def isfullscreen(): return tcod.console_is_fullscreen()
def main(): screen_width = 80 screen_height = 50 map_width = screen_width map_height = screen_height - 5 room_max_size = 10 room_min_size = 6 max_rooms = 30 fov_algorithm = 0 fov_light_walls = True fov_radius = 10 max_monsters_per_room = 3 colors = { 'dark_wall': libtcod.Color(0, 0, 100), 'dark_ground': libtcod.Color(50, 50, 150), 'light_wall': libtcod.Color(130, 110, 50), 'light_ground': libtcod.Color(200, 180, 500), } player = Entity(screen_width // 2, screen_height // 2, '@', libtcod.white, 'Player', blocks=True) entities = [player] libtcod.console_set_custom_font( 'dejavu10x10_gs_tc.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD) libtcod.console_init_root(screen_width, screen_height, 'libtcod tutorial revised', False) con = libtcod.console_new(screen_width, screen_height) game_map = GameMap(map_width, map_height) game_map.make_map(max_rooms, room_min_size, room_max_size, map_width, map_height, player, entities, max_monsters_per_room) fov_recompute = True fov_map = initialize_fov(game_map) key = libtcod.Key() mouse = libtcod.Mouse() while not libtcod.console_is_window_closed(): libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS, key, mouse) if fov_recompute: recompute_fov(fov_map, player.x, player.y, fov_radius, fov_light_walls, fov_algorithm) render_all(con, entities, game_map, fov_map, fov_recompute, screen_width, screen_height, colors) fov_recompute = False libtcod.console_flush() clear_all(con, entities) action = handle_keys(key) move = action.get('move') exit = action.get('exit') fullscreen = action.get('fullscreen') if move: dx, dy = move destination_x = player.x + dx destination_y = player.y + dy if not game_map.is_blocked(destination_x, destination_y): target = get_blocking_entities_at_location( entities, destination_x, destination_y) if target: print( 'You kick the {} in the shins, much to its cansternation!' .format(target.name)) else: player.move(dx, dy) fov_recompute = True if exit: return True if fullscreen: libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen())
def play_game(player, entities, game_map, message_log, game_state, con, panel, constants, camera): fov_recompute = True fov_map = initalize_fov(game_map) camera.update(player) key = libtcod.Key() mouse = libtcod.Mouse() previous_game_state = game_state targeting_item = None while not libtcod.console_is_window_closed(): libtcod.sys_check_for_event( libtcod.EVENT_KEY_PRESS | libtcod.EVENT_MOUSE, key, mouse) if fov_recompute: recompute_fov(fov_map, player.x, player.y, constants['fov_radius'], constants['fov_light_walls'], constants['fov_algorithm']) render_all(con, panel, entities, player, game_map, fov_map, fov_recompute, message_log, constants['screen_width'], constants['screen_height'], constants['bar_width'], constants['panel_height'], constants['panel_y'], mouse, constants['colors'], game_state, camera) fov_recompute = False libtcod.console_flush() clear_all(con, entities, camera) action = handle_keys(key, game_state) mouse_action = handle_mouse(mouse) move = action.get('move') wait = action.get('wait') pickup = action.get('pickup') show_inventory = action.get('show_inventory') drop_inventory = action.get('drop_inventory') inventory_index = action.get('inventory_index') take_stairs = action.get('take_stairs') level_up = action.get('level_up') show_character_screen = action.get('show_character_screen') exit = action.get('exit') fullscreen = action.get('fullscreen') fullscreen_console = action.get('fullscreen_console') left_click = mouse_action.get('left_click') right_click = mouse_action.get('right_click') wheel_up = mouse_action.get('wheel_up') wheel_down = mouse_action.get('wheel_down') player_turn_results = [] if wheel_up: message_log.scroll_up() if wheel_down: message_log.scroll_down() if move and game_state == GameStates.PLAYERS_TURN: dx, dy = move destination_x = player.x + dx destination_y = player.y + dy if not game_map.is_blocked(destination_x, destination_y): target = get_blocking_entities_at_location( entities, destination_x, destination_y) if target: attack_results = player.fighter.attack(target) player_turn_results.extend(attack_results) else: player.move(dx, dy) camera.update(player) fov_recompute = True game_state = GameStates.ENEMY_TURN elif wait: game_state = GameStates.ENEMY_TURN elif pickup and game_state == GameStates.PLAYERS_TURN: for entity in entities: if entity.usable and entity.x == player.x and entity.y == player.y: pickup_results = player.inventory.add_item(entity) player_turn_results.extend(pickup_results) break else: message_log.add_message( Message( 'You fumble around on the ground but find nothing.', libtcod.yellow)) if show_inventory: previous_game_state = game_state game_state = GameStates.SHOW_INVENTORY if drop_inventory: previous_game_state = game_state game_state = GameStates.DROP_INVENTORY if inventory_index is not None and previous_game_state != GameStates.PLAYER_DEAD and inventory_index < len( player.inventory.items): item = player.inventory.items[inventory_index] if game_state == GameStates.SHOW_INVENTORY: player_turn_results.extend( player.inventory.use(item, entities=entities, fov_map=fov_map)) elif game_state == GameStates.DROP_INVENTORY: player_turn_results.extend(player.inventory.drop_item(item)) if take_stairs and game_state == GameStates.PLAYERS_TURN: for entity in entities: if entity.stairs and entity.x == player.x and entity.y == player.y: entities = game_map.next_floor(player, message_log, constants) fov_map = initalize_fov(game_map) camera.update(player) fov_recompute = True libtcod.console_clear(con) break else: message_log.add_message( Message('There are no stairs here.', libtcod.yellow)) if level_up: if level_up == 'hp': player.fighter.base_max_hp += 20 # TODO: Consider putting these in constants player.fighter.hp += 20 elif level_up == 'str': player.fighter.base_power += 1 elif level_up == 'def': player.fighter.base_defense += 1 elif level_up == 'speed': player.fighter.base_speed -= 10 # game_state = previous_game_state # This is the only way I found that the level up book doesn't lock game_state = GameStates.PLAYERS_TURN # the game, hopefully it's fine but it may give an extra turn on lvl up if show_character_screen: previous_game_state = game_state game_state = GameStates.CHARACTER_SCREEN if fullscreen_console: previous_game_state = game_state game_state = GameStates.FULLSCREEN_CONSOLE if game_state == GameStates.TARGETING: if left_click: target_x, target_y = left_click item_use_results = player.inventory.use(targeting_item, entities=entities, fov_map=fov_map, target_x=target_x, target_y=target_y) player_turn_results.extend(item_use_results) elif right_click: player_turn_results.append({'targeting_cancelled': True}) if exit: if game_map.dungeon_level == -1: # Testing arena case needs no save file return True elif game_state in (GameStates.SHOW_INVENTORY, GameStates.DROP_INVENTORY, GameStates.CHARACTER_SCREEN, GameStates.FULLSCREEN_CONSOLE): game_state = previous_game_state elif game_state == GameStates.TARGETING: player_turn_results.append({'targeting_cancelled': True}) else: save_game(player, entities, game_map, message_log, game_state) return True if fullscreen: libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen()) for player_turn_result in player_turn_results: message = player_turn_result.get('message') dead_entity = player_turn_result.get('dead') item_added = player_turn_result.get('item_added') item_consumed = player_turn_result.get('consumed') item_dropped = player_turn_result.get('item_dropped') equip = player_turn_result.get('equip') targeting = player_turn_result.get('targeting') targeting_cancelled = player_turn_result.get('targeting_cancelled') xp = player_turn_result.get('xp') if message: message_log.add_message(message) if targeting_cancelled: game_state = previous_game_state message_log.add_message(Message('Targeting cancelled')) if xp: leveled_up = player.level.add_xp(xp) message_log.add_message(Message('You gain {0} xp.'.format(xp))) if leveled_up: message_log.add_message( Message( 'You have honed your skills to reach level {0}.'. format(player.level.current_level), libtcod.cyan)) previous_game_state = game_state game_state = GameStates.LEVEL_UP if dead_entity: if dead_entity == player: message, game_state = kill_player(dead_entity) else: message = kill_monster(dead_entity) message_log.add_message(message) if item_added: entities.remove(item_added) game_state = GameStates.ENEMY_TURN if item_consumed: game_state = GameStates.ENEMY_TURN if targeting: previous_game_state = GameStates.PLAYERS_TURN game_state = GameStates.TARGETING targeting_item = targeting message_log.add_message( targeting_item.usable.targeting_message) if equip: equip_results = player.equipment.toggle_equip(equip) for equip_result in equip_results: equipped = equip_result.get('equipped') unequipped = equip_result.get('unequipped') if equipped: message_log.add_message( Message('You equipped the {0}.'.format( equipped.name))) if unequipped: message_log.add_message( Message('You unequipped the {0}.'.format( unequipped.name))) game_state = GameStates.ENEMY_TURN if item_dropped: entities.append(item_dropped) game_state = GameStates.ENEMY_TURN if game_state == GameStates.ENEMY_TURN: for entity in entities: if entity.ai: current_moves = entity.fighter.current_moves current_moves -= player.fighter.base_speed while (current_moves <= 0): enemy_turn_results = entity.ai.take_turn( player, fov_map, game_map, entities) for enemy_turn_result in enemy_turn_results: message = enemy_turn_result.get('message') dead_entity = enemy_turn_result.get('dead') if message: message_log.add_message(message) if dead_entity: if dead_entity == player: message, game_state = kill_player( dead_entity) else: message = kill_monster(dead_entity) message_log.add_message(message) if game_state == GameStates.PLAYER_DEAD: break # Break game loop if game_state == GameStates.PLAYER_DEAD: break # Break game loop current_moves += entity.fighter.speed entity.fighter.current_moves = current_moves else: game_state = GameStates.PLAYERS_TURN
def main(): # screen size in pixels screen_width = 80 screen_height = 50 map_width = 80 map_height = 45 room_max_size = 10 room_min_size = 6 max_rooms = 30 fov_algorithm = 0 fov_light_walls = True fov_radius = 10 max_monsters_per_room = 3 colors = { 'dark_wall': libtcod.Color(0, 0, 100), 'dark_ground': libtcod.Color(50, 50, 150), 'light_wall': libtcod.Color(130, 110, 50), 'light_ground': libtcod.Color(200, 180, 50) } player = Entity(0, 0, "@", libtcod.white) entities = [player] # selecting a custom font through libtcod # using a picture of all the characters for the font libtcod.console_set_custom_font( "arial10x10.png", libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD) # renders the actual window # boolean attribute is True if fullscreen libtcod.console_init_root( screen_width, screen_height, "roguelike tutorial", False) con = libtcod.console_new(screen_width, screen_height) game_map = GameMap(map_width, map_height) game_map.make_map(max_rooms, room_min_size, room_max_size, map_width, map_height, player, entities, max_monsters_per_room) fov_recompute = True fov_map = initialize_fov(game_map) key = libtcod.Key() mouse = libtcod.Mouse() # game loop while not libtcod.console_is_window_closed(): # event listener for mouse and keypad input libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS, key, mouse) if fov_recompute: recompute_fov(fov_map, player.x, player.y, fov_radius, fov_light_walls, fov_algorithm) render_all(con, entities, game_map, fov_map, fov_recompute, screen_width, screen_height, colors) fov_recompute = False # this puts everything on the screen libtcod.console_flush() clear_all(con, entities) 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): player.move(dx, dy) fov_recompute = True # if the key is ESC... if exit: # exit window return True if fullscreen: libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen())
def handle_keys(): """Handle keyboard movement.""" global key if key.vk == libtcod.KEY_ENTER and key.lalt: #Alt+Enter: toggle fullscreen libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen()) elif key.vk == libtcod.KEY_ESCAPE: return 'exit' #exit game if var.game_state == 'playing': #movement keys if key.vk == libtcod.KEY_UP: player_move_or_attack(0, -1) elif key.vk == libtcod.KEY_DOWN: player_move_or_attack(0, 1) elif key.vk == libtcod.KEY_LEFT: player_move_or_attack(-1, 0) elif key.vk == libtcod.KEY_RIGHT: player_move_or_attack(1, 0) else: #test for other keys key_char = chr(key.c) if key_char == 'g': #pick up an item for g_object in var.game_objects: #look for an item in the player's tile if g_object.x == var.player.x and g_object.y == var.player.y and g_object.item: g_object.item.pick_up() break if key_char == 'i': #show the inventory; if an item is selected, use it chosen_item = inventory_menu(\ 'Press the key next to an item to use it, or any other to cancel.\n') if chosen_item is not None: chosen_item.use() if key_char == 'd': #show the inventory; if an item is selected, drop it chosen_item = inventory_menu(\ 'Press the key next to an item to drop it, or any other to cancel.\n') if chosen_item is not None: chosen_item.drop() if key_char == 'u': #go down stairs, if the player is on them if var.stairs.x == var.player.x and var.stairs.y == var.player.y: next_level() if key_char == 'c': #show character information level_up_xp = var.LEVEL_UP_BASE + var.player.level * var.LEVEL_UP_FACTOR msgbox('Character Information\n\nLevel ' + str(var.player.level) +\ '\nExperience: ' + str(var.player.fighter.xp) +\ '\nExperience to level up: ' + str(level_up_xp) +\ '\n\nMaximum HP: ' + str(var.player.fighter.max_hp) +\ '\nAttack: ' + str(var.player.fighter.power) +\ '\nDefense: ' + str(var.player.fighter.defense), var.CHARACTER_SCREEN_WIDTH) return 'didnt-take-turn'
def main(): screen_width = 80 screen_height = 50 map_width = 80 map_height = 45 room_max_size = 10 room_min_size = 6 max_rooms = 30 fov_algorithm = 0 fov_light_walls = True fov_radius = 10 colors = { 'dark_wall': libtcod.Color(0, 0, 100), 'dark_ground': libtcod.Color(50, 50, 150), 'light_wall': libtcod.Color(130, 110, 50), 'light_ground': libtcod.Color(200, 180, 50) } player = Entity(int(screen_width / 2), int(screen_height / 2), '@', libtcod.white) npc = Entity(int(screen_height / 2 - 5), int(screen_height / 2 - 5), '@', libtcod.yellow) entities = [npc, player] libtcod.console_set_custom_font( 'arial10x10.png', libtcod.FONT_TYPE_GRAYSCALE | libtcod.FONT_LAYOUT_TCOD) libtcod.console_init_root(screen_width, screen_height, 'THE GAME', False) con = libtcod.console_new(screen_width, screen_height) game_map = GameMap(map_width, map_height) game_map.make_map(max_rooms, room_min_size, room_max_size, map_width, map_height, player) fov_recompute = True fov_map = initialize_fov(game_map) key = libtcod.Key() mouse = libtcod.Mouse() while not libtcod.console_is_window_closed(): libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS, key, mouse) if fov_recompute: recompute_fov(fov_map, player.x, player.y, fov_radius, fov_light_walls, fov_algorithm) render_all(con, entities, game_map, fov_map, fov_recompute, screen_width, screen_height, colors) fov_recompute = False libtcod.console_flush() clear_all(con, entities) 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): player.move(dx, dy) fov_recompute = True if exit: return True if fullscreen: libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen())
def main(): screen_width = 80 screen_height = 50 map_width = 80 map_height = 45 room_max_size = 10 room_min_size = 6 max_rooms = 30 colors = { 'dark_wall': libtcod.Color(0, 0, 100), 'dark_ground': libtcod.Color(50, 50, 150) } player = Entity(int(screen_width / 2), int(screen_height / 2), '@', libtcod.white) npc = Entity(int(screen_width / 2 - 5), int(screen_height / 2), '@', libtcod.yellow) entities = [player, npc] libtcod.console_set_custom_font( 'arial10x10.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD) libtcod.console_init_root(screen_width, screen_height, 'Roguelike Demo', False) con = libtcod.console_new(screen_width, screen_height) game_map = GameMap(map_width, map_height) game_map.make_map(max_rooms, room_min_size, room_max_size, map_width, map_height, player) key = libtcod.Key() mouse = libtcod.Mouse() while not libtcod.console_is_window_closed(): libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS, key, mouse) render_all(con, entities, game_map, screen_width, screen_height, colors) libtcod.console_flush() clear_all(con, entities) 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): player.move(dx, dy) if exit: return True if fullscreen: libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen())
def main(): #----------------------------------------------------------------------------------------------- ################################################################################################ #----------------------------------------------------------------------------------------------- # CONFIG SCREENWIDTH = 80 SCREENHEIGHT = 50 MAPWIDTH = 80 MAPHEIGHT = 43 BARWIDTH = 20 PANELHEIGHT = 7 PANELY = SCREENHEIGHT - PANELHEIGHT MESSAGEX = BARWIDTH + 2 MESSAGEWIDTH = SCREENWIDTH - BARWIDTH - 2 MESSAGEHEIGHT = PANELHEIGHT - 2 ROOMMAX = 10 ROOMMIN = 6 NUMROOMSMAX = 30 FOVALGORITHM = 0 FOVLIGHTWALLS = True FOVRADIUS = 15 MAXMONSTERSPERROOM = 3 MAXITEMSPERROOM = 2 COLORS = { 'darkWall': tcod.Color(0,0,100), 'darkGround': tcod.Color(50,50,150), 'lightWall': tcod.Color(130,110,50), 'lightGround': tcod.Color(200,180,50) } #----------------------------------------------------------------------------------------------- ################################################################################################ #----------------------------------------------------------------------------------------------- # INITIALIZATION fighterComponent = Fighter(hp=30, defense=2, power=5) inventoryComponent = Inventory(26) player = Entity(0, 0, '@', tcod.white, 'Player', blocks=True, renderOrder=RenderOrder.ACTOR, fighter=fighterComponent, inventory=inventoryComponent) # Player Entity Object entities = [player] # Entity List #----------------------------------------------------------------------------------------------- # Initialize Font tcod.console_set_custom_font('arial10x10.png', tcod.FONT_TYPE_GREYSCALE | tcod.FONT_LAYOUT_TCOD) # Initialize Console Window tcod.console_init_root(SCREENWIDTH, SCREENHEIGHT, title = 'ASCII Roguelike', fullscreen = False) baseConsole = tcod.console_new(SCREENWIDTH, SCREENHEIGHT) # Base Console panel = tcod.console_new(SCREENWIDTH, PANELHEIGHT) #----------------------------------------------------------------------------------------------- MAP = GameMap(MAPWIDTH, MAPHEIGHT) # CREATE MAP MAP.makeMap(NUMROOMSMAX, ROOMMIN, ROOMMAX, MAPWIDTH, MAPHEIGHT, player, entities, MAXMONSTERSPERROOM, MAXITEMSPERROOM) fovRecompute = True # FOV Recomputing Boolean fovMap = initializeFOV(MAP) # Initialize FOV Map #----------------------------------------------------------------------------------------------- messageLog = MessageLog(MESSAGEX, MESSAGEWIDTH, MESSAGEHEIGHT) key = tcod.Key() # Store Keyboard Input mouse = tcod.Mouse() # Store Mouse Input gameState = GameStates.PLAYERTURN # Start On Player's Turn previousGameState = gameState #----------------------------------------------------------------------------------------------- ################################################################################################ #----------------------------------------------------------------------------------------------- # GAME LOOP while not tcod.console_is_window_closed(): tcod.sys_check_for_event(tcod.EVENT_KEY_PRESS | tcod.EVENT_MOUSE, key, mouse) # Capture User Input #----------------------------------------------------------------------------------------------- if fovRecompute: # Recompute FOV Based on Player Position recomputeFOV(fovMap, player.x, player.y, FOVRADIUS, FOVLIGHTWALLS, FOVALGORITHM) # Render All Entities renderAll(baseConsole, panel, entities, player, MAP, fovMap, fovRecompute, messageLog, SCREENWIDTH, SCREENHEIGHT, BARWIDTH, PANELHEIGHT, PANELY, mouse, COLORS, gameState) fovRecompute = False # Turn Off FOV Recompute Until Player Move #----------------------------------------------------------------------------------------------- tcod.console_flush() # Update Console to Current State clearAll(baseConsole, entities) # Clear Entities #----------------------------------------------------------------------------------------------- action = handleKeys(key, gameState) # Get Key Press # Key Press Action move = action.get('move') # Movement pickup = action.get('pickup') # Pickup Object showInventory = action.get('showInventory') inventoryIndex = action.get('inventoryIndex') exit = action.get('exit') # Exit Boolean fullscreen = action.get('fullscreen') # Fullscreen Boolean playerTurnResults = [] # Initialize Player's Turn Results # Check for movement and players turn if move and gameState == GameStates.PLAYERTURN: dx,dy = move # Movement Deltas # Movement Destination destinationX = player.x + dx destinationY = player.y + dy # If map is not blocked: if not MAP.isBlocked(destinationX, destinationY): # Check for blocking entities target = getBlockingEntities(entities, destinationX, destinationY) if target: # player.fighter.attack(target) attackResults = player.fighter.attack(target) # Gather Attack Results playerTurnResults.extend(attackResults) # Add to Player Turn Results else: player.move(dx,dy) # Move Player By Delta fovRecompute = True gameState = GameStates.ENEMYTURN # Set To Enemy's Turn elif pickup and gameState == GameStates.PLAYERTURN: for entity in entities: if entity.item and entity.x == player.x and entity.y == player.y: pickupResults = player.inventory.addItem(entity) playerTurnResults.extend(pickupResults) break else: messageLog.addMessage(Message('There is nothing to pick up.', tcod.yellow)) if showInventory: previousGameState = gameState gameState = GameStates.INVENTORY if inventoryIndex is not None and previousGameState != GameStates.PLAYERDEAD and inventoryIndex < len(player.inventory.items): item = player.inventory.items[inventoryIndex] playerTurnResults.extend(player.inventory.use(item)) if exit: # Exit Window if gameState == GameStates.INVENTORY: gameState = previousGameState else: return True if fullscreen: # Fullscreen tcod.console_set_fullscreen(not tcod.console_is_fullscreen()) for playerTurnResult in playerTurnResults: message = playerTurnResult.get('message') deadEntity = playerTurnResult.get('dead') itemAdded = playerTurnResult.get('itemAdded') itemConsumed = playerTurnResult.get('consumed') if message: messageLog.addMessage(message) if deadEntity: if deadEntity == player: message, gameState = killPlayer(deadEntity) else: message = killMonster(deadEntity) messageLog.addMessage(message) if itemAdded: entities.remove(itemAdded) gameState = GameStates.ENEMYTURN if itemConsumed: gameState = GameStates.ENEMYTURN if gameState == GameStates.ENEMYTURN: for entity in entities: if entity.ai: # entity.ai.takeTurn(player, fovMap, MAP, entities) enemyTurnResults = entity.ai.takeTurn(player, fovMap, MAP, entities) for enemyTurnResult in enemyTurnResults: message = enemyTurnResult.get('message') deadEntity = enemyTurnResult.get('dead') if message: messageLog.addMessage(message) if deadEntity: if deadEntity == player: message, gameState = killPlayer(deadEntity) else: message = killMonster(deadEntity) messageLog.addMessage(message) if gameState == GameStates.PLAYERDEAD: break if gameState == GameStates.PLAYERDEAD: break else: gameState = GameStates.PLAYERTURN # Set To Player's Turn
def main(): # Screen size screen_width = 80 screen_height = 50 # HP Bar bar_width = 20 panel_height = 7 panel_y = screen_height - panel_height # Message log size message_x = bar_width + 2 message_width = screen_width - bar_width - 2 message_height = panel_height - 1 # Map and rooms map_width = 80 map_heigth = 43 room_max_size = 10 room_min_size = 6 max_rooms = 30 # FOV fov_algorithm = 0 fov_light_walls = True fov_radius = 10 # Monsters max_monsters_per_room = 3 # Color dictinoary colors = { 'dark_wall': libtcod.Color(0, 0, 100), 'dark_ground': libtcod.Color(50, 50, 150), 'light_wall': libtcod.Color(130, 110, 50), 'light_ground': libtcod.Color(200, 180, 50) } # Setup player and entities fighter_component = Fighter(hp=30, defense=2, power=5) player = Entity(0, 0, '@', libtcod.white, 'Player', blocks=True, render_order=RenderOrder.ACTOR, fighter=fighter_component) entities = [player] # Setup libtcod console libtcod.console_set_custom_font( 'arial10x10.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD) libtcod.console_init_root(screen_width, screen_height, 'RL-ONE', False) con = libtcod.console_new(screen_width, screen_height) # UI panel panel = libtcod.console_new(screen_width, panel_height) # Draw map game_map = GameMap(map_width, map_heigth) game_map.make_map(max_rooms, room_min_size, room_max_size, map_width, map_heigth, player, entities, max_monsters_per_room) # Recompute FOV boolean, this only happens when the player moves fov_recompute = True # Initialize FOV fov_map = initialize_fov(game_map) # Message log message_log = MessageLog(message_x, message_width, message_height) # Input controls key = libtcod.Key() mouse = libtcod.Mouse() # Game state game_state = GameStates.PLAYERS_TURN # Game loop while not libtcod.console_is_window_closed(): libtcod.sys_check_for_event( libtcod.EVENT_KEY_PRESS | libtcod.EVENT_MOUSE, key, mouse) if fov_recompute: recompute_fov(fov_map, player.x, player.y, fov_radius, fov_light_walls, fov_algorithm) render_all(con, panel, entities, player, game_map, fov_map, fov_recompute, message_log, screen_width, screen_height, bar_width, panel_height, panel_y, mouse, colors) fov_recompute = False libtcod.console_flush() clear_all(con, entities) action = handle_keys(key) move = action.get('move') exit = action.get('exit') fullscreen = action.get('fullscreen') # Results list player_turn_results = [] # Players turn if move and game_state == GameStates.PLAYERS_TURN: dx, dy = move destination_x = player.x + dx destination_y = player.y + dy if not game_map.is_blocked(destination_x, destination_y): target = get_blocking_entities_at_location( entities, destination_x, destination_y) if target: attack_results = player.fighter.attack(target) player_turn_results.extend(attack_results) else: player.move(dx, dy) fov_recompute = True game_state = GameStates.ENEMY_TURN for player_turn_result in player_turn_results: message = player_turn_result.get('message') dead_entity = player_turn_result.get('dead') if message: message_log.add_message(message) if dead_entity: if dead_entity == player: message, game_state = kill_player(dead_entity) else: message = kill_monster(dead_entity) message_log.add_message(message) # Enemies turn if game_state == GameStates.ENEMY_TURN: for entity in entities: if entity.ai: enemy_turn_results = entity.ai.take_turn( player, fov_map, game_map, entities) for enemy_turn_result in enemy_turn_results: message = enemy_turn_result.get('message') dead_entity = enemy_turn_result.get('dead') if message: message_log.add_message(message) if dead_entity: if dead_entity == player: message, game_state = kill_player(dead_entity) else: message = kill_monster(dead_entity) message_log.add_message(message) if game_state == GameStates.PLAYER_DEAD: break if game_state == GameStates.PLAYER_DEAD: break else: game_state = GameStates.PLAYERS_TURN if exit: return True if fullscreen: libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen())
def main(): # Tela # Largura largura_tela = 80 # Altura altura_tela = 50 # Mapa mapa_largura = 80 mapa_altura = 43 # Sala tamanho_max_sala = 10 tamanho_min_sala = 6 max_salas = 30 # Algoritimo para visao (Famoso Fog) visao_algoritimo = 0 visao_ilumina_parede = True area_visao = 3 # interface barra_largura = 20 painel_altura = 7 painel_y = altura_tela - painel_altura msg_x = barra_largura + 2 msg_alt = largura_tela - barra_largura - 2 msg_lar = painel_altura + 6 # Monstros monstros_por_sala = 3 # Cores cores = { 'parede_escura': libtcod.Color(0, 0, 100), 'chao_escuro': libtcod.Color(180, 180, 180), 'parede_iluminada': libtcod.Color(130, 110, 50), 'chao_iluminado': libtcod.Color(200, 180, 50) } # Jogador combatente = Combatente(hp=30, defesa=2, forca=5) player = Entidade(0, 0, '@', libtcod.white, 'Diego', bloqueia=True, combatente=combatente, ordem_desenho=OrdemDesenho.ATOR) # npc = Entidade(int(largura_tela / 2 - 5), int(altura_tela / 2), 'N', libtcod.yellow, 'SHOP', bloqueia=False) # Objetos do Mapa (NPC , Jogador, Monstros, Itens e etc ... ) entidades = [player] libtcod.console_set_custom_font( 'arial10x10.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD) libtcod.console_init_root(largura_tela, altura_tela, 'Dugeon Diego', False) console = libtcod.console_new(largura_tela, altura_tela) painel = libtcod.console_new(largura_tela, painel_altura) mapa_jogo = MapaJogo(mapa_largura, mapa_altura) # mapa_jogo.fazer_mapa_estatico() # Desenha a DG. mapa_jogo.fazer_mapa(max_salas, tamanho_min_sala, tamanho_max_sala, mapa_largura, mapa_altura, player, entidades, monstros_por_sala) recalcula_visao = True visao = inicializa_visao(mapa_jogo) msg_log = MensagemLog(msg_x, msg_lar, msg_alt) # cria variaveis para teclas e Mouse. key = libtcod.Key() mouse = libtcod.Mouse() estado_jogo = EstadoJogo.TURNO_JOGADOR # Com tudo pronto aqui começa o laço principal while not libtcod.console_is_window_closed(): # captura eventos libtcod.sys_check_for_event( libtcod.EVENT_KEY_PRESS | libtcod.EVENT_MOUSE, key, mouse) if recalcula_visao: recalcular_visao(visao, player.x, player.y, area_visao, visao_ilumina_parede, visao_algoritimo) # desenhar desenhar_tudo(console, entidades, mapa_jogo, largura_tela, altura_tela, cores, visao, recalcula_visao, player, painel, barra_largura, largura_tela, painel_altura, painel_y, msg_log, mouse) recalcula_visao = False libtcod.console_flush() # limpar limpar_tudo(console, entidades) # Recebe um dicionario acao = captura_teclado(key) mover = acao.get('mover') sair = acao.get('sair') tela_cheia = acao.get('fullscreen') jogador_resultado = [] if mover and estado_jogo == EstadoJogo.TURNO_JOGADOR: dx, dy = mover destino_x = player.x + dx destino_y = player.y + dy if not mapa_jogo.is_bloqueado(destino_x, destino_y): alvo = obter_entidade_com_bloqueio_por_localizacao( entidades, destino_x, destino_y) if alvo: j_ataque_resultado = player.combatente.atacar(alvo) jogador_resultado.extend(j_ataque_resultado) else: player.mover(dx, dy) recalcula_visao = True estado_jogo = EstadoJogo.TURNO_INIMIGO if sair: return True if tela_cheia: libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen()) for resultado in jogador_resultado: msg = resultado.get('mensagem') morto = resultado.get('morto') if msg: msg_log.add_msg(msg) if morto: if morto == player: msg, estado_jogo = matar_jogador(morto) else: msg = kill_monster(morto) msg_log.add_msg(msg) if estado_jogo == EstadoJogo.TURNO_INIMIGO: for entidade in entidades: if entidade.ai: entidade.ai.acao_ai(player, visao, mapa_jogo, entidades) enimigo_resultado = entidade.ai.acao_ai( player, visao, mapa_jogo, entidades) for resultado in enimigo_resultado: msg = resultado.get('mensagem') morto = resultado.get('morto') if msg: msg_log.add_msg(msg) if morto: if morto == player: message, estado_jogo = matar_jogador(morto) else: message = kill_monster(morto) msg_log.add_msg(message) if estado_jogo == EstadoJogo.JOGADOR_MORTO: break if estado_jogo == EstadoJogo.JOGADOR_MORTO: break else: estado_jogo = EstadoJogo.TURNO_JOGADOR
def main(): screen_width = 80 screen_height = 50 map_width = 80 map_height = 45 colors = { 'dark_wall': libtcod.Color(0, 0, 100), 'dark_ground': libtcod.Color(50, 50, 150) } player = Entity(int(screen_width / 2), int(screen_height / 2), '@', libtcod.peach) npc = Entity(int(screen_width / 2 - 5), int(screen_height / 2), '@', libtcod.yellow) entities = [npc, player] libtcod.console_set_custom_font( 'arial10x10.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD) libtcod.console_init_root(screen_width, screen_height, 'Ringo\'s Vanilla Rogue', False) con = libtcod.console_new(screen_width, screen_height) game_map = GameMap(map_width, map_height) key = libtcod.Key() mouse = libtcod.Mouse() #game loop while not libtcod.console_is_window_closed(): #check for input libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS, key, mouse) #put entities in places render_all(con, entities, game_map, screen_width, screen_height, colors) #draw to the screen libtcod.console_flush() #clear it? clear_all(con, entities) 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): player.move(dx, dy) if exit: return True if fullscreen: libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen())
def main(): screen_width = 80 screen_height = 50 map_width = 80 map_height = 45 colors = { "dark_wall": libtcod.Color(0, 0, 100), "dark_ground": libtcod.Color(50, 50, 150) } # Create the player and an npc using the Entity class player = Entity(int(screen_width / 2), int(screen_height / 2), "@", libtcod.purple) npc = Entity(int(screen_width / 2 - 5), int(screen_height / 2), "@", libtcod.green) entities = [npc, player] # Telling libtcod which file to use, second argument is file type libtcod.console_set_custom_font( "arial10x10.png", libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD) # This is what creates the screen, sets the game name and specifies fullscreen boolean libtcod.console_init_root(screen_width, screen_height, "TBD", False) # Creating the console con = libtcod.console_new(screen_width, screen_height) #Initialize map game_map = GameMap(map_width, map_height) # Variables for holding keyboard/mouse input key = libtcod.Key() mouse = libtcod.Mouse() # Loops that continues until the game window is closed # Checks for key/mouse press # Specifies character colour, where to put them, what they'll be represented by # and the background. The 0 is the console we're drawing to. # console_flush() draws everything while not libtcod.console_is_window_closed(): libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS, key, mouse) render_all(con, entities, game_map, screen_width, screen_height, colors) libtcod.console_flush() clear_all(con, entities) # This calls the handle_keys function to determine what should be done # depending on which key was pressed. action = handle_keys(key) move = action.get("move") exit = action.get("exit") fullscreen = action.get("fullscreen") # takes the x/y move amounts and applies them to player's position if move: dx, dy = move if not game_map.is_blocked(player.x + dx, player.y + dy): player.move(dx, dy) if exit: return True if fullscreen: libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen())
def do_game_window_actions(self, action): a_fullscreen = action.get('fullscreen') if a_fullscreen: tcod.console_set_fullscreen(not tcod.console_is_fullscreen())
def consume_actions(key,mouse,game_state,player,game_map, entities,fov_recompute,fov_map,message_log,constants,con,targeting_item,previous_game_state): action = handle_keys(key,mouse,game_state) mouse_action = handle_mouse(key,mouse,game_state,constants,con,player) exit_pressed = False avail_actions = get_available_actions(action, mouse_action) player_turn_results = [] if avail_actions[Action.MOVE] and game_state == GameStates.PLAYERS_TURN: player_turn_results, player, fov_recompute, game_state = handle_move_input(avail_actions[Action.MOVE], player, game_map, entities, player_turn_results, fov_recompute, game_state) elif avail_actions[Action.WAIT]: player.fighter.heal(.2) game_state = GameStates.ENEMY_TURN elif avail_actions[Action.PICKUP] and game_state == GameStates.PLAYERS_TURN: player_turn_results, message_log = handle_pickup_input(entities, player, player_turn_results, message_log) if avail_actions[Action.SHOW_INVENTORY]: previous_game_state = game_state game_state = GameStates.SHOW_INVENTORY if avail_actions[Action.SHOW_WEAPON_INVENTORY]: previous_game_state = game_state game_state = GameStates.SHOW_WEAPON_INVENTORY if avail_actions[Action.SHOW_ARMOR_INVENTORY]: previous_game_state = game_state game_state = GameStates.SHOW_ARMOR_INVENTORY if avail_actions[Action.SHOW_SCROLL_INVENTORY]: previous_game_state = game_state game_state = GameStates.SHOW_SCROLL_INVENTORY if avail_actions[Action.SHOW_QUAFF_INVENTORY]: previous_game_state = game_state game_state = GameStates.SHOW_QUAFF_INVENTORY if avail_actions[Action.DROP_INVENTORY]: previous_game_state = game_state game_state = GameStates.DROP_INVENTORY curr_avail_action_check = Action.INVENTORY_INDEX curr_avail_action_check_mouse = Action.INVENTORY_INDEX_MOUSE if ((avail_actions[curr_avail_action_check] is not None and avail_actions[curr_avail_action_check] < len(player.inventory.items)) or (avail_actions[curr_avail_action_check_mouse] is not None and avail_actions[curr_avail_action_check_mouse] < len(player.inventory.items))) \ and previous_game_state != GameStates.PLAYER_DEAD: if avail_actions[curr_avail_action_check] is not None: inventory_index = avail_actions[curr_avail_action_check] else: inventory_index = avail_actions[curr_avail_action_check_mouse] player_turn_results = handle_inventory_index_input(player, inventory_index, game_state, player_turn_results, entities, fov_map) curr_avail_action_check = Action.WEAPON_INVENTORY_INDEX curr_avail_action_check_mouse = Action.WEAPON_INVENTORY_INDEX_MOUSE if ((avail_actions[curr_avail_action_check] is not None and avail_actions[curr_avail_action_check] < len(get_weapon_inventory_index_options(player))) or (avail_actions[curr_avail_action_check_mouse] is not None and avail_actions[curr_avail_action_check_mouse] < len(player.inventory.items))) \ and previous_game_state != GameStates.PLAYER_DEAD: if avail_actions[curr_avail_action_check] is not None: inventory_index = avail_actions[curr_avail_action_check] else: inventory_index = avail_actions[curr_avail_action_check_mouse] player_turn_results = handle_weapon_inventory_index_input(player, inventory_index, game_state, player_turn_results, entities, fov_map) curr_avail_action_check = Action.ARMOR_INVENTORY_INDEX curr_avail_action_check_mouse = Action.ARMOR_INVENTORY_INDEX_MOUSE if ((avail_actions[curr_avail_action_check] is not None and avail_actions[curr_avail_action_check] < len( get_armor_inventory_index_options(player))) or (avail_actions[curr_avail_action_check_mouse] is not None and avail_actions[ curr_avail_action_check_mouse] < len(player.inventory.items))) \ and previous_game_state != GameStates.PLAYER_DEAD: if avail_actions[curr_avail_action_check] is not None: inventory_index = avail_actions[curr_avail_action_check] else: inventory_index = avail_actions[curr_avail_action_check_mouse] player_turn_results = handle_armor_inventory_index_input(player, inventory_index, game_state, player_turn_results, entities, fov_map) curr_avail_action_check = Action.SCROLL_INVENTORY_INDEX curr_avail_action_check_mouse = Action.SCROLL_INVENTORY_INDEX_MOUSE if ((avail_actions[curr_avail_action_check] is not None and avail_actions[curr_avail_action_check] < len(get_scroll_inventory_index_options(player))) or (avail_actions[curr_avail_action_check_mouse] is not None and avail_actions[curr_avail_action_check_mouse] < len(player.inventory.items))) \ and previous_game_state != GameStates.PLAYER_DEAD: if avail_actions[curr_avail_action_check] is not None: inventory_index = avail_actions[curr_avail_action_check] else: inventory_index = avail_actions[curr_avail_action_check_mouse] player_turn_results = handle_scroll_inventory_index_input(player, inventory_index, game_state, player_turn_results, entities, fov_map) curr_avail_action_check = Action.QUAFF_INVENTORY_INDEX curr_avail_action_check_mouse = Action.QUAFF_INVENTORY_INDEX_MOUSE if ((avail_actions[curr_avail_action_check] is not None and avail_actions[curr_avail_action_check] < len( get_quaff_inventory_index_options(player))) or (avail_actions[curr_avail_action_check_mouse] is not None and avail_actions[ curr_avail_action_check_mouse] < len(player.inventory.items))) \ and previous_game_state != GameStates.PLAYER_DEAD: if avail_actions[curr_avail_action_check] is not None: inventory_index = avail_actions[curr_avail_action_check] else: inventory_index = avail_actions[curr_avail_action_check_mouse] player_turn_results = handle_quaff_inventory_index_input(player, inventory_index, game_state, player_turn_results, entities, fov_map) if avail_actions[Action.TAKE_STAIRS_DOWN] and game_state == GameStates.PLAYERS_TURN: player, entities, game_map, message_log, game_state, fov_map, fov_recompute = handle_take_stairs_down_input( entities, player, game_map, message_log, game_state, constants, con, fov_map, fov_recompute) if avail_actions[Action.TAKE_STAIRS_UP] and game_state == GameStates.PLAYERS_TURN: player, entities, game_map, message_log, game_state, fov_map, fov_recompute = handle_take_stairs_up_input( entities, player, game_map, message_log, game_state, constants, con, fov_map, fov_recompute) curr_avail_action_check = Action.LEVEL_UP curr_avail_action_check_mouse = Action.LEVEL_UP_MOUSE if avail_actions[curr_avail_action_check] or avail_actions[curr_avail_action_check_mouse]: if avail_actions[curr_avail_action_check] is not None: inventory_index = avail_actions[curr_avail_action_check] else: inventory_index = avail_actions[curr_avail_action_check_mouse] player, game_state = handle_level_up_input(player, inventory_index, previous_game_state) if avail_actions[Action.SHOW_CHARACTER_SCREEN]: previous_game_state = game_state game_state = GameStates.CHARACTER_SCREEN if game_state == GameStates.TARGETING: player_turn_results = handle_targeting_input(avail_actions[Action.LEFT_CLICK], avail_actions[Action.RIGHT_CLICK], player, targeting_item, entities, fov_map, player_turn_results) if avail_actions[Action.EXIT]: game_state, player_turn_results, exit_pressed = handle_exit_input(player, game_state, previous_game_state, player_turn_results, entities, game_map, message_log) if avail_actions[Action.FULLSCREEN]: libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen()) return action,entities,fov_map,fov_recompute,game_map,game_state,message_log,mouse_action,player,player_turn_results,previous_game_state,exit_pressed
def play_game(player, entities, game_map, message_log, game_state, con, panel, constants): fov_recompute = True key = libtcod.Key() mouse = libtcod.Mouse() fov_map = initialize_fov(game_map) previous_game_state = game_state targeting_item = None while not libtcod.console_is_window_closed(): if fov_recompute: recompute_fov(fov_map, player.x, player.y, constants["fov_radius"], constants["fov_light_walls"], constants["fov_algorithm"]) libtcod.sys_check_for_event( libtcod.EVENT_KEY_PRESS | libtcod.EVENT_MOUSE, key, mouse) render_all(con, panel, entities, player, game_map, fov_map, fov_recompute, message_log, constants["screen_width"], constants["screen_height"], constants["bar_width"], constants["panel_height"], constants["panel_y"], mouse, constants["colors"], game_state) libtcod.console_flush() clear_all(con, entities) fov_recompute = False action = handle_keys(key, game_state) mouse_action = handle_mouse(mouse) move = action.get("move") wait = action.get("wait") pickup = action.get("pickup") show_inventory = action.get("show_inventory") drop_inventory = action.get("drop_inventory") inventory_index = action.get("inventory_index") show_char_screen = action.get("show_character_screen") take_stairs = action.get("take_stairs") level_up = action.get("level_up") exit = action.get("exit") fullscreen = action.get("fullscreen") left_click = mouse_action.get("left_click") right_click = mouse_action.get("right_click") player_turn_results = [] if wait: game_state = GameStates.ENEMY_TURN if level_up: if level_up == "hp": player.fighter.base_max_hp += 20 player.fighter.hp += 20 elif level_up == "str": player.fighter.base_power += 1 elif level_up == "dex": player.fighter.base_defense += 1 game_state = previous_game_state if show_char_screen: previous_game_state = game_state game_state = GameStates.CHARACTER_SCREEN if take_stairs and game_state == GameStates.PLAYERS_TURN: for entity in entities: if entity.stairs and entity.x == player.x and entity.y == player.y: entities = game_map.next_floor(player, message_log, constants) fov_map = initialize_fov(game_map) fov_recompute = True libtcod.console_clear(con) break else: message_log.add_message( Message("There are no stairs here.", libtcod.yellow)) if move and game_state == GameStates.PLAYERS_TURN: dx, dy = move if not game_map.is_blocked(player.x + dx, player.y + dy): destination_x = player.x + dx destination_y = player.y + dy target = player.get_blocking_entities_at_location( entities, destination_x, destination_y) if target: attack_results = player.fighter.attack(target) player_turn_results.extend(attack_results) else: fov_recompute = True player.move(dx, dy) game_state = GameStates.ENEMY_TURN if pickup and game_state == GameStates.PLAYERS_TURN: for entity in entities: if entity.item and entity.x == player.x and entity.y == player.y: pickup_results = player.inventory.add_item(entity) player_turn_results.extend(pickup_results) break else: message_log.add_message( Message("Tehre is nothing here to pick up.", libtcod.yellow)) if show_inventory: previous_game_state = game_state game_state = GameStates.SHOW_INVENTORY if drop_inventory: previous_game_state = game_state game_state = GameStates.DROP_INVENTORY if inventory_index is not None and previous_game_state != GameStates.PLAYER_DEAD and inventory_index < len( player.inventory.items): item = player.inventory.items[inventory_index] if game_state == GameStates.SHOW_INVENTORY: player_turn_results.extend( player.inventory.use(item, entities=entities, fov_map=fov_map)) elif game_state == GameStates.DROP_INVENTORY: player_turn_results.extend(player.inventory.drop_item(item)) if game_state == GameStates.TARGETING: if left_click: target_x, target_y = left_click item_use_results = player.inventory.use(targeting_item, entities=entities, fov_map=fov_map, target_x=target_x, target_y=target_y) player_turn_results.extend(item_use_results) elif right_click: player_turn_results.append({"targeting_cancelled": True}) if exit: if game_state in (GameStates.SHOW_INVENTORY, GameStates.DROP_INVENTORY, GameStates.CHARACTER_SCREEN): game_state = previous_game_state elif game_state == GameStates.TARGETING: player_turn_results.append({"targeting_cancelled": True}) else: save_game(player, entities, game_map, message_log, game_state) return True if fullscreen: libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen()) for player_turn_result in player_turn_results: message = player_turn_result.get("message") dead_entity = player_turn_result.get("dead") item_added = player_turn_result.get("item_added") item_consumed = player_turn_result.get("consumed") item_dropped = player_turn_result.get("item_dropped") equip = player_turn_result.get("equip") targeting = player_turn_result.get("targeting") targeting_cancelled = player_turn_result.get("targeting_cancelled") xp = player_turn_result.get("xp") if message: message_log.add_message(message) if dead_entity: if dead_entity == player: message, game_state = kill_player(dead_entity) else: message = kill_monster(dead_entity) message_log.add_message(message) if item_added: entities.remove(item_added) game_state = GameStates.ENEMY_TURN if item_consumed: game_state = GameStates.ENEMY_TURN if item_dropped: entities.append(item_dropped) game_state = GameStates.ENEMY_TURN if equip: equip_results = player.equipment.toggle_equip(equip) for equip_result in equip_results: equipped = equip_result.get("equipped") dequipped = equip_result.get("dequipped") if equipped: message_log.add_message( Message("You equipped the {}".format( equipped.name))) if dequipped: message_log.add_message( Message("You dequipped the {}".format( dequipped.name))) game_state = GameStates.ENEMY_TURN if targeting: previous_game_state = GameStates.PLAYERS_TURN game_state = GameStates.TARGETING targeting_item = targeting message_log.add_message(targeting_item.item.targeting_message) if targeting_cancelled: game_state = previous_game_state message_log.add_message(Message("Targeting cancelled")) if xp: leveled_up = player.level.add_xp(xp) message_log.add_message( Message("You gain {} experience points.".format(xp), libtcod.white)) if leveled_up: message_log.add_message( Message( "You battle skills grow stronger! You reached level {}." .format(player.level.current_level), libtcod.yellow)) previous_game_state = game_state game_state = GameStates.LEVEL_UP if game_state == GameStates.ENEMY_TURN: for entity in entities: if entity.ai: enemy_turn_results = entity.ai.take_turn( player, fov_map, game_map, entities) for enemy_turn_result in enemy_turn_results: message = enemy_turn_result.get("message") dead_entity = enemy_turn_result.get("dead") if message: message_log.add_message(message) if dead_entity: if dead_entity == player: message, game_state = kill_player(dead_entity) else: message = kill_monster(dead_entity) message_log.add_message(message) if game_state == GameStates.PLAYER_DEAD: break if game_state == GameStates.PLAYER_DEAD: break else: game_state = GameStates.PLAYERS_TURN
def handle_keys(): global key; if key.vk == libtcod.KEY_ENTER and key.lalt: #Alt+Enter: toggle fullscreen libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen()) elif key.vk == libtcod.KEY_ESCAPE: return 'exit' #exit game if game_state == 'playing': #movement keys if key.vk == libtcod.KEY_UP or key.vk == libtcod.KEY_KP8: player_move_or_attack(0, -1) elif key.vk == libtcod.KEY_DOWN or key.vk == libtcod.KEY_KP2: player_move_or_attack(0, 1) elif key.vk == libtcod.KEY_LEFT or key.vk == libtcod.KEY_KP4: player_move_or_attack(-1, 0) elif key.vk == libtcod.KEY_RIGHT or key.vk == libtcod.KEY_KP6: player_move_or_attack(1, 0) elif key.vk == libtcod.KEY_HOME or key.vk == libtcod.KEY_KP7: player_move_or_attack(-1, -1) elif key.vk == libtcod.KEY_PAGEUP or key.vk == libtcod.KEY_KP9: player_move_or_attack(1, -1) elif key.vk == libtcod.KEY_END or key.vk == libtcod.KEY_KP1: player_move_or_attack(-1, 1) elif key.vk == libtcod.KEY_PAGEDOWN or key.vk == libtcod.KEY_KP3: player_move_or_attack(1, 1) elif key.vk == libtcod.KEY_KP5: pass #do nothing ie wait for the monster to come to you else: #test for other keys key_char = chr(key.c) if key_char == 'g': #pick up an item for object in objects: #look for an item in the player's tile if object.x == player.x and object.y == player.y and object.item: object.item.pick_up() break if key_char == 'i': #show the inventory; if an item is selected, use it chosen_item = inventory_menu('Press the key next to an item to use it, or any other to cancel.\n') if chosen_item is not None: chosen_item.use() if key_char == 'd': #show the inventory; if an item is selected, drop it chosen_item = inventory_menu('Press the key next to an item to drop it, or any other to cancel.\n') if chosen_item is not None: chosen_item.drop() if key_char == 'c': #show character information level_up_xp = LEVEL_UP_BASE + player.level * LEVEL_UP_FACTOR msgbox('Character Information\n\nLevel: ' + str(player.level) + '\nExperience: ' + str(player.fighter.xp) + '\nExperience to level up: ' + str(level_up_xp) + '\n\nMaximum HP: ' + str(player.fighter.max_hp) + '\nAttack: ' + str(player.fighter.power) + '\nDefense: ' + str(player.fighter.defense), CHARACTER_SCREEN_WIDTH) if key_char == '<': #go down stairs, if the player is on them if stairs.x == player.x and stairs.y == player.y: next_level() return 'didnt-take-turn'
def main(): # TODO: refactor this stuff around. make certain variables global, etc. screen_width = 80 screen_height = 50 # map settings map_width = 50 map_height = 40 room_max_size = 11 room_min_size = 6 max_rooms = 50 # fov settings fov_algo = tcod.FOV_DIAMOND fov_light_walls = True fov_radius = 7 tcod.console_set_custom_font( FONT, tcod.FONT_TYPE_GREYSCALE | tcod.FONT_LAYOUT_TCOD) tcod.console_init_root(screen_width, screen_height, 'nRogue', False, tcod.RENDERER_OPENGL2, vsync=False) game_map = GameMap(map_width, map_height) con = tcod.console.Console(screen_width, screen_height) key = tcod.Key() mouse = tcod.Mouse() player = Fighter(0, 0, '@', tcod.white, 'Player') entities = [player] game_map.make_map(max_rooms, room_min_size, room_max_size, map_width, map_height, player) fov_recompute = True game_map.populate_map(entities) while not tcod.console_is_window_closed(): tcod.sys_check_for_event(tcod.EVENT_KEY_PRESS, key, mouse) if fov_recompute: game_map.compute_fov(player.x, player.y, fov_radius, fov_algo, fov_light_walls) render_all(con, entities, game_map, map_width, map_height, COLORS, fov_recompute) fov_recompute = False tcod.console_flush() clear_all(con, entities) action = handle_keys(key) move_or_attack = action.get('move_or_attack') exit = action.get('exit') fullscreen = action.get('fullscreen') if move_or_attack: # python returns true for any non-zero value dx, dy = move_or_attack player.move_or_attack(dx, dy, game_map) fov_recompute = True if exit: return True if fullscreen: tcod.console_set_fullscreen(not tcod.console_is_fullscreen()) if key.vk == tcod.KEY_ESCAPE: return True
def play_game(player, entities, game_map, message_log, game_state, con, panel, constants, priority_queue, global_variables, world): fov_recompute = True fov_map = initialize_fov(game_map) key = libtcod.Key() mouse = libtcod.Mouse() # Variables used to make decisions during the game. targeting_item = None previous_game_state = game_state # Turn on camera. camera = Camera(constants['camera_width'], constants['camera_height'], player.x, player.y, constants['map_width'], constants['map_height']) # Actviate cursor. cursor = Cursor() while not libtcod.console_is_window_closed(): libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS | libtcod.EVENT_MOUSE, key, mouse) if fov_recompute: recompute_fov(fov_map, player.x, player.y, constants['fov_radius'], constants['fov_light_walls'], constants['fov_algorithm']) # Only render and recompute FOV while on the player's turn. if not game_state == GameStates.ENEMY_TURN: render_all(con, panel, entities, player, game_map, fov_map, fov_recompute, message_log, constants['screen_width'], constants['screen_height'], constants['bar_width'], constants['panel_height'], constants['panel_y'], mouse, constants['colors'], game_state, camera, cursor) fov_recompute = False libtcod.console_flush() # The console is cleared, but it will only be flushed on the player's turn. clear_all(con, entities, camera.x, camera.y, cursor) action = handle_keys(key, game_state) mouse_action = handle_mouse(mouse) enemy = None # If it's not the player's turn, it's _this_ enemy's turn. # Decide the entity's turn. if not priority_queue.empty() and game_state == GameStates.ENEMY_TURN: queue_ID = priority_queue.get_ID() # This removes the topmost ID from the queue. for entity in entities: if queue_ID == entity.ID and not entity.is_dead(): # If the entity is dead, do nothing. It has already been removed from the queue. if entity.ai == None: #it's the player # The player gets reinserted into the queue after their action. game_state = GameStates.PLAYERS_TURN break else: # The enemy gets reinserted into the queue after their action. enemy = entity break """ List of possible actions taken by the player. TODO: Cut things up via GameState more clearly. """ move = action.get('move') wait = action.get('wait') pickup = action.get('pickup') show_inventory = action.get('show_inventory') drop_inventory = action.get('drop_inventory') inventory_index = action.get('inventory_index') exit = action.get('exit') fullscreen = action.get('fullscreen') level_up = action.get('level_up') show_character_screen = action.get('show_character_screen') show_extract_materia_menu = action.get('show_extract_materia_menu') # Prompt the player to extra materia from creature. extraction_index = action.get('extraction_index') # Select this entry from the extraction menu. look = action.get('look') # Enter the LOOK GameState. select = action.get('select') # A target has been selected via keyboard. left_click = mouse_action.get('left_click') right_click = mouse_action.get('right_click') player_turn_results = [] if move and game_state == GameStates.PLAYERS_TURN: # TODO: Prevent player from moving outside the map. dx, dy = move destination_x = player.x + dx destination_y = player.y + dy if not game_map.is_blocked(destination_x, destination_y): target = get_blocking_entities_at_location(entities, destination_x, destination_y) if target: attack_results = player.fighter.attack(target) player_turn_results.extend(attack_results) else: # Check to see if the player is lined up with the center. # If so, move the camera with the player. camera_x, camera_y = camera.absolute_center() if player.x == camera_x: camera.move(dx, 0) if player.y == camera_y: camera.move(0, dy) player.move(dx, dy) fov_recompute = True priority_queue.put(player.fighter.speed, player.ID) # The player spends their turn to move/attack. game_state = GameStates.ENEMY_TURN elif move and game_state == GameStates.LOOK: # Move the cursor. dx, dy = move cursor.move(dx, dy, constants['camera_width'], constants['camera_height'], camera.x, camera.y) elif wait and game_state == GameStates.PLAYERS_TURN: # Puts the player second in queue. priority_queue.put_next(player.ID) game_state = GameStates.ENEMY_TURN elif pickup and game_state == GameStates.PLAYERS_TURN: for entity in entities: if entity.item and entity.x == player.x and entity.y == player.y: pickup_results = player.inventory.add_item(entity) player_turn_results.extend(pickup_results) priority_queue.put(player.fighter.speed, player.ID) # The player spends their turn picking up stuff. game_state = GameStates.ENEMY_TURN break else: message_log.add_message(Message('There is nothing here to pick up.', libtcod.yellow)) if show_inventory and game_state == GameStates.PLAYERS_TURN: previous_game_state = game_state game_state = GameStates.SHOW_INVENTORY if drop_inventory and game_state == GameStates.PLAYERS_TURN: previous_game_state = game_state game_state = GameStates.DROP_INVENTORY if inventory_index is not None and previous_game_state != GameStates.PLAYER_DEAD and inventory_index < len(player.inventory.items): item = player.inventory.items[inventory_index] if game_state == GameStates.SHOW_INVENTORY: player_turn_results.extend(player.inventory.use(item)) elif game_state == GameStates.DROP_INVENTORY: player_turn_results.extend(player.inventory.drop_item(item)) if game_state == GameStates.TARGETING: # Mouse targeting if left_click: target_x, target_y = left_click target_x += camera.x target_y += camera.y item_use_results = player.inventory.use(targeting_item, entities=entities, fov_map=fov_map, target_x=target_x, target_y=target_y) player_turn_results.extend(item_use_results) elif right_click: player_turn_results.append({'targeting_cancelled': True}) # Keyboard targeting if move: # Move the cursor. dx, dy = move cursor.move(dx, dy, constants['camera_width'], constants['camera_height'], camera.x, camera.y) if select: # Hit the chosen target. target_x, target_y = cursor.x, cursor.y item_use_results = player.inventory.use(targeting_item, entities=entities, fov_map=fov_map, target_x=target_x, target_y=target_y) player_turn_results.extend(item_use_results) if exit: if game_state in (GameStates.SHOW_INVENTORY, GameStates.DROP_INVENTORY, GameStates.CHARACTER_SCREEN, GameStates.MATERIA_SCREEN, GameStates.LOOK): game_state = previous_game_state elif game_state == GameStates.TARGETING: player_turn_results.append({'targeting_cancelled': True}) else: save_game(player, entities, game_map, message_log, game_state, priority_queue, global_variables, world) return True if fullscreen: libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen()) if level_up: if level_up == 'hp': player.fighter.max_hp += 20 player.fighter.hp += 20 elif level_up == 'str': player.fighter.power += 1 elif level_up == 'def': player.fighter.defense += 1 game_state = previous_game_state if show_character_screen and game_state == GameStates.PLAYERS_TURN: previous_game_state = game_state game_state = GameStates.CHARACTER_SCREEN if show_extract_materia_menu and game_state == GameStates.PLAYERS_TURN: previous_game_state = game_state game_state = GameStates.MATERIA_SCREEN if extraction_index is not None and previous_game_state != GameStates.PLAYER_DEAD: # Create the list of items that have materia item_list = player.inventory.item_list_with_property('materia') # Check to see the index is inside this list if extraction_index < len(item_list): # Create an item called "type Materia (lvl n)" extracting_item = item_list[extraction_index] # This is the pokeball entity. materia_type = extracting_item.item.caught_entity.materia[0] materia_level = extracting_item.item.caught_entity.materia[1] materia_name = materia_type.capitalize() + ' Materia (lvl ' + str(materia_level) + ')' item_component = Item(use_function=item_functions.materia, type=materia_type, level=materia_level) materia_item = Entity(0, 0, '*', libtcod.white, materia_name, global_variables.get_new_ID(), RenderOrder.ITEM, item=item_component) # Add it to the inventory player.inventory.add_item(materia_item) entities.append(materia_item) # Remove the entity from which it came from the parent entity holding it extracting_item.item.caught_entity = None # Return the pokeball to a "catch" mode instead of release. # TODO: There are now two instances of switching the pokeball from one function to another. Consider moving this inside the Item class. extracting_item.item.swap_messages() extracting_item.item.use_function = item_functions.catch if look: previous_game_state = game_state cursor.x, cursor.y = player.x, player.y game_state = GameStates.LOOK """ List of possible results from actions taken by the player. """ for player_turn_result in player_turn_results: message = player_turn_result.get('message') dead_entity = player_turn_result.get('dead') item_added = player_turn_result.get('item_added') item_consumed = player_turn_result.get('consumed') item_dropped = player_turn_result.get('item_dropped') targeting = player_turn_result.get('targeting') targeting_cancelled = player_turn_result.get('targeting_cancelled') thrown = player_turn_result.get('thrown') # This item was thrown. Returns the item entity. catch = player_turn_result.get('catch') # This item catches pokemon. Returns the caught entity. release = player_turn_result.get('release') # This item released pokemon. Returns the released entity. xp = player_turn_result.get('xp') if message: message_log.add_message(message) if dead_entity: if dead_entity == player: message, game_state = kill_player(dead_entity) else: message = kill_monster(dead_entity) message_log.add_message(message) if item_added: entities.remove(item_added) if item_consumed: # Removed from inventory in inventory.py pass if item_dropped: entities.append(item_dropped) if targeting: previous_game_state = GameStates.PLAYERS_TURN game_state = GameStates.TARGETING cursor.x, cursor.y = player.x, player.y targeting_item = targeting message_log.add_message(targeting_item.item.targeting_message) if targeting_cancelled: game_state = previous_game_state message_log.add_message(Message('Targeting cancelled')) if thrown: # Removed from inventory in inventory.py thrown.x, thrown.y = player_turn_result.get('target_xy') entities.append(thrown) game_state = GameStates.PLAYERS_TURN if catch: entities.remove(catch) if release: release.x, release.y = player_turn_result.get('target_xy') entities.append(release) priority_queue.put(release.fighter.speed, release.ID) if xp: leveled_up = player.level.add_xp(xp) message_log.add_message(Message('You gain {0} experience points.'.format(xp))) if leveled_up: message_log.add_message(Message( 'Your battle skills grow stronger! You reached level {0}'.format(player.level.current_level) + '!', libtcod.yellow)) previous_game_state = game_state game_state = GameStates.LEVEL_UP if game_state == GameStates.ENEMY_TURN and enemy: enemy_turn_results = enemy.ai.take_turn(player, fov_map, game_map, entities) for enemy_turn_result in enemy_turn_results: message = enemy_turn_result.get('message') dead_entity = enemy_turn_result.get('dead') if message: message_log.add_message(message) if dead_entity: if dead_entity == player: message, game_state = kill_player(dead_entity) else: message = kill_monster(dead_entity) message_log.add_message(message) if game_state == GameStates.PLAYER_DEAD: break if game_state == GameStates.PLAYER_DEAD: break elif not enemy.ai == None: priority_queue.put(enemy.fighter.speed, enemy.ID)
def main(): # Defaults for screen screen_width = 80 screen_height = 50 map_width = 80 map_height = 45 colors = { 'dark_wall': tcod.Color(0, 0, 100), 'dark_ground': tcod.Color(50, 50, 150) } player = Entity(int(screen_width / 2), int(screen_height / 2), '@', tcod.white) npc = Entity(int(screen_width / 2 - 5), int(screen_height / 2), '@', tcod.white) entities = [npc, player] # Describes which font to use tcod.console_set_custom_font( 'arial10x10.png', tcod.FONT_TYPE_GREYSCALE | tcod.FONT_LAYOUT_TCOD) # Renders initial screen tcod.console_init_root(screen_width, screen_height, 'TCOD Tutorial Revised', False) con = tcod.console_new(screen_width, screen_height) game_map = GameMap(map_width, map_height) key = tcod.Key() mouse = tcod.Mouse() # Main game loop while not tcod.console_is_window_closed(): tcod.sys_check_for_event(tcod.EVENT_KEY_PRESS, key, mouse) render_all(con, entities, game_map, screen_width, screen_height, colors) tcod.console_flush() clear_all(con, entities) action = handle_keys(key) move = action.get('move') enable_exit = action.get('exit') enable_fullscreen = action.get('fullscreen') if move: dx, dy = move if not game_map.is_blocked(player.x + dx, player.y + dy): player.move(dx, dy) if enable_exit: return True if enable_fullscreen: tcod.console_set_fullscreen(not tcod.console_is_fullscreen())
def play_game(player, entities, game_map, message_log, game_state, con, panel, constants): fov_recompute = True fov_map = initialize_fov(game_map) key = libtcod.Key() mouse = libtcod.Mouse() game_state = GameStates.PLAYERS_TURN previous_game_state = game_state targeting_item = None while not libtcod.console_is_window_closed(): libtcod.sys_check_for_event( libtcod.EVENT_KEY_PRESS | libtcod.EVENT_MOUSE, key, mouse) if fov_recompute: recompute_fov(fov_map, player.x, player.y, constants['fov_radius'], constants['fov_light_walls'], constants['fov_algorithm']) render_all(con, panel, entities, player, game_map, fov_map, fov_recompute, message_log, constants['screen_width'], constants['screen_height'], constants['bar_width'], constants['panel_height'], constants['panel_y'], mouse, constants['colors'], game_state) for_recompute = False libtcod.console_flush() clear_all(con, entities) action = handle_keys(key, game_state) mouse_action = handle_mouse(mouse) move = action.get('move') wait = action.get('wait') pickup = action.get('pickup') show_inventory = action.get('show_inventory') drop_inventory = action.get('drop_inventory') inventory_index = action.get('inventory_index') take_stairs = action.get('take_stairs') level_up = action.get('level_up') show_character_screen = action.get('show_character_screen') exit = action.get('exit') fullscreen = action.get('fullscreen') left_click = mouse_action.get('left_click') right_click = mouse_action.get('right_click') player_turn_results = [] if move and game_state == GameStates.PLAYERS_TURN: dx, dy = move destination_x = player.x + dx destination_y = player.y + dy if not game_map.is_blocked(destination_x, destination_y): target = get_blocking_entities_at_location( entities, destination_x, destination_y) if target: attack_results = player.fighter.attack(target) player_turn_results.extend(attack_results) else: player.move(dx, dy) fov_recompute = True game_state = GameStates.ENEMY_TURN elif wait: game_state = GameStates.ENEMY_TURN elif pickup and game_state == GameStates.PLAYERS_TURN: for entity in entities: if entity.item and entity.x == player.x and entity.y == player.y: pickup_results = player.inventory.add_item(entity) player_turn_results.extend(pickup_results) break else: message_log.add_message( Message('There is nothing here to pick up.', libtcod.yellow)) if show_inventory: previous_game_state = game_state game_state = GameStates.SHOW_INVENTORY if drop_inventory: previous_game_state = game_state game_state = GameStates.DROP_INVENTORY if inventory_index is not None \ and previous_game_state != GameStates.PLAYER_DEAD \ and inventory_index < len(player.inventory.items): item = player.inventory.items[inventory_index] if game_state == GameStates.SHOW_INVENTORY: player_turn_results.extend( player.inventory.use(item, entities=entities, fov_map=fov_map)) elif game_state == GameStates.DROP_INVENTORY: player_turn_results.extend(player.inventory.drop_item(item)) if take_stairs and game_state == GameStates.PLAYERS_TURN: for entity in entities: if entity.stairs and entity.x == player.x and entity.y == player.y: entities = game_map.next_floor(player, message_log, constants) fov_map = initialize_fov(game_map) fov_recompute = True libtcod.console_clear(con) break else: message_log.add_message( Message('There are no stairs here.', libtcod.yellow)) if level_up: if level_up == 'hp': player.fighter.base_max_hp += 20 player.fighter.hp += 20 elif level_up == 'str': player.fighter.base_power += 1 elif level_up == 'def': player.fighter.base_defense += 1 game_state = previous_game_state if show_character_screen: previous_game_state = game_state game_state = GameStates.CHARACTER_SCREEN if game_state == GameStates.TARGETING: if left_click: target_x, target_y = left_click item_use_results = player.inventory.use(targeting_item, entities=entities, fov_map=fov_map, target_x=target_x, target_y=target_y) player_turn_results.extend(item_use_results) elif right_click: player_turn_results.append({'targeting_cancelled': True}) if exit: if game_state in (GameStates.SHOW_INVENTORY, GameStates.DROP_INVENTORY, GameStates.CHARACTER_SCREEN): game_state = previous_game_state elif game_state == GameStates.TARGETING: player_turn_results.append({'targeting_cancelled': True}) else: save_game(player, entities, game_map, message_log, game_state) return True if fullscreen: libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen()) for player_turn_result in player_turn_results: message = player_turn_result.get('message') dead_entity = player_turn_result.get('dead') item_added = player_turn_result.get('item_added') item_consumed = player_turn_result.get('consumed') item_dropped = player_turn_result.get('item_dropped') equip = player_turn_result.get('equip') targeting = player_turn_result.get('targeting') targeting_cancelled = player_turn_result.get('targeting_cancelled') xp = player_turn_result.get('xp') if message: message_log.add_message(message) if dead_entity: if dead_entity == player: message, game_state = kill_player(dead_entity) else: message = kill_monster(dead_entity) message_log.add_message(message) if item_added: entities.remove(item_added) game_state = GameStates.ENEMY_TURN if item_consumed: game_state = GameStates.ENEMY_TURN if item_dropped: entities.append(item_dropped) game_state = GameStates.ENEMY_TURN if equip: equip_results = player.equipment.toggle_equip(equip) for equip_result in equip_results: equipped = equip_result.get('equipped') dequipped = equip_result.get('dequipped') if equipped: message_log.add_message( Message(f'You equipped the {equipped.name}')) if dequipped: message_log.add_message( Message(f'You dequipped the {dequipped.name}')) game_state = GameStates.ENEMY_TURN if targeting: previous_game_state = GameStates.PLAYERS_TURN game_state = GameStates.TARGETING targeting_item = targeting message_log.add_message(targeting_item.item.targeting_message) if targeting_cancelled: game_state = previous_game_state message_log.add_message(Message('Targeting cancelled')) if xp: leveled_up = player.level.add_xp(xp) message_log.add_message( Message(f'You gain {xp} experience points')) if leveled_up: message_log.add_message( Message( f'Your battle skills grow stronger! You reached level {player.level.current_level}!', libtcod.yellow)) previous_game_state = game_state game_state = GameStates.LEVEL_UP if game_state == GameStates.ENEMY_TURN: for entity in entities: if entity.ai: enemy_turn_results = entity.ai.take_turn( player, fov_map, game_map, entities) for enemy_turn_result in enemy_turn_results: message = enemy_turn_result.get('message') dead_entity = enemy_turn_result.get('dead') if message: message_log.add_message(message) if dead_entity: if dead_entity == player: message, game_state = kill_player(dead_entity) else: message = kill_monster(dead_entity) message_log.add_message(message) if game_state == GameStates.PLAYER_DEAD: break if game_state == GameStates.PLAYER_DEAD: break else: game_state = GameStates.PLAYERS_TURN
def main(): screen_width = 80 screen_height = 50 map_width = 80 map_height = 45 room_max_size = 10 room_min_size = 4 max_rooms = 30 fov_algorithm = 0 fov_light_walls = True fov_radius = 10 max_monsters_per_room = 3 colors = { 'dark_wall': tcod.Color(0, 0, 100), 'dark_ground': tcod.Color(50, 50, 150), 'light_wall': tcod.Color(130, 110, 50), 'light_ground': tcod.Color(200, 180, 50) } fighter_component = Fighter(hp=30, defense=2, power=5) player = Entity(0, 0, '@', tcod.white, 'Player', blocks=True, fighter=fighter_component) entities = [player] tcod.console_set_custom_font( '.\\assets\\fonts\\arial10x10.png', tcod.FONT_TYPE_GREYSCALE | tcod.FONT_LAYOUT_TCOD) tcod.console_init_root(screen_width, screen_height, 'libtcod tutorial revised', fullscreen=False) con = tcod.console_new(screen_width, screen_height) game_map = GameMap(map_width, map_height) game_map.make_map(max_rooms, room_min_size, room_max_size, map_width, map_height, player, entities, max_monsters_per_room) fov_recompute = True fov_map = initialize_fov(game_map) key = tcod.Key() mouse = tcod.Mouse() game_state = GameStates.PLAYERS_TURN while not tcod.console_is_window_closed(): tcod.sys_check_for_event(tcod.EVENT_KEY_PRESS, key, mouse) if fov_recompute: recompute_fov(fov_map, player.x, player.y, fov_radius, fov_light_walls, fov_algorithm) render_all(con, entities, game_map, fov_map, fov_recompute, screen_width, screen_height, colors) fov_recompute = False tcod.console_flush() clear_all(con, entities) action = handle_keys(key) move = action.get('move') exit = action.get('exit') fullscreen = action.get('fullscreen') msg = action.get('msg') if move and game_state == GameStates.PLAYERS_TURN: dx, dy = move destination_x = player.x + dx destination_y = player.y + dy if not game_map.is_blocked(destination_x, destination_y): target = get_blocking_entites_at_location( entities, destination_x, destination_y) if target: player.fighter.attack(target) else: player.move(dx, dy) fov_recompute = True game_state = GameStates.ENEMY_TURN print('Enemy Turn!') if exit: print('Thanks for playing!') return True if fullscreen: tcod.console_set_fullscreen(not tcod.console_is_fullscreen()) if game_state == GameStates.ENEMY_TURN: for entity in entities: if entity.ai: entity.ai.take_turn(player, fov_map, game_map, entities) game_state = GameStates.PLAYERS_TURN print('Player Turn!') if msg: tcod.console_print(con, 1, 1, ' ') tcod.console_print(con, 1, 1, msg) print('window was closed')
def main(): screen_width = 80 screen_height = 50 bar_width = 20 panel_height = 7 panel_y = screen_height - panel_height message_x = bar_width + 2 message_width = screen_width - bar_width - 2 message_height = panel_height - 1 # Size of map map_width = 80 map_height = 43 # Variables for rooms room_max_size = 10 room_min_size = 6 max_rooms = 30 fov_algorithm = 0 fov_light_walls = True fov_radius = 10 max_monsters_per_room = 3 max_items_per_room = 2 colors = { 'dark_wall': libtcod.Color(0, 0, 100), 'dark_ground': libtcod.Color(50, 50, 100), 'light_wall': libtcod.Color(130, 110, 50), 'light_ground': libtcod.Color(200, 180, 50) } fighter_component = Fighter(hp=30, defense=2, power=5) inventory_component = Inventory(26) player = Entity(0, 0, '@', libtcod.white, 'Player', blocks=True, render_order=RenderOrder.ACTOR, fighter=fighter_component, inventory=inventory_component) entities = [player] libtcod.console_set_custom_font( 'arial10x10.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD) libtcod.console_init_root(screen_width, screen_height, 'libtcod tutorial revised', False) con = libtcod.console_new(screen_width, screen_height) panel = libtcod.console_new(screen_width, panel_height) game_map = GameMap(map_width, map_height) game_map.make_map(max_rooms, room_min_size, room_max_size, map_width, map_height, player, entities, max_monsters_per_room, max_items_per_room) fov_recompute = True fov_map = initialize_fov(game_map) message_log = MessageLog(message_x, message_width, message_height) key = libtcod.Key() mouse = libtcod.Mouse() game_state = GameStates.PLAYERS_TURN previous_game_state = game_state while not libtcod.console_is_window_closed(): libtcod.sys_check_for_event( libtcod.EVENT_KEY_PRESS | libtcod.EVENT_MOUSE, key, mouse) if fov_recompute: recompute_fov(fov_map, player.x, player.y, fov_radius, fov_light_walls, fov_algorithm) render_all(con, panel, entities, player, game_map, fov_map, fov_recompute, message_log, screen_width, screen_height, bar_width, panel_height, panel_y, mouse, colors, game_state) fov_recompute = False libtcod.console_flush() clear_all(con, entities) action = handle_keys(key, game_state) move = action.get('move') pickup = action.get('pickup') show_inventory = action.get('show_inventory') drop_inventory = action.get('drop_inventory') inventory_index = action.get('inventory_index') exit = action.get('exit') fullscreen = action.get('fullscreen') player_turn_results = [] if move and game_state == GameStates.PLAYERS_TURN: dx, dy = move destination_x = player.x + dx destination_y = player.y + dy if not game_map.is_blocked(destination_x, destination_y): target = get_blocking_entities_at_location( entities, destination_x, destination_y) if target: player.fighter.attack(target) attack_results = player.fighter.attack(target) player_turn_results.extend(attack_results) else: player.move(dx, dy) fov_recompute = True game_state = GameStates.ENEMY_TURN elif pickup and game_state == GameStates.PLAYERS_TURN: for entity in entities: if entity.item and entity.x == player.x and entity.y == player.y: pickup_results = player.inventory.add_item(entity) player_turn_results.extend(pickup_results) break else: message_log.add_message( Message('There is nothing here to pick up.', libtcod.yellow)) if show_inventory: previous_game_state = game_state game_state = GameStates.SHOW_INVENTORY if drop_inventory: previous_game_state = game_state game_state = GameStates.DROP_INVENTORY if inventory_index is not None and previous_game_state != GameStates.PLAYER_DEAD and inventory_index < len( player.inventory.items): item = player.inventory.items[inventory_index] if game_state == GameStates.SHOW_INVENTORY: player_turn_results.extend( player.inventory.use(item, entities=entities, fov_map=fov_map)) elif game_state == GameStates.DROP_INVENTORY: player_turn_results.extend(player.inventory.drop_item(item)) if exit: if game_state in (GameStates.SHOW_INVENTORY, GameStates.DROP_INVENTORY): game_state == previous_game_state else: return True if fullscreen: libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen()) for player_turn_result in player_turn_results: message = player_turn_result.get('message') dead_entity = player_turn_result.get('dead') item_added = player_turn_result.get('item_added') item_consumed = player_turn_result.get('consumed') item_dropped = player_turn_result.get('item_dropped') if message: message_log.add_message(message) if dead_entity: if dead_entity == player: message, game_state = kill_player(dead_entity) else: message = kill_monster(dead_entity) message_log.add_message(message) if item_added: entities.remove(item_added) game_state = GameStates.ENEMY_TURN if item_consumed: game_state = GameStates.ENEMY_TURN if item_dropped: entities.append(item_dropped) game_state = GameStates.ENEMY_TURN if game_state == GameStates.ENEMY_TURN: for entity in entities: if entity.ai: enemy_turn_results = entity.ai.take_turn( player, fov_map, game_map, entities) for enemy_turn_result in enemy_turn_results: message = enemy_turn_result.get('message') dead_entity = enemy_turn_result.get('dead') if message: message_log.add_message(message) if dead_entity: if dead_entity == player: message, game_state = kill + player( dead_entity) else: message = kill_monster(dead_entity) message_log.add_message(message) if game_state == GameStates.PLAYER_DEAD: break if game_state == GameStates.PLAYER_DEAD: break else: game_state = GameStates.PLAYERS_TURN
def main(): # defines screen size screen_width = 80 screen_height = 50 # place player in center player_x = int(screen_width / 2) player_y = int(screen_height / 2) # use font from file we're reading libtcod.console_set_custom_font('arial10x10.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD) # create screen form width and height variable and title libtcod.console_init_root(screen_width, screen_height, 'libtcod tutorial', False) con = libtcod.console_new(screen_width, screen_height) # allow key board and mouse input # take key and mouse input from key = libtcod.Key() mouse = libtcod.Mouse() # game loop while not libtcod.console_is_window_closed(): # add color to @ symbol assigned to 0 # check for inputs libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS, key, mouse) libtcod.console_set_default_foreground(con, libtcod.white) libtcod.console_put_char(con, player_x, player_y, '@', libtcod.BKGND_NONE) libtcod.console_blit(con, 0, 0, screen_width, screen_height, 0, 0, 0) # libtcod.console_set_default_foreground(0, libtcod.white) # console 0,x coord, y coord, and @ symbol, background none # libtcod.console_put_char(0,1,1,'@',libtcod.BKGND_NONE) #now modified with new coordinates # libtcod.console_put_char(0, player_x, player_y, '@', libtcod.BKGND_NONE) # present everything libtcod.console_flush() libtcod.console_put_char(con, player_x, player_y, ' ', libtcod.BKGND_NONE) # allow get out of program by pressing esc # key = libtcod.console_check_for_keypress() # grab from handle_key action = handle_keys(key) # if key for move move = action.get('move') # if key for exit get_out_exit = action.get('exit') # if key for fullscreen fullscreen = action.get('fullscreen') # if move is grabbed if move: # grab x and y coordinate dx, dy = move # and adjust player_x += dx player_y += dy # if key.vk == libtcod.KEY_ESCAPE: # new exit function if get_out_exit: return True # fullscreen if if fullscreen: libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen())
def main(): screen_width = 80 screen_height = 50 bar_width = 20 panel_height = 7 panel_y = screen_height - panel_height message_x = bar_width + 2 message_width = screen_width - bar_width - 2 message_height = panel_height - 1 map_width = 80 map_height = 43 room_max_size = 30 room_min_size = 6 max_rooms = 30 fov_algorithm = 2 fov_light_walls = True fov_radius = 10 max_monsters_per_room = 3 max_items_per_room = 2 colors = { 'dark_wall': libtcod.Color(0, 0, 100), 'dark_ground': libtcod.Color(50, 50, 150), 'light_wall': libtcod.Color(130, 110, 50), 'light_ground': libtcod.Color(200, 180, 50) } til = Tile(False) fighter_component = Fighter(30, defense=2, power=5) player = Entity(0, 0, '@', libtcod.white, 'Player', blocks=True, fighter=fighter_component, render_order=RenderOrder.ACTOR) entities = [player] libtcod.console_set_custom_font( 'arial10x10.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD) libtcod.console_init_root(screen_width, screen_height, 'libtcod tutorial revised', False) con = libtcod.console_new(screen_width, screen_height) panel = libtcod.console_new(screen_width, panel_height) map = GameMap(map_width, map_height) map.make_map(max_rooms, room_min_size, room_max_size, map_width, map_height, player, entities, max_monsters_per_room, max_items_per_room) fov_recompute = True fov_map = initialize_fov(map) message_log = MessageLog(message_x, message_width, message_height) key = libtcod.Key() mouse = libtcod.Mouse() game_state = GameStates.PLAYERS_TURN while not libtcod.console_is_window_closed(): libtcod.sys_check_for_event( libtcod.EVENT_KEY_PRESS | libtcod.EVENT_MOUSE, key, mouse) if fov_recompute: recompute_fov(fov_map, player.x, player.y, fov_radius, fov_light_walls, fov_algorithm) entities_in_render_order = sorted(entities, key=lambda x: x.render_order.value) render_all(con, panel, entities_in_render_order, player, map, fov_map, fov_recompute, message_log, screen_width, screen_height, bar_width, panel_height, panel_y, mouse, colors) fov_recompute = False libtcod.console_flush() clear_all(con, entities) action = handle_keys(key) move = action.get('move') exit = action.get('exit') fullscreen = action.get('fullscreen') player_turn_results = [] if move and game_state == GameStates.PLAYERS_TURN: dx, dy = move destination_x = player.x + dx destination_y = player.y + dy if not map.is_blocked(destination_x, destination_y): target = get_blocking_entities_at_location( entities, destination_x, destination_y) if target: attack_results = player.fighter.attack(target) player_turn_results.extend(attack_results) else: player.move(dx, dy) fov_recompute = True game_state = GameStates.ENEMY_TURN else: message_log.add_message(Message('stuck')) if fullscreen: libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen()) if exit: return True for player_turn_result in player_turn_results: message = player_turn_result.get('message') dead_entity = player_turn_result.get('dead') if message: message_log.add_message(message) if dead_entity: if dead_entity == player: message, game_state = kill_player(dead_entity) else: message = kill_monster(dead_entity) message_log.add_message(message) if game_state == GameStates.ENEMY_TURN: for entity in entities_in_render_order: if entity != player and entity.ai != None: enemy_turn_results = entity.ai.take_turn( fov_map, player, map, entities) for enemy_turn_result in enemy_turn_results: message = enemy_turn_result.get('message') dead_entity = enemy_turn_result.get('dead') if message: message_log.add_message(message) if dead_entity: if dead_entity == player: message, game_state = kill_player(dead_entity) else: message = kill_monster(dead_entity) message_log.add_message(message) if game_state == GameStates.PLAYER_DEAD: break game_state = GameStates.PLAYERS_TURN
def play_game(player, entities, game_map, message_log, game_state, con, panel, constants): fov_recompute = True fov_map = initialize_fov(game_map) key = libtcod.Key() mouse = libtcod.Mouse() game_state = GameStates.PLAYERS_TURN previous_game_state = game_state targeting_item = None while not libtcod.console_is_window_closed(): libtcod.sys_check_for_event( libtcod.EVENT_KEY_PRESS | libtcod.EVENT_MOUSE, key, mouse) if fov_recompute: recompute_fov(fov_map, player.x, player.y, constants['fov_radius'], constants['fov_light_walls'], constants['fov_algorithm']) render_all(con, panel, entities, player, game_map, fov_map, fov_recompute, message_log, constants['screen_width'], constants['screen_height'], constants['bar_width'], constants['panel_height'], constants['panel_y'], mouse, constants['colors'], game_state) fov_recompute = False libtcod.console_flush() clear_all(con, entities) action = handle_keys(key, game_state) mouse_action = handle_mouse(mouse) move = action.get('move') pickup = action.get('pickup') show_inventory = action.get('show_inventory') drop_inventory = action.get('drop_inventory') inventory_index = action.get('inventory_index') exit = action.get('exit') fullscreen = action.get('fullscreen') left_click = mouse_action.get('left_click') right_click = mouse_action.get('right_click') player_turn_results = [] if move and game_state == GameStates.PLAYERS_TURN: dx, dy = move destination_x = player.x + dx destination_y = player.y + dy if not game_map.is_blocked(destination_x, destination_y): target = get_blocking_entities_at_location( entities, destination_x, destination_y) if target and target.z == player.z: player.push(target, entities, fov_map, game_map) else: player.move(dx, dy, fov_map) fov_recompute = True game_state = GameStates.ENEMY_TURN elif pickup and game_state == GameStates.PLAYERS_TURN: for entity in entities: if entity.item and entity.x == player.x and entity.y == player.y: pickup_results = player.inventory.add_item(entity) player_turn_results.extend(pickup_results) break else: message_log.add_message( Message('There is nothing here to pick up.', libtcod.yellow)) if exit: if game_state in (GameStates.SHOW_INVENTORY, GameStates.DROP_INVENTORY): game_state = previous_game_state elif game_state == GameStates.TARGETING: player_turn_results.append({'targeting_cancelled': True}) else: save_game(player, entities, game_map, message_log, game_state) return True if fullscreen: libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen()) for player_turn_result in player_turn_results: message = player_turn_result.get('message') dead_entity = player_turn_result.get('dead') item_added = player_turn_result.get('item_added') item_consumed = player_turn_result.get('consumed') item_dropped = player_turn_result.get('item_dropped') targeting = player_turn_result.get('targeting') targeting_cancelled = player_turn_result.get('targeting_cancelled') if message: message_log.add_message(message) if dead_entity: if dead_entity == player: message, game_state = kill_player(dead_entity) else: message = kill_monster(dead_entity) message_log.add_message(message) if game_state == GameStates.ENEMY_TURN: player.depth += 10 if player.depth % 30 == 0: game_map.place_entities(0, entities, 2, 0) for entity in entities: if entity.name == 'Boulder': if entity.z < 0: entities.remove(entity) else: entity.z -= 1 elif entity.ai: enemy_turn_results = entity.ai.take_turn( player, fov_map, game_map, entities) if entity.z > 0: guyintheway = get_blocking_entities_at_location( entities, entity.x, entity.y) if guyintheway: entity.x = entity.x + randint(-1, 1) entity.y = entity.y + randint(-1, 1) if entity.x == 0 and entity.y == 0: entity.x = entity.x + randint(-1, 1) entity.y = entity.y + randint(-1, 1) entity.z -= 1 target = get_blocking_entities_at_location( entities, entity.x, entity.y) if target: if not target == entity: if target.z == 0: entities.remove(target) else: target.z -= 1 for enemy_turn_result in enemy_turn_results: message = enemy_turn_result.get('message') dead_entity = enemy_turn_result.get('dead') if message: message_log.add_message(message) if dead_entity: if dead_entity == player: message, game_state = kill_player(dead_entity) else: message = kill_monster(dead_entity) message_log.add_message(message) if game_state == GameStates.PLAYER_DEAD: break if game_state == GameStates.PLAYER_DEAD: libtcod.console_clear(0) break else: game_state = GameStates.PLAYERS_TURN