def main(): screen_width = 80 screen_height = 80 map_width = 80 map_height = 70 colors = { 'dark_wall': libtcod.Color(45, 65, 100), 'dark_ground': libtcod.Color(13, 23, 15) } player = Entity(int(screen_width / 2), int(screen_height / 2), '@', libtcod.white) entities = [player] libtcod.console_set_custom_font('arial10x10.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD) libtcod.console_init_root(screen_width, screen_height, 'table tosser', False) con = libtcod.console_new(screen_width, screen_height) game_map = GameMap(map_width, map_height) key = libtcod.Key() mouse = libtcod.Mouse() while not libtcod.console_is_window_closed(): libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS, key, mouse) render_all(con, entities, game_map, screen_width, screen_height, colors) libtcod.console_flush() clear_all(con, entities) action = handle_keys(key) move = action.get('move') exit = action.get('exit') fullscreen = action.get('fullscreen') if move: dx, dy = move if not game_map.is_blocked(player.x + dx, player.y +dy): player.move(dx, dy) if exit: return True if fullscreen: libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen())
def main(): screen_width = 80 screen_height = 50 map_width = 80 map_height = 45 room_max_size = 10 room_min_size = 6 max_rooms = 30 colors = { 'dark_wall': libtcod.Color(0, 0, 100), 'dark_ground': libtcod.Color(50, 50, 150) } player = Entity(40, 25, '@', libtcod.white, name="Player") npc = Entity(25, 20, 'N', libtcod.yellow, name="NPC") entities = [npc, player] libtcod.console_set_custom_font( 'arial10x10.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD) libtcod.console_init_root(screen_width, screen_height, 'libtcod tutorial revised', False) con = libtcod.console_new(screen_width, screen_height) game_map = GameMap(map_width, map_height) game_map.make_map(max_rooms, room_min_size, room_max_size, player, entities, 3, 2) key = libtcod.Key() mouse = libtcod.Mouse() while not libtcod.console_is_window_closed(): libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS, key, mouse) render_all(con, entities, game_map, screen_width, screen_height, colors) libtcod.console_flush() clear_all(con, entities) action = handle_keys(key) move = action.get('move') exit_game = action.get('exit') full_screen = action.get('fullscreen') if move: dx, dy = move if not game_map.is_blocked(player.x + dx, player.y + dy): player.move(dx, dy) if exit_game: return True if full_screen: libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen())
def main(): screen_width = 80 screen_height = 50 map_width = 80 map_height = 45 room_max_size = 10 room_min_size = 6 max_rooms = 1000 colors = { # Gray 'dark_wall': libtcod.darkest_gray, # Brown 'dark_ground': libtcod.orange * libtcod.darker_gray } player = Entity(int(screen_width / 2), int(screen_height / 2), '@', libtcod.white) npc = Entity(int(screen_width / 2 - 5), int(screen_height / 2), '@', libtcod.yellow) entities = [player, npc] libtcod.console_set_custom_font('arial10x10.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD) libtcod.console_init_root(screen_width, screen_height, 'RoguelikeDev Tutorial', False) con = libtcod.console_new(screen_width, screen_height) game_map = GameMap(map_width, map_height) game_map.make_map(max_rooms, room_min_size, room_max_size, map_width, map_height, player) key = libtcod.Key() mouse = libtcod.Mouse() while not libtcod.console_is_window_closed(): libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS, key, mouse) render_all(con, entities, game_map, screen_width, screen_height, colors) libtcod.console_flush() clear_all(con, entities) action = handle_keys(key) move = action.get('move') exit = action.get('exit') fullscreen = action.get('fullscreen') if move: dx, dy = move if not game_map.is_blocked(player.x + dx, player.y + dy): player.move(dx, dy) if exit: return True if fullscreen: libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen())
def main(): screen_width = 80 screen_height = 50 map_width = 80 map_height = 45 colors = {'dark_wall': (0, 0, 100), 'dark_ground': (50, 50, 150)} player = Entity(int(screen_width / 2), int(screen_height / 2), '@', (255, 255, 255)) npc = Entity(int(screen_width / 2 - 5), int(screen_height / 2), '@', (255, 255, 0)) entities = [npc, player] tdl.set_font('arial10x10.png', greyscale=True, altLayout=True) root_console = tdl.init(screen_width, screen_height, title='Roguelike Tutorial Revised') con = tdl.Console(screen_width, screen_height) game_map = tdl.map.Map(map_width, map_height) make_map(game_map) while not tdl.event.is_window_closed(): render_all(con, entities, game_map, root_console, screen_width, screen_height, colors) tdl.flush() clear_all(con, entities) for event in tdl.event.get(): if event.type == 'KEYDOWN': user_input = event break else: user_input = None if not user_input: continue action = handle_keys(user_input) move = action.get('move') exit = action.get('exit') fullscreen = action.get('fullscreen') if move: dx, dy = move if game_map.walkable[player.x + dx, player.y + dy]: player.move(dx, dy) if exit: return True if fullscreen: tdl.set_fullscreen(not tdl.get_fullscreen())
def main(): screen_width = 80 screen_height = 50 map_width = 80 map_height = 45 colors = { "dark_wall": libtcod.Color(0, 0, 100), "dark_ground": libtcod.Color(50, 50, 150) } player = Entity(int(screen_width / 2), int(screen_height / 2), "@", libtcod.fuchsia) npc = Entity(int(screen_width / 2 - 5), int(screen_height / 2), "@", libtcod.yellow) entities = [npc, player] # http://roguecentral.org/doryen/data/libtcod/doc/1.5.1/html2/console_set_custom_font.html?c=false&cpp=false&cs=false&py=true&lua=false libtcod.console_set_custom_font( "arial10x10.png", libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD) # Creates the window, title, and fullscreen libtcod.console_init_root(screen_width, screen_height, "B@rd", False) # Draw a new console con = libtcod.console_new(screen_width, screen_height) game_map = GameMap(map_width, map_height) # Holds keyboard and mouse input key = libtcod.Key() mouse = libtcod.Mouse() # Game loop (until screen is closed) while not libtcod.console_is_window_closed(): libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS, key, mouse) render_all(con, entities, game_map, screen_width, screen_height, colors) libtcod.console_flush() clear_all(con, entities) action = handle_keys(key) move = action.get("move") exit = action.get("exit") fullscreen = action.get("fullscreen") if move: dx, dy = move if not game_map.is_blocked(player.x + dx, player.y + dy): player.move(dx, dy) if exit: return True if fullscreen: libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen())
def main(): screen_width = 80 screen_height = 50 map_width = 80 map_height = 45 room_max_size = 10 room_min_size = 6 max_rooms = 30 colors = { 'dark_wall': libtcod.Color(0, 0 ,100), 'dark_ground': libtcod.Color(50, 50, 150) } #Player and NPC settings. Defines entities player = Entity(int(screen_width / 2), int(screen_height / 2), '@', libtcod.white) npc = Entity(int(screen_width / 2 - 5), int(screen_height / 2), '@', libtcod.yellow) entities = [npc, player] #Sets font img libtcod.console_set_custom_font('arial10x10.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD) #Init for root console(Width, Height, Window name, fullscreen) libtcod.console_init_root(screen_width, screen_height, 'TepisRL', False) #Consoles con = libtcod.console_new(screen_width, screen_height) #Calls map gen game_map = GameMap(map_width, map_height) game_map.make_map(max_rooms, room_min_size, room_max_size, map_width, map_height, player) #Calls key functions key = libtcod.Key() mouse = libtcod.Mouse() #Game loop while not libtcod.console_is_window_closed(): libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS, key, mouse) render_all(con, entities, game_map, screen_width, screen_height, colors) libtcod.console_flush() clear_all(con, entities) #Handles recognition of keypresses for movement action = handle_keys(key) move = action.get('move') exit = action.get('exit') fullscreen = action.get('fullscreen') if move: dx, dy = move if not game_map.is_blocked(player.x + dx, player.y + dy): player.move(dx, dy) if exit: return True if fullscreen: libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen())
def main(): screen_width = 80 screen_height = 50 map_width = 80 map_height = 45 colors = { 'dark_wall': libtcod.Color(0, 0, 100), 'dark_ground': libtcod.Color(50, 50, 150) } player = Entity(int(screen_width / 2), int(screen_height / 2), '@', libtcod.green) npc = Entity(int(screen_width / 2 - 5), int(screen_height / 2), '@', libtcod.yellow) entities = [npc, player] libtcod.console_set_custom_font( 'arial10x10.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD) libtcod.console_init_root(screen_width, screen_height, 'libtcod tutorial revised', False) con = libtcod.console_new(screen_width, screen_height) game_map = GameMap(map_width, map_height) key = libtcod.Key() mouse = libtcod.Mouse() while not libtcod.console_is_window_closed(): libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS, key, mouse) render_all(con, entities, game_map, screen_width, screen_height, colors) libtcod.console_flush() clear_all(con, entities) action = handle_keys(key) move = action.get('move') exit = action.get('exit') fullscreen = action.get('fullscreen') if move: dx, dy = move player.move(dx, dy) if exit: return True if fullscreen: libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen())
def main(): tcod.sys_set_fps(20) # Prevents 100% CPU usage screen_width = 80 screen_height = 50 map_width = 80 map_height = 45 colors = { # Colors for objects outside of FOV 'dark_wall': tcod.Color(0, 0, 100), 'dark_ground': tcod.Color(50, 50, 150) } player = Entity(int(screen_width / 2), int(screen_height / 2), '@', tcod.white) npc = Entity(int(screen_width / 2 - 5), int(screen_height / 2), '@', tcod.yellow) entities = [npc, player] tcod.console_set_custom_font('arial10x10.png', tcod.FONT_TYPE_GREYSCALE | tcod.FONT_LAYOUT_TCOD) tcod.console_init_root(screen_width, screen_height, 'RoguePy', False) # Last boolean determines if game is fullscrean con = tcod.console_new(screen_width, screen_height) game_map = GameMap(map_width, map_height) key = tcod.Key() mouse = tcod.Mouse() # Game loop while not tcod.console_is_window_closed(): tcod.sys_check_for_event(tcod.EVENT_KEY_PRESS, key, mouse) render_all(con, entities, game_map, screen_width, screen_height, colors) tcod.console_flush() clear_all(con, entities) action = handle_keys(key) move = action.get('move') exit = action.get('exit') fullscreen = action.get('fullscreen') if move: dx, dy = move if not game_map.is_blocked(player.x + dx, player.y + dy): player.move(dx, dy) if exit: return True if fullscreen: tcod.console_set_fullscreen(not tcod.console_is_fullscreen())
def main(): libtcod.console_set_custom_font('arial10x10.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD) libtcod.console_init_root(screen_width, screen_height, 'libtcod tutorial revised', False) con = libtcod.console_new(screen_width, screen_height) dialog_prompt = DialogPrompt(dialog_pos_x, dialog_pos_y, "npc_dialog", dialog_width, dialog_height, con) game_map = GameMap(map_width, map_height) game_map.switch_map(0) fov_recomputer = True game_entities = [] dialog_entities = [] player = GameObject(3, 3, '@', libtcod.white, "Hero", True) npc = GameObject(int(screen_width / 2 - 5), int(screen_height / 2), '@', libtcod.yellow, "Bad Guy", True) game_entities.append(player) game_entities.append(npc) key= libtcod.Key() mouse = libtcod.Mouse() libtcod.console_set_window_title(game_title+ " - " + game_map.map_name) while not libtcod.console_is_window_closed(): libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS, key, mouse) draw_all(con, game_entities, game_map, screen_width, screen_height) libtcod.console_flush() clear_all(con, game_entities) if key.c == ord('a'): dialog_prompt.npc_dialog('main') action = handle_keys(key) move = action.get('move') exit = action.get('exit') if move: dx, dy = move if not game_map.is_blocked(player.x + dx, player.y + dy) and not game_map.is_transport(player.x + dx, player.y + dy): game_map.unblock(player.x, player.y) player.move(dx,dy) elif game_map.is_transport(player.x + dx, player.y + dy): transport = game_map.spaces[player.x + dx][player.y + dy].transport game_map.switch_map(transport.new_map_index) libtcod.console_set_window_title( game_title + " - " + game_map.map_name) game_map.unblock(player.x, player.y) player.move(dx , dy ) player.move(transport.dx, transport.dy) if key.vk == libtcod.KEY_ESCAPE: return True game_map.update_blocked(game_entities)
def main(): screen_width = 80 screen_height = 50 map_width = 80 map_height = 45 colors = { "dark_wall": libtcod.Color(0, 0, 100), "dark_ground": libtcod.Color(50, 50, 150) } player = Entity(screen_width // 2, screen_height // 2, '@', libtcod.white) npc = Entity(screen_width // 2 - 5, screen_height // 2, '@', libtcod.red) entities = [npc, player] libtcod.console_set_custom_font("arial10x10.png", libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD) libtcod.console_init_root(screen_width, screen_height, "libtcod tutorial revisited", False) con = libtcod.console_new(screen_width, screen_height) game_map = GameMap(map_width, map_height) key = libtcod.Key() mouse = libtcod.Mouse() while not libtcod.console_is_window_closed(): libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS, key, mouse) render_all(con, entities, game_map, screen_width, screen_height, colors) libtcod.console_flush() clear_all(con, entities) action = handle_keys(key) move = action.get('move') playerquit = action.get('exit') fullscreen = action.get('fullscreen') if move: dx, dy = move if not game_map.is_blocked(player.x + dx, player.y + dy): player.move(dx, dy) if playerquit: return True if fullscreen: libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen())
def main(): screen_width = 80 screen_height = 50 player = Entity(screen_width // 2, screen_height // 2, '@', libtcod.white) npc = Entity(screen_width // 2 - 5, screen_height // 2, '@', libtcod.yellow) entities = [npc, player] libtcod.console_set_custom_font( 'arial10x10.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD) libtcod.console_init_root(screen_width, screen_height, 'libtcod tutorial', False) con = libtcod.console_new(screen_width, screen_height) key = libtcod.Key() mouse = libtcod.Mouse() while not libtcod.console_is_window_closed(): libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS, key, mouse) render_all(con, entities, screen_width, screen_height) libtcod.console_flush() clear_all(con, entities) action = handle_keys(key) move = action.get( 'move' ) #returns relative coordinates as a tuple if the action was 'move' exit_game = action.get('exit') #gets True if action was 'exit' fullscreen = action.get( 'fullscreen') #gets True if action was 'fullscreen' if move: dx, dy = move # player_x += dx # player_y += dy player.move(dx, dy) # if key.vk == libtcod.KEY_ESCAPE: if exit_game: return True if fullscreen: libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen())
def main(): screen_width = 80 screen_height = 50 player_x = int(screen_width / 2) player_y = int(screen_height / 2) map_width = 80 map_height = 45 player = Entity(player_x, player_y, '@', libtcod.white) npc = Entity(int(screen_width/2-5), int(screen_height/2-5), '@', libtcod.yellow) entities = [player, npc] libtcod.console_set_custom_font('arial10x10.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD) libtcod.console_init_root(screen_width, screen_height, 'libtcod tutorial revised', False) con = libtcod.console_new(screen_width, screen_height) key = libtcod.Key() mouse = libtcod.Mouse() while not libtcod.console_is_window_closed(): libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS, key, mouse) render_all(con, entities, screen_width, screen_height) libtcod.console_flush() clear_all(con, entities) action = handle_keys(key) move = action.get('move') exit = action.get('exit') fullscreen = action.get('fullscreen') if move: dx, dy = move player.move(dx, dy) if exit: return True if fullscreen: libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen)
def render_refresh(con, panel, mouse, entities, player, game_map, fov_map, fov_recompute, message_log, constants, game_state, names_list, colors_list): 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'], constants['options_tutorial_enabled'], game_state, names_list, colors_list, constants['tick'], constants['tick_speed']) libtcod.console_flush() clear_all(con, entities)
def play_game(player, entities, game_map, message_log, game_state, con, panel, constants): fov_recompute = True fov_map = initialize_fov(game_map) game_state = GameStates.PLAYERS_TURN previous_game_state = game_state targeting_item = None while not libtcod.console_is_window_closed(): libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS | libtcod.EVENT_MOUSE, key, mouse) if fov_recompute: message_log.add_message(Message('MESSAGE SCROLLING', libtcod.black)) # message log scrolling recompute_fov(fov_map, player.x, player.y, constants['fov_radius'], constants['fov_light_walls'], constants['fov_algorithm']) dead_check = player.fighter.hp if dead_check <= 0: message, game_state = kill_player(player) 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'], key,mouse, constants['colors'], game_state) fov_recompute = False libtcod.console_flush() clear_all(con, entities) #Consume player actions action,entities,fov_map,fov_recompute,game_map,game_state,message_log,mouse_action,player,player_turn_results,previous_game_state,exit_pressed = consume_actions(key,mouse,game_state,player,game_map, entities,fov_recompute,fov_map,message_log,constants,con,targeting_item,previous_game_state) if exit_pressed: return True #Consume action results for player_turn_result in player_turn_results: available_results,message_log,message,game_state,entities,player,game_state,targeting_item, previous_game_state = consume_results(player_turn_result,message_log,entities,player,game_state,targeting_item, previous_game_state) #consume NPC actions player,message_log,game_state = handle_npc_turn(game_state,player,entities,fov_map,game_map,message_log)
def rendering_proc(player, entities, game_map, message_log, game_state, con, panel, constants, fov_map, mouse, fov_recompute=False): 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'], constants['colors'], game_state, mouse) fov_recompute = False libtcod.console_flush() clear_all(con, entities)
def play_game(player, entities, game_map, message_log, game_state, con, panel, constants): fov_recompute = True fov_map = initialize_fov(game_map) key = libtcod.Key() mouse = libtcod.Mouse() game_state = GameStates.PLAYERS_TURN previous_game_state = game_state targeting_item = None while not libtcod.console_is_window_closed(): libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS | libtcod.EVENT_MOUSE, key, mouse) if fov_recompute: recompute_fov(fov_map, player.x, player.y, constants['fov_radius'], constants['fov_light_walls'], constants['fov_algorithm']) render_all(con, panel, entities, player, game_map, fov_map, fov_recompute, message_log, constants['screen_width'], constants['screen_height'], constants['bar_width'], constants['panel_height'], constants['panel_y'], mouse, constants['colors'], game_state) fov_recompute = False libtcod.console_flush() clear_all(con, entities) action = handle_keys(key, game_state) mouse_action = handle_mouse(mouse) move = action.get('move') wait = action.get('wait') pickup = action.get('pickup') show_inventory = action.get('show_inventory') drop_inventory = action.get('drop_inventory') inventory_index = action.get('inventory_index') take_stairs = action.get('take_stairs') level_up = action.get('level_up') show_character_screen = action.get('show_character_screen') exit = action.get('exit') fullscreen = action.get('fullscreen') left_click = mouse_action.get('left_click') right_click = mouse_action.get('right_click') player_turn_results = [] if move and game_state == GameStates.PLAYERS_TURN: dx, dy = move destination_x = player.x + dx destination_y = player.y + dy if not game_map.is_blocked(destination_x, destination_y): target = get_blocking_entities_at_location(entities, destination_x, destination_y) if target: attack_results = player.fighter.attack(target) player_turn_results.extend(attack_results) else: player.move(dx, dy) fov_recompute = True game_state = GameStates.ENEMY_TURN elif wait: game_state = GameStates.ENEMY_TURN elif pickup and game_state == GameStates.PLAYERS_TURN: for entity in entities: if entity.item and entity.x == player.x and entity.y == player.y: pickup_results = player.inventory.add_item(entity) player_turn_results.extend(pickup_results) break else: message_log.add_message(Message('There is nothing here to pick up.', libtcod.yellow)) if show_inventory: previous_game_state = game_state game_state = GameStates.SHOW_INVENTORY if drop_inventory: previous_game_state = game_state game_state = GameStates.DROP_INVENTORY if inventory_index is not None and previous_game_state != GameStates.PLAYER_DEAD and inventory_index < len( player.inventory.items): item = player.inventory.items[inventory_index] if game_state == GameStates.SHOW_INVENTORY: player_turn_results.extend(player.inventory.use(item, entities=entities, fov_map=fov_map)) elif game_state == GameStates.DROP_INVENTORY: player_turn_results.extend(player.inventory.drop_item(item)) if take_stairs and game_state == GameStates.PLAYERS_TURN: for entity in entities: if entity.stairs and entity.x == player.x and entity.y == player.y: entities = game_map.next_floor(player, message_log, constants) fov_map = initialize_fov(game_map) fov_recompute = True libtcod.console_clear(con) break else: message_log.add_message(Message('There are no stairs here.', libtcod.yellow)) if level_up: if level_up == 'hp': player.fighter.max_hp += 20 player.fighter.hp += 20 elif level_up == 'str': player.fighter.power += 1 elif level_up == 'def': player.fighter.defense += 1 game_state = previous_game_state if show_character_screen: previous_game_state = game_state game_state = GameStates.CHARACTER_SCREEN if game_state == GameStates.TARGETING: if left_click: target_x, target_y = left_click item_use_results = player.inventory.use(targeting_item, entities=entities, fov_map=fov_map, target_x=target_x, target_y=target_y) player_turn_results.extend(item_use_results) elif right_click: player_turn_results.append({'targeting_cancelled': True}) if exit: if game_state in (GameStates.SHOW_INVENTORY, GameStates.DROP_INVENTORY, GameStates.CHARACTER_SCREEN): game_state = previous_game_state elif game_state == GameStates.TARGETING: player_turn_results.append({'targeting_cancelled': True}) else: save_game(player, entities, game_map, message_log, game_state) return True if fullscreen: libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen()) for player_turn_result in player_turn_results: message = player_turn_result.get('message') dead_entity = player_turn_result.get('dead') item_added = player_turn_result.get('item_added') item_consumed = player_turn_result.get('consumed') item_dropped = player_turn_result.get('item_dropped') targeting = player_turn_result.get('targeting') targeting_cancelled = player_turn_result.get('targeting_cancelled') xp = player_turn_result.get('xp') if message: message_log.add_message(message) if dead_entity: if dead_entity == player: message, game_state = kill_player(dead_entity) else: message = kill_monster(dead_entity) message_log.add_message(message) if item_added: entities.remove(item_added) game_state = GameStates.ENEMY_TURN if item_consumed: game_state = GameStates.ENEMY_TURN if item_dropped: entities.append(item_dropped) game_state = GameStates.ENEMY_TURN if targeting: previous_game_state = GameStates.PLAYERS_TURN game_state = GameStates.TARGETING targeting_item = targeting message_log.add_message(targeting_item.item.targeting_message) if targeting_cancelled: game_state = previous_game_state message_log.add_message(Message('Targeting cancelled')) if xp: leveled_up = player.level.add_xp(xp) message_log.add_message(Message('You gain {0} experience points.'.format(xp))) if leveled_up: message_log.add_message(Message( 'Your battle skills grow stronger! You reached level {0}'.format( player.level.current_level) + '!', libtcod.yellow)) previous_game_state = game_state game_state = GameStates.LEVEL_UP if game_state == GameStates.ENEMY_TURN: 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 play_game(player, entities, game_map, message_log, game_state, con, panel, constants): fov_recompute = True fov_map = initialize_fov(game_map) key = libtcod.Key() mouse = libtcod.Mouse() if not game_state == GameStates.PLAYER_DEAD: game_state = GameStates.PLAYERS_TURN begin_player_turn = True previous_game_state = game_state targeting_item = None if not game_state == GameStates.PLAYER_DEAD: PLAYERDEADSTATE = False else: PLAYERDEADSTATE = True 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'], constants['kill_count'], game_state, constants['wall_tile'], constants['floor_tile']) 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') wait = action.get('wait') pickup = action.get('pickup') show_inventory = action.get('show_inventory') drop_inventory = action.get('drop_inventory') drop_equipment = action.get('drop_equipment') show_equipment_inventory = action.get('show_equipment_inventory') show_bag = action.get('show_bag') inventory_index = action.get('inventory_index') equipment_inventory_index = action.get('equipment_inventory_index') take_stairs = action.get('take_stairs') level_up = action.get('level_up') show_character_screen = action.get('show_character_screen') show_help_menu = action.get('show_help_menu') exit = action.get('exit') exit_quit_menu = action.get('exit_quit_menu') fullscreen = action.get('fullscreen') cast_magic_wand = action.get('cast_magic_wand') shoot_bow = action.get('shoot_bow') drop_menu = action.get('drop_menu') sell_menu = action.get('sell_menu') sell_equipment_menu = action.get('sell_equipment_menu') buy_menu = action.get('buy_menu') buy_equipment_menu = action.get('buy_equipment_menu') shop_menu = action.get('shop_menu') shop_menu_index = action.get('shop_menu_index') shop_equipment_menu_index = action.get('shop_equipment_menu_index') left_click = mouse_action.get('left_click') right_click = mouse_action.get('right_click') player_turn_results = [] if begin_player_turn and game_state == GameStates.PLAYERS_TURN: begin_player_turn = False if player.fighter.status: player_turn_results.extend(player.fighter.status.update()) 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, constants, entities=entities) #playsound('sounds/attack.m4a', block=False) player_turn_results.extend(attack_results) else: player.move(dx, dy) fov_recompute = True game_state = GameStates.ENEMY_TURN elif wait: game_state = GameStates.ENEMY_TURN elif pickup and game_state == GameStates.PLAYERS_TURN: for entity in entities: if entity.item and ( not entity.equippable ) and entity.x == player.x and entity.y == player.y: pickup_results = player.inventory.add_item(entity) player_turn_results.extend(pickup_results) break elif entity.item and entity.x == player.x and entity.y == player.y: pickup_results = player.equipment_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 show_equipment_inventory: previous_game_state = game_state game_state = GameStates.SHOW_EQUIPMENT_INVENTORY if drop_inventory: previous_game_state = game_state game_state = GameStates.DROP_INVENTORY if drop_equipment: previous_game_state = game_state game_state = GameStates.DROP_EQUIPMENT if show_bag: previous_game_state = game_state game_state = GameStates.SHOW_BAG if drop_menu: previous_game_state = game_state game_state = GameStates.DROP_MENU 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)) elif game_state == GameStates.SELL_MENU: player_turn_results.extend( player.inventory.sell(item, game_state)) if equipment_inventory_index is not None and previous_game_state != GameStates.PLAYER_DEAD and equipment_inventory_index < len( player.equipment_inventory.items): equip_item = player.equipment_inventory.items[ equipment_inventory_index] if game_state == GameStates.SHOW_EQUIPMENT_INVENTORY: player_turn_results.extend( player.equipment_inventory.use(equip_item)) elif game_state == GameStates.DROP_EQUIPMENT: player_turn_results.extend( player.equipment_inventory.drop_item(equip_item)) elif game_state == GameStates.SELL_EQUIPMENT_MENU: player_turn_results.extend( player.equipment_inventory.sell(equip_item, game_state)) if shop_menu_index is not None and previous_game_state != GameStates.PLAYER_DEAD: item = game_map.shop_items[shop_menu_index] if game_state == GameStates.BUY_MENU: player_turn_results.extend( player.inventory.buy(item, game_state)) if shop_equipment_menu_index is not None and previous_game_state != GameStates.PLAYER_DEAD: item = game_map.shop_equipment_items[shop_equipment_menu_index] if game_state == GameStates.BUY_EQUIPMENT_MENU: player_turn_results.extend( player.equipment_inventory.buy(item, game_state)) if take_stairs and game_state == GameStates.PLAYERS_TURN: for entity in entities: if entity.stairs and entity.x == player.x and entity.y == player.y: entities = game_map.next_floor(player, message_log, constants) fov_map = initialize_fov(game_map) fov_recompute = True libtcod.console_clear(con) break else: message_log.add_message( Message("There are no stairs here...", libtcod.yellow)) if cast_magic_wand and game_state == GameStates.PLAYERS_TURN: wand = player.inventory.search("Magic Wand") staff = player.inventory.search("Wizard Staff") if wand is None and staff is None: message_log.add_message( Message("You cannot cast magic without a magic item!", libtcod.orange)) else: player_turn_results.extend( player.inventory.use(wand, entities=entities, fov_map=fov_map)) game_state = GameStates.ENEMY_TURN if shoot_bow and game_state == GameStates.PLAYERS_TURN: bow = player.inventory.search("Long Bow") arrow = player.inventory.search("Arrow") if bow is None and arrow is None: message_log.add_message( Message( "You don't have anything to shoot with at this time!", libtcod.orange)) elif bow is None and arrow is not None: message_log.add_message( Message("You cannot shoot an arrow without a bow!", libtcod.orange)) elif bow is not None and arrow is None: message_log.add_message( Message("You need arrows to use your bow", libtcod.orange)) else: player_turn_results.extend( player.inventory.use(bow, entities=entities, fov_map=fov_map)) game_state = GameStates.ENEMY_TURN if level_up: if level_up == 'hp': player.fighter.base_max_hp += 20 player.fighter.hp += 20 message_log.add_message( Message("You leveled up your HP!", libtcod.light_cyan)) elif level_up == 'str': player.fighter.base_power += 1 message_log.add_message( Message("You leveled up your ATTACK!", libtcod.light_cyan)) elif level_up == 'def': player.fighter.base_defense += 1 message_log.add_message( Message("You leveled up your DEFENSE!", libtcod.light_cyan)) elif level_up == 'mgk': player.fighter.base_magic += 1 message_log.add_message( Message("You leveled up your MAGIC!", libtcod.light_cyan)) elif level_up == 'mgk_def': player.fighter.base_magic_defense += 1 message_log.add_message( Message("You leveled up your MAGIC RESISTANCE!", libtcod.light_cyan)) game_state = previous_game_state if show_character_screen: previous_game_state = game_state game_state = GameStates.CHARACTER_SCREEN if show_help_menu: previous_game_state = game_state game_state = GameStates.HELP_MENU if sell_menu: previous_game_state = game_state game_state = GameStates.SELL_MENU if sell_equipment_menu: previous_game_state = game_state game_state = GameStates.SELL_EQUIPMENT_MENU if buy_menu: previous_game_state = game_state game_state = GameStates.BUY_MENU if buy_equipment_menu: previous_game_state = game_state game_state = GameStates.BUY_EQUIPMENT_MENU if shop_menu: previous_game_state = game_state game_state = GameStates.SHOP_MENU if game_state == GameStates.TARGETING: if left_click: target_x, target_y = left_click item_use_results = player.inventory.use(targeting_item, entities=entities, fov_map=fov_map, target_x=target_x, target_y=target_y) player_turn_results.extend(item_use_results) elif right_click: player_turn_results.append({'targeting_cancelled': True}) if exit: if game_state in (GameStates.SHOW_INVENTORY, GameStates.DROP_INVENTORY, GameStates.DROP_EQUIPMENT, GameStates.CHARACTER_SCREEN, GameStates.HELP_MENU, GameStates.SHOW_EQUIPMENT_INVENTORY, GameStates.SELL_MENU, GameStates.BUY_MENU, GameStates.SELL_EQUIPMENT_MENU, GameStates.BUY_EQUIPMENT_MENU): game_state = previous_game_state elif game_state == GameStates.TARGETING: player_turn_results.append({'targeting_cancelled': True}) elif game_state == GameStates.SHOW_BAG: if PLAYERDEADSTATE == True: game_state = GameStates.PLAYER_DEAD else: game_state = GameStates.PLAYERS_TURN elif game_state == GameStates.SHOP_MENU: if PLAYERDEADSTATE == True: game_state = GameStates.PLAYER_DEAD else: game_state = GameStates.PLAYERS_TURN elif game_state == GameStates.PLAYERS_TURN: game_state = GameStates.QUIT_MENU elif game_state == GameStates.DROP_MENU: game_state = GameStates.PLAYERS_TURN else: save_game(player, entities, game_map, message_log, game_state) return True if exit_quit_menu: if game_state == GameStates.QUIT_MENU: game_state = previous_game_state 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') equipment_item_added = player_turn_result.get( 'equipment_item_added') item_consumed = player_turn_result.get('consumed') equipment_consumed = player_turn_result.get('equipment_consumed') item_dropped = player_turn_result.get('item_dropped') loot_dropped = player_turn_result.get('loot_dropped') staff_used = player_turn_result.get('staff_used') equip = player_turn_result.get('equip') targeting = player_turn_result.get('targeting') targeting_cancelled = player_turn_result.get('targeting_cancelled') xp = player_turn_result.get('xp') item_bought = player_turn_result.get('item_bought') equipment_bought = player_turn_result.get('equipment_bought') end_turn = player_turn_result.get('end_turn') if message: message_log.add_message(message) if targeting_cancelled: game_state = previous_game_state message_log.add_message(Message('Targeting cancelled')) if xp: leveled_up = player.level.add_xp(xp) message_log.add_message( Message("You gain {0} experience points.".format(xp))) if leveled_up: message_log.add_message( Message( "Your battle prowess grows stronger! You reached level {0}!" .format(player.level.current_level), libtcod.yellow)) previous_game_state = game_state game_state = GameStates.LEVEL_UP if dead_entity: if dead_entity == player: PLAYERDEADSTATE = True message, game_state = kill_player(dead_entity, constants) message_log.add_message(message) else: monster_name = '' monster_name = dead_entity.name message = kill_monster(dead_entity, player, constants) constants['kill_count'] += 1 message_log.add_message(message) while dead_entity.equipment_inventory.items: item = dead_entity.equipment_inventory.items[0] dead_entity.equipment_inventory.loot_item(item) entities.append(item) message_log.add_message( Message( "The {0} dropped the {1}.".format( monster_name, item.name), libtcod.yellow)) while dead_entity.inventory.items: item = dead_entity.inventory.items[0] dead_entity.inventory.loot_item(item) entities.append(item) message_log.add_message( Message( "The {0} dropped the {1}.".format( monster_name, item.name), libtcod.yellow)) if item_added: entities.remove(item_added) game_state = GameStates.ENEMY_TURN if equipment_item_added: entities.remove(equipment_item_added) game_state = GameStates.ENEMY_TURN if item_consumed: game_state = GameStates.ENEMY_TURN if item_bought: game_map.shop_items.remove(item_bought) game_state = GameStates.ENEMY_TURN if equipment_bought: game_map.shop_equipment_items.remove(equipment_bought) game_state = GameStates.ENEMY_TURN if equipment_consumed: game_state = GameStates.ENEMY_TURN if staff_used: game_state = GameStates.ENEMY_TURN if end_turn: 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 loot_dropped: entities.append(loot_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 if game_state == GameStates.ENEMY_TURN: fov_recompute = True for entity in entities: if entity.ai: if entity.fighter.status: entity.fighter.status.update() enemy_turn_results = entity.ai.take_turn( player, fov_map, game_map, entities, constants) 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: PLAYERDEADSTATE = True message, game_state = kill_player( dead_entity, constants) message_log.add_message(message) else: monster_name = '' monster_name = dead_entity.name message = kill_monster(dead_entity, player, constants) constants['kill_count'] += 1 message_log.add_message(message) while dead_entity.equipment_inventory.items: item = dead_entity.equipment_inventory.items[ 0] dead_entity.equipment_inventory.loot_item( item) entities.append(item) message_log.add_message( Message( "The {0} dropped the {1}.".format( monster_name, item.name), libtcod.yellow)) while dead_entity.inventory.items: item = dead_entity.inventory.items[0] dead_entity.inventory.loot_item(item) entities.append(item) message_log.add_message( Message( "The {0} dropped the {1}.".format( monster_name, item.name), libtcod.yellow)) if game_state == GameStates.PLAYER_DEAD: break if game_state == GameStates.PLAYER_DEAD: break else: game_state = GameStates.PLAYERS_TURN begin_player_turn = True
def play_game(player, entities, game_map, message_log, game_state, root_console, con, panel, constants): tdl.set_font('arial10x10.png', greyscale=True, altLayout=True) fov_recompute = True mouse_coordinates = (0, 0) previous_game_state = game_state targeting_item = None while not tdl.event.is_window_closed(): if fov_recompute: game_map.compute_fov(player.x, player.y, fov=constants['fov_algorithm'], radius=constants['fov_radius'], light_walls=constants['fov_light_walls']) render_all(con, panel, entities, player, game_map, fov_recompute, root_console, message_log, constants['screen_width'], constants['screen_height'], constants['bar_width'], constants['panel_height'], constants['panel_y'], mouse_coordinates, constants['colors'], game_state) tdl.flush() clear_all(con, entities) fov_recompute = False for event in tdl.event.get(): if event.type == 'KEYDOWN': user_input = event break elif event.type == 'MOUSEMOTION': mouse_coordinates = event.cell elif event.type == 'MOUSEDOWN': user_mouse_input = event break else: user_input = None user_mouse_input = None if not (user_input or user_mouse_input): continue action = handle_keys(user_input, game_state) mouse_action = handle_mouse(user_mouse_input) move = action.get('move') wait = action.get('wait') pickup = action.get('pickup') show_inventory = action.get('show_inventory') drop_inventory = action.get('drop_inventory') inventory_index = action.get('inventory_index') take_stairs = action.get('take_stairs') level_up = action.get('level_up') show_character_screen = action.get('show_character_screen') exit = action.get('exit') fullscreen = action.get('fullscreen') left_click = mouse_action.get('left_click') right_click = mouse_action.get('right_click') player_turn_results = [] if move and game_state == GameStates.PLAYERS_TURN: dx, dy = move destination_x = player.x + dx destination_y = player.y + dy if game_map.walkable[destination_x, destination_y]: target = get_blocking_entities_at_location( entities, destination_x, destination_y) if target: attack_results = player.fighter.attack(target) player_turn_results.extend(attack_results) else: player.move(dx, dy) fov_recompute = True game_state = GameStates.ENEMY_TURN elif wait: game_state = GameStates.ENEMY_TURN elif pickup and game_state == GameStates.PLAYERS_TURN: for entity in entities: if entity.item and entity.x == player.x and entity.y == player.y: pickup_results = player.inventory.add_item( entity, constants['colors']) player_turn_results.extend(pickup_results) break else: message_log.add_message( Message('There is nothing here to pick up.', constants['colors'].get('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, constants['colors'], entities=entities, game_map=game_map)) elif game_state == GameStates.DROP_INVENTORY: player_turn_results.extend( player.inventory.drop_item(item, constants['colors'])) 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: game_map, entities = next_floor(player, message_log, entity.stairs.floor, constants) fov_recompute = True con.clear() break else: message_log.add_message( Message('There are no stairs here.', constants['colors'].get('yellow'))) if level_up: if level_up == 'hp': player.fighter.base_max_hp += 20 player.fighter.hp += 20 elif level_up == 'str': player.fighter.base_power += 1 elif level_up == 'def': player.fighter.base_defense += 1 game_state = previous_game_state if show_character_screen: previous_game_state = game_state game_state = GameStates.CHARACTER_SCREEN if game_state == GameStates.TARGETING: if left_click: target_x, target_y = left_click item_use_results = player.inventory.use(targeting_item, constants['colors'], entities=entities, game_map=game_map, target_x=target_x, target_y=target_y) player_turn_results.extend(item_use_results) elif right_click: player_turn_results.append({'targeting_cancelled': True}) if exit: if game_state in (GameStates.SHOW_INVENTORY, GameStates.DROP_INVENTORY, GameStates.CHARACTER_SCREEN): game_state = previous_game_state elif game_state == GameStates.TARGETING: player_turn_results.append({'targeting_cancelled': True}) else: save_game(player, entities, game_map, message_log, game_state) return True if fullscreen: tdl.set_fullscreen(not tdl.get_fullscreen()) for player_turn_result in player_turn_results: message = player_turn_result.get('message') dead_entity = player_turn_result.get('dead') item_added = player_turn_result.get('item_added') item_consumed = player_turn_result.get('consumed') item_dropped = player_turn_result.get('item_dropped') equip = player_turn_result.get('equip') targeting = player_turn_result.get('targeting') targeting_cancelled = player_turn_result.get('targeting_cancelled') xp = player_turn_result.get('xp') if message: message_log.add_message(message) if dead_entity: if dead_entity == player: message, game_state = kill_player(dead_entity, constants['colors']) else: message = kill_monster(dead_entity, constants['colors']) message_log.add_message(message) if item_added: entities.remove(item_added) game_state = GameStates.ENEMY_TURN if item_consumed: game_state = GameStates.ENEMY_TURN if item_dropped: entities.append(item_dropped) game_state = GameStates.ENEMY_TURN if equip: equip_results = player.equipment.toggle_equip(equip) for equip_result in equip_results: equipped = equip_result.get('equipped') dequipped = equip_result.get('dequipped') if equipped: message_log.add_message( Message(f'You equipped the {equipped.name}')) if dequipped: message_log.add_message( Message(f'You dequipped the {dequipped.name}')) game_state = GameStates.ENEMY_TURN if targeting: previous_game_state = GameStates.PLAYERS_TURN game_state = GameStates.TARGETING targeting_item = targeting message_log.add_message(targeting_item.item.targeting_message) if targeting_cancelled: game_state = previous_game_state message_log.add_message(Message('Targeting cancelled')) if xp: leveled_up = player.level.add_xp(xp) message_log.add_message( Message('You gain {0} experience points.'.format(xp))) if leveled_up: message_log.add_message( Message( 'Your battle skills grow stronger! You reached level {0}' .format(player.level.current_level) + '!', constants['colors'].get('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, 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, constants['colors']) else: message = kill_monster(dead_entity, constants['colors']) message_log.add_message(message) if game_state == GameStates.PLAYER_DEAD: break if game_state == GameStates.PLAYER_DEAD: break else: game_state = GameStates.PLAYERS_TURN
def main(): screen_width = 80 screen_height = 50 bar_width = 20 panel_height = 7 panel_y = screen_height - panel_height message_x = bar_width + 2 message_width = screen_width - bar_width - 2 message_height = panel_height - 1 map_width = 80 map_height = 43 room_max_size = 10 room_min_size = 6 max_rooms = 30 fov_algorithm = 0 fov_light_walls = True fov_radius = 10 max_monsters_per_room = 3 max_items_per_room = 2 colors = { 'dark_wall' : libtcod.Color(0, 0, 100), 'dark_ground' : libtcod.Color(50, 50, 150), 'light_wall' : libtcod.Color(130, 110, 50), 'light_ground' : libtcod.Color(200, 180, 50) } fighter_component = Fighter(hp=30, defense=2, power=5) inventory_component = Inventory(26) player = Entity(0, 0, '@', libtcod.white, 'Player', blocks=True, render_order=RenderOrder.ACTOR, fighter=fighter_component, inventory=inventory_component) entities = [player] libtcod.console_set_custom_font('arial10x10.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD) libtcod.console_init_root(screen_width, screen_height, 'libtcod tutorial revised', False) con = libtcod.console_new(screen_width, screen_height) panel = libtcod.console_new(screen_width, panel_height) game_map = GameMap(map_width, map_height) game_map.make_map(max_rooms, room_min_size, room_max_size, map_width, map_height, player, entities, max_monsters_per_room, max_items_per_room) fov_recompute = True fov_map = initialize_fov(game_map) message_log = MessageLog(message_x, message_width, message_height) key = libtcod.Key() mouse = libtcod.Mouse() game_state = GameStates.PLAYERS_TURN previous_game_state = game_state while not libtcod.console_is_window_closed(): libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS | libtcod.EVENT_MOUSE, key, mouse) if fov_recompute: recompute_fov(fov_map, player.x, player.y, fov_radius, fov_light_walls, fov_algorithm) render_all(con, panel, entities, player, game_map, fov_map, fov_recompute, message_log, screen_width, screen_height, bar_width, panel_height, panel_y, mouse, colors, game_state) fov_recompute = False libtcod.console_flush() clear_all(con, entities) action = handle_keys(key, game_state) move = action.get('move') pickup = action.get('pickup') show_inventory = action.get('show_inventory') drop_inventory = action.get('drop_inventory') inventory_index = action.get('inventory_index') exit = action.get('exit') fullscreen = action.get('fullscreen') player_turn_results = [] if move and game_state == GameStates.PLAYERS_TURN: dx, dy = move destination_x = player.x + dx destination_y = player.y + dy if not game_map.is_blocked(destination_x, destination_y): target = get_blocking_entities_at_location(entities, destination_x, destination_y) if target: 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 pickup here.', 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 exit: if game_state in (GameStates.SHOW_INVENTORY, GameStates.DROP_INVENTORY): game_state = previous_game_state else: return True if fullscreen: libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen()) for player_turn_result in player_turn_results: message = player_turn_result.get('message') dead_entity = player_turn_result.get('dead') item_added = player_turn_result.get('item_added') item_consumed = player_turn_result.get('consumed') item_dropped = player_turn_result.get('item_dropped') if message: message_log.add_message(message) if dead_entity: if dead_entity == player: message, game_state = kill_player(dead_entity) else: message = kill_monster(dead_entity) message_log.add_message(message) if item_added: entities.remove(item_added) game_state = GameStates.ENEMY_TURN if item_consumed: game_state = GameStates.ENEMY_TURN if item_dropped: entities.append(item_dropped) game_state = GameStates.ENEMY_TURN if game_state == GameStates.ENEMY_TURN: for entity in entities: if entity.ai: enemy_turn_results = entity.ai.take_turn(player, fov_map, game_map, entities) for enemy_turn_result in enemy_turn_results: message = enemy_turn_result.get('message') dead_entity = enemy_turn_result.get('dead') if message: message_log.add_message(message) if dead_entity: if dead_entity == player: message, game_state = kill_player(dead_entity) else: message = kill_monster(dead_entity) message_log.add_message(message) if game_state == GameStates.PLAYER_DEAD: break if game_state == GameStates.PLAYER_DEAD: break else: game_state = GameStates.PLAYERS_TURN
def play_game(player, entities, game_map, message_log, game_state, con, panel, constants): # bool to store whether we update fov or not fov_recompute = True fov_map = initialize_fov(game_map) # key and mouse to capture input key = libtcod.Key() mouse = libtcod.Mouse() mouse_pos = 0 # set game state game_state = GameStates.PLAYERS_TURN previous_game_state = game_state # make sure set to none targeting_item = None #-------GAME LOOP------- while not libtcod.console_is_window_closed(): libtcod.sys_check_for_event( libtcod.EVENT_KEY_PRESS | libtcod.EVENT_MOUSE, key, mouse) # This is will update the mouse when it is moved. if mouse.x != mouse_pos: fov_recompute = True mouse_pos = mouse.x #if player doesn't move fov won't update. if fov_recompute: recompute_fov(fov_map, player.x, player.y, constants['fov_radius'], constants['fov_light_walls'], constants['fov_algorithm']) #update everything 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) #----ACTIONS----- move = action.get('move') pickup = action.get('pickup') show_inventory = action.get('show_inventory') inventory_index = action.get('inventory_index') drop_inventory = action.get('drop_inventory') 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 fov_recompute = True 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 #after player's turn set to enemy turn game_state = GameStates.ENEMY_TURN elif pickup and game_state == GameStates.PLAYERS_TURN: fov_recompute = True 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: fov_recompute = True previous_game_state = game_state game_state = GameStates.SHOW_INVENTORY if drop_inventory: fov_recompute = True 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): fov_recompute = True 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 fov_recompute = True elif game_state == GameStates.TARGETING: player_turn_results.append({'targeting_canclled': True}) else: # save when the game is exited save_game(player, entities, game_map, message_log, game_state) return True if fullscreen: libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen()) ##-----PLAYER TURN RESULTS 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) fov_recompute = True 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 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 game_state == GameStates.ENEMY_TURN: for entity in entities: if entity.ai: #if an entity object has an ai, it gets a turn. 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: #after all the enemies move, players turn game_state = GameStates.PLAYERS_TURN
def play_game(player, entities, game_map, message_log, game_state, con, panel, constants): fov_recompute = True fov_map = initialize_fov(game_map) key = libtcod.Key() mouse = libtcod.Mouse() game_state = GameStates.PLAYERS_TURN previous_game_state = game_state targeting_item = None while not libtcod.console_is_window_closed(): libtcod.sys_check_for_event( libtcod.EVENT_KEY_PRESS | libtcod.EVENT_MOUSE, key, mouse) if fov_recompute: recompute_fov(fov_map, player.x, player.y, constants['fov_radius'], constants['fov_light_walls'], constants['fov_algorithm']) render_all(con, panel, entities, player, game_map, fov_map, fov_recompute, message_log, constants['screen_width'], constants['screen_height'], constants['bar_width'], constants['panel_height'], constants['panel_y'], mouse, constants['colors'], game_state) fov_recompute = False libtcod.console_flush() clear_all(con, entities) action = handle_keys(key, game_state) mouse_action = handle_mouse(mouse) move = action.get('move') wait = action.get('wait') pickup = action.get('pickup') show_inventory = action.get('show_inventory') drop_inventory = action.get('drop_inventory') inventory_index = action.get('inventory_index') take_stairs = action.get('take_stairs') level_up = action.get('level_up') show_character_screen = action.get('show_character_screen') exit = action.get('exit') fullscreen = action.get('fullscreen') left_click = mouse_action.get('left_click') right_click = mouse_action.get('right_click') player_turn_results = [] if move and game_state == GameStates.PLAYERS_TURN: dx, dy = move destination_x = player.x + dx destination_y = player.y + dy if not game_map.is_blocked(destination_x, destination_y): target = get_blocking_entities_at_location( entities, destination_x, destination_y) if target: attack_results = player.fighter.attack(target) player_turn_results.extend(attack_results) else: player.move(dx, dy) fov_recompute = True game_state = GameStates.ENEMY_TURN elif wait: game_state = GameStates.ENEMY_TURN elif pickup and game_state == GameStates.PLAYERS_TURN: for entity in entities: if entity.item and entity.x == player.x and entity.y == player.y: pickup_results = player.inventory.add_item(entity) player_turn_results.extend(pickup_results) break else: message_log.add_message( Message('There is nothing here to pick up.', libtcod.yellow)) if show_inventory: previous_game_state = game_state game_state = GameStates.SHOW_INVENTORY if drop_inventory: previous_game_state = game_state game_state = GameStates.DROP_INVENTORY if inventory_index is not None and previous_game_state != GameStates.PLAYER_DEAD and inventory_index < len( player.inventory.items): item = player.inventory.items[inventory_index] if game_state == GameStates.SHOW_INVENTORY: player_turn_results.extend( player.inventory.use(item, entities=entities, fov_map=fov_map)) elif game_state == GameStates.DROP_INVENTORY: player_turn_results.extend(player.inventory.drop_item(item)) if take_stairs and game_state == GameStates.PLAYERS_TURN: for entity in entities: if entity.stairs and entity.x == player.x and entity.y == player.y: entities = game_map.next_floor(player, message_log, constants) fov_map = initialize_fov(game_map) fov_recompute = True libtcod.console_clear(con) break else: message_log.add_message( Message('There are no stairs here.', libtcod.yellow)) if level_up: if level_up == 'hp': player.fighter.base_max_hp += 20 player.fighter.hp += 20 elif level_up == 'str': player.fighter.base_power += 1 elif level_up == 'def': player.fighter.base_defense += 1 game_state = previous_game_state if show_character_screen: previous_game_state = game_state game_state = GameStates.CHARACTER_SCREEN if game_state == GameStates.TARGETING: if left_click: target_x, target_y = left_click item_use_results = player.inventory.use(targeting_item, entities=entities, fov_map=fov_map, target_x=target_x, target_y=target_y) player_turn_results.extend(item_use_results) elif right_click: player_turn_results.append({'targeting_cancelled': True}) if exit: if game_state in (GameStates.SHOW_INVENTORY, GameStates.DROP_INVENTORY, GameStates.CHARACTER_SCREEN): game_state = previous_game_state elif game_state == GameStates.TARGETING: player_turn_results.append({'targeting_cancelled': True}) else: save_game(player, entities, game_map, message_log, game_state) return True if fullscreen: libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen()) for player_turn_result in player_turn_results: message = player_turn_result.get('message') dead_entity = player_turn_result.get('dead') item_added = player_turn_result.get('item_added') item_consumed = player_turn_result.get('consumed') item_dropped = player_turn_result.get('item_dropped') equip = player_turn_result.get('equip') targeting = player_turn_result.get('targeting') targeting_cancelled = player_turn_result.get('targeting_cancelled') xp = player_turn_result.get('xp') if message: message_log.add_message(message) if dead_entity: if dead_entity == player: message, game_state = kill_player(dead_entity) else: message = kill_monster(dead_entity) message_log.add_message(message) if item_added: entities.remove(item_added) game_state = GameStates.ENEMY_TURN if item_consumed: game_state = GameStates.ENEMY_TURN if item_dropped: entities.append(item_dropped) game_state = GameStates.ENEMY_TURN if equip: equip_results = player.equipment.toggle_equip(equip) for equip_result in equip_results: equipped = equip_result.get('equipped') dequipped = equip_result.get('dequipped') if equipped: message_log.add_message( Message('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 if targeting: previous_game_state = GameStates.PLAYERS_TURN game_state = GameStates.TARGETING targeting_item = targeting message_log.add_message(targeting_item.item.targeting_message) if targeting_cancelled: game_state = previous_game_state message_log.add_message(Message('Targeting cancelled')) if xp: leveled_up = player.level.add_xp(xp) message_log.add_message( Message('You gain {0} experience points.'.format(xp))) if leveled_up: previous_game_state = game_state game_state = GameStates.LEVEL_UP if game_state == GameStates.ENEMY_TURN: for entity in entities: if entity.ai: enemy_turn_results = entity.ai.take_turn( player, fov_map, game_map, entities) for enemy_turn_result in enemy_turn_results: message = enemy_turn_result.get('message') dead_entity = enemy_turn_result.get('dead') if message: message_log.add_message(message) if dead_entity: if dead_entity == player: message, game_state = kill_player(dead_entity) else: message = kill_monster(dead_entity) message_log.add_message(message) if game_state == GameStates.PLAYER_DEAD: break if game_state == GameStates.PLAYER_DEAD: break else: game_state = GameStates.PLAYERS_TURN
def main(): """ Main game function """ fighter_component = Fighter(hp=30, defense=2, power=5) player = Entity(0, 0, '@', tcod.white, 'Player', blocks=True, render_order=RenderOrder.ACTOR, fighter=fighter_component) entities = [player] # Import font tcod.console_set_custom_font( FONT, tcod.FONT_TYPE_GREYSCALE | tcod.FONT_LAYOUT_TCOD) # Console initialization tcod.console_init_root(screen_width, screen_height, 'Pilferer %s' % VERSION, False, vsync=False) con = tcod.console.Console(screen_width, screen_height) # Mapping game_map = GameMap(map_width, map_height) game_map.make_map(max_rooms, room_min_size, room_max_size, map_width, map_height, player, entities, max_monsters_per_room) # FOV fov_recompute = True fov_map = initialize_fov(game_map) # Variables for holding input key = tcod.Key() mouse = tcod.Mouse() # Game state game_state = GameStates.PLAYERS_TURN # Main game loop while not tcod.console_is_window_closed(): # FOV if fov_recompute: recompute_fov(fov_map, player.x, player.y, fov_radius, fov_light_walls, fov_algorithm) # Draw render_all(con, entities, player, game_map, fov_map, fov_recompute, screen_width, screen_height, colors) fov_recompute = False tcod.console_flush() clear_all(con, entities) # INDPUT HANDLING tcod.sys_check_for_event(tcod.EVENT_KEY_PRESS, key, mouse) action = handle_keys(key) # Command move player_turn_results = [] move = action.get('move') 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_entity_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 # Command exit exit = action.get('exit') if exit: return True # Command Fullscreen fullscreen = action.get('fullscreen') if fullscreen: tcod.console_set_fullscreen(not tcod.console_is_fullscreen()) # Results for player_turn_result in player_turn_results: message = player_turn_result.get('message') dead_entity = player_turn_result.get('dead') if message: print(message) if dead_entity: if dead_entity == player: message, game_state = kill_player(dead_entity) else: message = kill_monster(dead_entity) print(message) # Monster turns 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: print(message) if dead_entity: if dead_entity == player: message, game_state = kill_player(dead_entity) else: message = kill_monster(dead_entity) print(message) if game_state == GameStates.PLAYER_DEAD: break if game_state == GameStates.PLAYER_DEAD: break else: game_state = GameStates.PLAYERS_TURN game_state = GameStates.PLAYERS_TURN
def main(): screen_width = 80 screen_height = 50 map_width = 80 map_height = 45 room_max_size = 10 room_min_size = 6 max_rooms = 30 fov_algorithm = 0 fov_light_walls = True fov_radius = 10 colours = { 'dark_wall': libtcod.Color(0, 0, 100), 'dark_ground': libtcod.Color(50, 50, 150), 'light_wall': libtcod.Color(130, 110, 50), 'light_ground': libtcod.Color(200, 180, 50) } player = Entity(int(screen_width / 2), int(screen_height / 2), '@', libtcod.white) npc = Entity(int(screen_width / 2 - 5), int(screen_height / 2), '@', libtcod.yellow) entities = [npc, player] libtcod.console_set_custom_font( 'arial10x10.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD) libtcod.console_init_root(screen_width, screen_height, 'libtcod tutorial revised', False) con = libtcod.console_new(screen_width, screen_height) game_map = GameMap(map_width, map_height) game_map.make_map(max_rooms, room_min_size, room_max_size, map_width, map_height, player) fov_recompute = True fov_map = initialise_fov(game_map) key = libtcod.Key() mouse = libtcod.Mouse() while not libtcod.console_is_window_closed(): libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS, key, mouse) if fov_recompute: recompute_fov(fov_map, player.x, player.y, fov_radius) render_all(con, entities, game_map, fov_map, fov_recompute, screen_width, screen_height, colours) fov_recompute = False libtcod.console_flush() clear_all(con, entities) action = handle_keys(key) move = action.get('move') exit = action.get('exit') fullscreen = action.get('fullscreen') if move: dx, dy = move if not game_map.is_blocked(player.x + dx, player.y + dy): player.move(dx, dy) fov_recompute = True if exit: return True if fullscreen: return True
def main(): screen_width = 80 screen_height = 50 map_width = 80 map_height = 45 room_max_size = 10 room_min_size = 6 max_rooms = 30 fov_algorithm = 0 fov_light_walls = True fov_radius = 10 max_monsters_per_room = 3 colors = { 'dark_wall': libtcod.Color(0, 0, 100), 'dark_ground': libtcod.Color(50, 50, 150), 'light_wall': libtcod.Color(130, 110, 50), 'light_ground': libtcod.Color(200, 180, 50) } fighter_component = Fighter(hp=30, defense=2, power=5) player = Entity(0, 0, '@', libtcod.white, "Player", blocks=True, render_order=RenderOrder.ACTOR, fighter=fighter_component) entities = [player] libtcod.console_set_custom_font('arial10x10.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD) libtcod.console_init_root(screen_width, screen_height, 'libtcod tutorial revised', False) con = libtcod.console_new(screen_width, screen_height) game_map = GameMap(map_width, map_height) game_map.make_map(max_rooms, room_min_size, room_max_size, map_width, map_height, player, entities, max_monsters_per_room) fov_recompute = True fov_map = initialize_fov(game_map) key = libtcod.Key() mouse = libtcod.Mouse() game_state = GameStates.PLAYERS_TURN while not libtcod.console_is_window_closed(): libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS, key, mouse) if fov_recompute: recompute_fov(fov_map, player.x, player.y, fov_radius, fov_light_walls, fov_algorithm) render_all(con, entities, player, game_map, fov_map, fov_recompute, screen_width, screen_height, colors) libtcod.console_flush() clear_all(con, entities) action = handle_keys(key) move = action.get('move') exit = action.get('exit') fullscreen = action.get('fullscreen') 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 if exit: 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') if message: print(message) if dead_entity: if dead_entity == player: message, game_state = kill_player(dead_entity) else: message = kill_monster(dead_entity) print(message) 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: print(message) if dead_entity: if dead_entity == player: message, game_state = kill_player(dead_entity) else: message = kill_monster(dead_entity) print(message) if game_state == GameStates.PLAYER_DEAD: break if game_state == GameStates.PLAYER_DEAD: break else: game_state = GameStates.PLAYERS_TURN
def main(): screen_width = 80 screen_height = 50 map_width = 80 map_height = 45 room_max_size = 10 room_min_size = 6 max_rooms = 30 fov_algorithm = 'BASIC' fov_light_walls = True fov_radius = 10 max_monsters_per_room = 3 colors = { 'dark_wall': (0, 0, 100), 'dark_ground': (50, 50, 150), 'light_wall': (130, 110, 50), 'light_ground': (200, 150, 50), 'desaturated_green': (63, 127, 63), 'darker_green': (0, 127, 0), 'dark_red': (191, 0, 0) } fighter_component = Fighter(hp=30, defense=2, power=5) player = Entity(0, 0, '@', (255, 255, 255), 'Player', \ blocks=True, render_order=RenderOrder.ACTOR, fighter=fighter_component) entities = [player] player_x = int(screen_width / 2) player_y = int(screen_height / 2) tdl.set_font('arial12x12.png', greyscale=True, altLayout=True) root_console = tdl.init(screen_width, screen_height, title='Roguelike Tutorial Revised', \ renderer='GLSL') con = tdl.Console(screen_width, screen_height) game_map = GameMap(map_width, map_height) make_map(game_map, max_rooms, room_min_size, room_max_size, map_width, map_height, player, \ entities, max_monsters_per_room, colors) fov_recompute = True game_state = GameStates.PLAYERS_TURN while not tdl.event.is_window_closed(): if fov_recompute: game_map.compute_fov(player.x, player.y, fov=fov_algorithm, radius=fov_radius, light_walls=fov_light_walls) render_all(con, entities, player, game_map, fov_recompute, root_console, screen_width, screen_height, colors) tdl.flush() clear_all(con, entities) fov_recompute = False for event in tdl.event.get(): if event.type == 'KEYDOWN': user_input = event break else: user_input = None if not user_input: continue action = handle_keys(user_input) move = action.get('move') exit = action.get('exit') fullscreen = action.get('fullscreen') screenshot = action.get('screenshot') 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 game_map.walkable[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 if screenshot: tdl.screenshot() if exit: return True if fullscreen: tdl.set_fullscreen(not tdl.get_fullscreen()) for player_turn_result in player_turn_results: message = player_turn_result.get('message') dead_entity = player_turn_result.get('dead') if message: print(message) if dead_entity: if dead_entity == player: message, game_state = kill_player(dead_entity, colors) else: message = kill_monster(dead_entity, colors) print(message) if game_state == GameStates.ENEMY_TURN: for entity in entities: if entity.ai: enemy_turn_results = entity.ai.take_turn( player, 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: print(message) if dead_entity: if dead_entity == player: message, game_state = kill_player( dead_entity, colors) else: message = kill_monster(dead_entity, colors) print(message) if game_state == GameStates.PLAYER_DEAD: break if game_state == GameStates.PLAYER_DEAD: break else: game_state = GameStates.PLAYERS_TURN
def main(): # map vars map_width = 40 map_height = 60 room_min_size = 6 room_max_size = 10 max_rooms = 50 max_monsters_per_room = 3 max_items_per_room = 2 # screen vars viewport_width = 80 viewport_height = 60 rerender_viewport = True map_x = viewport_width map_y = 0 screen_width = viewport_width + map_width # stats panel vars bar_width = 20 panel_width = screen_width panel_height = 8 panel_x = 0 panel_y = viewport_height message_x = bar_width + 2 message_width = screen_width - bar_width - 2 message_height = panel_height - 1 screen_height = viewport_height + panel_height # fov vars fov_algorithm = 12 fov_light_walls = True fov_radius = 10 fov_recompute = True # game vars game_state = GameStates.PLAYER_TURN previous_game_state = game_state colours = { 'dark_wall': tcod.Color(0, 0, 100), 'dark_ground': tcod.Color(50, 50, 150), 'light_wall': tcod.Color(130, 110, 50), 'light_ground': tcod.Color(200, 180, 50) } # init player fighter_component = Fighter(30, 2, 5) inventory_component = Inventory(26) player = Entity(0, 0, '@', tcod.white, 'Player', render_order=RenderOrder.ACTOR, blocks=True, fighter=fighter_component, inventory=inventory_component) player_rot = 0.0 entities = [player] # load assets wall_texture = Image.open('assets/wall_vines0.png') orc_texture = Image.open('assets/orc.png') troll_texture = Image.open('assets/troll.png') potion_texture = Image.open('assets/ruby.png') # init game map game_map = GameMap(map_width, map_height) game_map.make_map(max_rooms, room_min_size, room_max_size, map_width, map_height, player, entities, max_monsters_per_room, max_items_per_room, orc_texture, troll_texture, potion_texture) game_map.recompute_fov(player.x, player.y, fov_radius, fov_light_walls, fov_algorithm) # init message log message_log = MessageLog(message_x, message_width, message_height) tcod.console_set_custom_font( 'arial10x10.png', tcod.FONT_TYPE_GREYSCALE | tcod.FONT_LAYOUT_TCOD) with tcod.console_init_root(screen_width, screen_height, 'firstpersonroguelike', vsync=True) as root_console: viewport_console = tcod.console.Console(viewport_width, viewport_height) map_console = tcod.console.Console(map_width, map_height) panel_console = tcod.console.Console(panel_width, panel_height) mouse = (0, 0) time_start = perf_counter() # game loop begin while True: for event in tcod.event.wait(): tcod.console_flush() player_turn_results = [] # handle inputs if event.type == "QUIT": raise SystemExit() elif event.type == "MOUSEMOTION": # record the new mouse position, render_all will take care of the rest mouse = event.tile # player keyboard interaction elif event.type == "KEYDOWN": action = handle_keys(event, game_state) if len(action) == 0: continue elif action.get('exit'): if game_state in (GameStates.SHOW_INVENTORY, GameStates.DROP_INVENTORY): game_state = previous_game_state else: raise SystemExit() elif action.get( 'pickup') and game_state is GameStates.PLAYER_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( 'You inspect the empty ground below you. Looks pretty dirty', tcod.yellow)) game_state = GameStates.ENEMIES_TURN elif action.get('show_inventory' ) and game_state is GameStates.PLAYER_TURN: previous_game_state = game_state game_state = GameStates.SHOW_INVENTORY elif action.get('drop_inventory' ) and game_state is GameStates.PLAYER_TURN: previous_game_state = game_state game_state = GameStates.DROP_INVENTORY elif action.get( 'inventory_index' ) is not None and previous_game_state is not GameStates.PLAYER_DEAD: inventory_index = action.get('inventory_index') if inventory_index < len(player.inventory.items): item = player.inventory.items[inventory_index] if game_state is GameStates.SHOW_INVENTORY: message_log.add_message( Message('Used ' + item.name)) player_turn_results.extend( player.inventory.use_item(item)) elif game_state is GameStates.DROP_INVENTORY: message_log.add_message( Message('Dropped ' + item.name)) player_turn_results.extend( player.inventory.drop_item(item)) game_state = GameStates.ENEMIES_TURN elif action.get( 'move') and game_state is GameStates.PLAYER_TURN: dx, dy = action.get('move') to_x, to_y = player.x + dx, player.y + dy if game_map.walkable(to_x, to_y): target = get_blocking_entities_at_location( entities, to_x, to_y) if target: player_turn_results.extend( player.fighter.attack(target)) else: player.move(dx, dy) fov_recompute = True # end our turn game_state = GameStates.ENEMIES_TURN elif action.get( 'move2') and game_state is GameStates.PLAYER_TURN: to_x = player.x to_y = player.y if action.get('move2') == 'forward': to_x += int(math.sin(player_rot) * 1.5) to_y += int(math.cos(player_rot) * 1.5) elif action.get('move2') == 'rearward': to_x -= int(math.sin(player_rot) * 1.5) to_y -= int(math.cos(player_rot) * 1.5) if game_map.walkable(to_x, to_y): target = get_blocking_entities_at_location( entities, to_x, to_y) if target: player_turn_results.extend( player.fighter.attack(target)) else: player.x = to_x player.y = to_y fov_recompute = True # end our turn game_state = GameStates.ENEMIES_TURN elif action.get( 'turn') and game_state is GameStates.PLAYER_TURN: dir = action.get('turn') if dir == 'left': player_rot += math.pi / 4.0 elif dir == 'right': player_rot -= math.pi / 4.0 rerender_viewport = True # end_switch event.type # process player's turn results for ptr in player_turn_results: message = ptr.get('message') dead_entity = ptr.get('dead') if message: message_log.add_message(message) if dead_entity: if dead_entity == player: message, game_state = kill_player(player) else: message = kill_monster(dead_entity) message_log.add_message(message) if ptr.get('item_added'): entities.remove(ptr.get('item_added')) if ptr.get('item_dropped'): entities.append(ptr.get('item_dropped')) # end_for player_turn_results # run the enemy turn if game_state is GameStates.ENEMIES_TURN: for entity in entities: if entity.ai: enemy_turn_results = entity.ai.take_turn( player, game_map, entities) for etr in enemy_turn_results: message = etr.get('message') dead_entity = etr.get('dead') if message: message_log.add_message(message) if dead_entity: if dead_entity is player: message, game_state = kill_player( player) else: message = kill_monster(dead_entity) message_log.add_message(message) if game_state is GameStates.PLAYER_DEAD: break if game_state is GameStates.PLAYER_DEAD: break else: game_state = GameStates.PLAYER_TURN # endif enemy turn if fov_recompute: game_map.recompute_fov(player.x, player.y, fov_radius, fov_light_walls, fov_algorithm) # draw each part of the screen render_viewport( root_console, viewport_console, 0, 0, viewport_width, viewport_height, game_map, entities, colours, player.x, player.y, player_rot, fov_radius, rerender_viewport or game_state is GameStates.ENEMIES_TURN, wall_texture) render_map(root_console, map_console, game_map, entities, map_x, map_y, colours, fov_recompute) # TODO: extend render_menu to accept a top-left co-ordinate (start_x, start_y) render_menu(root_console, player, screen_width, screen_height, game_state) fps = 1.0 / (perf_counter() - time_start) render_panel(root_console, panel_console, entities, player, game_map, message_log, bar_width, panel_width, panel_height, panel_x, panel_y, mouse, fps) time_start = perf_counter() tcod.console_flush() clear_all(map_console, entities) fov_recompute = False
def main(): screen_width = 80 screen_height = 50 map_width = 80 map_height = 45 room_max_size = 10 room_min_size = 6 max_rooms = 30 max_monsters_per_room = 3 fov_algorithm = 0 fov_light_walls = True fov_radius = 10 colors = { 'dark_wall': libtcod.Color(0, 0, 100), 'dark_ground': libtcod.Color(50, 50, 150), 'light_wall': libtcod.Color(130, 110, 50), 'light_ground': libtcod.Color(200, 180, 50) } player = Entity(0, 0, '@', libtcod.white, 'Player', blocks=True) entities = [player] libtcod.console_set_custom_font( 'arial10x10.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD) libtcod.console_init_root(screen_width, screen_height, 'RogueLike', False) con = libtcod.console_new(screen_width, screen_height) game_map = GameMap(map_width, map_height) game_map.make_map(max_rooms, room_min_size, room_max_size, map_width, map_height, player, entities, max_monsters_per_room) fov_recompute = True fov_map = initialize_fov(game_map) key = libtcod.Key() mouse = libtcod.Mouse() game_state = GameStates.PLAYERS_TURN while not libtcod.console_is_window_closed(): libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS, key, mouse) if fov_recompute: recompute_fov(fov_map, player.x, player.y, fov_radius, fov_light_walls, fov_algorithm) render_all(con, entities, game_map, fov_map, fov_recompute, screen_width, screen_height, colors) fov_recompute = False libtcod.console_flush() clear_all(con, entities) action = handle_keys(key) move = action.get('move') exit = action.get('exit') fullscreen = action.get('fullscreen') if move 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: print('You kick the ' + target.name + ' in thr shins, much to its annoyance!') else: player.move(dx, dy) fov_recompute = True game_state = GameStates.ENEMY_TURN if exit: return True if fullscreen: libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen()) if game_state == GameStates.ENEMY_TURN: for entity in entities: if entity != player: print('The ' + entity.name + ' ponders the meaning of its existence') game_state = GameStates.PLAYERS_TURN
def main(): screen_width = 80 screen_height = 50 map_width = 80 map_height = 45 room_max_size = 10 room_min_size = 6 max_rooms = 30 fov_algorithm = 0 fov_light_walls = True fov_radius = 10 max_monsters_per_room = 3 colors = { "dark_wall": libtcod.Color(0, 0, 100), "dark_ground": libtcod.Color(50, 50, 150), "light_wall": libtcod.Color(130, 110, 50), "light_ground": libtcod.Color(200, 180, 50), } player = Entity(0, 0, "@", libtcod.white, "Player", blocks=True) entities = [player] libtcod.console_set_custom_font( "arial10x10.png", libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD, ) libtcod.console_init_root( screen_width, screen_height, "libtcod tutorial revisited", False ) con = libtcod.console_new(screen_width, screen_height) game_map = GameMap(map_width, map_height) game_map.make_map( max_rooms, room_min_size, room_max_size, map_width, map_height, player, entities, max_monsters_per_room, ) fov_recompute = True fov_map = initialize_fov(game_map) key = libtcod.Key() mouse = libtcod.Mouse() game_state = GameStates.PLAYERS_TURN while not libtcod.console_is_window_closed(): libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS, key, mouse) if fov_recompute: recompute_fov( fov_map, player.x, player.y, fov_radius, fov_light_walls, fov_algorithm, ) render_all( con, entities, game_map, fov_map, fov_recompute, screen_width, screen_height, colors, ) fov_recompute = False libtcod.console_flush() clear_all(con, entities) action = handle_keys(key) move = action.get("move") leave = action.get("exit") fullscreen = action.get("fullscreen") 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: print( f"you kick the {target.name} in the shins, much to its annoyance!" ) else: player.move(dx, dy) fov_recompute = True game_state = GameStates.ENEMY_TURN if leave: return True if fullscreen: libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen()) if game_state == GameStates.ENEMY_TURN: for entity in entities: if entity != player: print( f"The {entity.name} ponders the meaning of its existence" ) game_state = GameStates.PLAYERS_TURN
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'], 'rogue', 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 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'], constants['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(player.x + dx, player.y + dy): target = get_blocking_entities_at_location( entities, destination_x, destination_y) if target: player.fighter.attack(target) attack_results = player.fighter.attack(target) player_turn_results.extend(attack_results) else: player.move(dx, dy) fov_recompute = True game_state = GameStates.ENEMY_TURN elif pickup and game_state == GameStates.PLAYERS_TURN: for entity in entities: if entity.item and entity.x == player.x and entity.y == player.y: pickup_results = player.inventory.add_item(entity) player_turn_results.extend(pickup_results) break else: message_log.add_message( Message('There is nothing here to pick up.', libtcod.yellow)) if show_inventory: previous_game_state = game_state game_state = GameStates.SHOW_INVENTORY if drop_inventory: previous_game_state = game_state game_state = GameStates.DROP_INVENTORY if inventory_index is not None and previous_game_state != GameStates.PLAYER_DEAD and inventory_index < len( player.inventory.items): item = player.inventory.items[inventory_index] if game_state == GameStates.SHOW_INVENTORY: player_turn_results.extend( player.inventory.use(item, entities=entities, fov_map=fov_map)) elif game_state == GameStates.DROP_INVENTORY: player_turn_results.extend(player.inventory.drop_item(item)) if 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.append(item_use_results) elif right_click: player_turn_results.append({'targeting_cancelled': True}) if exit: if game_state in (game_state.SHOW_INVENTORY, game_state.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 = game_state.ENEMY_TURN if item_consumed: game_state.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 play_game(player, entities, game_map, message_log, game_state, con, message_panel, char_info_panel, area_info_panel, under_mouse_panel, constants, floor_index, original_entity_index, entity_index, fov_index): fov_recompute = True fov_map = initialize_fov(game_map) key = libtcod.Key() mouse = libtcod.Mouse() game_state = GameStates.PLAYERS_TURN previous_game_state = game_state mouse_x = mouse.cx old_mouse_x = mouse_x mouse_y = mouse.cy old_mouse_y = mouse_y #attack_animation_x = 0 #attack_animation_y = 0 clean_map = False #attacked = False #animation_time = 200 #animation_distance = 0 targeting_item = None equipment_choice = 0 npc = None item = None while not libtcod.console_is_window_closed(): libtcod.sys_check_for_event( libtcod.EVENT_KEY_PRESS | libtcod.EVENT_MOUSE, key, mouse) """ if animation_time == 0: if attacked: animation_distance += 1 animation_time = 200 if animation_distance == 5: animation_distance = 0 attacked = False """ if clean_map == True: fov_recompute = True clean_map = False if fov_recompute: recompute_fov(fov_map, player.x, player.y, constants['fov_radius'], constants['fov_light_walls'], constants['fov_algorithm']) render_all(con, message_panel, char_info_panel, area_info_panel, under_mouse_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['tiles'], constants['colors'], game_state, npc, targeting_item, item, equipment_choice) fov_recompute = False libtcod.console_flush() clear_all(con, entities, fov_map, game_map) action = handle_keys(key, game_state) mouse_action = handle_mouse(mouse) ############################################ if game_state == GameStates.EQUIPMENT_SCREEN and not action.get( 'exit'): for equipment in action: if equipment: equipment_choice = equipment break ############################################ move = action.get('move') ranged_attack = action.get('ranged_attack') interact = action.get('interact') inspect_item = action.get('inspect_item') wait = action.get('wait') pickup = action.get('pickup') show_inventory = action.get('show_inventory') drop_inventory = action.get('drop_inventory') inventory_index = action.get('inventory_index') take_stairs = action.get('take_stairs') level_up = action.get('level_up') show_character_screen = action.get('show_character_screen') show_equipment_screen = action.get('show_equipment_screen') exit = action.get('exit') fullscreen = action.get('fullscreen') left_click = mouse_action.get('left_click') right_click = mouse_action.get('right_click') player_turn_results = [] if move and game_state == GameStates.PLAYERS_TURN: dx, dy = move destination_x = player.x + dx destination_y = player.y + dy if not game_map.is_blocked(destination_x, destination_y): target = get_blocking_entities_at_location( entities, destination_x, destination_y) if target and not target.invulnerable: attack_results = player.combat_class.attack(target) player_turn_results.extend(attack_results) clean_map = True elif not target: player.move(dx, dy) if player.combat_class.turns_until_rest == 0: pass else: player.combat_class.turns_until_rest -= 1 fov_recompute = True game_state = GameStates.ENEMY_TURN elif move and game_state == GameStates.INTERACT: dx, dy = move destination_x = player.x + dx destination_y = player.y + dy if not game_map.is_blocked(destination_x, destination_y): blocking_target = get_blocking_entities_at_location( entities, destination_x, destination_y) non_blocking_target = get_non_blocking_entities_at_location( entities, destination_x, destination_y) if blocking_target: try: if blocking_target.dialogue.dialogue: npc = blocking_target except (AttributeError): pass if blocking_target.bonfire is not None: message_log.add_message( Message( 'You see a mysterious bonfire. You cannot resist touching it', libtcod.light_violet)) entity_index = blocking_target.bonfire.reset_entities( game_map, original_entity_index, entity_index) game_state = GameStates.PLAYERS_TURN else: message_log.add_message( Message('You see {0}'.format(blocking_target.name), libtcod.white)) elif non_blocking_target: message_log.add_message( Message('You see {0}'.format(non_blocking_target.name), libtcod.white)) else: message_log.add_message( Message('There is nothing to inspect here.', libtcod.white)) elif wait: if player.combat_class.turns_until_rest == 0: pass else: player.combat_class.turns_until_rest -= 1 message = player.combat_class.rest() message_log.add_message(Message(message, libtcod.green)) 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.white)) 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 interact: previous_game_state = GameStates.PLAYERS_TURN game_state = GameStates.INTERACT message_log.add_message(Message('You begin to look around.')) if ranged_attack: if player.equipment.main_hand.equippable.ranged: previous_game_state = GameStates.PLAYERS_TURN game_state = GameStates.TARGETING message_log.add_message(Message('Choose a target to attack.')) else: message_log.add_message( Message('This weapon cannot attack at range.')) 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)) elif game_state == GameStates.CHOOSE_ITEM_TO_INSPECT: previous_game_state = GameStates.CHOOSE_ITEM_TO_INSPECT game_state = GameStates.INSPECT_ITEM message_log.add_message( Message('You inspect the {0}.'.format(item.name))) 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: if entity.name == 'Stairs Down': if len(floor_index) == game_map.dungeon_level: entities = game_map.new_floor( player, message_log, constants) fov_map = initialize_fov(game_map) floor_index.append(game_map.tiles) entity_index.append(entities) original_entity_index.append(entities) fov_index.append(fov_map) fov_recompute = True libtcod.console_clear(con) break elif len(floor_index) > game_map.dungeon_level: # Update the entity index with the floors NEW entity list entity_index[game_map.dungeon_level - 1] = entities entities, player, fov_map = game_map.next_floor( player, entity_index, floor_index, fov_index, message_log, constants) fov_recompute = True libtcod.console_clear(con) break elif entity.name == 'Stairs Up': entity_index[game_map.dungeon_level - 1] = entities entities, player, fov_map = game_map.previous_floor( player, entity_index, floor_index, fov_index, message_log, constants) fov_recompute = True libtcod.console_clear(con) break else: message_log.add_message( Message('There are no stairs here.', libtcod.yellow)) if level_up: if level_up == 'str': player.combat_class.base_strength += 1 elif level_up == 'dex': player.combat_class.base_dexterity += 1 elif level_up == 'sta': player.combat_class.base_stamina += 1 elif level_up == 'int': player.combat_class.base_intelligence += 1 game_state = previous_game_state if show_character_screen: previous_game_state = game_state game_state = GameStates.CHARACTER_SCREEN if show_equipment_screen: previous_game_state = game_state game_state = GameStates.EQUIPMENT_SCREEN if game_state == GameStates.TARGETING: mouse_x = mouse.cx mouse_y = mouse.cy if (old_mouse_y != mouse_y or old_mouse_x != mouse_x) and libtcod.map_is_in_fov( fov_map, mouse_x, mouse_y): fov_recompute = True elif libtcod.map_is_in_fov( fov_map, old_mouse_x, old_mouse_y) and not libtcod.map_is_in_fov( fov_map, mouse_x, mouse_y): clean_map = True old_mouse_x = mouse_x old_mouse_y = mouse_y if left_click and targeting_item != None: 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) fov_recompute = True elif right_click: player_turn_results.append({'targeting_cancelled': True}) fov_recompute = True elif left_click and targeting_item == None: target_x, target_y = left_click if not game_map.tiles[target_x][target_y].blocked: target = get_blocking_entities_at_location( entities, target_x, target_y) else: message_log.add_message( Message('You can\'t attack that.', libtcod.yellow)) if target and not target.invulnerable: attack_results = player.combat_class.attack(target) player_turn_results.extend(attack_results) fov_recompute = True game_state = GameStates.ENEMY_TURN if game_state == GameStates.SHOW_INVENTORY: if inspect_item: previous_game_state = game_state game_state = GameStates.CHOOSE_ITEM_TO_INSPECT message_log.add_message( Message('Choose an item to inspect.', libtcod.yellow)) if game_state == GameStates.EQUIPMENT_SCREEN: if equipment_choice: previous_game_state = game_state game_state = GameStates.EQUIPMENT_DETAILS if exit: if game_state in (GameStates.SHOW_INVENTORY, GameStates.DROP_INVENTORY, GameStates.CHARACTER_SCREEN, GameStates.INTERACT): if game_state == (GameStates.INTERACT): player_turn_results.append({'interacting_cancelled': True}) game_state = previous_game_state npc = None else: game_state = previous_game_state elif game_state == GameStates.CHOOSE_ITEM_TO_INSPECT: game_state = GameStates.SHOW_INVENTORY previous_game_state = GameStates.PLAYERS_TURN message_log.add_message( Message('Item inspection cancelled.', libtcod.yellow)) elif game_state == GameStates.INSPECT_ITEM: game_state = previous_game_state elif game_state == GameStates.EQUIPMENT_SCREEN: game_state = GameStates.PLAYERS_TURN elif game_state == GameStates.EQUIPMENT_DETAILS: game_state = previous_game_state equipment_choice = False elif game_state == GameStates.TARGETING: player_turn_results.append({'targeting_cancelled': True}) game_state = previous_game_state fov_recompute = True else: libtcod.console_clear(0) save_game(player, entities, game_map, message_log, game_state, floor_index, original_entity_index, entity_index, fov_index) return True if fullscreen: libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen()) for player_turn_result in player_turn_results: message = player_turn_result.get('message') dead_entity = player_turn_result.get('dead') item_added = player_turn_result.get('item_added') item_consumed = player_turn_result.get('consumed') item_dropped = player_turn_result.get('item_dropped') equip = player_turn_result.get('equip') targeting = player_turn_result.get('targeting') targeting_cancelled = player_turn_result.get('targeting_cancelled') xp = player_turn_result.get('xp') interacting_cancelled = player_turn_result.get( 'interacting_cancelled') if message: message_log.add_message(message) if dead_entity: if dead_entity == player: message, game_state = kill_player(dead_entity) else: dead_entity.alive = False 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 equip: equip_results = player.equipment.toggle_equip(equip) for equip_result in equip_results: equipped = equip_result.get('equipped') unequipped = equip_result.get('unequipped') if equipped: message_log.add_message( Message('You equipped the {0}.'.format( equipped.name))) if unequipped: message_log.add_message( Message('You unequipped the {0}.'.format( unequipped.name))) game_state = GameStates.ENEMY_TURN if 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 interacting_cancelled: game_state = previous_game_state message_log.add_message(Message('You stop looking around.')) if xp: leveled_up = player.level.add_xp(xp) message_log.add_message( Message('You gain {0} experience points.'.format(xp), libtcod.lighter_yellow)) if leveled_up: message_log.add_message( Message( 'Your skills grow more honed. You reach level {0}'. format(player.level.current_level) + "!", libtcod.yellow)) previous_game_state = game_state game_state = GameStates.LEVEL_UP 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: if wait: enemy_turn_results = entity.ai.approach_player_on_wait( player, fov_map, game_map, entities) else: enemy_turn_results = entity.ai.take_turn( player, fov_map, game_map, entities) for enemy_turn_result in enemy_turn_results: message = enemy_turn_result.get('message') dead_entity = enemy_turn_result.get('dead') if message: message_log.add_message(message) if dead_entity: if dead_entity == player: message, game_state = kill_player( dead_entity) else: message = kill_monster(dead_entity) message_log.add_message(message) if game_state == GameStates.PLAYER_DEAD: break if game_state == GameStates.PLAYER_DEAD: break else: game_state = GameStates.PLAYERS_TURN
def main(): screen_width = 80 screen_height = 50 bar_width = 20 panel_height = 7 panel_y = screen_height - panel_height message_x = bar_width + 2 message_width = screen_width - bar_width - 2 message_height = panel_height - 1 map_width = 80 map_height = 43 fov_algorithm = 'BASIC' fov_light_walls = True fov_radius = 10 colors = { 'dark_wall': (0, 0, 100), 'dark_ground': (50, 50, 150), 'light_wall': (130, 110, 50), 'light_ground': (200, 180, 50), 'white': (255, 255, 255), 'black': (0, 0, 0), 'light red': (255, 100, 100), 'red': (255, 0, 0), 'yellow': (255, 255, 0), 'orange': (255, 127, 0), 'green': ( 0, 255, 0, ), 'light_red': (255, 114, 114), 'darker_red': (127, 0, 0), 'highlight': (199, 234, 70) } mech_component = Mech(hp=30, peak_momentum=6) weapon_component = Weapon(name="Laser", damage=5, min_targets=0, max_targets=5, color=colors.get('green'), range=10) player = Entity(int(screen_width / 2), int(screen_height / 2), '@', colors.get('white'), "player", mech=mech_component, weapon=weapon_component) npc = Entity(int(screen_width / 2 - 5), int(screen_height / 2), '@', colors.get('yellow'), "NPC") cursor_component = Cursor() cursor = Entity( -1, -1, ' ', colors.get('red'), "cursor", cursor=cursor_component ) # The ' ' isn't actually "nothing". To have nothing, I would have to mess with a render order. entities = [npc, player, cursor] tdl.set_font('arial10x10.png', greyscale=True, altLayout=True) root_console = tdl.init(screen_width, screen_height, title='MVP v0.0') con = tdl.Console(screen_width, screen_height) panel = tdl.Console(screen_width, panel_height) game_map = GameMap(map_width, map_height) make_map(game_map) message_log = MessageLog(message_x, message_width, message_height) mouse_coordinates = (0, 0) game_state = GameStates.PLAYER_TURN previous_game_state = game_state turn_state = TurnStates.UPKEEP_PHASE fov_recompute = True while not tdl.event.is_window_closed(): if fov_recompute: game_map.compute_fov(player.x, player.y, fov=fov_algorithm, radius=fov_radius, light_walls=fov_light_walls) render_all(con, panel, entities, game_map, fov_recompute, root_console, message_log, screen_width, screen_height, bar_width, panel_height, panel_y, mouse_coordinates, colors) tdl.flush() clear_all(con, entities) for event in tdl.event.get(): if event.type == 'KEYDOWN': user_input = event break elif event.type == 'MOUSEMOTION': mouse_coordinates = event.cell else: user_input = None fov_recompute = False action = handle_keys(user_input, game_state) impulse = None # This is to avoid logic problems. change_game_state = None # This is to avoid logic problems. move = action.get('move') # Attempt to move. impulse = action.get('impulse') # Adjust mech impulse. next_turn_phase = action.get( 'next turn phase') # Move to the next phase. change_game_state = action.get( 'change game state') # Go to different game_state select = action.get( 'select') # A target has been selected via keyboard. exit = action.get('exit') # Exit whatever screen is open. fullscreen = action.get('fullscreen') # Set game to full screen. if exit: if game_state == GameStates.TARGETING: # Turn off cursor cursor.char = ' ' cursor.x = -1 cursor.y = -1 fov_recompute = True game_state = previous_game_state else: return True if fullscreen: tdl.set_fullscreen(not tdl.get_fullscreen()) if game_state == GameStates.PLAYER_TURN: # See game_states.py for the turn structure. # Turns order is reversed so ensure that the loop runs once for each if turn_state == TurnStates.POST_ATTACK_PHASE: # Reset map flags and remove targets. reset_flags(game_map) for x, y in player.weapon.targets: erase_cell(con, x, y) turn_state = TurnStates.UPKEEP_PHASE game_state = GameStates.ENEMY_TURN if turn_state == TurnStates.ATTACK_PHASE: if change_game_state == GameStates.TARGETING: # Turn on cursor. cursor.char = 'X' # If there were no previous targets, start on the player. if len(player.weapon.targets) == 0: cursor.x = player.x cursor.y = player.y else: cursor.x, cursor.y = player.weapon.targets[-1] fov_recompute = True previous_game_state = game_state game_state = GameStates.TARGETING if next_turn_phase: turn_state = TurnStates.POST_ATTACK_PHASE if turn_state == TurnStates.PRE_ATTACK_PHASE: message_log.add_message( Message('Begin ATTACK PHASE.', colors.get('white'))) message_log.add_message( Message( 'Press f to target. Press ESC to stop targeting. Enter to change phase.', colors.get('orange'))) fov_recompute = True turn_state = TurnStates.ATTACK_PHASE if turn_state == TurnStates.POST_MOVEMENT_PHASE: reset_flags(game_map) player.reset( ) # Reset the mech for the next turn. ### Move this to the post-attack phase fov_recompute = True turn_state = TurnStates.PRE_ATTACK_PHASE if turn_state == TurnStates.MOVEMENT_PHASE: if move: dx, dy = move if game_map.walkable[player.x + dx, player.y + dy]: player.move(dx, dy) fov_recompute = True if next_turn_phase and player.mech.has_spent_minimum_momentum( ): turn_state = TurnStates.POST_MOVEMENT_PHASE elif next_turn_phase and not player.mech.has_spent_minimum_momentum( ): message_log.add_message( Message('Must spend more momentum.', colors.get('red'))) if turn_state == TurnStates.PRE_MOVEMENT_PHASE: if impulse is not None: player.mech.impulse = impulse turn_state = TurnStates.MOVEMENT_PHASE message_log.add_message( Message('Impulse set to {0}.'.format(impulse), colors.get('orange'))) fov_recompute = True highlight_legal_moves(player, game_map) if turn_state == TurnStates.UPKEEP_PHASE and game_state == GameStates.PLAYER_TURN: # This is added to avoid starting the Upkeep Phase when the turn just ended. message_log.add_message( Message('Begin PLAYER TURN.', colors.get('white'))) message_log.add_message( Message('Begin MOVEMENT PHASE.', colors.get('white'))) message_log.add_message( Message('Choose impulse. PAGEUP, PAGEDOWN or HOME.', colors.get('orange'))) turn_state = TurnStates.PRE_MOVEMENT_PHASE fov_recompute = True if game_state == GameStates.ENEMY_TURN: message_log.add_message( Message('Begin ENEMY TURN.', colors.get('white'))) fov_recompute = True game_state = GameStates.PLAYER_TURN if game_state == GameStates.TARGETING: if move: dx, dy = move # Ensure the first target is in firing range. if len(player.weapon.targets) == 0: if player.distance(cursor.x + dx, cursor.y + dy) <= player.weapon.range: cursor.fly(dx, dy) fov_recompute = True else: message_log.add_message( Message('Out of range.', colors.get('red'))) # Ensure that the next targets are adjacent to the previous target elif len(player.weapon.targets) > 0: tar_x, tar_y = player.weapon.targets[ -1] # Get the most recent target added. if abs(tar_x - (cursor.x + dx)) + abs(tar_y - (cursor.y + dy)) <= 1: cursor.fly(dx, dy) fov_recompute = True else: message_log.add_message( Message('Invalid target.', colors.get('red'))) if select: if len(player.weapon.targets) < player.weapon.max_targets: if set_targeted( game_map, cursor.x, cursor.y ): # At the moment, this always returns True. In the future, this may change. fov_recompute = True player.weapon.targets.append((cursor.x, cursor.y)) else: message_log.add_message( Message('Targeting failed.', colors.get('red')))