def main_menu(): img = libtcod.image_load('menu_background.png') while not libtcod.console_is_window_closed(): #show the background image, at twice the regular console resolution libtcod.image_blit_2x(img, 0, 0, 0) #show the game's title, and some credits! libtcod.console_set_default_foreground(0, libtcod.light_yellow) libtcod.console_print_ex(0, SCREEN_WIDTH//2, SCREEN_HEIGHT//2-4, libtcod.BKGND_NONE, libtcod.CENTER, 'TOMBS OF THE ANCIENT KINGS') libtcod.console_print_ex(0, SCREEN_WIDTH//2, SCREEN_HEIGHT-2, libtcod.BKGND_NONE, libtcod.CENTER, 'By Jotaf') #show options and wait for the player's choice choice = menu('', ['Play a new game', 'Continue last game', 'Quit'], 24) if choice == 0: #new game new_game() play_game() if choice == 1: #load last game try: load_game() except: msgbox('\n No saved game to load.\n', 24) continue play_game() elif choice == 2: #quit break
def play_game(): global key, mouse player_action = None mouse = libtcod.Mouse() key = libtcod.Key() while not libtcod.console_is_window_closed(): #render the screen libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS|libtcod.EVENT_MOUSE,key,mouse) render_all() libtcod.console_flush() #level up if needed check_level_up() #erase all objects at their old locations, before they move for object in objects: object.clear() #handle keys and exit game if needed player_action = handle_keys() if player_action == 'exit': save_game() break #let monsters take their turn if game_state == 'playing' and player_action != 'didnt-take-turn': for object in objects: if object.ai: object.ai.take_turn()
def run(self): root = self.window_manager.rootc root.init_root() while (not self.end_game) and (not tcod.console_is_window_closed()): #events = sys_get_events() #self.handle_keys(key) if self.ipykernel: self.ipy_kernel_callback(self.ipykernel) root.clear() self.step(root) root.flush()
def main(): key_event = tcod.Key() mouse_event = tcod.Mouse() #Build start menu start_frame = Frame("start frame") start_panel = Panel(0, 0, constants.CONSOLE_WIDTH, constants.CONSOLE_HEIGHT) start_panel.set_controller(GamePanelController()) start_frame.add_panel(start_panel) core.add_frame(start_frame) core.set_current_frame("start frame") #Main Loop while not tcod.console_is_window_closed(): #Get input tcod.sys_check_for_event(tcod.EVENT_KEY_PRESS, key_event, mouse_event) if key_event.vk == tcod.KEY_ESCAPE: return True else: core.current_frame.receive_events(key_event, mouse_event) core.current_frame.update() renderer.render()
#the list of objects with those two objects = [npc, player] #generate map (at this point it's not drawn to the screen) make_map() #create the FOV map, according to the generated map fov_map = tcod.map_new(MAP_WIDTH, MAP_HEIGHT) for y in range(MAP_HEIGHT): for x in range(MAP_WIDTH): tcod.map_set_properties(fov_map, x, y, not map[x][y].block_sight, not map[x][y].blocked) fov_recompute = True while not tcod.console_is_window_closed(): #render the screen render_all() tcod.console_flush() #erase all objects at their old locations, before they move for object in objects: object.clear() #handle keys and exit game if needed exit = handle_keys() if exit: break
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(): 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 saved game to load", 50, constants["screen_width"], constants["screen_width"]) libtcod.console_flush() action = handle_main_menu(key) new_game = action.get("new_game") load_saved_game = action.get("load_game") exit_game = action.get("exit") if show_load_error_message and (new_game or load_saved_game or exit_game): show_load_error_message = False elif new_game: player, entities, game_map, message_log, game_state = get_game_variables( constants) game_state = GameStates.PLAYERS_TURN show_main_menu = False elif load_saved_game: try: player, entities, game_map, message_log, game_state = load_game( ) show_main_menu = False except FileNotFoundError: show_load_error_message = True elif exit_game: break else: libtcod.console_clear(con) play_game(player, entities, game_map, message_log, game_state, constants, con, panel) show_main_menu = True
def play_game(player, entities, game_map, message_log, game_state, con, panel, constants): fov_recompute = True fov_map = initialize_fov(game_map) key = tcod.Key() mouse = tcod.Mouse() game_state = GameStates.PLAYERS_TURN previous_game_state = game_state targeting_item = None 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, 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 tcod.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.', 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 game_state in (GameStates.SHOW_INVENTORY, GameStates.DROP_INVENTORY): game_state = previous_game_state elif game_state == GameStates.TARGETING: player_turn_results.append({'targeting_cancelled': True}) else: save_game(player, entities, game_map, message_log, game_state) return True if fullscreen: tcod.console_set_fullscreen(not tcod.console_is_fullscreen()) for player_turn_result in player_turn_results: message = player_turn_result.get('message') dead_entity = player_turn_result.get('dead') item_added = player_turn_result.get('item_added') item_consumed = player_turn_result.get('consumed') item_dropped = player_turn_result.get('item_dropped') targeting = player_turn_result.get('targeting') targeting_cancelled = player_turn_result.get('targeting_cancelled') if message: message_log.add_message(message) if dead_entity: if dead_entity == player: message, game_state = kill_player(dead_entity) else: message = kill_monster(dead_entity) message_log.add_message(message) if 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 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() game_state = GameStates.PLAYERS_TURN previous_game_state = game_state targeting_item = None # game loop while not libtcod.console_is_window_closed(): # captures user input - will update the key and mouse variables with what the user inputs 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']) # draw the entities and blit the changes to the screen - only render the item inventory when the game state is in the inventory state 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 # present everything on the screen libtcod.console_flush() # clear entities after drawing to screen - this makes it so that when entities move, they do not leave a trail behind clear_all(con, entities) # gives a way to gracefully exit program by hitting the ESC key # gets any keyboard input to the program and stores in the key variable 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 = [] # move the player only on the players turn if move and game_state == GameStates.PLAYERS_TURN: dx, dy = move destination_x = player.x + dx destination_y = player.y + dy # check if there is something at the destination that would block the player - if not, move the player there 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 # change to enemy's turn after player's move game_state = GameStates.ENEMY_TURN elif wait: game_state = GameStates.ENEMY_TURN # if the player did not move and performed the pickup action by pressing the key 'g'... elif pickup and game_state == GameStates.PLAYERS_TURN: # loop through each entity on the map, check if it is an item and occupies the same space as the player for entity in entities: # if the entity is an item and in the same position as the player if entity.item and entity.x == player.x and entity.y == player.y: # add the item to the inventory and append the results to player_turn_results pickup_results = player.inventory.add_item(entity) player_turn_results.extend(pickup_results) break # makes it so the player only picks up one item at a time 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 # take the index selected, use the item selected 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}) # checks if the key pressed was the Esc key - if it was, then exit the loop 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_dropped: entities.append(item_dropped) 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 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 # note that this is a for-else statement; without a break statement, this else will always happen else: game_state = GameStates.PLAYERS_TURN
def main(): constants = get_constants() # tell libtcod which font to use; dictate the file to read from, and the other two arguments tell libtcod which # type of file is being read libtcod.console_set_custom_font( 'arial10x10.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD) # create the screen with specified width and height; title; boolean value to say full screen or not libtcod.console_init_root(constants['screen_width'], constants['screen_height'], 'libtcod tutorial revised', False) con = libtcod.console_new(constants['screen_width'], constants['screen_height']) # create a new console to hold the HP bar and message log panel = libtcod.console_new(constants['screen_width'], constants['panel_height']) player = None entities = [] game_map = None message_log = None game_state = None show_main_menu = True show_load_error_message = False main_menu_background_image = libtcod.image_load('menu_background.png') key = libtcod.Key() mouse = libtcod.Mouse() while not libtcod.console_is_window_closed(): libtcod.sys_check_for_event( libtcod.EVENT_KEY_PRESS | libtcod.EVENT_MOUSE, key, mouse) if show_main_menu: main_menu(con, main_menu_background_image, constants['screen_width'], constants['screen_height']) if show_load_error_message: message_box(con, 'No save game to load', 50, constants['screen_width'], constants['screen_height']) libtcod.console_flush() action = handle_main_menu(key) new_game = action.get('new_game') load_saved_game = action.get('load_game') exit_game = action.get('exit') if show_load_error_message and (new_game or load_saved_game or exit_game): show_load_error_message = False elif new_game: player, entities, game_map, message_log, game_state = get_game_variables( constants) game_state = GameStates.PLAYERS_TURN show_main_menu = False elif load_saved_game: try: player, entities, game_map, message_log, game_state = load_game( ) show_main_menu = False except FileNotFoundError: show_load_error_message = True elif exit_game: break else: libtcod.console_clear(con) play_game(player, entities, game_map, message_log, game_state, con, panel, constants) show_main_menu = True
def main(): constants = get_constants() libtcod.console_set_custom_font( 'TiledFont.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD, 32, 10) libtcod.console_init_root(constants['screen_width'], constants['screen_height'], constants['window_title'], False) 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_character_screen = False start_game = None 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') choose_fighter = action.get('choose_fighter') 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, 'barbarian') game_state = GameStates.PLAYER_TURN show_main_menu = False start_game = True elif load_saved_game: try: player, entities, game_map, message_log, game_state = load_game( ) show_main_menu = False start_game = True except FileNotFoundError: show_load_error_message = True elif choose_fighter: show_main_menu = False #show_character_screen = True new_character_screen(con, main_menu_background_image, constants['screen_width'], constants['screen_height']) libtcod.console_flush() '''TODO: Задать переменную, отвечающую за то, что открыто ли окно choose_fighter или нет''' action_character_menu = handle_new_character_menu_keys(key) warrior = 'warrior' thief = 'thief' barbarian = 'barbarian' exit_character_menu = 'exit' if action_character_menu.get('warrior') == warrior: player, entities, game_map, message_log, game_state = get_game_variables( constants, warrior) game_state = GameStates.PLAYER_TURN #show_character_screen = False #show_main_menu = False #start_game = True #start_game = True choose_fighter = False elif action_character_menu.get('thief') == thief: player, entities, game_map, message_log, game_state = get_game_variables( constants, thief) #game_state = GameStates.PLAYER_TURN #show_character_screen = False choose_fighter = False elif action_character_menu.get('barbarian') == barbarian: player, entities, game_map, message_log, game_state = get_game_variables( constants, barbarian) game_state = GameStates.PLAYER_TURN #show_character_screen = False elif action_character_menu.get('exit') == exit_character_menu: show_character_screen = False show_main_menu = True main_menu(con, main_menu_background_image, constants['screen_width'], constants['screen_height']) libtcod.console_flush() show_character_screen = False elif choose_fighter == False: start_game = True game_state.PLAYER_TURN elif exit_game: break elif start_game == True: libtcod.console_clear(con) play_game(player, entities, game_map, message_log, game_state, con, panel, constants) show_main_menu = True
def run(self): while (not self.end_game) and (not tcod.console_is_window_closed()): events = sys_get_events() self.clock.tick() self.kernel.do_one_iteration()
def window_closed(self): return tcod.console_is_window_closed()
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 main(): #------------------------------------------------# # INIT #------------------------------------------------# # new way to init rog.Rogue.create_settings() # later controllers might depend on settings rog.Rogue.create_window() rog.Rogue.create_consoles() rog.Rogue.create_world() rog.Rogue.create_controller() rog.Rogue.create_data() rog.Rogue.create_map() rog.Rogue.create_clock() rog.Rogue.create_updater() rog.Rogue.create_view() rog.Rogue.create_log() rog.Rogue.create_savedGame() rog.Rogue.create_processors() rog.Rogue.create_perturn_managers() rog.Rogue.create_const_managers() rog.Rogue.create_player(0,0) # what position should be given? rog.init_keyBindings() ## #map generation ## rog.map_generate(rog.map(),rog.dlvl()) ## ## # init player object ## pc=rog.create_player(15,15) ## # TODO: observer for player #### obs=observer.Observer_playerChange() #### pc.observer_add(obs) ## rog.view_center(pc) ## ## ## ##TESTING ## rog.gain(pc,"hpmax",100) ## log=rog.create_stuff(THG.LOG, 18,18) ## rog.burn(log,200) ## box=rog.create_stuff(THG.BOX,20,18) ## #fluids.smoke(16,16) ## '''pc.stats.hpmax = 20 ## pc.stats.mpmax = 20 ## pc.stats.sight = 20 ## pc.stats.spd = 100 ## pc.stats.nvision = False''' ## #LIGHT ## pc.light=rog.create_light(pc.x,pc.y, 5, owner=pc) ## #HEAL ## rog.givehp(pc) ## rog.givemp(pc) ## #EQUIP ## #print(pc.stats.get('atk')) ## item=gear.create_weapon("sword",pc.x,pc.y) ## rog.equip(pc, item, EQ_MAINHAND) ## #print(pc.stats.get('atk')) ## #rog.deequip(pc,EQ_MAINHAND) ## #print(pc.stats.get('atk')) ## #BUFF ## #rog.effect_add(pc,{'atk':5}) ## #rog.effect_add(pc,{'dmg':5}) ## #rog.effect_add(pc,{'arm':0}) ## #rog.effect_add(pc,{'msp':15}) ## #rog.effect_add(pc,{'msp':-50})''' ## ''' ## z = rog.create_monster('Z',15,16) ## rog.effect_add(z,{'arm':1}) ## rog.effect_add(z,{'dfn':4}) ## rog.effect_add(z,{'atk':3}) ## rog.effect_add(z,{'dmg':3}) ## rog.givehp(z)''' ## z=rog.create_monster('z',13,19,COL['ltblue']) ## a=rog.create_monster('a',12,13,COL['scarlet']) ## o=rog.create_monster('U',19,18,COL['red']) ## a=rog.create_monster('a',15,17,COL['scarlet']) ## W=rog.create_monster('W',20,15,COL['purple']) ## '''import dice ## for x in range(ROOMW): ## for y in range(ROOMH): ## if dice.roll(100) > 98: ## if not (rog.wallat(x,y) ## or rog.monat(x,y) ): ## r=rog.create_monster('r',x,y) ## r.color=COL['ltgray'] ## print("num monsters: ", len(rog.list_creatures())) ## ''' #-----------------------------------------------# # # MAIN GAME LOOP # # #-----------------------------------------------# rog.game_set_state("normal") ## # initialize fov for creatures with sight ## # IS THIS NOT WORKING???? WHAT'S GOING ON? ## for creat in rog.list_creatures(): ## if creat.stats.sight > 0: ## rog.fov_compute(creat) while rog.game_is_running(): # manually close game # if libtcod.console_is_window_closed(): rog.end() # defeat conditions # if rog.on(pc, DEAD): rog.game_set_state("game over") # get input # pcInput=IO.get_raw_input() pcAct=IO.handle_mousekeys(pcInput).items() # commands that are available from anywhere # player.commands_const(pc, pcAct) # Finally record game state after any/all changes # gameState=rog.game_state() #----------# # PLAY # #----------# # # normal game play # if gameState == "normal": game.play(pc, pcAct) # # other game states # # elif (gameState == "move view" or gameState == "look" or gameState == "busy" or gameState == "message history" ): manager=rog.get_active_manager() manager.run(pcAct) if manager.result: rog.close_active_manager() # elif gameState == "game over": rog.msg("You died...")
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 input_mode = InputModes.INPUT_NUMPAD 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(12) 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, input_mode) 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 here to pick up.", libtcod.blue)) 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 main(): # Setting the screen size screen_width = 80 screen_height = 50 # Variables to keep track of player position player_x = int(screen_width/2) player_y = int(screen_height/2) # Telling libtcod which font to use. We read the font details from the arial10x10.png file that we saved down. # The other two parts are telling libtcod which type of file we're reading. libtcod.console_set_custom_font('arial10x10.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD) # This line actually creates the screen, using the screen heigh and width that we specified. # The boolean at the end tells it whether to go full screen or not. libtcod.console_init_root(screen_width, screen_height, 'Adversarial Attacker', False) # Define console con = libtcod.console_new(screen_width, screen_height) # Variables to hold our keyboard and mouse inputs. key = libtcod.Key() mouse = libtcod.Mouse() # This begins what's called our 'game loop'. This won't end until we close the game. while not libtcod.console_is_window_closed(): # This line captures new events (inputs from the user) # This updates the key variable with the user input but doesn't actually do anything with it yet. libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS, key, mouse) # Provides the font color for our foreground, which is our '@' symbol. # The first arguement is the console we're drawing to. libtcod.console_set_default_foreground(con, libtcod.white) # The first argument is the console we're printing to again. # The second and third arguments are x and y coordinates for where to draw. # The third argument is what to draw. # The fourth argument sets the background to none. libtcod.console_put_char(con, player_x, player_y, "@", libtcod.BKGND_NONE) # This part actually draws our symbol. libtcod.console_blit(con, 0, 0, screen_width, screen_height, 0, 0, 0) # This line presents everything on the screen. libtcod.console_flush() # Removes the trailing '@' symbol when we move (so that we don't create a snake). libtcod.console_put_char(con, player_x, player_y, ' ', libtcod.BKGND_NONE) # Use the handle_keys function that we created to translate our key press into an action. action = handle_keys(key) # Grab our actions (if they exist) move = action.get('move') exit = action.get('exit') fullscreen = action.get('fullscreen') # Carry out our movement action if it exists. if move: # Set dx and dy values to our move coordinates. dx, dy = move # Update player (x,y) position using (dx,dy). player_x += dx player_y += dy # Exit the game if that was the action taken by the user. if exit: return True # Go fullscreen if that was the action taken by the user. if fullscreen: libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen())
def main(): constants = get_constants() libtcod.console_set_custom_font('taffer.png', libtcod.FONT_LAYOUT_ASCII_INROW) libtcod.console_init_root(constants['screen_width'], constants['screen_height'], constants['window_title'], False) con = libtcod.console.Console(constants['screen_width'], constants['screen_height']) panel = libtcod.console_new(constants['screen_width'], constants['panel_height']) player = None entities = [] game_map = None message_log = None game_state = None show_main_menu = True show_load_error_message = False main_menu_background_image = libtcod.image_load('menu_background.png') key = libtcod.Key() mouse = libtcod.Mouse() while not libtcod.console_is_window_closed(): libtcod.sys_check_for_event( libtcod.EVENT_KEY_PRESS | libtcod.EVENT_MOUSE, key, mouse) if show_main_menu: main_menu(con, main_menu_background_image, constants['screen_width'], constants['screen_height']) if show_load_error_message: message_box(con, 'No save game to load', 50, constants['screen_width'], constants['screen_height']) libtcod.console_flush() action = handle_main_menu(key) new_game = action.get('new_game') load_saved_game = action.get('load_game') exit_game = action.get('exit') if show_load_error_message and (new_game or load_saved_game or exit_game): show_load_error_message = False elif new_game: player, entities, game_map, message_log, game_state = get_game_variables( constants) game_state = GameStates.PLAYERS_TURN show_main_menu = False elif load_saved_game: try: player, entities, game_map, message_log, game_state = load_game( ) show_main_menu = False except FileNotFoundError: show_load_error_message = True elif exit_game: break else: libtcod.console_clear(con) play_game(player, entities, game_map, message_log, game_state, con, panel, constants) show_main_menu = True
def main(): 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 play_game(player, entities, game_map, message_log, game_state, con, panel, constants): # Intialize FOV map. fov_recompute = True # Recompute FOV after the player moves fov_map = initialize_fov(game_map) target_fov_map = initialize_fov(game_map) fov_map_no_walls = initialize_fov(game_map) # Capture keyboard and mouse input key = libtcod.Key() mouse = libtcod.Mouse() game_state = GameStates.PLAYERS_TURN previous_game_state = game_state # Store the item that the player used to enter targeting mode (ie lightning scroll). This is so that we know what item we need to remove from inventory etc. targeting_item = None cursor_radius = 1 # For showing object descriptions description_recompute = True description_list = [] description_index = 0 prev_mouse_y = None prev_mouse_x = None # Start music mixer.init() mixer.music.load(os.path.join(definitions.ROOT_DIR, 'data', 'music', 'bgm2.mp3')) #mixer.music.play(loops=-1) #Our main loop while not libtcod.console_is_window_closed(): # Check for input libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS | libtcod.EVENT_MOUSE, key, mouse) for animator in Animator.animators: animator.update() if Animator.blocking > 0: if not game_state == GameStates.BLOCKING_ANIMATION: previous_game_state = game_state game_state = GameStates.BLOCKING_ANIMATION if Animator.blocking == 0 and game_state == GameStates.BLOCKING_ANIMATION: game_state = previous_game_state # Recompute FOV if fov_recompute: recompute_fov(fov_map, player.x, player.y, constants['fov_radius'], constants['fov_light_walls'], constants['fov_algorithm']) recompute_fov(fov_map_no_walls, player.x, player.y, constants['fov_radius'], light_walls=False, algorithm=constants['fov_algorithm']) # Show object descriptions if description_recompute == True: for entity in entities: if (prev_mouse_x != mouse.cx) or (prev_mouse_y != mouse.cy): description_list = [] description_index = 0 if entity.x == mouse.cx and entity.y == mouse.cy: description_list.append(entity) prev_mouse_x = mouse.cx prev_mouse_y = mouse.cy if len(description_list) > 0: description_recompute = False # We need to check to see if the mouse position changed and then clear our description list if it did, otherwise it will keep growing if (prev_mouse_x != mouse.cx) or (prev_mouse_y != mouse.cy): description_list = [] description_index = 0 description_recompute = True if mouse.lbutton_pressed: description_index += 1 if description_index > (len(description_list) - 1): description_index = 0 # Draw our scene render_all(con, panel, mouse, 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, description_list, description_index, cursor_radius, target_fov_map, fov_map_no_walls) fov_recompute = False libtcod.console_flush() # Clear our 'drawing consoles' so we dont leave a trail on the main console screen clear_all(con, entities) # Store input results action = handle_keys(key, game_state) mouse_action = handle_mouse(mouse) key = libtcod.Key() mouse = libtcod.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') take_stairs = action.get('take_stairs') level_up = action.get('level_up') show_character_screen = action.get('show_character_screen') ability_1 = action.get('ability_1') exit = action.get('exit') fullscreen = action.get('fullscreen') left_click = mouse_action.get('left_click') right_click = mouse_action.get('right_click') #Instatiate our message queue for the players turn player_turn_results = [] # Player Actions # Move if move and game_state == GameStates.PLAYERS_TURN: if not move == 'wait': dx, dy = move destination_x = player.x + dx destination_y = player.y + dy # Check to see if the location we want to move to is blocked by a wall or inhabited by a creature if not game_map.is_blocked(destination_x, destination_y): target = get_blocking_entities_at_location(entities, destination_x, destination_y) # If blocked by a creature, attack if target: attack_results = player.fighter.attack(target) player_turn_results.extend(attack_results) # Otherwise, move. else: player.move(dx, dy) fov_recompute = True game_state = GameStates.ENEMY_TURN else: 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)) #Ability complete checks: for ability in player.abilities: if player.animator.caller == ability and player.animator.complete: player_turn_results.extend(ability.use(complete=True)) player.animator.caller = None player.animator.complete = None if ability_1: player_turn_results.extend(player.abilities[0].use()) if show_inventory: if game_state != GameStates.SHOW_INVENTORY: previous_game_state = game_state player.inventory.sort_items() game_state = GameStates.SHOW_INVENTORY if drop_inventory: if game_state != GameStates.DROP_INVENTORY: previous_game_state = game_state game_state = GameStates.DROP_INVENTORY #Use or drop item in 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) target_fov_map = initialize_fov(game_map) fov_map_no_walls = 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.con += 1 message_log.add_message(Message('Your Constitution has increased by 1!', libtcod.yellow)) elif level_up == 'str': player.fighter.base_power += 1 message_log.add_message(Message('Your Strength has increased by 1!', libtcod.yellow)) elif level_up == 'def': player.fighter.base_defense += 1 message_log.add_message(Message('Your Defense has increased by 1!', libtcod.yellow)) hp_increase = randint(player.fighter.hitdie[0], player.fighter.hitdie[1]) + int((player.fighter.con - 10) / 2) player.fighter.base_max_hp += hp_increase player.fighter.hp += hp_increase message_log.add_message(Message('Your HP has increased by {0}'.format(hp_increase) + '!', libtcod.yellow)) game_state = previous_game_state if show_character_screen: if not game_state == GameStates.CHARACTER_SCREEN: previous_game_state = game_state game_state = GameStates.CHARACTER_SCREEN if game_state == GameStates.TARGETING: if hasattr(targeting_item, 'item'): cursor_radius = targeting_item.item.function_kwargs.get('radius') else: cursor_radius = targeting_item.function_kwargs.get('radius') if left_click: target_x, target_y = left_click if hasattr(targeting_item, 'item'): item_use_results = player.inventory.use(targeting_item, entities=entities, fov_map=fov_map, target_fov_map=target_fov_map,target_x=target_x, target_y=target_y) else: item_use_results = targeting_item.use(entities=entities, fov_map=fov_map, target_fov_map=target_fov_map,target_x=target_x, target_y=target_y) player_turn_results.extend(item_use_results) cursor_radius = 1 elif right_click: player_turn_results.append({'targeting_cancelled': True}) cursor_radius = 1 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}) cursor_radius = 1 else: save_game(player, entities, game_map, message_log, game_state) return True if fullscreen: libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen()) # Check player message queue and react accordingly 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') ability_used = player_turn_result.get("ability_used") xp = player_turn_result.get('xp') if message: message_log.add_message(message) if targeting_cancelled: game_state = previous_game_state message_log.add_message(Message('Targeting cancelled')) if 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 if hasattr(targeting_item, 'item'): message_log.add_message(targeting_item.item.targeting_message) else: message_log.add_message(targeting_item.targeting_message) if ability_used: if Animator.blocking == 0: 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 removed the {0}'.format(dequipped.name))) game_state = GameStates.ENEMY_TURN 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)) if (player.level.current_level % 2) == 0: previous_game_state = game_state game_state = GameStates.LEVEL_UP else: hp_increase = randint(player.fighter.hitdie[0], player.fighter.hitdie[1]) + int((player.fighter.con - 10) / 2) player.fighter.base_max_hp += hp_increase player.fighter.hp += hp_increase message_log.add_message(Message('Your HP has increased by {0}'.format(hp_increase) + '!', libtcod.yellow)) # 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) # Capture enemy turn message queue, analyze and react accordingly. 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, constants, con, panel): fov_recompute = True fov_map = initialize_fov(game_map) key = libtcod.Key() mouse = libtcod.Mouse() previous_game_state = game_state targeting_item = None while not libtcod.console_is_window_closed(): libtcod.sys_check_for_event( libtcod.EVENT_KEY_PRESS | libtcod.EVENT_MOUSE, key, mouse) if fov_recompute: recompute_fov(fov_map, player.x, player.y, constants["fov_radius"], constants["fov_light_walls"], constants["fov_algorithm"]) render_all(con, panel, entities, player, game_map, fov_map, fov_recompute, message_log, constants["screen_width"], constants["screen_height"], constants["bar_width"], constants["panel_height"], constants["panel_y"], mouse, constants["colours"], 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") inventory_index = action.get("inventory_index") drop_inventory = action.get("drop_inventory") 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.ENEMIES_TURN elif wait: game_state = GameStates.ENEMIES_TURN elif pickup and game_state == GameStates.PLAYERS_TURN: for entity in entities: if entity.item and entity.x == player.x and entity.y == player.y: pickup_results = player.inventory.add_item(entity) player_turn_results.extend(pickup_results) break else: message_log.add_message( Message("There is nothing to pick up here!", libtcod.yellow)) if show_inventory: previous_game_state = game_state game_state = GameStates.SHOWING_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.SHOWING_INVENTORY: player_turn_results.extend( player.inventory.use(item, entities=entities, fov_map=fov_map)) elif game_state == GameStates.DROPPING_INVENTORY: player_turn_results.extend(player.inventory.drop_item(item)) if drop_inventory: previous_game_state = game_state game_state = GameStates.DROPPING_INVENTORY 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.SHOWING_INVENTORY, GameStates.DROPPING_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("equipped") 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.ENEMIES_TURN if item_dropped: entities.append(item_dropped) game_state = GameStates.ENEMIES_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.ENEMIES_TURN if targeting: previous_game_state = GameStates.PLAYERS_TURN game_state = GameStates.TARGETING targeting_item = targeting message_log.add_message(targeting_item.item.targeting_message) if targeting_cancelled: game_state = previous_game_state message_log.add_message(Message("Targeting cancelled.")) if item_consumed: game_state = GameStates.ENEMIES_TURN 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( "You leveled up to level {0}".format( player.level.current_level) + "!", libtcod.yellow)) previous_game_state = game_state game_state = GameStates.LEVELED_UP if game_state == GameStates.ENEMIES_TURN: for entity in entities: if entity.ai: enemy_turn_results = entity.ai.take_turn( player, fov_map, game_map, entities) if enemy_turn_results: 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(): #define main variables constants = get_constants() # Limit FPS to 100 so we dont kill CPUs libtcod.sys_set_fps(60) # Load font and create root console (what you see) libtcod.console_set_custom_font(os.path.join(definitions.ROOT_DIR,'Nice_curses_12x12.png'), libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_ASCII_INROW) libtcod.console_init_root(constants['screen_width'], constants['screen_height'], constants['window_title'], False) # Create game area and info area, this will be drawn to our root console so that we can see them con = libtcod.console_new(constants['screen_width'], constants['screen_height']) panel = libtcod.console_new(constants['screen_width'], constants['panel_height']) player = get_dummy_player(Warrior()) entities = [] game_map = None message_log: MessageLog = None game_state = None show_main_menu = True show_game = False show_load_error_message = False main_menu_background_image = libtcod.image_load(os.path.join(definitions.ROOT_DIR,'data','menu_background.png')) # Capture keyboard and mouse input key = libtcod.Key() mouse = libtcod.Mouse() mixer.init() mixer.music.load(os.path.join(definitions.ROOT_DIR, 'data', 'music', 'title.mp3')) #mixer.music.play(loops=-1) #Our main loop while not libtcod.console_is_window_closed(): # Check for input 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: show_main_menu = False show_game = True elif load_saved_game: try: player, entities, game_map, message_log, game_state = load_game() show_main_menu = False except FileNotFoundError: show_load_error_message = True elif exit_game: break elif show_game == True: action = handle_role_select(key) warrior = action.get('warrior') ranger = action.get('ranger') rogue = action.get('rogue') paladin = action.get('paladin') warlock = action.get('warlock') back = action.get('exit') accept = action.get('accept') libtcod.console_clear(0) role_menu(con,constants['screen_width'],constants['screen_height'], player.role) libtcod.console_flush() if warrior: player = get_dummy_player(Warrior()) role_menu(con,constants['screen_width'],constants['screen_height'], player.role) libtcod.console_flush() if ranger: player = get_dummy_player(Ranger()) role_menu(con,constants['screen_width'],constants['screen_height'], player.role) libtcod.console_flush() if rogue: player = get_dummy_player(Rogue()) role_menu(con,constants['screen_width'],constants['screen_height'], player.role) libtcod.console_flush() if paladin: player = get_dummy_player(Paladin()) role_menu(con,constants['screen_width'],constants['screen_height'], player.role) libtcod.console_flush() if warlock: player = get_dummy_player(Warlock()) role_menu(con,constants['screen_width'],constants['screen_height'], player.role) libtcod.console_flush() if accept: player, entities, game_map, message_log, game_state = get_game_variables(constants, player) show_game = False if back: show_main_menu = True else: libtcod.console_clear(con) game_state = GameStates.PLAYERS_TURN play_game(player, entities, game_map, message_log, game_state, con, panel, constants) show_main_menu = True
def main(): constants = get_constants() # Fonts tcod.console_set_custom_font( 'arial10x10.png', tcod.FONT_TYPE_GREYSCALE | tcod.FONT_LAYOUT_TCOD) # Initialise console tcod.console_init_root(constants['screen_width'], constants['screen_height'], constants['window_title'], False, renderer=tcod.RENDERER_SDL2, vsync=True) # Variables for console and display panel con = tcod.console.Console(constants['screen_width'], constants['screen_height']) panel = tcod.console.Console(constants['screen_width'], constants['screen_height']) # Initalise variables 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 = tcod.image_load('menu_background.png') key = tcod.Key() mouse = tcod.Mouse() while not tcod.console_is_window_closed(): tcod.sys_check_for_event(tcod.EVENT_KEY_PRESS | tcod.EVENT_MOUSE, key, mouse) if show_main_menu: main_menu(con, main_menu_background_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') if show_load_error_message and (new_game or load_saved_game or exit_game): show_load_error_message = False elif new_game: player, entities, game_map, message_log, game_state = get_game_variables( constants) game_state = GameStates.PLAYERS_TURN show_main_menu = False elif load_saved_game: try: player, entities, game_map, message_log, game_state = load_game( ) show_main_menu = False except FileNotFoundError: show_load_error_message = True elif exit_game: break else: con.clear() play_game(player, entities, game_map, message_log, game_state, con, panel, constants) show_main_menu = True
def main(): screen_width = 80 # /4 = 20 screen_height = 50 # /4 ~= 12 # Map panel parameters map_width = 45 map_height = 40 fov_algorithm = libtcod.FOV_SHADOW fov_light_walls = True fov_radius = 9 # Health/Stats panel parameters bar_x = 4 bar_width = 24 panel_height = screen_height - map_height - 1 panel_y = screen_height - panel_height # Message panel parameters message_x = bar_width + bar_x + 2 message_width = screen_width - bar_width - bar_x - 2 message_height = panel_height - 2 message_log = MessageLog(message_x, message_width, message_height) # set up player entity and active entity list # TODO: Allow player to assign stats when starting to play fighter_component = Fighter( hp=30, defense=5, spdefense=5, attack=5, spattack=5, speed=5) player = Entity(int(screen_width / 2), int(screen_height / 2), '@', libtcod.white, 'Player', render_order=RenderOrder.ACTOR, blocks=True, fighter=fighter_component) entities = [] # set up console 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, libtcod.RENDERER_SDL2, vsync=True) # set up all panels con = libtcod.console.Console(screen_width, screen_height) panel = libtcod.console.Console(screen_width, panel_height) # load map, entities and player game_world = GameWorld(map_width, map_height) game_world.loadfirstfloor(player, entities) # player field of vision variables fov_recompute = True fov_map = initialize_fov(game_world.currmap) # input variables key = libtcod.Key() mouse = libtcod.Mouse() # state variables game_state = GameState.PLAYERS_TURN while not libtcod.console_is_window_closed(): # poll input libtcod.sys_check_for_event( libtcod.EVENT_KEY_PRESS | libtcod.EVENT_MOUSE, key, mouse) # compute field of vision if fov_recompute: if game_world.currmap.islair: current_fov_radius = 20 else: current_fov_radius = fov_radius recompute_fov(fov_map, player.x, player.y, current_fov_radius, fov_light_walls, fov_algorithm) # draw screen render_all(con, panel, entities, player, game_world.currmap, message_log, fov_map, fov_recompute, screen_width, screen_height, bar_x, bar_width, panel_height, panel_y, mouse) fov_recompute = False libtcod.console_flush() # erase previous player position clear_all(con, entities) # parse input action = handle_keys(key) move = action.get('move') exit = action.get('exit') fullscreen = action.get('fullscreen') confirm = action.get('confirm') cancel = action.get('cancel') wait = action.get('wait') player_turn_results = [] # update if move and game_state == GameState.PLAYERS_TURN: dx, dy = move # saves dx and dy outside of the while loop too dest_x = player.x + dx dest_y = player.y + dy if not game_world.currmap.tileblocked(dest_x, dest_y): target = get_blocking_entities_at_location( entities, dest_x, dest_y) if target: if target.door: game_world.movetonextroom(player, entities, target.door.direction) fov_map = initialize_fov(game_world.currmap) fov_recompute = True con.clear(fg=(0, 0, 0)) elif target.stairs: game_world.movetonextfloor(player, entities) fov_map = initialize_fov(game_world.currmap) fov_recompute = True con.clear(fg=(0, 0, 0)) elif target.fighter: attack_results = player.fighter.attacktarget( target, player.fighter.attacks[0]) player_turn_results.extend(attack_results) else: player.move(dx, dy) fov_recompute = True if (game_state == GameState.PLAYERS_TURN): game_state = GameState.ENEMY_TURN if wait and game_state == GameState.PLAYERS_TURN: game_state = GameState.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: message_log.add_message(message) if dead_entity: if dead_entity == player: message, game_state = kill_player(dead_entity) else: if (dead_entity.char == '@'): # dead boss, spawn stairs, update world game_world.bosses_cleared[game_world.current_floor] = True entities.extend( game_world.currmap.spawnstairsdown()) message = kill_monster(dead_entity) message_log.add_message(message) if game_state == GameState.ENEMY_TURN: for entity in entities: if entity.ai: enemy_turn_results = entity.ai.take_turn( game_world.currmap, 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: if (dead_entity.char == '@'): # dead boss, spawn stairs, update world game_world.bosses_cleared[game_world.current_floor] = True entities.extend( game_world.currmap.spawnstairsdown()) message = kill_monster(dead_entity) message_log.add_message(message) if game_state == GameState.PLAYER_DEAD: break if game_state == GameState.PLAYER_DEAD: break else: game_state = GameState.PLAYERS_TURN
def main(): #------------------------------------------------# # INIT #------------------------------------------------# rog.Rogue.create_settings() # later controllers may depend on settings rog.Rogue.create_window() rog.Rogue.create_consoles() rog.Rogue.create_world() rog.Rogue.create_controller() rog.Rogue.create_data() rog.Rogue.create_map(ROOMW, ROOMH) ## rog.Rogue.create_fov_maps() rog.Rogue.create_clock() rog.Rogue.create_updater() rog.Rogue.create_view() rog.Rogue.create_log() rog.Rogue.create_savedGame() # TODO: learn/use Pickle. rog.Rogue.create_processors() rog.Rogue.create_perturn_managers() rog.Rogue.create_const_managers() rog.Rogue.create_const_entities() rog.init_keyBindings() #map generation rog.getmap(rog.dlvl()).init_specialGrids( ) # inits fov_map; do this before you init terrain rog.getmap(rog.dlvl()).init_terrain(WALL) # clear the map to all walls rog.getmap(rog.dlvl()).generate_dlvl(rog.dlvl()) # init player # TESTING THIS IS ALL TEMPORARY!!! # temporary: find a position to place the player xpos = 15 ypos = 15 _borders = 10 while rog.getmap(rog.dlvl()).tileat(xpos, ypos) == WALL: xpos += 1 if xpos >= ROOMW - _borders: xpos = _borders ypos += 1 if ypos >= ROOMH: print("~~ ! FATAL ERROR ! Failed to place player in the map!") break rog.Rogue.create_player(xpos, ypos) # create player pc = rog.pc() # TESTING # HELP THE PLAYER TO SEE rog.create_envlight(16) ## rog.make(rog.pc(), NVISION) # w = rog.create_monster('W', xpos, ypos - 1) ## rog.world().add_component(w, cmp.AttractedToMen()) ## rog.world().add_component(w, cmp.AttractedToWomen()) ## ## #testing speech ## rog.init_person(w) # ## w2=rog.create_monster('W',xpos,ypos+1) ## w3=rog.create_monster('W',xpos,ypos+2) ## rog.setskill(pc, SKL_BOXING, 100) ## rog.setskill(pc, SKL_PERSUASION, 0) ## rog.setskill(pc, SKL_ARMOR, 100) ## rog.sets(pc, 'dex', 12*MULT_STATS) ## rog.sets(pc, 'int', 4*MULT_STATS) ## rog.setskill(pc, SKL_UNARMORED, 40) ## for x in range(20): ## rog.create_monster("L", 1+x*5,1) ## rog.alts(pc, 'sight', 50) weap = rog.create_weapon("sword", xpos, ypos, mat=MAT_METAL) weap = rog.create_weapon("buckler", xpos, ypos, mat=MAT_METAL) ## ## rog.wound(pc, WOUND_BURN, 2) ## rog.damage(weap, 200) ## rog.fitgear(weap, pc) ## print(rog.equip( ## pc,weap,EQ_MAINHANDW ## )) ## rog.create_weapon("wooden club", xpos,ypos) ## rog.create_weapon("estoc", xpos-1,ypos) ## shield=rog.create_weapon("metal shield", 0,0) ## rog.equip( ## pc,shield,EQ_OFFHAND ## ) ## rog.fitgear(shield, pc) ## armor=rog.create_armor("metal gear", 0,0) ## rog.equip( ## pc,armor,EQ_FRONT ## ) ## rog.fitgear(armor, pc) ## helm=rog.create_headwear("metal helm", 0,0) ## rog.equip( ## pc,helm,EQ_MAINHEAD ## ) ## rog.fitgear(helm, pc) ## leg1=rog.create_legwear("metal mail legging", 0,0) ## rog.equip( ## pc,leg1,EQ_MAINLEG ## ) ## rog.fitgear(leg1, pc) ## leg2=rog.create_legwear("metal mail legging", 0,0) ## rog.equip( ## pc,leg2,EQ_OFFLEG ## ) ## rog.fitgear(leg2, pc) ## arm1=rog.create_armwear("metal vambrace", 0,0) ## rog.equip( ## pc,arm1,EQ_MAINARM ## ) ## rog.fitgear(arm1, pc) ## arm2=rog.create_armwear("metal vambrace", 0,0) ## rog.equip( ## pc,arm2,EQ_OFFARM ## ) ## rog.fitgear(arm2, pc) ## foot1=rog.create_footwear("metal boot", 0,0) ## rog.equip( ## pc,foot1,EQ_MAINFOOT ## ) ## rog.fitgear(foot1, pc) ## foot2=rog.create_footwear("metal boot", 0,0) ## rog.equip( ## pc,foot2,EQ_OFFFOOT ## ) ## rog.fitgear(foot2, pc) # # create light so player can see ## log=rog.create_rawmat("log", 18,18) ## rog.burn(log,500) # # /TESTING /TEMPORARY # # TODO?: observer for player ## obs=observer.Observer_playerChange() ## pc.observer_add(obs) #-----------------------------------------------# # # MAIN GAME LOOP # # #-----------------------------------------------# rog.game_set_state("normal") while rog.game_is_running(): # manually close game # if libtcod.console_is_window_closed(): rog.end() # defeat conditions # if rog.on(rog.pc(), DEAD): rog.game_set_state("game over") # get input # pcInput = IO.get_raw_input() pcAct = IO.handle_mousekeys(pcInput).items() # commands that are available from anywhere # player.commands_const(rog.pc(), pcAct) # Finally record game state after any/all changes # gameState = rog.game_state() #----------# # PLAY # #----------# # # normal game play # if gameState == "normal": game.play(pc, pcAct) # # manager game states # # elif gameState == "manager": manager = rog.get_active_manager() manager.run(pcAct) if manager.result: rog.close_active_manager() # elif gameState == "game over": rog.msg("You died...")
def start(debug=False): SETTINGS = SettingsObject("res/settings.json") # Start setting up the screens/panels # Sets the window dimensions window_width = SETTINGS.getSetting("WindowWidth") window_height = SETTINGS.getSetting("WindowHeight") screen_names = SETTINGS.getSetting("Screens") # Initiate the Display display = DisplayObject() # Puts all screen settings into a dictionary for screen_name in screen_names: screen_data = SETTINGS.getSetting(screen_name) display.panelInfo[screen_name] = screen_data # Create the displays: map_console = libtcod.console_new( display.panelInfo["MapScreen"]["ScreenWidth"], display.panelInfo["MapScreen"]["ScreenHeight"]) hud_console = libtcod.console_new( display.panelInfo["HUDScreen"]["ScreenWidth"], display.panelInfo["HUDScreen"]["ScreenHeight"]) quest_console = libtcod.console_new( display.panelInfo["QuestScreen"]["ScreenWidth"], display.panelInfo["QuestScreen"]["ScreenHeight"]) toast_console = libtcod.console_new( display.panelInfo["ToastScreen"]["ScreenWidth"], display.panelInfo["ToastScreen"]["ScreenHeight"]) # Set the font libtcod.console_set_custom_font( SETTINGS.getResource("FontFile"), libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD) # Initialize display libtcod.console_init_root(window_width, window_height, 'Roguelike Me', False) # Create the game object: Game = GameObject(SETTINGS, debug) # # MAIN LOOP # key = libtcod.Key() mouse = libtcod.Mouse() while not libtcod.console_is_window_closed(): # Turn key press into an action, otherwise return None action = processKeyPress(key) # Process action if not None if action != None: if action[0] == 'fullscreen': libtcod.console_set_fullscreen( not libtcod.console_is_fullscreen()) elif action[0] == 'quit' and action[1] == True: return True else: # If the action is meant for the game itself instead of the program, # send it forward to the game to update later Game.addActionToBacklog(action) # Process backlog after keypress if the action did something. Game.processBacklog() # Get all draw updates from the Game toDraw = Game.getToDraw(map_console, hud_console, quest_console, toast_console) # Forward all draw updates to the display display.setDrawOrders(toDraw) # Print to Panels display.drawToPanels(True) display.renderPanels(map_console, hud_console, quest_console, toast_console) # Wait for Keypress libtcod.sys_wait_for_event(libtcod.EVENT_KEY_PRESS, key, mouse, True)
def run(self): key = libtcod.Key() mouse = libtcod.Mouse() while not libtcod.console_is_window_closed(): # Loop to process game events event = self.model.get_next_event() while event is not None: if event.type == model.Event.STATE and event.name == model.Event.STATE_GAME_OVER: self.set_mode(Controller.GAME_MODE_GAME_OVER) elif event.name == model.Event.GAME_ENTER_SHOP: self.set_mode(Controller.GAME_MODE_SHOP) self.view.process_event(event) self.model.process_event(event) event = self.model.get_next_event() # Draw the view self.view.draw() # Wait for event......... libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS, key, mouse) # key = libtcod.console_wait_for_keypress(True) action = self.handle_keys(key) if action is None: print(f'key={key}, mode={self.mode}') # print(f'Game Mode={self.mode}; last mode={self.last_mode}') # Common actions exit = action.get('exit') fullscreen = action.get('fullscreen') move = action.get('move') use = action.get('use') debug = action.get('debug') zoom = action.get('zoom') help = action.get('help') save = action.get('save') load = action.get('load') examine = action.get('examine') if debug is True: self.model.print() self.view.print() if help is True: self.help() elif zoom is not None: if self.view.font_zoom(zoom) is True: self.view.initialise(self.model) # If we are in PLAYING mode elif self.mode == Controller.GAME_MODE_PLAYING: player_turn = True # Game playing actions attack = action.get('attack') wait = action.get('wait') cast = action.get('cast') stairs = action.get('take stairs') pickup = action.get('pickup') inventory = action.get('show_inventory') character = action.get('show_character') shop = action.get('enter_shop') journal = action.get('show_journal') spellbook = action.get('show_spellbook') pause = action.get('pause') if move: dx, dy = move self.model.move_player(dx, dy) player_turn = False elif attack: if self.model.attack() is True: player_turn = False elif cast: if self.model.cast_spell(slot=cast) is True: player_turn = False elif stairs: self.model.take_stairs() player_turn = False elif pickup: self.model.take_item() player_turn = False elif use: self.model.use_item() player_turn = False elif examine: self.model.check_item() elif inventory: self.set_mode(Controller.GAME_MODE_INVENTORY) elif shop: self.set_mode(Controller.GAME_MODE_SHOP) elif journal: self.set_mode(Controller.GAME_MODE_JOURNAL) elif spellbook: self.set_mode(Controller.GAME_MODE_SPELLBOOK) elif character: self.set_mode(Controller.GAME_MODE_CHARACTER) elif pause: self.set_mode(Controller.GAME_MODE_PAUSED) elif wait: player_turn = False elif debug: self.model.debug() if player_turn is False: # Tick the model libtcod.sys_sleep_milli(100) self.model.tick() # If we are in START mode elif self.mode == Controller.GAME_MODE_START: new_game = action.get('new_game') new_character = action.get("new_character") if new_game: self.set_mode(Controller.GAME_MODE_PLAYING) elif new_character: self.set_mode(Controller.GAME_MODE_CHARACTER_CREATION) elif load: self.game_load() elif exit: return True # If we are in CHARACTER mode elif self.mode == Controller.GAME_MODE_CHARACTER: level_up = action.get('level-up') ability_upgrade = action.get('ability_upgrade') if move: dx, dy = move self.view.character_view.change_selection(dy) elif level_up: self.model.level_up() elif ability_upgrade: stat_name = self.view.character_view.get_selected_stat() self.model.ability_upgrade(stat_name) # If we are in CHARACTER CREATION mode elif self.mode == Controller.GAME_MODE_CHARACTER_CREATION: edit_name = action.get('edit_name') edit_class = action.get('edit_class') edit_race = action.get('edit_race') select = action.get("select") randomize = action.get("randomize") if edit_name: self.view.character_creation_view.mode = view.CreateCharacterView.MODE_NAME_PICK elif edit_class: self.view.character_creation_view.mode = view.CreateCharacterView.MODE_CLASS_PICK elif edit_race: self.view.character_creation_view.mode = view.CreateCharacterView.MODE_RACE_PICK elif randomize: name = model.ThemeManager.get_random_history("Name") class_name = random.choice( model.CombatClassFactory.get_playable_classes()) race_name = random.choice( model.RaceFactory.get_available_races()) self.model.add_player( self.model.generate_player(name, class_name, race_name)) self.view.character_creation_view.initialise(self.model) elif move: dx, dy = move self.view.character_creation_view.change_selection(dy) elif select: # if self.view.character_creation_view.mode == view.CreateCharacterView.MODE_CLASS_PICK: name = self.view.character_creation_view.character_name class_name = self.view.character_creation_view.get_selected_class( ) race_name = self.view.character_creation_view.get_selected_race( ) self.model.add_player( self.model.generate_player(name, class_name, race_name)) self.view.character_creation_view.initialise(self.model) self.view.character_creation_view.mode = view.CreateCharacterView.MODE_DISPLAY_CHARACTER # else: # self.view.character_creation_view.mode = view.CreateCharacterView.MODE_DISPLAY_CHARACTER # If we are in INVENTORY mode elif self.mode == Controller.GAME_MODE_INVENTORY: equip = action.get('equip') drop = action.get('drop') e = self.view.inventory_view.get_selected_item() if move: dx, dy = move self.view.inventory_view.change_selection(dy) else: if e is not None: if equip: self.model.equip_item(e) elif drop: self.model.drop_item(e) elif use: self.model.use_item(e) elif examine: self.model.check_item(e) else: pass # self.events.add_event(model.Event(type=model.Event.GAME, # name=model.Event.ACTION_FAILED, # description=f"No item selected!")) # If we are in spell book mode elif self.mode == Controller.GAME_MODE_SPELLBOOK: learn = action.get('learn') memorise = action.get('memorise') toggle = action.get('toggle') confirm = action.get('confirm') if move: dx, dy = move self.view.spellbook_view.change_selection(dy) self.view.spellbook_view.change_level_filter(dx) elif exit: # If the spell book is not locked and you are trying to exit then go into confirm mode if self.model.player.fighter.spell_book.is_locked is False: self.view.spellbook_view.confirm() exit = False elif confirm: v = self.view.spellbook_view # If we are checking user wants to exit if v.mode == view.SpellBookView.MODE_CONFIRM_SPELLS: # If they confirmed that they wanted to save then lock spell book and exit if v.save is True: exit = True self.model.player.fighter.spell_book.is_locked = True v.mode = view.SpellBookView.MODE_CATALOGUE else: if toggle: self.view.spellbook_view.toggle_mode() else: e = self.view.spellbook_view.get_selected_item() if e is not None: if memorise: self.model.memorise_spell(e) elif learn: self.model.learn_spell(e) # If we are in SHOP mode elif self.mode == Controller.GAME_MODE_SHOP: buy = action.get("buy") sell = action.get("sell") confirm = action.get("confirm") if move: dx, dy = move self.view.shop_view.change_selection(dy=dy, dx=dx) elif buy: self.view.shop_view.mode = view.ShopView.MODE_BUY elif sell: self.view.shop_view.mode = view.ShopView.MODE_SELL elif confirm: if self.view.shop_view.mode == view.ShopView.MODE_BUY: new_item = self.view.shop_view.get_selected_buy_item() success = self.model.buy_item(new_item) print( f'Buying {self.view.shop_view.get_selected_buy_item().description}: success={success}' ) elif self.view.shop_view.mode == view.ShopView.MODE_SELL: old_item = self.view.shop_view.get_selected_sell_item() success = self.model.sell_item(old_item) print( f'Selling {self.view.shop_view.get_selected_sell_item().description}: success={success}' ) # If we are in CHARACTER mode elif self.mode == Controller.GAME_MODE_JOURNAL: if move: dx, dy = move self.view.journal_view.change_selection(dy) # If we are in GAME PAUSED mode elif self.mode == Controller.GAME_MODE_PAUSED: play = action.get("play") if exit: self.set_mode(Controller.GAME_MODE_GAME_OVER) continue elif play: self.set_mode(Controller.GAME_MODE_PLAYING) elif save: self.game_save() elif load: self.game_load() # If we are in GAME OVER mode elif self.mode == Controller.GAME_MODE_GAME_OVER: start = action.get("start") if start: self.initialise() self.set_mode(Controller.GAME_MODE_START) if exit: self.set_mode(self.last_mode) if fullscreen: libtcod.console_set_fullscreen( not libtcod.console_is_fullscreen())
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 exit = handle_keys() if exit: break
def main(): #init a new game: console window, ui settings, fov settings, rooms, enemies, items constants = get_constants() #set the art to be used libtcod.console_set_custom_font( 'arial10x10.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD) #title displayed on console libtcod.console_init_root(constants['screen_width'], constants['screen_height'], constants['window_title'], False) #create a new console variables con = libtcod.console_new(constants['screen_width'], constants['screen_height']) panel = libtcod.console_new(constants['screen_width'], constants['panel_height']) # set up our game variables 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_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']) libtcod.console_flush() action = handle_main_menu(key) new_game = action.get('new_game') load_saved_game = action.get('load_game') exit_game = action.get('exit') if show_load_error_message and (new_game or load_saved_game or exit_game): show_load_error_message = False elif new_game: player, entities, game_map, message_log, game_state = get_game_variables( constants) game_state = GameStates.PLAYERS_TURN show_main_menu = False elif load_saved_game: try: player, entities, game_map, message_log, game_state = load_game( ) show_main_menu = False except FileNotFoundError: show_load_error_message = True elif exit_game: break else: libtcod.console_clear(con) play_game(player, entities, game_map, message_log, game_state, con, panel, constants) show_main_menu = True player, entities, game_map, message_log, game_state = get_game_variables( constants)
def main(): """Main function of roguelike game engine.""" constants = get_constants() libtcod.console_set_custom_font('arial10x10.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD) libtcod.console_init_root( constants['screen_width'], constants['screen_height'], constants['window_title'], False) con = libtcod.console_new( constants['screen_width'], constants['screen_height']) panel = libtcod.console_new( constants['screen_width'], constants['panel_height']) player = None entities = [] game_map = None message_log = None game_state = None show_main_menu = True show_load_error_message = False main_menu_background_image = libtcod.image_load('menu_background1.png') key = libtcod.Key() mouse = libtcod.Mouse() while not libtcod.console_is_window_closed(): libtcod.sys_check_for_event( libtcod.EVENT_KEY_PRESS | libtcod.EVENT_MOUSE, key, mouse) if show_main_menu: main_menu(con, main_menu_background_image, constants['screen_width'], constants['screen_height']) if show_load_error_message: message_box( con, 'No save game to load', 50, constants['screen_width'], constants['screen_height']) libtcod.console_flush() action = handle_main_menu(key) new_game = action.get('new_game') load_saved_game = action.get('load_game') exit_game = action.get('exit') if show_load_error_message and (new_game or load_saved_game or exit_game): show_load_error_message = False elif new_game: player, entities, game_map, message_log, game_state = get_game_variables( constants) game_state = GameStates.PLAYERS_TURN show_main_menu = False elif load_saved_game: try: player, entities, game_map, message_log, game_state = load_game() # if player dead, resurrect with half max hitpoints if game_state == GameStates.PLAYER_DEAD: player.char = '@' player.color = libtcod.white player.fighter.heal(player.fighter.max_hp // 2) 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(): 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 = 50 fov_algorithm = 5 fov_light_walls = True fov_radius = 10 max_monster_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.black, 'Player', blocks=True, render_order=RenderOrder.ACTOR, fighter=fighter_component, inventory=inventory_component) entities = [player] tcod.console_set_custom_font( "BBT\\fonts\\arial12x12.png", tcod.FONT_TYPE_GRAYSCALE | tcod.FONT_LAYOUT_TCOD) tcod.console_init_root(screen_width, screen_height, 'Battle Theater', False) con = tcod.console_new(screen_width, screen_height) panel = tcod.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_monster_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 targeting_item = None # 游戏主循环 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, 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 tcod.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_clikc = 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.', 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_result.extend(item_use_results) elif right_click: player_turn_results.append({'targeting_cancelled': True}) if exit: if game_state in (GameStates.SHOW_INVENTORY, GameStates.DROP_INVENTORY): game_state = previous_game_state elif game_state == GameStates.TARGETING: player_turn_results.append({'targeting_cancelled': True}) else: return True if fullscreen: tcod.console_set_fullscreen(not tcod.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(): # pull variables constants = get_constants() # set graphics template (source, type, layout) # --- original setup --- # libtcod.console_set_custom_font( # 'arial10x10.png', # libtcod.FONT_TYPE_GRAYSCALE | libtcod.FONT_LAYOUT_TCOD) # --- new font file --- libtcod.console_set_custom_font( 'TiledFont.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD, 32, 10) # create screen (width, height, title, fullscreen_boolean) libtcod.console_init_root(constants['screen_width'], constants['screen_height'], constants['window_title'], False) # initialize console 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() libtcod.console_set_fullscreen(True) # mixer.music.play(-1) # music_loop = mixer.Sound('Cave_Loop.wav') # mixer.Sound.play(music_loop) mixer.init() music_loop = mixer.Sound('Cave_Loop.wav') mixer.Sound.play(music_loop, loops=-1) while not libtcod.console_is_window_closed(): libtcod.sys_check_for_event( libtcod.EVENT_KEY_PRESS | libtcod.EVENT_MOUSE, key, mouse) if show_main_menu: main_menu(con, main_menu_background_image, constants['screen_width'], constants['screen_height']) if show_load_error_message: message_box(con, 'No save game to load', 50, constants['screen_width'], constants['screen_height']) libtcod.console_flush() action = handle_main_menu(key) new_game = action.get('new_game') load_saved_game = action.get('load_game') exit_game = action.get('exit') if show_load_error_message and (new_game or load_saved_game or exit_game): show_load_error_message = False elif new_game: player, entities, game_map, message_log, game_state = get_game_variables( constants) game_state = GameStates.PLAYER_TURN show_main_menu = False elif load_saved_game: try: player, entities, game_map, message_log, game_state = load_game( ) show_main_menu = False except FileNotFoundError: show_load_error_message = True elif exit_game: break else: libtcod.console_clear(con) play_game(player, entities, game_map, message_log, game_state, con, panel, constants) show_main_menu = 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), } 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(): screen_width = 80 screen_height = 80 bar_width = 20 panel_height = 10 panel_y = screen_height - panel_height map_width = 80 map_height = 80 - panel_height message_x = bar_width + 2 message_width = screen_width - bar_width - 2 message_height = panel_height - 1 fov_algorithm = 0 fov_light_walls = True fov_radius = 10 fov_recompute = True colors = 0 entities = [] items = [] effects = [] libtcod.console_set_custom_font( 'arial10x10.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD) libtcod.console_init_root(screen_width, screen_height, 'Project Magic Circle', False) con = libtcod.console_new(screen_width, screen_height) panel = libtcod.console_new(screen_width, panel_height) seed = 1000 map = GameMap(map_width, map_height) map.create_map(seed) fov_map = initialize_fov(map) nav_map = initialize_fov(map) nav_map_recompute = False message_log = MessageLog(message_x, message_width, message_height) map.place_entities(entities, 5, 5) player = entities[0] key = libtcod.Key() mouse = libtcod.Mouse() game_state = GameStates.STANDART realtime = False action_buffer = None message = Message('To get help press "?"', libtcod.white) message_log.add_message(message) targeting_item = None danger_level = 1 while not libtcod.console_is_window_closed(): if nav_map_recompute: fov_map = initialize_fov(map) nav_map = initialize_fov(map) fov_recompute = True nav_map_recompute = False if fov_recompute: recompute_fov(fov_map, player.x, player.y, fov_radius, fov_light_walls, fov_algorithm) render_all(con, panel, entities, effects, map, fov_map, fov_radius, 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 + effects) for entity in entities: try: entity.give_energy(int(entity.speed / 5)) except: pass for entity in entities: if entity == player: if action_buffer == None: if realtime: libtcod.sys_check_for_event( libtcod.EVENT_KEY_PRESS or libtcod.EVENT_MOUSE, key, mouse) else: while True: libtcod.sys_check_for_event( libtcod.EVENT_KEY_PRESS or libtcod.EVENT_MOUSE, key, mouse) render_all(con, panel, entities, effects, map, fov_map, fov_radius, fov_recompute, message_log, screen_width, screen_height, bar_width, panel_height, panel_y, mouse, colors, game_state) libtcod.console_flush() clear_all(con, entities) if is_valid_input(key, mouse): break keys_action = handle_keys(key, game_state) mouse_action = handle_mouse(mouse, game_state) action_buffer = (keys_action, mouse_action) if game_state != GameStates.TARGETING: targeting_item = None turn_results = entity.ai.take_action(action_buffer[0], action_buffer[1], map, fov_map, entities, game_state, targeting_item) if turn_results: for turn_result in turn_results: message = turn_result.get('message') dead_entity = turn_result.get('dead') fov_recompute = turn_result.get('fov_recompute') energy = turn_result.get('not_enough_energy') exit = turn_result.get('exit') fullscreen = turn_result.get('fullscreen') effect = turn_result.get('create effect') item_added = turn_result.get('item_added') item_dropped = turn_result.get('item_dropped') show_inventory = turn_result.get('show_inventory') drop_inventory = turn_result.get('drop_inventory') targeting = turn_result.get('targeting') show_help = turn_result.get('show_help') toggle_realtime = turn_result.get('toggle_realtime') go_deeper = turn_result.get('go_deeper') if message: message = Message(message, libtcod.white) message_log.add_message(message) if dead_entity: if dead_entity == player: message, game_state = kill_player(dead_entity) message = Message(message, libtcod.red) else: message = kill_monster(dead_entity) message = Message(message, libtcod.white) message_log.add_message(message) if effect: superimpose_effect(effect, effects) if energy == None: action_buffer = None if fov_recompute == None: fov_recompute = False if item_added: entities.remove(item_added) 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 item_dropped: entities.append(item_dropped) if targeting: targeting_item = targeting message = Message( targeting_item.item_aspect.targeting_message, libtcod.yellow) message_log.add_message(message) previous_game_state = GameStates.STANDART game_state = GameStates.TARGETING if exit: if game_state in { GameStates.SHOW_INVENTORY, GameStates.DROP_INVENTORY, GameStates.TARGETING, GameStates.HELP }: if game_state == GameStates.TARGETING: message = Message('Exited targeting', libtcod.yellow) message_log.add_message(message) game_state = previous_game_state else: return True if fullscreen: libtcod.console_set_fullscreen( not libtcod.console_is_fullscreen()) if show_help: previous_game_state = game_state game_state = GameStates.HELP if toggle_realtime: message = Message('Gamemode changed', libtcod.green) message_log.add_message(message) realtime = not realtime if go_deeper: '''clear_all(con, entities) items = [] effects = [] map = GameMap(map_width, map_height) map.initialize_tiles() map.create_map(seed+danger_level) fov_map = initialize_fov(map) nav_map = initialize_fov(map) map.place_entities(entities, 5+danger_level, 5+danger_level, player = player) player = entities[0] danger_level += 2''' pass else: action_buffer = None else: if entity.ai: turn_results = entity.ai.take_action( nav_map, entities, game_state) if turn_results: for turn_result in turn_results: message = turn_result.get('message') dead_entity = turn_result.get('dead') exit = turn_result.get('exit') fullscreen = turn_result.get('fullscreen') effect = turn_result.get('create effect') if message: message = Message(message, libtcod.white) message_log.add_message(message) if dead_entity: if dead_entity == player: message, game_state = kill_player( dead_entity) message = Message(message, libtcod.red) else: message = kill_monster(dead_entity) message = Message(message, libtcod.white) message_log.add_message(message) if effect: superimpose_effect(effect, effects) if game_state == GameStates.PLAYER_DEAD: break