def init_root(self, show_credits=False): RootConsole.active_root = self RootConsole.scratch = Console(self.width, self.height) RootConsole.temp_console = Console(self.width, self.height) tcod.console_init_root(self.width, self.height, self.title, self.fullscreen, self.renderer) if show_credits: tcod.console_credits() tcod.sys_set_fps(self.max_fps)
def init(game): global CON, CON_MAP, CON_BUFFER, CON_STATUS, CON_INV, MESSAGES, GAME GAME = game T.console_set_custom_font(*FONTS[FONT_INDEX]) CON = T.console_init_root(SCREEN_W, SCREEN_H, TITLE, False) CON_MAP = T.console_new(MAP_W, MAP_H) CON_BUFFER = T.console_new(SCREEN_W, BUFFER_H) CON_STATUS = T.console_new(STATUS_W, STATUS_H) CON_INV = T.console_new(INV_W, INV_H) MESSAGES = []
def session_console(request): if(pytest.config.getoption("--no-window")): pytest.skip("This test needs a rendering context.") FONT_FILE = 'libtcod/terminal.png' WIDTH = 12 HEIGHT = 10 TITLE = 'libtcod-cffi tests' FULLSCREEN = False RENDERER = getattr(tcod, 'RENDERER_' + request.param) tcod.console_set_custom_font(FONT_FILE) with tcod.console_init_root(WIDTH, HEIGHT, TITLE, FULLSCREEN, RENDERER) as con: yield con
monster.char = '%' monster.color = libtcod.dark_red monster.blocks = False monster.fighter = None monster.ai = None monster.name = 'remains of ' + monster.name monster.send_to_back() ############################################# # Initialization & Main Loop ############################################# libtcod.console_set_custom_font( 'arial10x10.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD) libtcod.console_init_root(SCREEN_WIDTH, SCREEN_HEIGHT, 'python/libtcod tutorial', False) libtcod.sys_set_fps(LIMIT_FPS) con = libtcod.console_new(SCREEN_WIDTH, SCREEN_HEIGHT) #create object representing the player fighter_component = Fighter(hp=30, defense=2, power=5, death_function=player_death) player = Object(0, 0, '@', 'player', libtcod.white, blocks=True, fighter=fighter_component)
def init(): global root tcod.console_set_custom_font('arial10x10.png', tcod.FONT_TYPE_GREYSCALE | tcod.FONT_LAYOUT_TCOD) root = tcod.console_init_root(screenwidth, screenheight, 'Test', False) tcod.sys_set_fps(limitfps)
import tcod.event import classes from random import randrange gPlayer = classes.Player() # Setup the console text tcod.console_set_custom_font( "terminal16x16_gs_ro.png", tcod.FONT_LAYOUT_ASCII_INROW, ) # Setup the console main window and flush it gWidth = 80 gHeight = 40 gRootConsole = tcod.console_init_root(gWidth,gHeight,order="F") stars = [] for y in range(0,gHeight): l = [] if y % (randrange(10)+3) == 0: for x in range(1,gWidth): if x % (randrange(30)+1) == 0: print(x) l.append(classes.MovingBackroundStar(x,y)) stars.append(l) def main(root_console): root_console.clear() for l in stars: for star in l: star.update()
# Make sure 'arial10x10.png' is in the same directory as this script. import tcod import tcod.event import hextile #Global Stuff WINDOW_WIDTH = 80 WINDOW_HEIGHT = 60 # Setup the font. tcod.console_set_custom_font( "arial10x10.png", tcod.FONT_LAYOUT_TCOD | tcod.FONT_TYPE_GREYSCALE, ) # Initialize the root console in a context. with tcod.console_init_root(WINDOW_WIDTH, WINDOW_HEIGHT, order="F") as root_console: root_console.print_(x=0, y=0, string='Hello World!') while True: tcod.console_flush() # Show the console. for event in tcod.event.wait(): if event.type == "QUIT": raise SystemExit() # The libtcod window will be closed at the end of this with-block.
def main(): #Set the initial variables constants = get_constants() color_palette = Palette() kolors = color_palette.get_colors() mod_key = 'none' mouse = 'none' #Build and initialize the random monster rosters cr = RosterLists() cr.build_roster_lists() cr.build_monster_manual() current_roster = cr.get_roster_lists() current_mm = cr.get_monster_manual() #Set the font file and settings tcod.console_set_custom_font( constants['font_file'], tcod.FONT_TYPE_GREYSCALE | tcod.FONT_LAYOUT_TCOD) tcod.console_map_ascii_codes_to_font(256, 32, 0, 1) #map all characters in 2nd row tcod.console_map_ascii_codes_to_font(256 + 32, 32, 0, 2) #map all characters in 3rd row #Create the screen, the root console root_con = tcod.console_init_root( constants['screen_width'], constants['screen_height'], constants['screen_title'], constants['screen_fullscreen'], constants['screen_renderer'], constants['screen_order'], constants['screen_vsync']) #Create another console where we'll draw before overlaying it on the root main_con = tcod.console.Console(constants['screen_width'], constants['screen_height'], constants['screen_order']) #Create panel console for the hp bar and message log panel_con = tcod.console.Console(constants['screen_width'], constants['panel_height'], constants['screen_order']) #Initialize player, entities, game_map player, entities, game_map, indoors, message_log, game_state = get_game_variables( constants, kolors, current_roster, current_mm) interface_skin = 'Tutorial' #Choices: Graph, Tutorial if interface_skin == 'Graph': color_palette.set_color('dark_wall', 70, 130, 180) color_palette.set_color('dark_ground', 70, 130, 180) #Initialize FOV and calculate on start fov_recompute = True fov_map = initialize_fov(game_map) game_type = 'normal' #choices normal, viewer #Initialize the message log # message_log = MessageLog(constants['message_x'], constants['message_width'], constants['message_height']) #Initialize main loop # game_state = GameStates.PLAYERS_TURN previous_game_state = game_state targeting_item = None end_game = False while not end_game: #Recomput FOV if necessary if fov_recompute: recompute_fov(fov_map, player.x, player.y, constants['fov_radius'], constants['fov_light_walls'], constants['fov_algorithm']) #Render all entities & tiles on main console and blit them to the root console render_all(main_con, root_con, panel_con, entities, player, game_map, fov_map, fov_recompute, message_log, constants['screen_width'], constants['screen_height'], kolors, game_type, interface_skin, indoors, constants['hp_bar_width'], constants['panel_height'], constants['panel_y'], mouse, game_state) #Reset FOV check fov_recompute = False #Update the console with our changes tcod.console_flush() #Erase all entities on main console so they won't smear on next update clear_all(main_con, entities) #initialize loop variables action = {'none': True} mouse_action = {'none': True} #Detect and handle events for event in tcod.event.get(): if event.type == "QUIT": #Window was closed action = {'exit': True} elif event.type == "KEYUP" and event.sym == 1073742049: mod_key = 'none' elif event.type == "KEYDOWN": #A key was depressed if event.mod == True and event.sym == 1073742049: mod_key = 'l_shift' action = handle_keys(event, mod_key, game_state) elif event.type == "MOUSEMOTION": #Mouse was moved mouse = event.tile elif event.type == "MOUSEBUTTONDOWN": mouse = event.tile mouse_action = handle_mouse(mouse, event.button) #else: #print(event) #Get action type 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') error = action.get('error') wait = action.get('wait') vision = action.get('vision') left_click = mouse_action.get('left_click') right_click = mouse_action.get('right_click') #List to store the results of damage player_turn_results = [] #Process player actions if move and game_state == GameStates.PLAYERS_TURN: dx, dy = move destination_x = player.x + dx destination_y = player.y + dy #If no terrain is blocking then try to move the player if not game_map.is_blocked(destination_x, destination_y): #If no entity is blocking then move the player 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) #Recalculate FOV if player moves 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.', tcod.yellow)) if show_inventory: previous_game_state = game_state game_state = GameStates.SHOW_INVENTORY if drop_inventory: previous_game_state = game_state game_state = GameStates.DROP_INVENTORY if inventory_index is not None and previous_game_state != GameStates.PLAYER_DEAD and inventory_index < len( player.inventory.items): item = player.inventory.items[inventory_index] if game_state == GameStates.SHOW_INVENTORY: player_turn_results.extend( player.inventory.use(item, entities=entities, fov_map=fov_map)) elif game_state == GameStates.DROP_INVENTORY: player_turn_results.extend(player.inventory.drop_item(item)) if game_state == GameStates.TARGETING: if left_click: target_x, target_y = left_click item_use_results = player.inventory.use(targeting_item, entities=entities, fov_map=fov_map, target_x=target_x, target_y=target_y) player_turn_results.extend(item_use_results) elif right_click: player_turn_results.append({'targeting_cancelled': True}) if exit: if exit == 'menu' and game_state in (GameStates.SHOW_INVENTORY, GameStates.DROP_INVENTORY): game_state = previous_game_state elif game_state == GameStates.TARGETING: player_turn_results.append({'targeting_cancelled': True}) else: if exit == True: end_game = True if error: error_text = error print("Error detected", error_text) if vision and game_state == GameStates.PLAYERS_TURN: if vision == 'third eye': if interface_skin == 'Tutorial': interface_skin = third_eye('open_eye', color_palette, indoors) elif interface_skin == 'Graph': interface_skin = third_eye('close_eye', color_palette, indoors) #Recalculate FOV fov_recompute = True game_state = GameStates.ENEMY_TURN if wait and game_state == GameStates.PLAYERS_TURN: if game_type == 'viewer': entities = game_map.next_map(player, map_type, constants, entities, kolors, current_roster, current_mm) fov_map = initialize_fov(game_map) fov_recompute = True main_con.clear(fg=(0, 0, 0)) else: game_state = GameStates.ENEMY_TURN #Process 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 targeting_cancelled: game_state = previous_game_state message_log.add_message(Message('Targeting cancelled')) if dead_entity: if dead_entity == player: message, game_state = kill_player(dead_entity) else: message = kill_monster(dead_entity) message_log.add_message(message) if item_added: entities.remove(item_added) game_state = GameStates.ENEMY_TURN if item_consumed: game_state = GameStates.ENEMY_TURN if targeting: previous_game_state = GameStates.PLAYERS_TURN game_state = GameStates.TARGETING targeting_item = targeting message_log.add_message(targeting_item.item.targeting_message) if item_dropped: entities.append(item_dropped) game_state = GameStates.ENEMY_TURN #Enemy turn if game_state == GameStates.ENEMY_TURN: for entity in entities: if entity.ai: enemy_turn_results = entity.ai.take_turn( player, fov_map, game_map, entities) for enemy_turn_result in enemy_turn_results: message = enemy_turn_result.get('message') dead_entity = enemy_turn_result.get('dead') if message: message_log.add_message(message) if dead_entity: if dead_entity == player: message, game_state = kill_player(dead_entity) else: message = kill_monster(dead_entity) message_log.add_message(message) if game_state == GameStates.PLAYER_DEAD: break if game_state == GameStates.PLAYER_DEAD: break else: game_state = GameStates.PLAYERS_TURN
def main(): 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 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, 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) mouse_action = handle_mouse(mouse) move = action.get('move') pickup = action.get('pickup') show_inventory = action.get('show_inventory') drop_inventory = action.get('drop_inventory') inventory_index = action.get('inventory_index') exit = action.get('exit') fullscreen = action.get('fullscreen') left_click = mouse_action.get('left_click') right_click = mouse_action.get('right_click') player_turn_results = [] if move and game_state == GameStates.PLAYERS_TURN: dx, dy = move destination_x = player.x + dx destination_y = player.y + dy if not game_map.is_blocked(destination_x, destination_y): target = get_blocking_entities_at_location( entities, destination_x, destination_y) if target: attack_results = player.fighter.attack(target) player_turn_results.extend(attack_results) else: player.move(dx, dy) fov_recompute = True game_state = GameStates.ENEMY_TURN elif pickup and game_state == GameStates.PLAYERS_TURN: for entity in entities: if entity.item and entity.x == player.x and entity.y == player.y: pickup_results = player.inventory.add_item(entity) player_turn_results.extend(pickup_results) break else: message_log.add_message( Message('There is nothing here to pick up.', libtcod.yellow)) if show_inventory: previous_game_state = game_state game_state = GameStates.SHOW_INVENTORY if drop_inventory: previous_game_state = game_state game_state = GameStates.DROP_INVENTORY if inventory_index is not None and previous_game_state != GameStates.PLAYER_DEAD and inventory_index < len( player.inventory.items): item = player.inventory.items[inventory_index] if game_state == GameStates.SHOW_INVENTORY: player_turn_results.extend( player.inventory.use(item, entities=entities, fov_map=fov_map)) elif game_state == GameStates.DROP_INVENTORY: player_turn_results.extend(player.inventory.drop_item(item)) if game_state == GameStates.TARGETING: if left_click: target_x, target_y = left_click item_use_results = player.inventory.use(targeting_item, entities=entities, fov_map=fov_map, target_x=target_x, target_y=target_y) player_turn_results.extend(item_use_results) elif right_click: player_turn_results.append({'targeting_cancelled': True}) if exit: if game_state in (GameStates.SHOW_INVENTORY, GameStates.DROP_INVENTORY): game_state = previous_game_state else: return True if fullscreen: libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen()) for player_turn_result in player_turn_results: message = player_turn_result.get('message') dead_entity = player_turn_result.get('dead') item_added = player_turn_result.get('item_added') item_consumed = player_turn_result.get('consumed') item_dropped = player_turn_result.get('item_dropped') targeting = player_turn_result.get('targeting') targeting_cancelled = player_turn_result.get('targeting_cancelled') if message: message_log.add_message(message) if targeting_cancelled: game_state = previous_game_state message_log.add_message(Message('Targeting cancelled')) if dead_entity: if dead_entity == player: message, game_state = kill_player(dead_entity) else: message = kill_monster(dead_entity) message_log.add_message(message) if item_added: entities.remove(item_added) game_state = GameStates.ENEMY_TURN if item_consumed: game_state = GameStates.ENEMY_TURN if targeting: previous_game_state = GameStates.PLAYERS_TURN game_state = GameStates.TARGETING targeting_item = targeting message_log.add_message(targeting_item.item.targeting_message) if item_dropped: entities.append(item_dropped) game_state = GameStates.ENEMY_TURN if game_state == GameStates.ENEMY_TURN: for entity in entities: if entity.ai: enemy_turn_results = entity.ai.take_turn( player, fov_map, game_map, entities) for enemy_turn_result in enemy_turn_results: message = enemy_turn_result.get('message') dead_entity = enemy_turn_result.get('dead') if message: message_log.add_message(message) if dead_entity: if dead_entity == player: message, game_state = kill_player(dead_entity) else: message = kill_monster(dead_entity) message_log.add_message(message) if game_state == GameStates.PLAYER_DEAD: break if game_state == GameStates.PLAYER_DEAD: break else: game_state = GameStates.PLAYERS_TURN
def main(): # TODO: refactor this stuff around. make certain variables global, etc. screen_width = 80 screen_height = 50 # map settings map_width = 50 map_height = 40 room_max_size = 11 room_min_size = 6 max_rooms = 50 # fov settings fov_algo = tcod.FOV_DIAMOND fov_light_walls = True fov_radius = 7 tcod.console_set_custom_font( FONT, tcod.FONT_TYPE_GREYSCALE | tcod.FONT_LAYOUT_TCOD) tcod.console_init_root(screen_width, screen_height, 'nRogue', False, tcod.RENDERER_OPENGL2, vsync=False) game_map = GameMap(map_width, map_height) con = tcod.console.Console(screen_width, screen_height) key = tcod.Key() mouse = tcod.Mouse() player = Fighter(0, 0, '@', tcod.white, 'Player') entities = [player] game_map.make_map(max_rooms, room_min_size, room_max_size, map_width, map_height, player) fov_recompute = True game_map.populate_map(entities) while not tcod.console_is_window_closed(): tcod.sys_check_for_event(tcod.EVENT_KEY_PRESS, key, mouse) if fov_recompute: game_map.compute_fov(player.x, player.y, fov_radius, fov_algo, fov_light_walls) render_all(con, entities, game_map, map_width, map_height, COLORS, fov_recompute) fov_recompute = False tcod.console_flush() clear_all(con, entities) action = handle_keys(key) move_or_attack = action.get('move_or_attack') exit = action.get('exit') fullscreen = action.get('fullscreen') if move_or_attack: # python returns true for any non-zero value dx, dy = move_or_attack player.move_or_attack(dx, dy, game_map) fov_recompute = True if exit: return True if fullscreen: tcod.console_set_fullscreen(not tcod.console_is_fullscreen()) if key.vk == tcod.KEY_ESCAPE: return True
player.move(player_dx, player_dy) # check for specific key presses combos, etc key = tcod.console_check_for_keypress() if key.vk == tcod.KEY_ENTER and key.lalt: # Alt-enter toggle fullscreen tcod.console_set_fullscreen(not tcod.console_is_fullscreen()) elif key.vk == tcod.KEY_ESCAPE: exit_game = True # set the game fps tcod.sys_set_fps(LIMIT_FPS) # initialize the root console tcod.console_init_root(SCREEN_WIDTH, SCREEN_HEIGHT, window_title, fullscreen) # main game loop while not exit_game: tcod.console_set_default_foreground(0, tcod.white) render_all() tcod.console_flush() for object in objects: object.clear() handle_keys()
def main(): constants = get_constants() # Initialize console stuff libtcod.console_set_custom_font('arial10x10.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD) # The font. libtcod.console_init_root(constants['screen_width'], constants['screen_height'], constants['window_title'], False) #The size of the root console. con = libtcod.console_new(constants['screen_width'], constants['screen_height']) # The main console of the game. panel = libtcod.console_new(constants['screen_width'], constants['panel_height']) # A UI panel for the game. # Declare these variables here, but fill them later player = None entities = [] game_map = None message_log = None game_state = None priority_queue = [] global_variables = None show_main_menu = True show_load_error_message = False main_menu_background_image = libtcod.image_load('menu_background.png') key = libtcod.Key() mouse = libtcod.Mouse() while not libtcod.console_is_window_closed(): libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS | libtcod.EVENT_MOUSE, key, mouse) if show_main_menu: main_menu(con, main_menu_background_image, constants['screen_width'], constants['screen_height']) if show_load_error_message: message_box(con, 'No save game to load', 50, constants['screen_width'], constants['screen_height']) libtcod.console_flush() action = handle_main_menu(key) new_game = action.get('new_game') load_saved_game = action.get('load_game') exit_game = action.get('exit') if show_load_error_message and (new_game or load_saved_game or exit_game): show_load_error_message = False elif new_game: player, entities, game_map, message_log, game_state, priority_queue, global_variables, world = get_game_variables(constants) show_main_menu = False elif load_saved_game: try: player, entities, game_map, message_log, game_state, priority_queue, global_variables, world = load_game() show_main_menu = False except FileNotFoundError: show_load_error_message = True elif exit_game: break else: libtcod.console_clear(con) play_game(player, entities, game_map, message_log, game_state, con, panel, constants, priority_queue, global_variables, world) show_main_menu = True # When play_game() is done, the menu reappears.
def main(): if len(sys.argv) > 1: randomseed = int(sys.argv[1]) seed(randomseed) global draw_offset_x global draw_offset_y p1 = NoiseGrid(size=64, precision=4) p2 = NoiseGrid(size=64, precision=4) p3 = NoiseGrid(size=64, precision=4) noisegrids = [p1, p2, p3] libtcod.console_set_custom_font('arial10x10.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD) root = libtcod.console_init_root(screen_width, screen_height, 'world gen', False, libtcod.RENDERER_SDL2, vsync=True) con = libtcod.console.Console(screen_width, screen_height) printbiome = '' world = WorldMap(map_width, map_height) region = None adjregions = {} draw_offset_x = (int)((screen_width - map_width) / 2) draw_offset_y = (int)((screen_height - map_height) / 2) printworld(root, con, world) libtcod.console_flush() viewstate = ViewState.WORLD while True: for event in tcod.event.wait(): if event.type == "QUIT": raise SystemExit() elif viewstate == ViewState.WORLD: draw_offset_x = (int)((screen_width - map_width) / 2) draw_offset_y = (int)((screen_height - map_height) / 2) printworld(root, con, world) if event.type == "MOUSEBUTTONDOWN": scrx, scry = event.tile mapx = scrx - draw_offset_x mapy = scry - draw_offset_y if (mapx >= 0 and mapx < map_width and mapy >= 0 and mapy < map_height): if event.button == libtcod.event.BUTTON_LEFT: region = RegionMap( mapx, mapy, world, noisegrids, regionside=regionside) viewstate = ViewState.REGION con.clear() printUI(con, world, region, viewstate) libtcod.console_flush() else: print(event.button) elif event.type == "KEYDOWN": if event.sym == libtcod.event.K_ESCAPE: raise SystemExit() elif viewstate == ViewState.REGION: draw_offset_x = (int)((screen_width - regionside) / 2) draw_offset_y = (int)((screen_height - regionside) / 2) printregion(root, con, region, adjregions) if event.type == "MOUSEBUTTONDOWN": scrx, scry = event.tile mapx = scrx - draw_offset_x mapy = scry - draw_offset_y if (mapx >= 0 and mapx < regionside and mapy >= 0 and mapy < regionside): if event.button == libtcod.event.BUTTON_LEFT: #localmap = LocalMap() viewstate = ViewState.LOCAL con.clear() printUI(con, world, region, viewstate) libtcod.console_flush() elif event.button == libtcod.event.BUTTON_RIGHT: newadj = {} cx, cy = (regionside//2, regionside//2) x, y = region.worldpos if (mapx < cx): if ((-1, 0) in adjregions): newadj[(-1, 0)] = \ adjregions[(-1, 0)] else: newadj[(-1, 0)] = \ RegionMap( x-1, y, world, noisegrids, regionside=regionside) if (mapx >= cx): if ((1, 0) in adjregions): newadj[(1, 0)] = \ adjregions[(1, 0)] else: newadj[(1, 0)] = \ RegionMap( x+1, y, world, noisegrids, regionside=regionside) if (mapy < cy): if ((0, -1) in adjregions): newadj[(0, -1)] = \ adjregions[(0, -1)] else: newadj[(0, -1)] = \ RegionMap( x, y-1, world, noisegrids, regionside=regionside) if (mapy >= cy): if ((0, 1) in adjregions): newadj[(0, 1)] = \ adjregions[(0, 1)] else: newadj[(0, 1)] = \ RegionMap( x, y+1, world, noisegrids, regionside=regionside) adjregions.clear() for key in newadj: adjregions[key] = newadj[key] elif event.type == "KEYDOWN": if event.sym == libtcod.event.K_ESCAPE: viewstate = ViewState.WORLD adjregions.clear() elif viewstate == ViewState.LOCAL: draw_offset_x = (int)((screen_width - map_width) / 2) draw_offset_y = (int)((screen_height - map_height) / 2) #printlocal(root, con, localmap) if event.type == "MOUSEBUTTONDOWN": scrx, scry = event.tile mapx = scrx - draw_offset_x mapy = scry - draw_offset_y if (mapx >= 0 and mapx < map_width and mapy >= 0 and mapy < map_height): if event.button == libtcod.event.BUTTON_LEFT: pass elif event.type == "KEYDOWN": if event.sym == libtcod.event.K_ESCAPE: viewstate = ViewState.REGION con.clear() libtcod.console_flush() con.clear() printUI(con, world, region, viewstate) libtcod.console_flush()
def main(): screen_width = 80 screen_height = 50 map_width = 80 map_height = 45 room_max_size = 10 room_min_size = 6 max_rooms = 30 fov_algorithm = 0 fov_light_walls = True fov_radius = 10 colors = { 'dark_wall': libtcod.Color(0, 0, 100), 'dark_ground': libtcod.Color(50, 50, 150), 'light_wall': libtcod.Color(130, 110, 50), 'light_ground': libtcod.Color(200, 180, 50) } player = Entity(int(screen_width / 2), int(screen_height / 2), '@', libtcod.white) npc = Entity(int(screen_height / 2 - 5), int(screen_height / 2 - 5), '@', libtcod.yellow) entities = [npc, player] libtcod.console_set_custom_font( 'arial10x10.png', libtcod.FONT_TYPE_GRAYSCALE | libtcod.FONT_LAYOUT_TCOD) libtcod.console_init_root(screen_width, screen_height, 'THE GAME', False) con = libtcod.console_new(screen_width, screen_height) game_map = GameMap(map_width, map_height) game_map.make_map(max_rooms, room_min_size, room_max_size, map_width, map_height, player) fov_recompute = True fov_map = initialize_fov(game_map) key = libtcod.Key() mouse = libtcod.Mouse() while not libtcod.console_is_window_closed(): libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS, key, mouse) if fov_recompute: recompute_fov(fov_map, player.x, player.y, fov_radius, fov_light_walls, fov_algorithm) render_all(con, entities, game_map, fov_map, fov_recompute, screen_width, screen_height, colors) fov_recompute = False libtcod.console_flush() clear_all(con, entities) action = handle_keys(key) move = action.get('move') exit = action.get('exit') fullscreen = action.get('fullscreen') if move: dx, dy = move if not game_map.is_blocked(player.x + dx, player.y + dy): player.move(dx, dy) fov_recompute = True if exit: return True if fullscreen: libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen())
def main(): tcod.console_init_root(SCREEN_WIDTH, SCREEN_HEIGHT, b"Frogue", False) tcod.sys_set_fps(30) game = Game() game.run()
key = get_key_event() if tcod.console_is_key_pressed(tcod.KEY_UP): make_map() if key.vk == tcod.KEY_ESCAPE: return True def make_map(): # global map for y in range(SCREEN_HEIGHT): for x in range(SCREEN_WIDTH): r = tcod.random_get_int(0, 0, len(colors) - 1) color = colors[r] tcod.console_set_char_background( con, x, y, tcod.Color(color[0], color[1], color[2]), tcod.BKGND_SET) tcod.console_blit(con, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0, 0) tcod.console_set_custom_font(FONT_FILE, tcod.FONT_TYPE_GREYSCALE | tcod.FONT_LAYOUT_TCOD) tcod.console_init_root(SCREEN_WIDTH, SCREEN_HEIGHT, TITLE, FULLSCREEN) con = tcod.console_new(SCREEN_WIDTH, SCREEN_HEIGHT) make_map() while not tcod.console_is_window_closed(): tcod.console_flush() exit = handle_keys() if exit: break
def main(): constants = get_constants() libtcod.console_set_custom_font( 'symbols.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD) libtcod.console_init_root(constants['screen_width'], constants['screen_height'], constants['window_title'], False, renderer=libtcod.RENDERER_SDL2, vsync=True) # con = libtcod.console_new(constants['screen_width'], constants['screen_height']) con = libtcod.console.Console(constants['screen_width'], constants['screen_height']) # panel = libtcod.console_new(constants['screen_width'], constants['panel_height']) panel = libtcod.console.Console(constants['screen_width'], constants['panel_height']) player = None entities = [] game_map = None message_log = None game_state = None show_main_menu = True main_menu_background_image = libtcod.image_load('menu_background.png') key = libtcod.Key() mouse = libtcod.Mouse() while not libtcod.console_is_window_closed(): libtcod.sys_check_for_event( libtcod.EVENT_KEY_PRESS | libtcod.EVENT_MOUSE, key, mouse) if show_main_menu: main_menu(con, main_menu_background_image, constants['screen_width'], constants['screen_height']) libtcod.console_flush() action = handle_main_menu(key) new_game = action.get('new_game') load_saved_game = action.get('load_game') exit_game = action.get('exit') arena = action.get('arena') if new_game: player, entities, game_map, message_log, game_state, camera = get_game_variables( constants) game_state = GameStates.PLAYERS_TURN show_main_menu = False elif load_saved_game: try: player, entities, game_map, message_log, game_state = load_game( ) show_main_menu = False except FileNotFoundError: popup(con, 'No save game to load', constants['screen_width'], constants['screen_height']) elif arena: player, entities, game_map, message_log, game_state = get_arena_variables( constants) game_state = GameStates.PLAYERS_TURN show_main_menu = False elif exit_game: break else: libtcod.console_clear(con) play_game(player, entities, game_map, message_log, game_state, con, panel, constants, camera) show_main_menu = True
def main(): """Game setup and loop""" 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_GRAYSCALE | libtcod.FONT_LAYOUT_TCOD) libtcod.console_init_root(screen_width, screen_height, 'Roguelike 2019', 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 # Main game loop while not libtcod.console_is_window_closed(): libtcod.sys_check_for_event( libtcod.EVENT_KEY_PRESS | libtcod.EVENT_MOUSE, key, mouse) if fov_recompute: recompute_fov(fov_map, player.x, player.y, fov_radius, fov_light_walls, fov_algorithm) render_all(con, panel, entities, player, game_map, fov_map, fov_recompute, message_log, screen_width, screen_height, bar_width, panel_height, panel_y, mouse, colors) libtcod.console_flush() clear_all(con, entities) action = handle_keys(key) move = action.get('move') pickup = action.get('pickup') 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 here to pick up', libtcod.yellow)) 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: message_log.add_message(message) if dead_entity: if dead_entity == player: message, game_state = kill_player(dead_entity) else: message = kill_monster(dead_entity) message_log.add_message(message) if game_state == GameStates.ENEMY_TURN: for entity in entities: 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(): # player_x = int(screen_width / 2) # player_y = int(screen_height / 2) # tcod.console_set_custom_font('arial10x10.png', tcod.FONT_TYPE_GREYSCALE | tcod.FONT_LAYOUT_TCOD) # tcod.console_set_custom_font('hontfont.png', tcod.FONT_TYPE_GRAYSCALE | tcod.FONT_LAYOUT_TCOD, 32, 10) tcod.console_set_custom_font('hontfont.png', tcod.FONT_LAYOUT_ASCII_INROW, 32, 9) load_customfont() print("here") tcod.console_init_root(GAME_WIDTH, GAME_HEIGHT, 'tcod tutorial revised', False) con = tcod.console_new(GAME_WIDTH, GAME_HEIGHT) key = tcod.Key() mouse = tcod.Mouse() gameMap = Map(GAME_WIDTH, GAME_HEIGHT, 9) gameMap.make_map() # for i in range(0, len(gameMap.roomList)): # print(gameMap.roomList[i]) newItem = item(itemList[0]) gameMap.insert_item(newItem, gameMap.roomList[1]) # print(gameMap.roomList[0].itemList) playersToTakeTurn = [] # for i in range(0, len(gameMap.roomList)): # print(gameMap.roomList[i].playerList) p1 = player(playerList[0]) gameMap.insert_player(p1, 0) playersToTakeTurn.append(p1) p2 = player(playerList[1]) gameMap.insert_player(p2, 0) playersToTakeTurn.append(p2) p3 = player(playerList[2]) gameMap.insert_player(p3, 0) playersToTakeTurn.append(p3) p4 = player(playerList[3]) gameMap.insert_player(p4, 0) playersToTakeTurn.append(p4) p5 = player(playerList[4]) gameMap.insert_player(p5, 0) playersToTakeTurn.append(p5) p6 = player(playerList[5]) gameMap.insert_player(p6, 0) playersToTakeTurn.append(p6) p7 = player(playerList[6]) gameMap.insert_player(p7, 0) playersToTakeTurn.append(p7) # for i in range(0, len(gameMap.roomList)): # print(gameMap.roomList[i].playerList) while not tcod.console_is_window_closed(): # tcod.sys_check_for_event(tcod.EVENT_KEY_PRESS, key, mouse) # tcod.console_set_default_foreground(0, tcod.white) render_all(gameMap, con) tcod.console_flush() # key = tcod.console_check_for_keypress() for i in range(0, len(gameMap.roomList)): print("room: ", i, " has ", len(gameMap.roomList[i].playerList), " players") for j in range(0, len(gameMap.roomList[i].playerList)): print(gameMap.roomList[i].playerList[j].name) # print(playersToTakeTurn) for p in playersToTakeTurn: print(p.name + "'s turn") handle_keys(p, gameMap)
def main(): constants = get_constants() libtcod.console_set_custom_font( 'dejavu_wide16x16_gs_tc.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD) libtcod.console_init_root(constants['screen_width'], constants['screen_height'], constants['window_title'], False) con = libtcod.console_new(constants['screen_width'], constants['screen_height']) # Debugging info: at this point con is a big integer. Later on we find that it is something # else. What is going on? # Hopefully fixed. Most likely an error in libtcod engine. Fixed when moving to more python # specific engine. Python-tcod panel = libtcod.console_new(constants['screen_width'], constants['panel_height']) player = None entities = [] game_map = None message_log = None game_state = None ggender = None show_main_menu = True show_load_error_message = False main_menu_background_image = libtcod.image_load('menu_background.png') key = libtcod.Key() mouse = libtcod.Mouse() while not libtcod.console_is_window_closed(): libtcod.sys_check_for_event( libtcod.EVENT_KEY_PRESS | libtcod.EVENT_MOUSE, key, mouse) if show_main_menu: main_menu(con, main_menu_background_image, constants['screen_width'], constants['screen_height']) if show_load_error_message: message_box(con, 'No save game to load', 50, constants['screen_width'], constants['screen_height']) libtcod.console_flush() action = handle_main_menu( key, player, ) new_game = action.get('new_game') load_saved_game = action.get('load_game') exit_game = action.get('exit') if show_load_error_message and (new_game or load_saved_game or exit_game): show_load_error_message = False elif new_game: player, entities, game_map, message_log, game_state, ggender = get_game_variables( constants) game_state = GameStates.PLAYERS_TURN show_main_menu = False elif load_saved_game: try: player, entities, game_map, message_log, game_state, ggender = load_game( ) show_main_menu = False except FileNotFoundError: show_load_error_message = True elif exit_game: break else: libtcod.console_flush() libtcod.console_clear(con) play_game(player, entities, game_map, message_log, game_state, con, panel, constants) show_main_menu = True
def blit(self, flush=False): self.con.blit(root_console, *PC_OFFSET, 0, 0, PC_WIDTH, PC_HEIGHT, 1, 1, 0) if flush: tcod.console_flush() # Setup the font. tcod.console_set_custom_font( "terminal8x12_gs_tc.png", tcod.FONT_LAYOUT_TCOD | tcod.FONT_TYPE_GREYSCALE, ) # Init root console root_console = tcod.console_init_root(SCREEN_WIDTH, SCREEN_HEIGHT, order="F") # Set defaults root_console.default_fg = [80, 80, 80] root_console.default_bg = [0, 0, 0] root_console.default_bg_blend = 0 def render_base(): root_console.draw_frame(MAP_OFFSET[0] - 1, MAP_OFFSET[1] - 1, MAP_WIDTH + 2, MAP_HEIGHT + 2) root_console.draw_frame(NAR_OFFSET[0] - 1, NAR_OFFSET[1] - 1, NAR_WIDTH + 3, NAR_HEIGHT + 2) root_console.draw_frame(PC_OFFSET[0] - 1, PC_OFFSET[1] - 1, PC_WIDTH + 3, PC_HEIGHT + 2)
def main(): # some initializations, main console title = 'NOIGUE L.C.' tcod.sys_set_fps(LIMIT_FPS) tcod.console_set_custom_font( "arial10x10.png", tcod.FONT_TYPE_GREYSCALE | tcod.FONT_LAYOUT_TCOD,) tcod.console_init_root(SCREEN_WIDTH, SCREEN_HEIGHT, title, renderer=tcod.RENDERER_SDL2, order="F") con = tcod.console.Console(MAP_WIDTH, MAP_HEIGHT) con.default_fg = tcod.white game_state = GameStates.INTRO_SCREEN # inputs key = tcod.Key() mouse = tcod.Mouse() while not tcod.console_is_window_closed(): tcod.sys_check_for_event( tcod.EVENT_KEY_PRESS, key, mouse) if game_state == GameStates.INTRO_SCREEN: game_state = start_screen(con, game_state, key, title) elif game_state == GameStates.EXIT: break elif game_state == GameStates.CLASS_CHOICE: game_state = class_choice(con, game_state, key, title) elif game_state == GameStates.PLAYERS_TURN: # start pyo server pyo_server = Server(duplex=0).boot() pyo_server.start() # create player player = Entity(0, 0, '@', tcod.white, 'Player', blocks=True, type=Noiseur(sound=Noise()), inventory=Inventory(26), render=RenderOrder.ENTITY) player.type.sound_out() entities = [player] previous_game_state = game_state # create sub-panels panels = [] panel = Panel(0, CAMERA_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT-CAMERA_HEIGHT, "log") panels.append(panel) panel = Panel(CAMERA_WIDTH, 0, SCREEN_WIDTH - CAMERA_WIDTH, CAMERA_HEIGHT, "sidebar") panels.append(panel) message_log = MessageLog( 2, SCREEN_WIDTH-4, SCREEN_HEIGHT-CAMERA_HEIGHT-2) # create game map and place entities, fov camera = Camera() game_map = GameMap() game_map.make_map(player, entities) fov = FOV(game_map) # compute fov fov.recompute_fov(player) # main loop targeting_item = None while not tcod.console_is_window_closed(): # get events tcod.sys_check_for_event( tcod.EVENT_KEY_PRESS | tcod.EVENT_MOUSE, key, mouse) # render everything render_all(con, panels, entities, player, game_map, fov, message_log, game_state, camera) tcod.console_flush() clear_all(con, panels, entities, camera) # get action from keyboard 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') if show_inventory: previous_game_state = game_state game_state=GameStates.SHOW_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') # deal with actions player_turn_results = [] if game_state == GameStates.PLAYERS_TURN: if move: dx, dy = move destination_x = player.x + dx destination_y = player.y + dy if not game_map.is_blocked(destination_x, destination_y): target = get_blocking_entities_at_location( entities, destination_x, destination_y) if not target: player.move(dx, dy) fov.recompute = True game_state = GameStates.ENEMY_TURN elif pickup : 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.', tcod.yellow)) if game_state == GameStates.SHOW_INVENTORY: if inventory_index is not None and inventory_index < len(player.inventory.items): item = player.inventory.items[inventory_index] player_turn_results.extend(player.inventory.use(item, entities=entities, fov_map=fov.map)) 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=int(target_x+camera.x), target_y=int(target_y+camera.y)) player_turn_results.extend(item_use_results) elif right_click: player_turn_results.append({'targeting_cancelled': True}) for player_turn_result in player_turn_results: message = player_turn_result.get('message') item_added = player_turn_result.get('item_added') item_consumed = player_turn_result.get('consumed') targeting = player_turn_result.get('targeting') targeting_cancelled = player_turn_result.get('targeting_cancelled') if message: message_log.add_message(message) if item_added: entities.remove(item_added) game_state = GameStates.ENEMY_TURN if item_consumed: game_state = GameStates.ENEMY_TURN if targeting: previous_game_state = GameStates.PLAYERS_TURN game_state = GameStates.TARGETING targeting_item = targeting message_log.add_message(targeting_item.item.targeting_message) if targeting_cancelled: game_state = previous_game_state message_log.add_message(Message('Targeting cancelled')) if game_state == GameStates.ENEMY_TURN: for entity in entities: if entity.ai: enemy_turn_results = entity.ai.take_turn( player, entity, fov, game_map, entities) for enemy_turn_result in enemy_turn_results: message = enemy_turn_result.get('message') if message: message_log.add_message(message) else: game_state = GameStates.PLAYERS_TURN if exit: if game_state == GameStates.SHOW_INVENTORY: game_state = previous_game_state elif game_state == GameStates.TARGETING: player_turn_results.append({'targeting_cancelled': True}) else: pyo_server.stop() return True if fullscreen: tcod.console_set_fullscreen( not tcod.console_is_fullscreen())
def main(): constants = get_constants() #sets the font of the console to arial10x10.png tcod.console_set_custom_font('arial10x10.png', tcod.FONT_TYPE_GREYSCALE | tcod.FONT_LAYOUT_TCOD) #creates a non-fullscreen window with the width and height defined earlier #and the title of "Tutorial" tcod.console_init_root(constants['screen_width'], constants['screen_height'], constants['window_title'], False) con = tcod.console_new(constants['screen_width'], constants['screen_height']) panel = tcod.console_new(constants['screen_width'], constants['panel_height']) player = None entities = [] world_map = None message_log = None game_state = None show_main_menu = True show_load_error_message = False main_menu_background_image = tcod.image_load('menu_background.png') key = tcod.Key() mouse = tcod.Mouse() while not tcod.console_is_window_closed(): tcod.sys_check_for_event(tcod.EVENT_KEY_PRESS, key, mouse) if show_main_menu: main_menu(con, main_menu_background_image, constants['screen_width'], constants['screen_height']) if show_load_error_message: message_box(con, 'No save game to load', 50, constants['screen_width'], constants['screen_height']) tcod.console_flush() action = handle_main_menu(key) new_game = action.get('new_game') load_saved_game = action.get('load_game') exit_game = action.get('exit_game') #get better method for main menu item selection if show_load_error_message and (new_game or load_saved_game or exit_game): show_load_error_message = False elif new_game: player, entities, world_map, message_log, game_state = get_game_variables(constants) game_state = GameStates.PLAYER_TURN current_enemy = None show_main_menu = False elif load_saved_game: try: player, entities, world_map, message_log, game_state, current_enemy = load_game() show_main_menu = False except FileNotFoundError: show_load_error_message = True elif exit_game: break else: tcod.console_clear(con) play_game(player, entities, world_map, message_log, game_state, current_enemy, con, panel, constants) show_main_menu = True
def main(): constants = get_constants() font_path = 'arial12x12.png' # this will look in the same folder as this script font_flags = libt.FONT_TYPE_GREYSCALE | libt.FONT_LAYOUT_TCOD # the layout may need to change with a different font libt.console_set_custom_font(font_path, font_flags) fullscreen = False libt.console_init_root(constants['screen_width'], constants['screen_height'], constants['window_title'], fullscreen) con = libt.console_new(constants['screen_width'], constants['screen_height']) panel = libt.console_new(constants['screen_width'], constants['panel_height']) player = None entities = [] game_map = None message_log = None game_state = None show_main_menu = True show_load_error_message = False main_menu_background_image = libt.image_load('menu_background.png') key = libt.Key() mouse = libt.Mouse() while not libt.console_is_window_closed(): libt.sys_check_for_event(libt.EVENT_KEY_PRESS | libt.EVENT_MOUSE, key, mouse) if show_main_menu: main_menu(con, main_menu_background_image, constants['screen_width'], constants['screen_height']) if show_load_error_message: message_box(con, 'No save game to load', 50, constants['screen_width'], constants['screen_height']) libt.console_flush() action = handle_main_menu(key) new_game = action.get('new_game') load_save_game = action.get('load_game') exit_game = action.get('exit') if show_load_error_message and (new_game or load_save_game or exit_game): show_load_error_message = False elif new_game: player, entities, game_map, message_log, game_state = get_game_variables( constants) game_state = GameStates.PLAYERS_TURN show_main_menu = False elif load_save_game: try: player, entities, game_map, message_log, game_state = load_game( ) show_main_menu = False except FileNotFoundError: show_load_error_message = True elif exit_game: break else: libt.console_clear(con) play_game(player, entities, game_map, message_log, game_state, con, panel, constants) show_main_menu = True
def main(): # Console Parameters screen_width = 80 screen_height = 50 # GUI Parameters bar_width = 20 panel_height = 7 panel_y = screen_height - panel_height # Message Parameters message_x = bar_width + 2 message_width = screen_width - bar_width - 2 message_height = panel_height - 1 # Map Parameters map_width = 80 map_height = 43 # Room Parameters room_max_size = 10 room_min_size = 6 max_rooms = 30 # Field of View Parameters fov_algorithm = 0 fov_light_walls = True fov_radius = 10 # Spawn parameters max_monsters_per_room = 3 max_items_per_room = 2 # Tile Colors 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) } # Entity Variables 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] # Map Generator game_map = GameMap( # defines the game map dimensions, calling GameMap class map_width, map_height ) game_map.make_map( # generates the 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) # Console & GUI libtcod.console_set_custom_font('assets/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) message_log = MessageLog(message_x, message_width, message_height) # Input Functions key = libtcod.Key() mouse = libtcod.Mouse() # Game States game_state = GameStates.PLAYERS_TURN previous_game_state = game_state ############################################################# # MAIN GAME LOOP # ############################################################# # So long as the window is open, do... while not libtcod.console_is_window_closed(): libtcod.sys_check_for_event( libtcod.EVENT_KEY_PRESS | libtcod.EVENT_MOUSE, key, mouse) # TRUE on Move, trigger recompute if fov_recompute: recompute_fov( fov_map, player.x, player.y, fov_radius, fov_light_walls, fov_algorithm ) # Render the frame 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 ) # Recompute FOV only on player move fov_recompute = False # Clean up the screen libtcod.console_flush() clear_all( con, entities ) # INPUT HANDLERS 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') # Entity Turn Logics # PLAYER TURN # Combat Results player_turn_results = [] # 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_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 # PASS TURN game_state = GameStates.ENEMY_TURN # Pickup 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: # If item x,y == player x,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)) # Show Inventory if show_inventory: previous_game_state = game_state game_state = GameStates.SHOW_INVENTORY # Drop Inventory if drop_inventory: previous_game_state = game_state game_state = GameStates.DROP_INVENTORY # Inventory Index 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)) # ESC Key Logic if exit: if game_state in (GameStates.SHOW_INVENTORY, GameStates.DROP_INVENTORY): game_state = previous_game_state else: return True # Fullscreen if fullscreen: libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen()) # Handling end of player turn 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 # ENEMY TURN # Handling the 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 # Pass turn back to Player 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 = 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, 'CAVELIKE', 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) fov_recompute = False libtcod.console_flush() clear_all(con, entities) action = handle_keys(key) move = action.get('move') exit = action.get('exit') fullscreen = action.get('fullscreen') player_turn_results = [] if move and game_state == GameStates.PLAYERS_TURN: dx, dy = move destination_x = player.x + dx destination_y = player.y + dy if not 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(): constants = get_constants() libtcod.console_set_custom_font( 'arial10x10.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD) libtcod.console_init_root(constants['screen_width'], constants['screen_height'], constants['window_title'], False) con = libtcod.console_new(constants['screen_width'], constants['screen_height']) panel = libtcod.console_new(constants['screen_width'], constants['panel_height']) player = None entities = [] game_map = None message_log = None game_state = None show_main_menu = True show_load_error_message = False main_menu_background_image = libtcod.image_load('menu_background.png') key = libtcod.Key() mouse = libtcod.Mouse() while not libtcod.console_is_window_closed(): libtcod.sys_check_for_event( libtcod.EVENT_KEY_PRESS | libtcod.EVENT_MOUSE, key, mouse) if show_main_menu: main_menu(con, main_menu_background_image, constants['screen_width'], constants['screen_height']) if show_load_error_message: message_box(con, 'No save game to load', 50, constants['screen_width'], constants['screen_height']) libtcod.console_flush() action = handle_main_menu(key) new_game = action.get('new_game') load_saved_game = action.get('load_game') exit_game = action.get('exit_game') if show_load_error_message and (new_game or load_saved_game or exit_game): show_load_error_message = False elif new_game: player, entities, game_map, message_log, game_state = get_game_variables( constants) game_state = GameStates.PLAYERS_TURN show_main_menu = False elif load_saved_game: try: player, entities, game_map, message_log, game_state = load_game( ) show_main_menu = False except FileNotFoundError: show_load_error_message = True elif exit_game: break else: libtcod.console_clear(con) play_game(player, entities, game_map, message_log, game_state, con, panel, constants) show_main_menu = True
def main(): #basic screen setup screen_width = 80 screen_height = 45 map_w = 60 map_h = 40 map_console_w = 60 map_console_h = 40 #tcod events setup key = tcod.Key() mouse = tcod.Mouse() #create player and put player in entities array player = ec.Entity(4, 2, drawval.CHARS["person"], 15, 0, 10, 10, cx.Faction.Ally, cx.DrawOrder.PLAYER, True, "You") entities = [player] player_state = 1 #player is alive player_gold = 0 player_floor = 1 # get and shuffle trap chars. 4 for floor tiles, and 4 for map glyphs G_TEMP_CHARS = drawval.TRAP_CHARS shuffle(G_TEMP_CHARS) G_TRAP_CHARS = G_TEMP_CHARS[0:4] G_GLYPH_CHARS = G_TEMP_CHARS[4:8] for x in range(0, len(G_GLYPH_CHARS)): G_GLYPH_CHARS[x] += 64 #create new level map level_map, paper_map = new_level(map_w, map_h, entities, G_TRAP_CHARS, G_GLYPH_CHARS) tcod.console_set_custom_font( cx.FONT_FILE[cx.SETTINGS[1]["sel"]], tcod.FONT_TYPE_GRAYSCALE | tcod.FONT_LAYOUT_ASCII_INROW, 32, 16) main_console = tcod.console_init_root(screen_width, screen_height, "D@N ROGUE", False, 3, "F", True) map_console = tcod.console.Console(map_console_w, map_console_h, "F", None) #menu_console = tcod.console.Console(30, 19, "F", None) message_console = tcod.console.Console( map_console.width, main_console.height - map_console.height) status_console = tcod.console.Console( main_console.width - map_console.width, main_console.height, "F", None) status_console.print(status_console.width // 2, 1, "The Unreliable\nCartographer", drawval.COLORS[15], drawval.COLORS[0], tcod.BKGND_DEFAULT, tcod.CENTER) re.legend_print(status_console, G_GLYPH_CHARS, 1, 4) messages = [] for x in range(0, message_console.height): messages.append("") welcome_message = "Welcome to *The Unreliable Cartographer.* This is a game about exploring ancient ruins to find an ancient artifact with a map of increasingly dubious accuracy and dodging traps along the way. Controls are on the left panel. We hope you like it!" re.messageprint(message_console, welcome_message, messages) #menu.menu_print(menu_console) #re.console_borders(menu_console,0,0,menu_console.width-1,menu_console.height-1) fg_sh = 15 bg_sh = 0 fov = player.fov(level_map, entities) #re.console_borders(map_console,0,0,map_console.width-1,map_console.height-1) jump_trigger = False no_enemies = False quit_trigger = False new_floor = False fov = player.fov(level_map, entities) re.draw_paper_map(paper_map, map_console) while True: draw_loop(player, level_map, paper_map, map_console, main_console, message_console, status_console, entities, player_state) status_con(status_console, 2, status_console.height - 2, player_floor, player_gold) for event in tcod.event.wait(): if event.type == "KEYDOWN": action = key_input(event.sym) #pause = False move = action.get('move') exit = action.get('exit') pause = action.get('pause') jump = action.get('jump') controlchange = action.get('controlchange') if player_state == 1: if move: dx, dy = move if not jump_trigger: player.move(dx, dy, level_map, entities, map_console, message_console, messages) elif jump_trigger: for z in range(0, 2): if player.jump(dx, dy, level_map, entities, map_console, message_console, messages) == False: break else: draw_loop(player, level_map, paper_map, map_console, main_console, message_console, status_console, entities, player_state) sleep(0.0080) jump_trigger = False player.lastx = dx player.lasty = dy quit_trigger = False if jump: if not jump_trigger: re.messageprint( message_console, "Press [DIR] to jump 2 squares away in a direction, any other key to cancel.", messages) jump_trigger = True no_enemies = True elif jump_trigger: re.messageprint(message_console, "Jump cancelled.", messages) jump_trigger = False no_enemies = True quit_trigger = False player_gold = player.istrapped(level_map, entities, map_console, message_console, messages, player_gold) print(player_gold) if not no_enemies: for entity in entities: if entity.dispname == "Boulder": entity.move(entity.persistent_x, entity.persistent_y, level_map, entities, map_console, message_console, messages) for entity in entities: if entity.istrap and entity.trapstate > 0: entity.do_trap(level_map, paper_map, main_console, map_console, fov, message_console, messages, entities) if entity.dispname == "gold": entities.remove(entity) if entity.dispname == "stairs": new_floor = True if entity.dispname == "artifact": player_state = 0 elif no_enemies: no_enemies = False if level_map.t_[player.x][player.y].type == "pit": fov = player.fov(level_map, entities) for z in drawval.CHARS["person_fall"]: player.char = z draw_loop(player, level_map, paper_map, map_console, main_console, message_console, status_console, entities, player_state) player_state = 0 entities.remove(player) re.messageprint(message_console, "Oh, dear! You've fallen down a pit!", messages) if player.stats.hp < 1: player_state = 0 status_con(status_console, 2, status_console.height - 2, player_floor, player_gold) if exit: if quit_trigger == False: re.messageprint( message_console, "Quit? [ESC] for 'Yes' or anything else for 'no'.", messages) no_enemies = True quit_trigger = True elif quit_trigger: return True if controlchange: no_enemies = True cx.SETTINGS[0]["sel"] = (cx.SETTINGS[0]["sel"] + 1) % 3 re.legend_print(status_console, G_GLYPH_CHARS, 1, 4) quit_trigger = False re.messageprint( message_console, "Changed controls to " + cx.INPUT_SEL_NAME[cx.SETTINGS[0]["sel"]] + ".", messages) #if pause: # no_enemies = True # menu.menu(main_console,menu_console) elif event.type == "WINDOWCLOSE": return True if new_floor: temp_player = entities.pop entities.clear() level_map.t_.clear() paper_map.t_.clear() player = ec.Entity(4, 2, drawval.CHARS["person"], 15, 0, 10, 10, cx.Faction.Ally, cx.DrawOrder.PLAYER, True, "You") entities = [player] player_floor += 1 player.x = 4 player.y = 2 map_console.clear() level_map, paper_map = new_level(map_w, map_h, entities, G_TRAP_CHARS, G_GLYPH_CHARS, player_floor) new_floor = False re.draw_paper_map(paper_map, map_console) fov = player.fov(level_map, entities) status_con(status_console, 2, status_console.height - 2, player_floor, player_gold)
def main(): # Game and map constants screen_width = 80 screen_height = 50 # HP Bar paramters bar_width = 20 # Panel for bars panel_height = 7 panel_y = screen_height - panel_height # Message bar message_x = bar_width + 2 message_width = screen_width - bar_width - 2 message_height = panel_height - 1 # Map size map_width = 80 map_height = 43 # Room parameters room_max_size = 10 room_min_size = 6 max_rooms = 30 # FoV Variables fov_algoritm = 0 fov_light_walls = True fov_radius = 10 # Entity limits max_monsters_per_room = 3 max_items_per_room = 2 colors = { 'dark_wall': tcod.Color(0, 0, 100), 'dark_ground': tcod.Color(50, 50, 150), 'light_wall': tcod.Color(130, 110, 50), 'light_ground': tcod.Color(200, 180, 50) } fighter_component = Fighter(hp=30, defense=2, power=5) inventory_component = Inventory(26) player = Entity(0, 0, '@', tcod.white, 'Player', blocks=True, render_order=RenderOrder.ACTOR, fighter=fighter_component, inventory=inventory_component) entities = [player] tcod.console_set_custom_font( 'Bisasam_24x24.png', tcod.FONT_TYPE_GREYSCALE | tcod.FONT_LAYOUT_CP437) tcod.console_init_root(screen_width, screen_height, 'tcodtutorial revised', False, renderer=tcod.RENDERER_SDL2) con = tcod.console.Console(screen_width, screen_height) panel = tcod.console.Console(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 = tcod.Key() mouse = tcod.Mouse() game_state = GameStates.PLAYERS_TURN previous_game_state = game_state while not tcod.console_is_window_closed(): tcod.sys_check_for_event(tcod.EVENT_KEY_PRESS | tcod.EVENT_MOUSE, key, mouse) if fov_recompute: recompute_fov(fov_map, player.x, player.y, fov_radius) 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 tcod.console_flush() clear_all(con, entities) action = handle_keys(key, game_state) move = action.get('move') pickup = action.get('pickup') exit = action.get('exit') fullscreen = action.get('fullscreen') show_inventory = action.get('show_inventory') inventory_index = action.get('inventory_index') player_turn_results = [] # Move and/or attack action 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 # Picking up an item 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.', tcod.yellow)) # Open inventory if show_inventory: previous_game_state = game_state game_state = GameStates.SHOW_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] print(item.name) # Exit game if exit: if game_state == GameStates.SHOW_INVENTORY: # Escape from inventory menu back to game game_state = previous_game_state else: return True # Set to fullscreen if fullscreen: tcod.console_set_fullscreen((not tcod.console_is_fullscreen())) ## PROCESS PLAYER'S TURN 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') 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 game_state == GameStates.ENEMY_TURN: for entity in entities: # If entity has 'ai', i.e. can move or attack etc, get 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 cycle_font(): global FONT_INDEX FONT_INDEX = (FONT_INDEX + 1) % len(FONTS) T.console_set_custom_font(*FONTS[FONT_INDEX]) T.console_init_root(SCREEN_W, SCREEN_H, TITLE, False)
def main(): # Screen boundries screen_width = 80 screen_height = 50 # Map boundries map_width = 80 map_height = 45 # Room boundries 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) } # Create player/npc 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] libtcod.console_set_custom_font( './images/arial10x10.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD) libtcod.console_init_root(screen_width, screen_height, 'Alone', False) con = libtcod.console_new(screen_width, screen_height) # Make 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) 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) 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 = screen_width map_height = screen_height - 5 room_max_size = 10 room_min_size = 6 max_rooms = 30 fov_algorithm = 0 fov_light_walls = True fov_radius = 10 max_monsters_per_room = 3 colors = { 'dark_wall': libtcod.Color(0, 0, 100), 'dark_ground': libtcod.Color(50, 50, 150), 'light_wall': libtcod.Color(130, 110, 50), 'light_ground': libtcod.Color(200, 180, 500), } player = Entity(screen_width // 2, screen_height // 2, '@', libtcod.white, 'Player', blocks=True) entities = [player] libtcod.console_set_custom_font( 'dejavu10x10_gs_tc.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD) libtcod.console_init_root(screen_width, screen_height, 'libtcod tutorial revised', False) con = libtcod.console_new(screen_width, screen_height) game_map = GameMap(map_width, map_height) game_map.make_map(max_rooms, room_min_size, room_max_size, map_width, map_height, player, entities, max_monsters_per_room) fov_recompute = True fov_map = initialize_fov(game_map) key = libtcod.Key() mouse = libtcod.Mouse() while not libtcod.console_is_window_closed(): libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS, key, mouse) if fov_recompute: recompute_fov(fov_map, player.x, player.y, fov_radius, fov_light_walls, fov_algorithm) render_all(con, entities, game_map, fov_map, fov_recompute, screen_width, screen_height, colors) fov_recompute = False libtcod.console_flush() clear_all(con, entities) action = handle_keys(key) move = action.get('move') exit = action.get('exit') fullscreen = action.get('fullscreen') if move: dx, dy = move destination_x = player.x + dx destination_y = player.y + dy if not game_map.is_blocked(destination_x, destination_y): target = get_blocking_entities_at_location( entities, destination_x, destination_y) if target: print( 'You kick the {} in the shins, much to its cansternation!' .format(target.name)) else: player.move(dx, dy) fov_recompute = True if exit: return True if fullscreen: libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen())
def create_console(title: str, console_width: int, console_height: int, font_file: str, fps_limit: int): libtcod.console_set_custom_font(fontFile=font_file, flags=libtcod.FONT_LAYOUT_ASCII_INROW) libtcod.console_init_root(w=console_width, h=console_height, title=title, fullscreen=False) libtcod.sys_set_fps(fps_limit) return libtcod.console_new(w=console_width, h=console_height)
def main(): constants = get_constants() libtcod.console_set_custom_font( 'arial10x10.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD) libtcod.console_init_root(constants['screen_width'], constants['screen_height'], constants['window_title'], False) con = libtcod.console_new(constants['screen_width'], constants['screen_height']) panel = libtcod.console_new(constants['screen_width'], constants['panel_height']) player, entities, game_map, message_log, game_state = get_game_variables( constants) fov_recompute = True # used to tell algo when to change field of view, i.e. only when moving not when standing still/attacking fov_map = initialize_fov(game_map) key = libtcod.Key() mouse = libtcod.Mouse() previous_game_state = game_state # Used to not skip a players turn if they do something like pick up an item targeting_item = None while not libtcod.console_is_window_closed( ): # a loop that won't end until the game window is closed libtcod.sys_check_for_event( libtcod.EVENT_KEY_PRESS | libtcod.EVENT_MOUSE, key, mouse ) # this captures user input and updates the key/mouse variables above if fov_recompute: recompute_fov( fov_map, player.x, player.y, constants['fov_radius'], constants['fov_light_walls'], constants['fov_algorithm']) # coming from fov_functions render_all( con, panel, entities, player, game_map, fov_map, fov_recompute, message_log, constants['screen_width'], constants['screen_height'], constants['bar_width'], constants['panel_height'], constants['panel_y'], mouse, constants['colors'], game_state ) # con is the current console, entities is drawn in render_functions fov_recompute = False libtcod.console_flush() # this presents everything on screen clear_all( con, entities ) # put a blank below our character so it doesn't leave a trail, see render_functions.py """Action Handling""" action = handle_keys( key, game_state ) # calls our handle keys function from the input_handlers file, using our key variable, to result in a dictionary called action mouse_action = handle_mouse(mouse) move = action.get( 'move' ) # uses the action dictionary created above to return move, exit, pickup or fullscreen from handle_keys function pickup = action.get('pickup') show_inventory = action.get('show_inventory') drop_inventory = action.get('drop_inventory') inventory_index = action.get('inventory_index') exit = action.get('exit') fullscreen = action.get('fullscreen') left_click = mouse_action.get('left_click') right_click = mouse_action.get('right_click') player_turn_results = [] if move and game_state == GameStates.PLAYERS_TURN: dx, dy = move destination_x = player.x + dx destination_y = player.y + dy if not game_map.is_blocked(destination_x, destination_y): target = get_blocking_entities_at_location( entities, destination_x, destination_y) if target: # i.e. does something to the target blocking the way attack_results = player.fighter.attack(target) player_turn_results.extend(attack_results) else: player.move(dx, dy) # gets move function from entity class fov_recompute = True # sets fov to True (i.e. to change) when we move game_state = GameStates.ENEMY_TURN # switches it to the enemies turn after moving elif pickup and game_state == GameStates.PLAYERS_TURN: for entity in entities: if entity.item and entity.x == player.x and entity.y == player.y: pickup_results = player.inventory.add_item(entity) player_turn_results.extend(pickup_results) break else: message_log.add_message( Message('There is nothing here to pick up.', libtcod.yellow)) if show_inventory: previous_game_state = game_state # ref'd at top, stops it skipping to enemies turn game_state = GameStates.SHOW_INVENTORY # switching to this game state with different keys if drop_inventory: previous_game_state = game_state game_state = GameStates.DROP_INVENTORY if inventory_index is not None and previous_game_state != GameStates.PLAYER_DEAD and inventory_index < len( player.inventory.items): item = player.inventory.items[inventory_index] if game_state == GameStates.SHOW_INVENTORY: player_turn_results.extend( player.inventory.use(item, entities=entities, fov_map=fov_map)) elif game_state == GameStates.DROP_INVENTORY: player_turn_results.extend(player.inventory.drop_item(item)) if game_state == GameStates.TARGETING: if left_click: target_x, target_y = left_click item_use_results = player.inventory.use(targeting_item, entities=entities, fov_map=fov_map, target_x=target_x, target_y=target_y) player_turn_results.extend(item_use_results) elif right_click: player_turn_results.append({'targeting_cancelled': True}) if exit: if game_state in (GameStates.SHOW_INVENTORY, GameStates.DROP_INVENTORY): game_state = previous_game_state # now the Esc key just exits the inventory menu, rather than the whole game elif game_state == GameStates.TARGETING: player_turn_results.append({'targeting_cancelled': True}) else: return True if fullscreen: libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen()) for player_turn_result in player_turn_results: message = player_turn_result.get('message') dead_entity = player_turn_result.get('dead') item_added = player_turn_result.get('item_added') item_consumed = player_turn_result.get('consumed') item_dropped = player_turn_result.get('item_dropped') targeting = player_turn_result.get('targeting') targeting_cancelled = player_turn_result.get('targeting_cancelled') if message: message_log.add_message(message) if targeting_cancelled: game_state = previous_game_state message_log.add_message(Message('Targeting cancelled')) if dead_entity: if dead_entity == player: message, game_state = kill_player(dead_entity) else: message = kill_monster(dead_entity) message_log.add_message(message) if item_added: entities.remove(item_added) game_state = GameStates.ENEMY_TURN if item_consumed: game_state = GameStates.ENEMY_TURN if targeting: previous_game_state = GameStates.PLAYERS_TURN game_state = GameStates.TARGETING targeting_item = targeting message_log.add_message(targeting_item.item.targeting_message) if item_dropped: entities.append(item_dropped) game_state = GameStates.ENEMY_TURN if game_state == GameStates.ENEMY_TURN: for entity in entities: if entity.ai: # note, the player and items etc won't have an AI component, so this skips them enemy_turn_results = entity.ai.take_turn( player, fov_map, game_map, entities ) # take turn managed in ai file, tells it to move towards or attack player for enemy_turn_result in enemy_turn_results: message = enemy_turn_result.get('message') dead_entity = enemy_turn_result.get('dead') if message: message_log.add_message(message) if dead_entity: if dead_entity == player: message, game_state = kill_player(dead_entity) else: message = kill_monster(dead_entity) message_log.add_message(message) if game_state == GameStates.PLAYER_DEAD: break if game_state == GameStates.PLAYER_DEAD: break else: game_state = GameStates.PLAYERS_TURN
elif libtcod.console_is_key_pressed(libtcod.KEY_DOWN): playery += 1 elif libtcod.console_is_key_pressed(libtcod.KEY_LEFT): playerx -= 1 elif libtcod.console_is_key_pressed(libtcod.KEY_RIGHT): playerx += 1 ############################################# # Initialization & Main Loop ############################################# libtcod.console_set_custom_font('arial10x10.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD) libtcod.console_init_root(SCREEN_WIDTH, SCREEN_HEIGHT, 'python/libtcod tutorial', False) libtcod.sys_set_fps(LIMIT_FPS) playerx = SCREEN_WIDTH//2 playery = SCREEN_HEIGHT//2 while not libtcod.console_is_window_closed(): libtcod.console_set_default_foreground(0, libtcod.white) libtcod.console_put_char(0, playerx, playery, '@', libtcod.BKGND_NONE) libtcod.console_flush() libtcod.console_put_char(0, playerx, playery, ' ', libtcod.BKGND_NONE) #handle keys and exit game if needed
def main(): screen_width = 80 screen_height = 50 bar_width = 20 panel_height = 7 panel_y = screen_height - panel_height message_x = bar_width + 2 message_width = screen_width - bar_width - 2 message_height = panel_height - 1 map_width = 80 map_height = 43 room_max_size = 30 room_min_size = 6 max_rooms = 30 fov_algorithm = 2 fov_light_walls = True fov_radius = 10 max_monsters_per_room = 3 max_items_per_room = 2 colors = { 'dark_wall': libtcod.Color(0, 0, 100), 'dark_ground': libtcod.Color(50, 50, 150), 'light_wall': libtcod.Color(130, 110, 50), 'light_ground': libtcod.Color(200, 180, 50) } til = Tile(False) fighter_component = Fighter(30, defense=2, power=5) player = Entity(0, 0, '@', libtcod.white, 'Player', blocks=True, fighter=fighter_component, render_order=RenderOrder.ACTOR) entities = [player] libtcod.console_set_custom_font( 'arial10x10.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD) libtcod.console_init_root(screen_width, screen_height, 'libtcod tutorial revised', False) con = libtcod.console_new(screen_width, screen_height) panel = libtcod.console_new(screen_width, panel_height) map = GameMap(map_width, map_height) map.make_map(max_rooms, room_min_size, room_max_size, map_width, map_height, player, entities, max_monsters_per_room, max_items_per_room) fov_recompute = True fov_map = initialize_fov(map) message_log = MessageLog(message_x, message_width, message_height) key = libtcod.Key() mouse = libtcod.Mouse() game_state = GameStates.PLAYERS_TURN while not libtcod.console_is_window_closed(): libtcod.sys_check_for_event( libtcod.EVENT_KEY_PRESS | libtcod.EVENT_MOUSE, key, mouse) if fov_recompute: recompute_fov(fov_map, player.x, player.y, fov_radius, fov_light_walls, fov_algorithm) entities_in_render_order = sorted(entities, key=lambda x: x.render_order.value) render_all(con, panel, entities_in_render_order, player, map, fov_map, fov_recompute, message_log, screen_width, screen_height, bar_width, panel_height, panel_y, mouse, colors) fov_recompute = False libtcod.console_flush() clear_all(con, entities) action = handle_keys(key) move = action.get('move') exit = action.get('exit') fullscreen = action.get('fullscreen') player_turn_results = [] if move and game_state == GameStates.PLAYERS_TURN: dx, dy = move destination_x = player.x + dx destination_y = player.y + dy if not map.is_blocked(destination_x, destination_y): target = get_blocking_entities_at_location( entities, destination_x, destination_y) if target: attack_results = player.fighter.attack(target) player_turn_results.extend(attack_results) else: player.move(dx, dy) fov_recompute = True game_state = GameStates.ENEMY_TURN else: message_log.add_message(Message('stuck')) if fullscreen: libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen()) if exit: return True for player_turn_result in player_turn_results: message = player_turn_result.get('message') dead_entity = player_turn_result.get('dead') if message: message_log.add_message(message) if dead_entity: if dead_entity == player: message, game_state = kill_player(dead_entity) else: message = kill_monster(dead_entity) message_log.add_message(message) if game_state == GameStates.ENEMY_TURN: for entity in entities_in_render_order: if entity != player and entity.ai != None: enemy_turn_results = entity.ai.take_turn( fov_map, player, map, entities) for enemy_turn_result in enemy_turn_results: message = enemy_turn_result.get('message') dead_entity = enemy_turn_result.get('dead') if message: message_log.add_message(message) if dead_entity: if dead_entity == player: message, game_state = kill_player(dead_entity) else: message = kill_monster(dead_entity) message_log.add_message(message) if game_state == GameStates.PLAYER_DEAD: break game_state = GameStates.PLAYERS_TURN