def main(): constants = get_constants() # libtcod.console_set_custom_font('TiledFont.png', libtcod.FONT_TYPE_GRAYSCALE | libtcod.FONT_LAYOUT_TCOD, 32, 10) # setting 10 to 8 will prove that the bitmaps are loaded init_fontmap('sprites1') libtcod.console_init_root(constants['screen_width'], constants['screen_height'], constants['window_title'], False, libtcod.RENDERER_SDL2) load_customfont() con = libtcod.console.Console(constants['screen_width'], constants['screen_height']) # con = libtcod.console_new(constants['screen_width'], constants['screen_height']) panel = libtcod.console.Console(constants['screen_width'], constants['panel_height']) player = None entities = [] game_map = None message_log = None game_state = None show_main_menu = True show_load_error_message = False main_menu_background_image = libtcod.image_load('zombie_splash.png') key = libtcod.Key() mouse = libtcod.Mouse() while not libtcod.console_is_window_closed(): libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS | libtcod.EVENT_MOUSE, key, mouse) if show_main_menu: main_menu(con, main_menu_background_image, constants['screen_width'], constants['screen_height']) if show_load_error_message: message_box(con, 'No save game to load', 50, constants['screen_width'], constants['screen_height']) libtcod.console_flush() action = handle_main_menu(key) new_game = action.get('new_game') load_saved_game = action.get('load_game') exit_game = action.get('exit') if show_load_error_message and (new_game or load_saved_game or exit_game): show_load_error_message = False elif new_game: player, entities, game_map, message_log, game_state = get_game_variables(constants) game_state = GameStates.PLAYERS_TURN show_main_menu = False elif load_saved_game: try: player, entities, game_map, message_log, game_state = load_game() show_main_menu = False except FileNotFoundError: show_load_error_message = True elif exit_game: break else: # libtcod.console_clear(con) con.clear() play_game(player, entities, game_map, message_log, game_state, con, panel, constants) show_main_menu = True
def main(): """Main function for the game.""" constants = get_constants() # setup Font font_path = '/home/bobbias/roguelike/arial10x10.png' font_flags = tcod.FONT_TYPE_GREYSCALE | tcod.FONT_LAYOUT_TCOD tcod.console_set_custom_font(font_path, font_flags) # init screen # window_title = 'Python 3 libtcod tutorial' con = tcod.console_init_root(constants['screen_width'], constants['screen_height'], order='F', renderer=tcod.RENDERER_OPENGL2) panel = tcod.console_new(constants['screen_width'], constants['panel_height']) player = None entities = [] game_map = None message_log = None game_state = None show_main_menu = True show_load_error_message = False main_menu_background_imgage = tcod.image_load('menu_background.png') key = tcod.Key() mouse = tcod.Mouse() while not tcod.console_is_window_closed(): tcod.sys_check_for_event(tcod.EVENT_KEY_PRESS | tcod.EVENT_MOUSE, key, mouse) if show_main_menu: main_menu(con, main_menu_background_imgage, constants['screen_width'], constants['screen_height']) if show_load_error_message: message_box(con, 'No save game to load!', 50, constants['screen_width'], constants['screen_height']) tcod.console_flush() action = handle_main_menu(key) new_game = action.get('new_game') load_saved_game = action.get('load_saved_game') exit_game = action.get('exit') if show_load_error_message and (new_game or load_saved_game or exit_game): show_load_error_message = False elif new_game: player, entities, game_map, message_log, game_state = get_game_variables( constants) game_state = GameStates.PLAYERS_TURN show_main_menu = False elif load_saved_game: try: player, entities, game_map, message_log, game_state = load_game( ) show_main_menu = False except FileNotFoundError: show_load_error_message = True elif exit_game: break else: tcod.console_clear(con) play_game(player, entities, game_map, message_log, game_state, con, panel, constants) show_main_menu = True
def main(): constants = get_constants() libtcod.console_set_custom_font('tennisOut.png', libtcod.FONT_LAYOUT_ASCII_INROW, 32, 10) libtcod.console_init_root(constants['screen_width'], constants['screen_height'], constants['window_title'], False) load_customfont() con = libtcod.console.Console(constants['screen_width'], constants['screen_height']) panel = libtcod.console.Console(constants['screen_width'], constants['panel_height']) show_main_menu = True show_load_error_message = False key = libtcod.Key() mouse = libtcod.Mouse() while not libtcod.console_is_window_closed(): libtcod.sys_check_for_event( libtcod.EVENT_KEY_PRESS | libtcod.EVENT_MOUSE, key, mouse) if show_main_menu: main_menu(con, constants['screen_width'], constants['screen_height']) if show_load_error_message: message_box(con, 'No save game to load', 50, constants['screen_width'], constants['screen_height']) libtcod.console_flush() action = handle_main_menu(key) new_game = action.get('new_game') load_saved_game = action.get('load_game') exit_game = action.get('exit') if show_load_error_message and (new_game or load_saved_game or exit_game): show_load_error_message = False elif new_game: show_main_menu = False elif load_saved_game: try: player, entities, game_map, message_log, game_state = load_game( ) play_game(player, entities, game_map, message_log, con, panel, constants) show_main_menu = False except FileNotFoundError: show_load_error_message = True elif exit_game: break else: libtcod.console_clear(con) character_selection(constants, con, panel) show_main_menu = True
def main(): constants = get_constants() tdl.set_font('arial10x10.png', greyscale=True, altLayout=True) root_console = tdl.init(constants['screen_width'], constants['screen_height'], constants['window_title']) con = tdl.Console(constants['screen_width'], constants['screen_height']) panel = tdl.Console(constants['screen_width'], constants['panel_height']) player = None store = None entities = [] game_map = None message_log = None game_state = None show_main_menu = True show_load_error_message = False main_menu_background_image = image_load('menu_background1.png') while not tdl.event.is_window_closed(): for event in tdl.event.get(): if event.type == 'KEYDOWN': user_input = event break else: user_input = None if show_main_menu: main_menu(con, root_console, main_menu_background_image, constants['screen_width'], constants['screen_height'], constants['colors']) if show_load_error_message: message_box(con, root_console, 'No save game to load', 50, constants['screen_width'], constants['screen_height']) tdl.flush() action = handle_main_menu(user_input) new_game = action.get('new_game') load_saved_game = action.get('load_game') exit_game = action.get('exit') if show_load_error_message and (new_game or load_saved_game or exit_game): show_load_error_message = False elif new_game: player, entities, game_map, message_log, game_state = get_game_variables(constants) game_state = GameStates.PLAYERS_TURN show_main_menu = False elif load_saved_game: try: player, entities, game_map, message_log, game_state = load_game() show_main_menu = False except FileNotFoundError: show_load_error_message = True elif exit_game: break else: root_console.clear() con.clear() panel.clear() play_game(player, entities, game_map, message_log, game_state, root_console, con, panel, constants) show_main_menu = True
def main(): constants = get_constants() libtcod.sys_set_fps(30) # Animate anim_time = libtcod.sys_elapsed_milli() anim_frame = 0 libtcod.console_set_custom_font( 'sprite-font.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD, 32, 48) libtcod.console_init_root(constants['screen_width'], constants['screen_height'], constants['window_title'], False) libtcod.console_set_default_background(0, colors.get('dark')) load_customfont() con = libtcod.console_new(constants['map_width'] * 3, constants['map_height'] * 2) panel = libtcod.console_new(constants['panel_width'], constants['screen_height']) tooltip = libtcod.console_new(constants['screen_width'], 1) messages_pane = libtcod.console_new(constants['message_width'], 1000) inventory_pane = libtcod.console_new(constants['message_width'], 40) player = None entities = [] game_map = None message_log = None game_state = None show_main_menu = True show_load_error_message = False main_menu_background_image_0 = libtcod.image_load('menu_background_0.png') main_menu_background_image_1 = libtcod.image_load('menu_background_1.png') main_menu_background_image_2 = libtcod.image_load('menu_background_2.png') main_menu_background_image_3 = libtcod.image_load('menu_background_3.png') log_scroll = 0 inv_scroll = 0 key = libtcod.Key() mouse = libtcod.Mouse() while not libtcod.console_is_window_closed(): anim_frame, anim_time = animation(anim_frame, anim_time) if anim_frame == 0: main_menu_background_image = main_menu_background_image_0 elif anim_frame == 1: main_menu_background_image = main_menu_background_image_1 elif anim_frame == 2: main_menu_background_image = main_menu_background_image_2 else: main_menu_background_image = main_menu_background_image_3 libtcod.sys_check_for_event( libtcod.EVENT_KEY_PRESS | libtcod.EVENT_MOUSE, key, mouse) if show_main_menu: main_menu(con, main_menu_background_image, constants['screen_width'], constants['screen_height'], constants['window_title']) if show_load_error_message: message_box(con, 'No save game to load', 50, constants['screen_width'], constants['screen_height']) libtcod.console_flush() action = handle_main_menu(key) new_game = action.get('new_game') load_saved_game = action.get('load_game') exit_game = action.get('exit') if show_load_error_message and (new_game or load_saved_game or exit_game): show_load_error_message = False elif new_game: player, entities, game_map, message_log, game_state = get_game_variables( constants) game_state = GameStates.PLAYERS_TURN show_main_menu = False elif load_saved_game: try: player, entities, game_map, message_log, game_state = load_game( ) show_main_menu = False except FileNotFoundError: show_load_error_message = True elif exit_game: break else: libtcod.console_clear(con) cam_x, cam_y = update_cam(player, constants) log_height, inv_height = update_panels_heights( player, constants['panel_height']) log_scroll = 0 inv_scroll = 0 inv_selected = 0 play_game(player, entities, game_map, message_log, con, panel, tooltip, messages_pane, inventory_pane, constants, cam_x, cam_y, anim_frame, anim_time, log_scroll, log_height, inv_scroll, inv_height, inv_selected) show_main_menu = True
def main(): ''' Loads and initializes all it needs. Starts game ''' constants = get_constants() # uses custom font image libtcod.console_set_custom_font("./art/terminal8x8_gs_ro.png", libtcod.FONT_LAYOUT_ASCII_INROW) libtcod.console_init_root(constants['screen_width'], constants['screen_height'], constants['window_title'], False) con = libtcod.console_new(constants['screen_width'], constants['screen_height']) panel = libtcod.console_new(constants['screen_width'], constants['panel_height']) player = None entities = [] game_map = None message_log = None game_state = None show_main_menu = True show_load_error_message = False # uses custom background image main_menu_background_image = libtcod.image_load('./art/background.png') key = libtcod.Key() mouse = libtcod.Mouse() while not libtcod.console_is_window_closed(): libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS | libtcod.EVENT_MOUSE, key, mouse) if show_main_menu: main_menu(con, main_menu_background_image, constants['screen_width'], constants['screen_height']) if show_load_error_message: message_box(con, 'No save game to load', 50, constants['screen_width'], constants['screen_height']) libtcod.console_flush() action = handle_main_menu(key) new_game = action.get('new_game') load_saved_game = action.get('load_game') rules_menu = action.get('rules_menu') exit_game = action.get('exit') fullscreen = action.get('fullscreen') # toggle fullscreen if fullscreen: libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen()) if show_load_error_message and (new_game or load_saved_game or exit_game): show_load_error_message = False elif new_game: player, entities, game_map, message_log, game_state = \ get_game_variables(constants) game_state = GameStates.PLAYERS_TURN show_main_menu = False elif load_saved_game: try: player, entities, game_map, message_log, game_state = load_game() show_main_menu = False except FileNotFoundError: show_load_error_message = True elif rules_menu: show_rules() elif exit_game: break else: libtcod.console_clear(con) play_game(player, entities, game_map, message_log, game_state, con, panel, constants) show_main_menu = True
def main(): constants = get_constants() libtcod.console_set_custom_font('img/Tocky-square-10x10.png', libtcod.FONT_LAYOUT_ASCII_INROW) libtcod.console_init_root(constants['screen_width'], constants['screen_height'], constants['window_title'], False) con = libtcod.console_new(constants['screen_width'], constants['screen_height']) panel = libtcod.console_new(constants['screen_width'], constants['panel_height']) panel_bg = libtcod.console_new(constants['screen_width'], constants['panel_height']) info = libtcod.console_new(constants['info_width'], constants['screen_height']) info_bg = libtcod.console_new(constants['info_width'], constants['screen_height']) player_con = libtcod.console_new(constants['player_con_width'], constants['player_con_x']) player_con_bg = libtcod.console_new(constants['player_con_width'], constants['player_con_x']) player = None entities = [] game_map = None message_log = None game_state = None show_main_menu = True show_character_select = False show_load_error_message = False main_menu_background_image = libtcod.image_load('img/castle.png') character_select_background_image = libtcod.image_load('img/castle.png') key = libtcod.Key() mouse = libtcod.Mouse() while not libtcod.console_is_window_closed(): libtcod.sys_check_for_event( libtcod.EVENT_KEY_PRESS | libtcod.EVENT_MOUSE, key, mouse) if show_main_menu and not show_character_select: main_menu(con, main_menu_background_image, constants['screen_width'], constants['screen_height']) if show_load_error_message: message_box(con, 'No Save Game to Load', 50, constants['screen_width'], constants['screen_height']) libtcod.console_flush() action = handle_main_menu(key) new_game = action.get('new_game') load_saved_game = action.get('load_game') exit_game = action.get('exit') if show_load_error_message and (new_game or load_saved_game or exit_game): show_load_error_message = False elif new_game: show_character_select = True show_main_menu = False elif load_saved_game: try: player, entities, game_map, message_log, game_state, permanent_cooldown_counter = load_game( ) show_main_menu = False except FileNotFoundError: show_load_error_message = True elif exit_game: break elif show_character_select and not show_main_menu: character_select_menu(con, character_select_background_image, constants['screen_width'], constants['screen_height']) libtcod.console_flush() action = handle_character_select(key) thief = action.get('thief') brute = action.get('brute') occultist = action.get('occultist') if thief: player, entities, game_map, message_log, game_state, permanent_cooldown_counter = initialize_game( constants, "thief") game_state = GameStates.PLAYERS_TURN show_character_select = False elif brute: player, entities, game_map, message_log, game_state, permanent_cooldown_counter = initialize_game( constants, "brute") player.fighter.heal( player.inventory.items[0][0].equippable.max_hp_bonus) game_state = GameStates.PLAYERS_TURN show_character_select = False elif occultist: player, entities, game_map, message_log, game_state, permanent_cooldown_counter = initialize_game( constants, "occultist") game_state = GameStates.PLAYERS_TURN show_character_select = False elif not (show_main_menu and show_character_select): libtcod.console_clear(con) play_game(player, entities, game_map, message_log, game_state, con, panel, panel_bg, info, info_bg, player_con, player_con_bg, constants, permanent_cooldown_counter) show_main_menu = True
def main(): constants = get_constants() libtcod.console_set_custom_font( 'arial10x10.png', libtcod.FONT_TYPE_GRAYSCALE | libtcod.FONT_LAYOUT_TCOD) libtcod.console_init_root(constants['screen_width'], constants['screen_height'], constants['window_title'], fullscreen=False, renderer=libtcod.RENDERER_SDL2, vsync=False) con = libtcod.console.Console(constants['screen_width'], constants['screen_height']) panel = libtcod.console.Console(constants['screen_width'], constants['panel_height']) player = None game_map = None message_log = None game_state = None show_main_menu = True show_load_error_message = False main_menu_background_image = libtcod.image_load('menu_background.png') key = libtcod.Key() mouse = libtcod.Mouse() while not libtcod.console_is_window_closed(): libtcod.sys_check_for_event( libtcod.EVENT_KEY_PRESS | libtcod.EVENT_MOUSE, key, mouse) if show_main_menu: main_menu(con, main_menu_background_image, constants['screen_width'], constants['screen_height']) if show_load_error_message: # This a terrible hack and doing this the "right" way will require tinkering with the render functions message_box(con, 'No save game to load', 20, constants['screen_width'], constants['screen_height'] // 2) libtcod.console_flush() action = handle_main_menu(key) new_game = action.get('new_game') load_saved_game = action.get('load_game') exit_game = action.get('exit') if show_load_error_message and (new_game or load_saved_game or exit_game): show_load_error_message = False elif new_game: player, game_map, message_log, game_state = get_game_variables( constants) game_state = GameStates.PLAYERS_TURN show_main_menu = False elif load_saved_game: try: player, game_map, message_log, game_state = load_game() show_main_menu = False except FileNotFoundError: show_load_error_message = True elif exit_game: break else: con.clear(fg=(191, 0, 0)) play_game(player, game_map, message_log, game_state, con, panel, constants) show_main_menu = True
def main(): constants = get_constants() tdl.set_font("arial10x10.png", greyscale=True, altLayout=True) root_console = tdl.init(constants["screen_width"], constants["screen_height"], constants["window_title"]) con = tdl.Console(constants["screen_width"], constants["screen_height"]) panel = tdl.Console(constants["screen_width"], constants["panel_height"]) player = None entities = [] game_map = None message_log = None game_state = None show_main_menu = True show_load_error_message = False # image for the main menu, change this to something unique later main_menu_background_image = image_load("menu_background.png") while not tdl.event.is_window_closed(): for event in tdl.event.get(): if event.type == "KEYDOWN": user_input = event break else: user_input = None if show_main_menu: main_menu(con, root_console, main_menu_background_image, constants["screen_width"], constants["screen_height"], constants["colors"]) if show_load_error_message: message_box(con, root_console, "No save game to load", 50, constants["screen_width"], constants["screen_height"]) tdl.flush() action = handle_main_menu(user_input) new_game = action.get("new_game") load_saved_game = action.get("load_game") exit_game = action.get("exit") if show_load_error_message and (new_game or load_saved_game or exit_game): show_load_error_message = False elif new_game: player, entities, game_map, message_log, game_state = get_game_variables( constants) game_state = GameStates.PLAYERS_TURN show_main_menu = False elif load_saved_game: try: player, entities, game_map, message_log, game_state = load_game( ) show_main_menu = False except FileNotFoundError: show_load_error_message = True elif exit_game: break else: root_console.clear() con.clear() panel.clear() play_game(player, entities, game_map, message_log, game_state, root_console, con, panel, constants) show_main_menu = True
def main(): constants = get_constants() libtcod.console_set_custom_font( 'rexpaint_cp437_10x10.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_ASCII_INROW) libtcod.console_init_root(constants['screen_width'], constants['screen_height'], title='MVP v0.0') con = libtcod.console_new(constants['screen_width'], constants['screen_height']) panel = libtcod.console_new(constants['screen_width'], constants['screen_height']) status = libtcod.console_new(constants['screen_width'], constants['screen_height']) player = None cursor = None entities = [] game_map = None message_log = None game_state = None turn_state = None event_queue = None player, cursor, entities, game_map, message_log, game_state, turn_state, event_queue = get_game_variables( constants) previous_game_state = game_state key = libtcod.Key() mouse = libtcod.Mouse() fov_recompute = True fov_map = initialize_fov(game_map) 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.location.x, player.location.y, constants['fov_radius'], constants['fov_light_walls'], constants['fov_algorithm']) entities_extended = entities.copy() entities_extended.append(cursor) render_all(con, panel, entities_extended, 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'], status, constants['status_height'], constants['status_width'], constants['status_x'], game_state, turn_state, player, cursor) libtcod.console_flush() clear_all(con, entities_extended) fov_recompute = False """ Handle the Player Actions. """ action = handle_keys(key, game_state) mouse_action = handle_mouse(mouse) if action or mouse_action or mouse.dx or mouse.dy: fov_recompute = True move = action.get('move') # Attempt to move. delta_speed = action.get( 'delta_speed' ) # Changes the max speed reached by the unit durnig autopathing movement. next_turn_phase = action.get( 'next_turn_phase') # Move to the next phase. reset_targets = action.get('reset_targets') # Reset targets. select = action.get( 'select') # A target has been selected via keyboard. show_weapons_menu = action.get( 'show_weapons_menu' ) # Show the weapons menu in order to choose a weapon to fire. weapons_menu_index = action.get( 'weapons_menu_index') # Choose an item from the weapons menu. look = action.get('look') # Enter the LOOK game state. exit = action.get('exit') # Exit whatever screen is open. fullscreen = action.get('fullscreen') # Set game to full screen. left_click = mouse_action.get('left_click') right_click = mouse_action.get('right_click') # TODO: Figure out where this should go. if look and game_state == GameStates.PLAYER_TURN: previous_game_state = game_state game_state = GameStates.LOOK cursor.cursor.turn_on(player) continue """ Handle the turn. """ if game_state == GameStates.ENEMY_TURN or game_state == GameStates.PLAYER_TURN: turn_results = [] active_entity = None # Choose the active entity. if not event_queue.empty(): entity_uuid = event_queue.fetch() for entity in entities: if entity.uuid == entity_uuid: entity.age += 1 active_entity = entity # This phase is not for the active_entity. if turn_state == TurnStates.UPKEEP_PHASE: for entity in entities: if entity.required_game_state == game_state: entity.reset() else: entity.reset(only_action_points=True) event_queue.register_list(entities) turn_state = TurnStates.PRE_MOVEMENT_PHASE # This phase is for the player only. if turn_state == TurnStates.PRE_MOVEMENT_PHASE: if game_state == GameStates.ENEMY_TURN: turn_state = TurnStates.MOVEMENT_PHASE else: # Change player max speed. if delta_speed: turn_results.append( player.propulsion.change_cruising_speed( delta_speed)) # Highlight the legal tiles. if not player.propulsion.legal_tiles: game_map.reset_highlighted() player.propulsion.calculate_movement_range(game_map) game_map.set_highlighted( player.propulsion.legal_tiles['red'], color=libtcod.dark_red) game_map.set_highlighted( player.propulsion.legal_tiles['green'], color=libtcod.light_green) game_map.set_highlighted( player.propulsion.legal_tiles['yellow'], color=libtcod.yellow) fov_recompute = True if player.propulsion.chosen_tile: player.propulsion.choose_tile( player.propulsion.chosen_tile, game_map) game_map.set_pathable(player.propulsion.path, color=libtcod.blue) fov_recompute = True # Left clicking choose path final destination. if left_click: game_map.reset_pathable() player.propulsion.path.clear() turn_results.append( player.propulsion.choose_tile((mouse.cx, mouse.cy), game_map)) game_map.set_pathable(player.propulsion.path, color=libtcod.blue) fov_recompute = True # Right clicking deconstructs path. if right_click: game_map.reset_pathable() player.propulsion.chosen_tile = None player.propulsion.reset() # Attempt to end the turn. if next_turn_phase: if not player.propulsion.chosen_tile: # Phase doesn't end, but goes through one last time with a chosen tile. player.propulsion.chosen_tile = (player.location.x, player.location.y) else: next_turn_phase = False player.propulsion.update_speed() turn_state = TurnStates.MOVEMENT_PHASE # This phase is the big one. All entities act, except for projectiles of this game_state. # This phase ends when all entities have spent their action_points. if turn_state == TurnStates.MOVEMENT_PHASE: if active_entity: if active_entity is player: if active_entity.required_game_state == game_state: turn_results.extend( movement(player, entities, game_map)) if next_turn_phase: next_turn_phase = False player.action_points = 0 elif not active_entity.required_game_state == game_state: turn_results.extend( player.arsenal.fire_active_weapon()) else: turn_results.extend( active_entity.ai.take_turn(game_state, turn_state, entities, game_map)) if active_entity is None and event_queue.empty(): turn_state = TurnStates.POST_MOVEMENT_PHASE # This phase is not for the active_entity. if turn_state == TurnStates.POST_MOVEMENT_PHASE: game_map.reset_flags() fov_recompute = True turn_state = TurnStates.PRE_ATTACK_PHASE # This phase is not for the active_entity. # Entities have their action points refilled in order to use their weapons. if turn_state == TurnStates.PRE_ATTACK_PHASE: if game_state == GameStates.PLAYER_TURN: message_log.add_message( Message( 'Press f to target. Press ESC to stop targeting. Enter to change phase. Press r to choose new targets.', libtcod.orange)) for entity in entities: entity.reset(only_action_points=True) event_queue.register_list(entities) turn_state = TurnStates.ATTACK_PHASE # This phase is for active_entity of the required game_state. # They choose their weapons and targets. if turn_state == TurnStates.ATTACK_PHASE: if active_entity: if active_entity is player: if active_entity.required_game_state == game_state: if show_weapons_menu: previous_game_state = game_state game_state = GameStates.SHOW_WEAPONS_MENU if reset_targets: active_entity.arsenal.reset() message_log.add_message( Message('Targeting reset.', libtcod.light_blue)) game_map.reset_flags() fov_recompute = True if next_turn_phase: next_turn_phase = False active_entity.action_points = 0 else: active_entity.action_points = 0 elif active_entity.ai: turn_results.extend( active_entity.ai.take_turn(game_state, turn_state, entities, game_map)) if active_entity is None and event_queue.empty(): turn_state = TurnStates.POST_ATTACK_PHASE # This phase is not for active_entity. if turn_state == TurnStates.POST_ATTACK_PHASE: fov_recompute = True game_map.reset_flags() w = player.arsenal.get_active_weapon() if w is not None: for x, y in w.targets: erase_cell(con, x, y) turn_state = TurnStates.CLEANUP_PHASE # This phase is useless? if turn_state == TurnStates.CLEANUP_PHASE: if game_state == GameStates.PLAYER_TURN: game_state = GameStates.ENEMY_TURN elif game_state == GameStates.ENEMY_TURN: game_state = GameStates.PLAYER_TURN turn_state = TurnStates.UPKEEP_PHASE # Communicate turn_results. for result in turn_results: message = result.get( 'message') # Push a message to the message log. dead_entity = result.get('dead') # Kill the entity. remove_entity = result.get( 'remove') # Remove the entity (leaves no corpse behind). new_projectile = result.get( 'new_projectile') # Creates a new projectile. end_turn = result.get('end_turn') # End the turn. if result: fov_recompute = True if dead_entity: if dead_entity == player: message, game_state = kill_player(dead_entity) else: message = kill_enemy(dead_entity) event_queue.release(dead_entity) if new_projectile: entity_type, location, target, APs, required_game_state = new_projectile projectile = factory.entity_factory( entity_type, location, entities) projectile.action_points = APs xo, yo = location xd, yd = target ### Path extending code. # Determine maximums, based on direction. if xd - xo > 0: max_x = game_map.width else: max_x = 0 if yd - yo > 0: max_y = game_map.height else: max_y = 0 # Determine the endpoints. if xo - xd == 0: # The projectile moves vertically. y_end = max_y else: y_end = yo - (xo - max_x) * (yo - yd) // (xo - xd) if yo - yd == 0: # The projectile moves horizontally. x_end = max_x else: x_end = xo - (yo - max_y) * (xo - xd) // (yo - yd) # Ensure the enpoints are in the map. if not 0 < y_end < game_map.width: y_end = max_y if not 0 < x_end < game_map.height: x_end = max_x projectile.projectile.path = list( libtcod.line_iter(int(x_end), int(y_end), xo, yo)) projectile.projectile.path.pop( ) # Don't start where the shooter is standing. projectile.projectile.path.pop( 0) # Don't end out of bounds. projectile.required_game_state = required_game_state event_queue.register(projectile) if remove_entity: entities.remove(remove_entity) if end_turn: active_entity.action_points = 0 if message: message_log.add_message(Message(message, libtcod.yellow)) # Refill the queue with the active_entity, if appropriate. if active_entity and active_entity.action_points > 0: event_queue.register(active_entity) """ Handle the targeting cursor. """ if game_state == GameStates.TARGETING: targeting_results = [] if move: cursor.cursor.move(move, player.arsenal.get_active_weapon(), player) elif select: cursor.cursor.target(game_map, player.arsenal.get_active_weapon()) fov_recompute = True for result in targeting_results: message = result.get('message') target = result.get('target') if message: message_log.add_message(Message(message, libtcod.red)) if target: pass """ Handle the Show Weapons Menu state. """ if game_state == GameStates.SHOW_WEAPONS_MENU: menu_results = [] if weapons_menu_index is not None and weapons_menu_index < len( player.arsenal.weapons ) and previous_game_state != GameStates.PLAYER_DEAD: menu_results.append( player.arsenal.weapons[weapons_menu_index].activate()) cursor.cursor.turn_on( player, player.arsenal.weapons[weapons_menu_index].targets) game_state = GameStates.TARGETING for result in menu_results: message = result.get('message') if message: message_log.add_message( Message(message, libtcod.dark_green)) """ Handle the Look game state. """ if game_state == GameStates.LOOK: if move: dx, dy = move cursor_movement(cursor, dx, dy) """ Handle the death of the player. """ if game_state == GameStates.PLAYER_DEAD: print('You have died.') return True """ Handle commands that activate regardless of game state. """ if exit: if game_state == GameStates.SHOW_WEAPONS_MENU or game_state == GameStates.LOOK: game_state = previous_game_state cursor.cursor.turn_off() elif game_state == GameStates.TARGETING: cursor.cursor.turn_off() fov_recompute = True game_state = previous_game_state else: return True if fullscreen: libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen())
def main(): constants = get_constants() #sets the font of the console to arial10x10.png tcod.console_set_custom_font('arial10x10.png', tcod.FONT_TYPE_GREYSCALE | tcod.FONT_LAYOUT_TCOD) #creates a non-fullscreen window with the width and height defined earlier #and the title of "Tutorial" tcod.console_init_root(constants['screen_width'], constants['screen_height'], constants['window_title'], False) con = tcod.console_new(constants['screen_width'], constants['screen_height']) panel = tcod.console_new(constants['screen_width'], constants['panel_height']) player = None entities = [] world_map = None message_log = None game_state = None show_main_menu = True show_load_error_message = False main_menu_background_image = tcod.image_load('menu_background.png') key = tcod.Key() mouse = tcod.Mouse() while not tcod.console_is_window_closed(): tcod.sys_check_for_event(tcod.EVENT_KEY_PRESS, key, mouse) if show_main_menu: main_menu(con, main_menu_background_image, constants['screen_width'], constants['screen_height']) if show_load_error_message: message_box(con, 'No save game to load', 50, constants['screen_width'], constants['screen_height']) tcod.console_flush() action = handle_main_menu(key) new_game = action.get('new_game') load_saved_game = action.get('load_game') exit_game = action.get('exit_game') #get better method for main menu item selection if show_load_error_message and (new_game or load_saved_game or exit_game): show_load_error_message = False elif new_game: player, entities, world_map, message_log, game_state = get_game_variables(constants) game_state = GameStates.PLAYER_TURN current_enemy = None show_main_menu = False elif load_saved_game: try: player, entities, world_map, message_log, game_state, current_enemy = load_game() show_main_menu = False except FileNotFoundError: show_load_error_message = True elif exit_game: break else: tcod.console_clear(con) play_game(player, entities, world_map, message_log, game_state, current_enemy, con, panel, constants) show_main_menu = True
def main(): constants = get_constants() libtcod.console_set_custom_font( 'arial10x10.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD) libtcod.console_init_root(constants['screen_width'], constants['screen_height'], constants['window_title'], False) con = libtcod.console_new(constants['screen_width'], constants['screen_height']) panel = libtcod.console_new(constants['screen_width'], constants['panel_height']) player, entities, game_map, message_log, game_state = get_game_variables( constants) fov_recompute = True # used to tell algo when to change field of view, i.e. only when moving not when standing still/attacking fov_map = initialize_fov(game_map) key = libtcod.Key() mouse = libtcod.Mouse() previous_game_state = game_state # Used to not skip a players turn if they do something like pick up an item targeting_item = None while not libtcod.console_is_window_closed( ): # a loop that won't end until the game window is closed libtcod.sys_check_for_event( libtcod.EVENT_KEY_PRESS | libtcod.EVENT_MOUSE, key, mouse ) # this captures user input and updates the key/mouse variables above if fov_recompute: recompute_fov( fov_map, player.x, player.y, constants['fov_radius'], constants['fov_light_walls'], constants['fov_algorithm']) # coming from fov_functions 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 ) # con is the current console, entities is drawn in render_functions fov_recompute = False libtcod.console_flush() # this presents everything on screen clear_all( con, entities ) # put a blank below our character so it doesn't leave a trail, see render_functions.py """Action Handling""" action = handle_keys( key, game_state ) # calls our handle keys function from the input_handlers file, using our key variable, to result in a dictionary called action mouse_action = handle_mouse(mouse) move = action.get( 'move' ) # uses the action dictionary created above to return move, exit, pickup or fullscreen from handle_keys function 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: # i.e. does something to the target blocking the way attack_results = player.fighter.attack(target) player_turn_results.extend(attack_results) else: player.move(dx, dy) # gets move function from entity class fov_recompute = True # sets fov to True (i.e. to change) when we move game_state = GameStates.ENEMY_TURN # switches it to the enemies turn after moving 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 # ref'd at top, stops it skipping to enemies turn game_state = GameStates.SHOW_INVENTORY # switching to this game state with different keys 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): game_state = previous_game_state # now the Esc key just exits the inventory menu, rather than the whole game elif game_state == GameStates.TARGETING: player_turn_results.append({'targeting_cancelled': True}) 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') targeting = player_turn_result.get('targeting') targeting_cancelled = player_turn_result.get('targeting_cancelled') if message: message_log.add_message(message) if targeting_cancelled: game_state = previous_game_state message_log.add_message(Message('Targeting cancelled')) 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.item.targeting_message) 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: # note, the player and items etc won't have an AI component, so this skips them enemy_turn_results = entity.ai.take_turn( player, fov_map, game_map, entities ) # take turn managed in ai file, tells it to move towards or attack player 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(): constants = get_constants() font_path = 'arial12x12.png' # this will look in the same folder as this script font_flags = libt.FONT_TYPE_GREYSCALE | libt.FONT_LAYOUT_TCOD # the layout may need to change with a different font libt.console_set_custom_font(font_path, font_flags) fullscreen = False libt.console_init_root(constants['screen_width'], constants['screen_height'], constants['window_title'], fullscreen) con = libt.console_new(constants['screen_width'], constants['screen_height']) panel = libt.console_new(constants['screen_width'], constants['panel_height']) player = None entities = [] game_map = None message_log = None game_state = None show_main_menu = True show_load_error_message = False main_menu_background_image = libt.image_load('menu_background.png') key = libt.Key() mouse = libt.Mouse() while not libt.console_is_window_closed(): libt.sys_check_for_event(libt.EVENT_KEY_PRESS | libt.EVENT_MOUSE, key, mouse) if show_main_menu: main_menu(con, main_menu_background_image, constants['screen_width'], constants['screen_height']) if show_load_error_message: message_box(con, 'No save game to load', 50, constants['screen_width'], constants['screen_height']) libt.console_flush() action = handle_main_menu(key) new_game = action.get('new_game') load_save_game = action.get('load_game') exit_game = action.get('exit') if show_load_error_message and (new_game or load_save_game or exit_game): show_load_error_message = False elif new_game: player, entities, game_map, message_log, game_state = get_game_variables( constants) game_state = GameStates.PLAYERS_TURN show_main_menu = False elif load_save_game: try: player, entities, game_map, message_log, game_state = load_game( ) show_main_menu = False except FileNotFoundError: show_load_error_message = True elif exit_game: break else: libt.console_clear(con) play_game(player, entities, game_map, message_log, game_state, con, panel, constants) show_main_menu = True
def main(): constants = get_constants() libtcod.console_set_custom_font( 'arial10x10.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD) libtcod.console_init_root( constants['screen_width'], constants['screen_height'], constants['window_title'], False) con = libtcod.console_new( constants['screen_width'], constants['screen_height']) panel = libtcod.console_new( constants['screen_width'], constants['panel_height']) player = None entities = [] game_map = None message_log = None game_state = None show_main_menu = True get_weapon = False show_load_error_message = False key = libtcod.Key() mouse = libtcod.Mouse() while not libtcod.console_is_window_closed(): libtcod.sys_check_for_event( libtcod.EVENT_KEY_PRESS | libtcod.EVENT_MOUSE, key, mouse) if show_main_menu: main_menu( con, None, constants['screen_width'], constants['screen_height']) if show_load_error_message: message_box( con, 'No save game to load', 50, constants['screen_width'], constants['screen_height']) libtcod.console_flush() action = handle_main_menu(key) new_game = action.get('new_game') load_saved_game = action.get('load_game') exit_game = action.get('exit') if show_load_error_message and ( new_game or load_saved_game or exit_game): show_load_error_message = False elif new_game: player, entities, game_map, message_log, game_state = ( get_game_variables(constants)) get_weapon = True game_state = GameStates.PLAYERS_TURN show_main_menu = False elif load_saved_game: try: player, entities, game_map, message_log, game_state = ( load_game()) show_main_menu = False except FileNotFoundError: show_load_error_message = True elif exit_game: break elif get_weapon: menu( con, 'Choose Weapon', ['Sword and Board (Chance to block when fighting straight on)', 'Daggers (Gets a sneaky stab in and can dodge when moving)', 'Pike (Attacks twice as far as other weapons)'], 64, constants['screen_width'], constants['screen_height']) libtcod.console_flush() action = handle_weapon_menu(key) exit = action.get('exit') sword = action.get('sword') daggers = action.get('daggers') pike = action.get('pike') if exit: libtcod.console_clear(0) show_main_menu = True get_weapon = False elif sword: get_weapon = False constants['weapon'] = 'sword' elif daggers: get_weapon = False constants['weapon'] = 'daggers' elif pike: get_weapon = False constants['weapon'] = 'pike' else: libtcod.console_clear(con) play_game(player, entities, game_map, message_log, game_state, con, panel, constants) libtcod.console_clear(0) libtcod.console_flush() show_main_menu = True
def main(): constants = get_constants() # Instantiate main window libtcod.console_set_custom_font( "arial10x10.png", libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD) libtcod.console_init_root(constants["screen_width"], constants["screen_height"], constants["window_title"], False) # Instantiate offscreen window for drawing window_main = libtcod.console_new(constants["screen_width"], constants["screen_height"]) # Instantiate UI window ui_panel = libtcod.console_new(constants["screen_width"], constants["ui_panel_height"]) player = None entities = [] game_map = None message_log = None game_state = None show_main_menu = True show_load_error_message = False main_menu_background_image = libtcod.image_load( "ainsley.png") #("menu_background.png") key = libtcod.Key() mouse = libtcod.Mouse() while not libtcod.console_is_window_closed(): libtcod.sys_check_for_event( libtcod.EVENT_KEY_PRESS | libtcod.EVENT_MOUSE, key, mouse) if show_main_menu: action_keyboard = handle_main_menu_keys(key) # .get() returns None if not found new_game = bool(action_keyboard.get("new_game")) load_saved_game = bool(action_keyboard.get("load_game")) exit_game = bool(action_keyboard.get("exit")) menu_click_results = main_menu(window_main, main_menu_background_image, constants["screen_width"], constants["screen_height"], mouse) # Handle results of mouse click on menu item for result in menu_click_results: item_clicked = chr(result.get("item_clicked")) if item_clicked: action_mouse = get_main_menu_option_from_index( item_clicked) new_game |= bool(action_mouse.get("new_game")) load_saved_game |= bool(action_mouse.get("load_game")) exit_game |= bool(action_mouse.get("exit")) if show_load_error_message: message_box(window_main, "No save game to load", 50, constants["screen_width"], constants["screen_height"]) libtcod.console_flush() if show_load_error_message and (new_game or load_saved_game or exit_game): show_load_error_message = False elif new_game: player, entities, game_map, message_log, game_state = get_game_variables( constants) game_state = GameStates.PLAYER_TURN show_main_menu = False elif load_saved_game: try: player, entities, game_map, message_log, game_state = load_game( ) show_main_menu = False except FileNotFoundError: show_load_error_message = True elif exit_game: break else: libtcod.console_clear(window_main) play_game(player, entities, game_map, message_log, game_state, window_main, ui_panel, constants, key, mouse) show_main_menu = True
def main(): constants = get_constants() libtcod.console_set_custom_font( 'tiledfont.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD, 32, 10) libtcod.console_init_root(constants['screen_width'], constants['screen_height'], constants['window_title'], False) # load the custom font rows load_customfont() # assign the custom font rows numbers to text (for easier calling when defining entities with custom tiles) # defining tiles (rather than numbers) wall_tile = 257 #note: see render_functions for where the wall and floor tiles are defined, these are not used. floor_tile = 256 player_tile = 258 quiz_tile = 259 exam_tile = 260 healingpotion_tile = 261 sword_tile = 263 shield_tile = 264 stairsdown_tile = 265 dagger_tile = 266 fighter_component = Fighter(hp=30, defense=1, power=3, name='Guardian') inventory_component = Inventory(8) level_component = Level() equipment_component = Equipment() player = Entity(0, 0, player_tile, libtcod.white, 'Guardian', blocks=True, render_order=RenderOrder.ACTOR, fighter=fighter_component, inventory=inventory_component, level=level_component, equipment=equipment_component) entities = [player] equippable_component = Equippable(EquipmentSlots.MAIN_HAND, power_bonus=1) dagger = Entity(0, 0, dagger_tile, libtcod.white, 'Elemental Absorber + 1', equippable=equippable_component) player.inventory.add_item(dagger) player.equipment.toggle_equip(dagger) equippable_component = Equippable(EquipmentSlots.OFF_HAND, max_hp_bonus=5, defense_bonus=0) shield = Entity(0, 0, shield_tile, libtcod.white, 'Elemental Deflector + 0', equippable=equippable_component) player.inventory.add_item(shield) player.equipment.toggle_equip(shield) player.fighter.hp += shield.equippable.max_hp_bonus con = libtcod.console.Console(constants['screen_width'], constants['screen_height']) panel = libtcod.console.Console(constants['screen_width'], constants['panel_height']) game_map = GameMap(constants['map_width'], constants['map_height']) game_map.make_map(constants['max_rooms'], constants['room_min_size'], constants['room_max_size'], constants['map_width'], constants['map_height'], player, entities) fov_recompute = True fov_map = initialize_fov(game_map) message_log = MessageLog(constants['message_x'], constants['message_width'], constants['message_height']) key = libtcod.Key() mouse = libtcod.Mouse() game_state = GameStates.PLAYERS_TURN previous_game_state = game_state # Welcome the player message_log.add_message( Message( 'Welcome, Guardian, to the elemental kingdom of Empyria! Aquire all the elements...or die trying! Fearsome foes await in the deepest depths of Empyria, where many Guardians have disappeared...', libtcod.white)) 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) 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') take_stairs = action.get('take_stairs') level_up = action.get('level_up') show_character_screen = action.get('show_character_screen') wait = action.get('wait') 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: attack_results = player.fighter.attack(target) player_turn_results.extend(attack_results) fov_recompute = True 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)) 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 con.clear() break else: message_log.add_message( Message( 'You search around, but the stairs are nowhere to be seen.', libtcod.yellow)) if level_up: if level_up == 'hp': player.fighter.base_max_hp += 30 player.fighter.hp += 30 elif level_up == 'str': player.fighter.base_power += 5 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 wait == True: game_state = GameStates.ENEMY_TURN fov_recompute = True if exit: if game_state in (GameStates.SHOW_INVENTORY, GameStates.DROP_INVENTORY, GameStates.CHARACTER_SCREEN): 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_pick_gold = player_turn_result.get('got_gold') item_dropped = player_turn_result.get('item_dropped') equip = player_turn_result.get('equip') if message: message_log.add_message(message) if dead_entity: if dead_entity == player: message, game_state = kill_player(dead_entity) else: # Using xp to increase player's stats, "absorbing" the element if dead_entity.fighter.name == 'Corrupted Guardian': previous_game_state = game_state game_state = GameStates.WIN message_log.add_message( Message( 'You have brought peace to Empyria and vanquished the Corrputed Guardian! Congratulations!', libtcod.yellow)) if dead_entity.fighter.element == 'blank': player.fighter.blank_element += dead_entity.fighter.xp message = kill_monster(dead_entity) if player.fighter.blank_element > player.fighter.max_blank_element: player.fighter.blank_element = player.fighter.max_blank_element elif dead_entity.fighter.element == 'fire': player.fighter.fire_element += dead_entity.fighter.xp message = kill_monster(dead_entity) if player.fighter.fire_element > player.fighter.max_fire_element: player.fighter.fire_element = player.fighter.max_fire_element elif dead_entity.fighter.element == 'air': player.fighter.air_element += dead_entity.fighter.xp message = kill_monster(dead_entity) if player.fighter.air_element > player.fighter.max_air_element: player.fighter.air_element = player.fighter.max_air_element elif dead_entity.fighter.element == 'ice': player.fighter.ice_element += dead_entity.fighter.xp message = kill_monster(dead_entity) if player.fighter.ice_element > player.fighter.max_ice_element: player.fighter.ice_element = player.fighter.max_ice_element elif dead_entity.fighter.element == 'lightning': player.fighter.lightning_element += dead_entity.fighter.xp message = kill_monster(dead_entity) if player.fighter.lightning_element > player.fighter.max_lightning_element: player.fighter.lightning_element = player.fighter.max_lightning_element elif dead_entity.fighter.element == 'earth': player.fighter.earth_element += dead_entity.fighter.xp message = kill_monster(dead_entity) if player.fighter.earth_element > player.fighter.max_earth_element: player.fighter.earth_element = player.fighter.max_earth_element elif dead_entity.fighter.element == 'psychic': player.fighter.psychic_element += dead_entity.fighter.xp message = kill_monster(dead_entity) if player.fighter.psychic_element > player.fighter.max_psychic_element: player.fighter.psychic_element = player.fighter.max_psychic_element elif dead_entity.fighter.element == 'water': player.fighter.water_element += dead_entity.fighter.xp message = kill_monster(dead_entity) if player.fighter.water_element > player.fighter.max_water_element: player.fighter.water_element = player.fighter.max_water_element 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_pick_gold: # entities.remove(item_gold) # 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 {0}.'.format( equipped.name))) if dequipped: message_log.add_message( Message('You dequipped the {0}.'.format( dequipped.name))) game_state = GameStates.ENEMY_TURN xp = player_turn_result.get('xp') if xp: # if not GameStates.WIN: leveled_up = player.level.add_xp(xp) message_log.add_message( Message('You gained {0} exp!'.format(xp))) if (player.fighter.blank_element == player.fighter.max_blank_element and player.fighter.fire_element == player.fighter.max_fire_element and player.fighter.air_element == player.fighter.max_air_element and player.fighter.ice_element == player.fighter.max_ice_element and player.fighter.lightning_element == player.fighter.max_lightning_element and player.fighter.earth_element == player.fighter.max_earth_element and player.fighter.psychic_element == player.fighter.max_psychic_element and player.fighter.water_element == player.fighter.max_water_element): message_log.add_message( Message( 'You have collected all of the elements and are now a true Elemental Guardian! You won! Or did you...?', libtcod.yellow)) if leveled_up: # if player.level.current_level == 5: # previous_game_state = game_state # game_state = GameStates.WIN # message_log.add_message(Message('You have collected 180 exp! You won!', libtcod.yellow)) # else: message_log.add_message( Message( 'Level up! You are now 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: 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 fov_recompute = True
def main(): constants = get_constants() libtcod.console_set_custom_font( 'tiledfont.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD, 32, 10) libtcod.console_init_root(constants['screen_width'], constants['screen_height'], constants['window_title'], False) # load the custom font rows load_customfont() # assign the custom font rows numbers to text (for easier calling when defining entities with custom tiles) # defining tiles (rather than numbers) wall_tile = 256 floor_tile = 257 player_tile = 258 quiz_tile = 259 exam_tile = 260 healingpotion_tile = 261 sword_tile = 263 shield_tile = 264 stairsdown_tile = 265 dagger_tile = 266 fighter_component = Fighter(hp=100, defense=1, power=3, name='Student') inventory_component = Inventory(26) level_component = Level() equipment_component = Equipment() player = Entity(0, 0, player_tile, libtcod.white, 'Student', blocks=True, render_order=RenderOrder.ACTOR, fighter=fighter_component, inventory=inventory_component, level=level_component, equipment=equipment_component) entities = [player] equippable_component = Equippable(EquipmentSlots.MAIN_HAND, power_bonus=1) dagger = Entity(0, 0, dagger_tile, libtcod.white, 'Pencil', equippable=equippable_component) player.inventory.add_item(dagger) player.equipment.toggle_equip(dagger) con = libtcod.console.Console(constants['screen_width'], constants['screen_height']) panel = libtcod.console.Console(constants['screen_width'], constants['panel_height']) game_map = GameMap(constants['map_width'], constants['map_height']) game_map.make_map(constants['max_rooms'], constants['room_min_size'], constants['room_max_size'], constants['map_width'], constants['map_height'], player, entities) fov_recompute = True fov_map = initialize_fov(game_map) message_log = MessageLog(constants['message_x'], constants['message_width'], constants['message_height']) key = libtcod.Key() mouse = libtcod.Mouse() game_state = GameStates.PLAYERS_TURN previous_game_state = game_state # Welcome the player message_log.add_message( Message( 'Welcome, Student, to the College of Doom! Aquire 180 credits to graduate...or die trying!', libtcod.white)) 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) 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') take_stairs = action.get('take_stairs') level_up = action.get('level_up') show_character_screen = action.get('show_character_screen') wait = action.get('wait') 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: attack_results = player.fighter.attack(target) player_turn_results.extend(attack_results) fov_recompute = True 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)) 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 con.clear() break else: message_log.add_message( Message( 'You search around, but Summer break is nowhere to be seen.', 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 wait == True: game_state = GameStates.ENEMY_TURN fov_recompute = True if exit: if game_state in (GameStates.SHOW_INVENTORY, GameStates.DROP_INVENTORY, GameStates.CHARACTER_SCREEN): 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') equip = player_turn_result.get('equip') 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 {0}.'.format( equipped.name))) if dequipped: message_log.add_message( Message('You dequipped the {0}.'.format( dequipped.name))) game_state = GameStates.ENEMY_TURN xp = player_turn_result.get('xp') if xp: leveled_up = player.level.add_xp(xp) message_log.add_message( Message( 'You passed the Final Exam and gained {0} credits!'. format(xp))) if leveled_up: if player.level.current_level == 5: previous_game_state = game_state game_state = GameStates.WIN message_log.add_message( Message( 'You have completed 180 credits at the College of Doom! You graduated!', libtcod.yellow)) else: message_log.add_message( Message( 'You made it through another year of school! You move on to school year {0}' .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 fov_recompute = True
def main(): constants = get_constants() libtcod.console_set_custom_font('images/simplerl_12x12.png', libtcod.FONT_LAYOUT_ASCII_INROW | libtcod.FONT_TYPE_GREYSCALE) libtcod.console_init_root(constants['screen_width'], constants['screen_height'], constants['window_title'], False, libtcod.RENDERER_SDL2, 'F', True) con = libtcod.console.Console(constants['screen_width'], constants['screen_height']) panel = libtcod.console.Console(constants['screen_width'], constants['panel_height']) player = None entities = [] game_map = None message_log = None game_state = None show_main_menu = True show_load_error_message = False main_menu_background_image = libtcod.image_load('images/menu_background1.png') in_handle = InputHandler() # main game loop while True: if show_main_menu: main_menu(con, main_menu_background_image, constants['screen_width'], constants['screen_height']) if show_load_error_message: message_box(con, 'No save game to load', 50, constants['screen_width'], constants['screen_height']) libtcod.console_flush() game_state = GameStates.MAIN_MENU in_handle.set_game_state(game_state) for event in tcod.event.get(): in_handle.dispatch(event) action = in_handle.get_action() new_game = action.get('new_game') load_saved_game = action.get('load_game') exit_game = action.get('exit') if show_load_error_message and (new_game or load_saved_game or exit_game): show_load_error_message = False elif new_game: player, entities, game_map, message_log, game_state = get_game_variables(constants) game_state = GameStates.PLAYERS_TURN show_main_menu = False elif load_saved_game: try: player, entities, game_map, message_log, game_state = load_game() show_main_menu = False except FileNotFoundError: show_load_error_message = True elif exit_game: break else: con.clear(fg=[63, 127, 63]) play_game(player, entities, game_map, message_log, game_state, con, panel, constants) show_main_menu = True
def main(): constants = get_constants() names_list = get_unidentified_names() colors_list = get_render_colors() load_customfont() libtcod.console_set_custom_font( 'fontplus.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_ASCII_INROW, 16, 30) libtcod.console_init_root(constants['screen_width'], constants['screen_height'], constants['window_title'], False) con = libtcod.console.Console(constants['screen_width'], constants['screen_height']) panel = libtcod.console.Console(constants['screen_width'], constants['panel_height']) player = None entities = [] game_map = None message_log = None game_state = None show_main_menu = True show_load_error_message = False main_menu_background_image = libtcod.image_load('menu_background.png') key = libtcod.Key() mouse = libtcod.Mouse() while not libtcod.console_is_window_closed(): libtcod.sys_check_for_event( libtcod.EVENT_KEY_PRESS | libtcod.EVENT_MOUSE, key, mouse) if show_main_menu: main_menu(con, main_menu_background_image, constants['screen_width'], constants['screen_height']) if show_load_error_message: message_box(con, 'No save game to load', 50, constants['screen_width'], constants['screen_height']) libtcod.console_flush() action = handle_main_menu(key) new_game = action.get('new_game') load_saved_game = action.get('load_game') exit_game = action.get('exit') if show_load_error_message and (new_game or load_saved_game or exit_game): show_load_error_message = False elif new_game: constants = get_constants() names_list = get_unidentified_names() colors_list = get_render_colors() if intro(constants): if not game_options(constants) == "nah": if not origin_options(constants) == "nah": if not character_name(constants) == 'nah': player, entities, game_map, message_log, game_state = get_game_variables( constants, names_list, colors_list) game_state = GameStates.PLAYERS_TURN show_main_menu = False elif load_saved_game: try: player, entities, game_map, message_log, game_state, constants = load_game( ) show_main_menu = False except FileNotFoundError: show_load_error_message = True elif exit_game: break else: libtcod.console_clear(con) play_game(player, entities, game_map, message_log, game_state, con, panel, constants, names_list, colors_list) show_main_menu = True
def main(): constants = get_constants() libtcod.console_set_custom_font('arial10x10.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD) libtcod.console_init_root(constants['screen_width'], constants['screen_height'], constants['window_title'], False) con = libtcod.console_new(constants['screen_width'], constants['screen_height']) panel = libtcod.console_new(constants['screen_width'], constants['panel_height']) player = None entities = [] game_map = None message_log = None game_state = None show_main_menu = True show_load_error_message = False main_menu_background_image = libtcod.image_load('menu_background1.png') key = libtcod.Key() mouse = libtcod.Mouse() while not libtcod.console_is_window_closed(): libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS | libtcod.EVENT_MOUSE, key, mouse) if show_main_menu: main_menu(con, main_menu_background_image, constants['screen_width'], constants['screen_height']) if show_load_error_message: message_box(con, 'No save game to load', 50, constants['screen_width'], constants['screen_height']) libtcod.console_flush() action = handle_main_menu(key) new_game = action.get('new_game') load_saved_game = action.get('load_game') exit_game = action.get('exit') if show_load_error_message and (new_game or load_saved_game or exit_game): show_load_error_message = False elif new_game: player, entities, game_map, message_log, game_state = get_game_variables(constants) game_state = GameStates.PLAYERS_TURN show_main_menu = False elif load_saved_game: try: player, entities, game_map, message_log, game_state = load_game() show_main_menu = False except FileNotFoundError: show_load_error_message = True elif exit_game: break else: libtcod.console_clear(con) play_game(player, entities, game_map, message_log, game_state, con, panel, constants) show_main_menu = True
def main(): # 定数を読み込む constants = get_constants() # フォントの指定と(libtcod.FONT_TYPE_GRAYSCALE | libtcod.FONT_LAYOUT_TCOD)でどのタイプのファイルを読み取るのかを伝える libtcod.console_set_custom_font("arial10x10.png", libtcod.FONT_TYPE_GRAYSCALE | libtcod.FONT_LAYOUT_TCOD) # ここで実際に画面を作成する、画面サイズとタイトルとフルスクリーンとレンダラーと画面の垂直同期を指定している libtcod.console_init_root(constants["screen_width"], constants["screen_height"], constants["window_title"], False) con = libtcod.console.Console(constants["screen_width"], constants["screen_height"]) panel = libtcod.console.Console(constants["screen_width"], constants["panel_height"]) player = None entities = [] game_map = None message_log = None game_state = None show_main_menu = True show_load_error_message = False main_menu_background_image = libtcod.image_load("menu_background.png") main_window = libtcod.console_is_window_closed() while True: if show_main_menu: main_menu(con, main_menu_background_image, constants["screen_width"], constants["screen_height"]) if show_load_error_message: message_box(con, "No save game to load", 50, constants["screen_width"], constants["screen_height"]) libtcod.console_flush() for events in libtcod.event.get(): if events.type == "KEYDOWN": action = handle_main_menu(events) new_game = action.get("new_game") load_saved_game = action.get("load_game") exit_game = action.get("exit") if show_load_error_message and (new_game or load_saved_game or exit_game): show_load_error_message = False elif new_game: player, entities, game_map, message_log, game_state = get_game_variables(constants) game_state = GameStates.PLAYERS_TURN show_main_menu = False elif load_saved_game: try: player, entities, game_map, message_log, game_state = load_game() show_main_menu = False except FileNotFoundError: show_load_error_message = True elif exit_game: return False else: libtcod.console_clear(con) play_game(player, entities, game_map, message_log, game_state, con, panel, constants) show_main_menu = True
def main(): constants = get_constants() libtcod.console_set_custom_font( 'arial10x10.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD) libtcod.console_init_root(constants['screen_width'], constants['screen_height'], constants['window_title'], False) con = libtcod.console_new(constants['screen_width'], constants['screen_height']) panel = libtcod.console_new(constants['screen_width'], constants['panel_height']) player, entities, game_map, message_log, game_state = get_game_variables( constants) fov_recompute = True fov_map = initialize_fov(game_map) 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) 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 != player: #if move(0,0) player would attack itself 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 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): game_state = previous_game_state elif game_state == GameStates.TARGETING: player_turn_results.append({'targeting_cancelled': True}) 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') targeting = player_turn_result.get('targeting') targeting_cancelled = player_turn_result.get('targeting_cancelled') if message: message_log.add_message(message) if targeting_cancelled: game_state = previous_game_state message_log.add_message(Message('Targeting cancelled')) 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.item.targeting_message) 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(): constants = initialize_new_game.get_constants() position = 0 terminal.open() terminal.set("window: size=" + str(constants['screen_width']) + "x" + str(constants['screen_height']) + ', title=Space Game') terminal.set("font: fonts\courbd.ttf, size=" + constants.get('fontsize')) # terminal.set("input.filter={keyboard, mouse}") player = None entities = [] game_map = None message_log = None game_state = None while True: # show the background image, at twice the regular console resolution # clear scene especially after quitting game terminal.layer(0) terminal.clear() # show the game's title, and some credits! title = 'SPACE GAME' titlex = int(constants['screen_width'] / 2 - len(title) / 2) titley = int(constants['screen_height'] / 2) subtitle = 'Game by Eric Younkin' subtitlex = int(constants['screen_width'] / 2 - len(subtitle) / 2) subtitley = int(constants['screen_height'] / 2 + 2) terminal.color('yellow') terminal.print_(titlex, titley, '[align=center]' + title) terminal.print_(subtitlex, subtitley, '[align=center][font=0xE000]' + subtitle) # show options and wait for the player's choice options = ['Play a new game', 'Continue last game', 'Quit'] menu('', options, 30, constants['screen_width'], constants['screen_height'], position=position, type='main') key = terminal.read() action = input_handlers.handle_menu_keys(key) menupos = action.get('menupos') select = action.get('select') ex = action.get('exit') resize = action.get('resize') if ex: break elif menupos: position += menupos if position < 0: position = 0 if position >= len(options) - 1: position = len(options) - 1 elif select: if position == 0: player, entities, game_map, message_log, game_state = initialize_new_game.get_game_variables( constants) game_state = GameStates.PLAYERS_TURN play_game(player, entities, game_map, message_log, game_state, constants) elif position == 1: # load last game try: player, entities, game_map, message_log, game_state = data_loaders.load_game( ) play_game(player, entities, game_map, message_log, game_state, constants) except FileNotFoundError: message_box('No save game to load', 50, constants['screen_width'], constants['screen_height']) play_game(player, entities, game_map, message_log, game_state, constants) elif position == 2: # quit break elif resize: print(size) if size == '8': size = '12' elif size == '12:': size = '16' elif size == '16': size = '8' terminal.set("font: fonts\courbd.ttf, size=" + size)
def main(): constants = get_constants() tc.console_set_custom_font('arial10x10.png', tc.FONT_TYPE_GREYSCALE | tc.FONT_LAYOUT_TCOD) tc.console.Console(constants['screen_width'], constants['screen_height']) tc.console_init_root(constants['screen_width'], constants['screen_height'], constants['window_title'], False, tc.RENDERER_SDL2, "F", True) con = tc.console.Console(constants['screen_width'], constants['screen_height']) panel = tc.console.Console(constants['screen_width'], constants['screen_height']) player = None entities = [] game_map = None message_log = None game_state = None show_main_menu = True show_load_error_message = False # main_menu_background_image = tc.image_load('menu_background.png') main_menu_background_image = tc.image_load('') key = tc.Key() mouse = tc.Mouse() event = tc.event.get() # for event in tcod.event.wait(): while event != "QUIT": tc.sys_check_for_event(tc.EVENT_KEY_PRESS | tc.EVENT_MOUSE, key, mouse) if show_main_menu: con.clear() clear_all(con, entities) tc.console_blit(con, 0, 0, constants['screen_width'], constants['screen_height'], 0, 0, 0, 1.0, 1.0) main_menu(con, main_menu_background_image, constants['screen_width'], constants['screen_height']) if show_load_error_message: message_box(con, 'No save game to load', 50, constants['screen_width'], constants['screen_height']) tc.console_flush() action = handle_main_menu(key) new_game = action.get('new_game') load_saved_game = action.get('load_game') exit_game = action.get('exit') if show_load_error_message and (new_game or load_saved_game or exit_game): show_load_error_message = False elif new_game: player, entities, game_map, message_log, game_state = get_game_variables( constants) show_main_menu = False #REVERT BEFORE COMMIT # game_state = GameStates.SHOW_TUTORIAL game_state = GameStates.PLAYERS_TURN elif load_saved_game: try: player, entities, game_map, message_log, game_state = load_game( ) show_main_menu = False except FileNotFoundError: show_load_error_message = True elif exit_game: break else: con.clear() play_game(player, entities, game_map, message_log, game_state, con, panel, constants) show_main_menu = True
def __init__(self): self.constants = get_constants()
import tcod as libtcod from game_states import GhostStates import random from loader_functions.initialize_new_game import get_constants from map_objects.coords import Coords from map_objects.map_functions import random_walkable_target from game_messages import Message constants = get_constants() class BasicMonster: def take_turn(self, target, fov_map, game_map, entities, superpill_mode): results = [] monster = self.owner if random.randint(1,100) <= monster.speed: if not constants['DEBUG_disableAI']: if libtcod.map_is_in_fov(fov_map, monster.x, monster.y): if monster.distance_to(target) >= 2: monster.move_astar(target, entities, game_map) elif target.fighter.hp > 0: attack_results = monster.fighter.attack(target) results.extend(attack_results) return results
def main(): constants = get_constants() (camera_x, camera_y) = (0, 0) fighter_component = Fighter(hp=30, defense=0, power=5) inventory_component = Inventory(26) player = Entity(0, 0, '@', 'white', 'You', blocks=True, render_order=RenderOrder.ACTOR, fighter=fighter_component, inventory=inventory_component) entities = [player] game_map = GameMap(constants['map_width'], constants['map_height'], constants['full_rooms']) game_map.make_bsp(constants['depth'], constants['min_size'], player, entities, constants['max_monsters_per_room'], constants['max_items_per_room']) fov_recompute = True fov_map = initialize_fov(game_map) message_log = MessageLog(constants['message_x'], constants['message_width'], constants['message_height']) camera = Camera(constants['camera_width'], constants['camera_height'], camera_x, camera_y) blt.color("white") blt.composition(True) game_state = GameStates.PLAYERS_TURN previous_game_state = game_state targeting_item = None while True: # pdb.set_trace() mouse_x = blt.state(blt.TK_MOUSE_X) mouse_y = blt.state(blt.TK_MOUSE_Y) mouse = (mouse_x, mouse_y) if fov_recompute: recompute_fov(fov_map, player.x, player.y, constants['fov_radius'], constants['fov_light_walls'], constants['fov_algorithm']) render_all(entities, player, game_map, camera, fov_map, fov_recompute, message_log, constants['sidebar_x'], constants['sidebar_width'], constants['panel_y'], constants['panel_height'], constants['screen_width'], constants['screen_height'], game_state, mouse) fov_recompute = False blt.refresh() blt.clear() action = handle_keys(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') left_click = action.get('left_click') right_click = action.get('right_click') if blt.TK_MOUSE_X or blt.TK_MOUSE_Y: fov_recompute = True 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 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 to pick up.', 'yellow')) if show_inventory: previous_game_state = game_state # print(previous_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: print(left_click) (target_x, target_y) = camera.to_map_coordinates(left_click[0], left_click[1]) print(target_x, target_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}) 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: return False 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 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.item.targeting_message) if targeting_cancelled: game_state = previous_game_state message_log.add_message(Message('Targeting cancelled')) 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(): """ L'une des deux fonctions principales du jeu, elle est appelée une seule et unique fois : au démarrage du jeu. Elle a la charge de gérer le choix d'affichage des menus et les réactions aux inputs du joueur. Lorsque le joueur quitte un menu pour retourner en jeu, elle appelle la deuxième fonction de ce module : play_game Parametres: ---------- Aucun Renvoi: ------- Aucun """ # Initialise le jeu en commencant par afficher le menu principal constants = get_constants() # The font has 32 chars in a row, and there's a total of 10 rows. Increase the "10" when you add new rows to the sample font file libtcod.console_set_custom_font('textures.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD, 32, 10) load_customfont() libtcod.console_init_root(constants['screen_width'], constants['screen_height'], "Rogue doesn't like", False) con = libtcod.console_new(constants['screen_width'], constants['screen_height']) panel = libtcod.console_new(constants['screen_width'], constants['panel_height']) player = None entities = [] game_map = None message_log = None game_state = None show_main_menu = True show_load_error_message = False show_command_menu = False show_scores = False main_menu_background_image = libtcod.image_load('menu_background.png') play_bg_music = False bg_music = sm.choose_sound(constants.get('sound').get('background_music')) key = libtcod.Key() mouse = libtcod.Mouse() score = (1, 0) while not libtcod.console_is_window_closed(): libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS | libtcod.EVENT_MOUSE, key, mouse) # Si le menu principal est affiche : if show_main_menu: main_menu(con, main_menu_background_image, constants['screen_width'], constants['screen_height']) if show_load_error_message: message_box(con, 'Rien a charger', 70, constants['screen_width'], constants['screen_height']) libtcod.console_flush() action = handle_main_menu(key) new_game = action.get('new_game') load_saved_game = action.get('load_game') exit_game = action.get('exit') sound = action.get('sound') command = action.get('command') back_to_game = action.get('back_to_game') scores = action.get('scores') if show_load_error_message and (new_game or load_saved_game or exit_game): show_load_error_message = False # Cree une nouvelle partie elif new_game: if score != (1, 0): save_score(score) player, entities, game_map, message_log, game_state = get_game_variables(constants) game_state = GameStates.PLAYERS_TURN show_main_menu = False # Charge une partie existante, si possible elif load_saved_game: try: player, entities, game_map, message_log, game_state, score = load_game() show_main_menu = False except FileNotFoundError: show_load_error_message = True elif exit_game: if score != (1, 0): save_game(player, entities, game_map, message_log, game_state, score) sm.close_sound() break # Lit ou arrete la musique de fond elif sound: play_bg_music = not play_bg_music bg_music.playpause() elif command: show_command_menu = True show_main_menu = False elif back_to_game and show_main_menu and game_state != GameStates.PLAYER_DEAD: show_main_menu = False elif scores: show_main_menu = False show_scores = True # Affiche le menu des commandes elif show_command_menu: action = handle_main_menu(key) command_menu(con, main_menu_background_image, constants['screen_width'], constants['screen_height']) libtcod.console_flush() back_to_game = action.get('back_to_game') if back_to_game: show_main_menu = True show_command_menu = False libtcod.console_clear(con) # Affiche les trois meilleurs scores et le dernier elif show_scores: action = handle_main_menu(key) scores_menu(con, main_menu_background_image, constants['screen_width'], constants['screen_height']) libtcod.console_flush() back_to_game = action.get('back_to_game') if back_to_game: show_main_menu = True show_scores = False libtcod.console_clear(con) # Lance une partie else: libtcod.console_clear(con) player, entities, game_map, message_log, game_state, bg_music, play_bg_music, score = play_game(player, entities, game_map, message_log, game_state, con, panel, constants, bg_music, play_bg_music, score) if game_state == GameStates.PLAYER_DEAD: show_scores = True else: show_main_menu = True
def main(): # game "constants" / "globals" setup debug_f = True omnivision = False constants = get_constants() # initial game state game_state = GameStates.MAIN_MENU show_load_error_message = False # input handling setup in_handle = InputHandler() mouse_x = 0 mouse_y = 0 # set tcod font tcod.console_set_custom_font( "arial10x10.png", tcod.FONT_TYPE_GREYSCALE | tcod.FONT_LAYOUT_TCOD ) # set up ui elements panel_ui = tcod.console.Console(constants["panel_ui_width"], constants["panel_ui_height"]) panel_map = tcod.console.Console(constants["panel_map_width"], constants["panel_map_height"]) main_menu_bg = tcod.image_load("menu_background1.png") # open tcod console context with tcod.console_init_root( constants["screen_width"], constants["screen_height"], constants["window_title"], fullscreen=False, renderer=tcod.RENDERER_SDL2, vsync=False) as root_console: while True: if game_state == GameStates.MAIN_MENU: in_handle.set_game_state(game_state) main_menu(root_console, main_menu_bg, constants["screen_width"], constants["screen_height"]) if show_load_error_message: message_box(root_console, "No save game to load", 50, constants["screen_width"], constants["screen_height"]) tcod.console_flush() for event in tcod.event.get(): in_handle.dispatch(event) user_in = in_handle.get_user_input() want_exit = user_in.get("exit") new_game = user_in.get("new_game") load_save = user_in.get("load_game") if (show_load_error_message and (new_game or load_save or want_exit)): show_load_error_message = False elif want_exit: return elif new_game: # set up game "runtime global" variables from scratch g_var = get_game_variables(constants, root_console, panel_map, debug_f) (player, entities, controlled_entity, curr_entity, game_state, prev_state, message_log, game_map, timeq, next_turn, render_update, targeting_item) = g_var elif load_save: # load game "runtime global" variables from save file try: g_var = load_game(constants) except FileNotFoundError: show_load_error_message = True if not show_load_error_message: (player, entities, controlled_entity, curr_entity, game_state, prev_state, message_log, game_map, timeq, next_turn, render_update, targeting_item) = g_var else: game_state = GameStates.MAIN_MENU else: play_game(constants, root_console, panel_ui, panel_map, debug_f, omnivision, in_handle, mouse_x, mouse_y, player, entities, controlled_entity, curr_entity, game_state, prev_state, message_log, game_map, timeq, next_turn, render_update, targeting_item) game_state = GameStates.MAIN_MENU
def main(): constants = get_constants() libtcod.console_set_custom_font( 'arial10x10.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD) libtcod.console_init_root(constants['screen_width'], constants['screen_height'], constants['window_title'], False) con = libtcod.console_new(constants['screen_width'], constants['screen_height']) panel = libtcod.console_new(constants['screen_width'], constants['panel_height']) player = None entities = [] game_map = None message_log = None game_state = None show_main_menu = True show_load_error_message = False main_menu_background_image = libtcod.image_load('menu_background.png') key = libtcod.Key() mouse = libtcod.Mouse() while not libtcod.console_is_window_closed(): libtcod.sys_check_for_event( libtcod.EVENT_KEY_PRESS | libtcod.EVENT_MOUSE, key, mouse) if show_main_menu: main_menu(con, main_menu_background_image, constants['screen_width'], constants['screen_height']) if show_load_error_message: message_box(con, 'No save game to load', 50, constants['screen_width'], constants['screen_height']) libtcod.console_flush() action = handle_main_menu(key) new_game = action.get('new_game') load_saved_game = action.get('load_game') exit_game = action.get('exit') if show_load_error_message and (new_game or load_saved_game or exit_game): show_load_error_message = False elif new_game: player, entities, game_map, message_log, game_state = get_game_variables( constants) game_state = GameStates.PLAYERS_TURN show_main_menu = False elif load_saved_game: try: player, entities, game_map, message_log, game_state = load_game( ) show_main_menu = False except FileNotFoundError: show_load_error_message = True elif exit_game: break else: libtcod.console_clear(con) play_game(player, entities, game_map, message_log, game_state, con, panel, constants) show_main_menu = True
def main(): constants = get_constants() # Initialize console stuff libtcod.console_set_custom_font('arial10x10.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD) # The font. libtcod.console_init_root(constants['screen_width'], constants['screen_height'], constants['window_title'], False) #The size of the root console. con = libtcod.console_new(constants['screen_width'], constants['screen_height']) # The main console of the game. panel = libtcod.console_new(constants['screen_width'], constants['panel_height']) # A UI panel for the game. # Declare these variables here, but fill them later player = None entities = [] game_map = None message_log = None game_state = None priority_queue = [] global_variables = None show_main_menu = True show_load_error_message = False main_menu_background_image = libtcod.image_load('menu_background.png') key = libtcod.Key() mouse = libtcod.Mouse() while not libtcod.console_is_window_closed(): libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS | libtcod.EVENT_MOUSE, key, mouse) if show_main_menu: main_menu(con, main_menu_background_image, constants['screen_width'], constants['screen_height']) if show_load_error_message: message_box(con, 'No save game to load', 50, constants['screen_width'], constants['screen_height']) libtcod.console_flush() action = handle_main_menu(key) new_game = action.get('new_game') load_saved_game = action.get('load_game') exit_game = action.get('exit') if show_load_error_message and (new_game or load_saved_game or exit_game): show_load_error_message = False elif new_game: player, entities, game_map, message_log, game_state, priority_queue, global_variables, world = get_game_variables(constants) show_main_menu = False elif load_saved_game: try: player, entities, game_map, message_log, game_state, priority_queue, global_variables, world = load_game() show_main_menu = False except FileNotFoundError: show_load_error_message = True elif exit_game: break else: libtcod.console_clear(con) play_game(player, entities, game_map, message_log, game_state, con, panel, constants, priority_queue, global_variables, world) show_main_menu = True # When play_game() is done, the menu reappears.