def play_game(): global key, mouse, game_state, game_msgs, map_state, inventory game_state = 'playing' map_state = 'map' game_msgs = [] inventory = [] equipment_component = Equipment(slot = 'right hand', add_equipment_skill = ('shot', 'reload')) gun = Object(1, 1, '/', 'gun', libtcod.sky, equipment = equipment_component) inventory.append(gun) # initialize the mouse and key with libtcod libaray mouse = libtcod.Mouse() key = libtcod.Key() while not libtcod.console_is_window_closed(): #check the input from the keyboard or mouse libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS|libtcod.EVENT_MOUSE, key, mouse) #display all (include the tile and objects) render_all() libtcod.console_flush() #handle the keyboard or mouse input player_action = handle_keys() if player_action == 'exit': break if game_state == 'playing' and player_action != 'didnt-take-turn': for object in objects: if object.ai: object.ai.take_turn()
def play_game(): #play the game! global camera_x, camera_y, key, mouse player_action = None #intializes controls mouse = libtcod.Mouse() key = libtcod.Key() (camera_x, camera_y) = (0,0) while not libtcod.console_is_window_closed(): #checks for mouse or keypress events libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS|libtcod.EVENT_MOUSE,key,mouse) #renders the console render_all() #update the console libtcod.console_flush() # check for a level up for player # check_level_up() #clears old position # for object in objects: # object.clear() #handle keys and exit game if needed player_action = handle_keys() if player_action == 'exit': #save_game() break
def play_game(): global key, mouse, player_action player_action = None mouse = libtcod.Mouse() key = libtcod.Key() while not libtcod.console_is_window_closed(): libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS | libtcod.EVENT_MOUSE, key, mouse) draw_all() libtcod.console_flush() clear_all() # handle keys and exit game if needed player_action = handle_keys() if player_action == 'exit': save_game() break if game_state == 'playing' and player_action != 'didnt-take-turn': for object in objects: if object.AI: object.AI.take_turn()
def playGame(): global key, mouse playerAction = 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) renderAll() libtcod.console_flush() checkLevelup() #Erase all objects at their old locations, before they move. for object in objects: object.clear() #Handle keys and exit the game if needed. playerAction = handleKeys() if playerAction == "exit": saveGame() break #Let monsters take their turn. if gameState == "playing" and playerAction != "no turn taken": for object in objects: if object.ai: object.ai.takeTurn()
def play_game(): global key, mouse player_action = None 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': for object in objects: if object.ai: if object.wait > 0: #don't take a turn if still waiting object.wait -= 1 else: object.ai.take_turn()
def play_game(): global key, mouse player_action = None mouse = libtcod.Mouse() key = libtcod.Key() while not libtcod.console_is_window_closed(): libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS|libtcod.EVENT_MOUSE,key,mouse) render_all() libtcod.console_flush() #check for player level up now, while objects are visible behind the menu check_level_up() #erase all objects after render in case they move before next flush for object in objects: object.clear() #handle player input and exit game if appropriate player_action = handle_keys() if player_action == 'exit': save_game() break #give monsters 1 turn for all player turns taken if game_state == 'playing' and player_action != 'didnt-take-turn': for object in objects: if object.ai: object.ai.take_turn()
def play_game(): global key, mouse player_action = None mouse = libtcod.Mouse() key = libtcod.Key() #main loop while not libtcod.console_is_window_closed(): #print to screen zero is the console to be printed to #libtcod.console_set_default_foreground(0, libtcod.crimson) #print character #libtcod.console_put_char(con,playerx,playery, '@', libtcod.BKGND_NONE) libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS|libtcod.EVENT_MOUSE,key, mouse) render_all() #flush console(present changes to console) libtcod.console_flush() #check for player level up check_level_up() 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 != 'turnNotTaken': for object in objects: if object.ai: object.ai.take_turn()
def play_game(): global key, mouse player_action = None mouse = libtcod.Mouse() key = libtcod.Key() while not libtcod.console_is_window_closed(): libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS|libtcod.EVENT_MOUSE, key, mouse) player_action = handle_keys() render_all() libtcod.console_flush() check_level_up() for object in objects: object.clear() if player_action == 'exit': save_game() break if game_state == 'playing': #and player_action != 'didnt-take-turn': for object in objects: if object.ai: if object.wait > 0: object.wait -= 1 else: object.ai.take_turn()
def play_game(): global key, mouse, gameobjects player_action = None mouse = libtcod.Mouse() key = libtcod.Key() # Play Game 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() check_level_up() for object in gameobjects: object.clear() # Handle key input and exit game if needed. player_action = handle_keys() if player_action == 'exit': save_game() break # Let the monsters take their turn. if game_state == 'playing' and player_action != 'didnt-take-turn': for object in gameobjects: if object.ai: object.ai.take_turn() if object.status_effect: object.status_effect.take_turn()
def handle_keys(): global player_x, player_y, m, turns #movement keys if libtcod.console_is_key_pressed(libtcod.KEY_UP): if libtcod.map_is_walkable(m, player_x, player_y - 1): player_y -= 1 turns += 1 elif libtcod.console_is_key_pressed(libtcod.KEY_DOWN): if libtcod.map_is_walkable(m, player_x, player_y + 1): player_y += 1 turns += 1 elif libtcod.console_is_key_pressed(libtcod.KEY_LEFT): if libtcod.map_is_walkable(m, player_x - 1, player_y): player_x -= 1 turns += 1 elif libtcod.console_is_key_pressed(libtcod.KEY_RIGHT): if libtcod.map_is_walkable(m, player_x + 1, player_y): player_x += 1 turns += 1 key = libtcod.Key() mouse = libtcod.Mouse() libtcod.sys_check_for_event(libtcod.EVENT_ANY, key, mouse) if key.vk == libtcod.KEY_ENTER and key.lalt: #Alt+Enter: toggle fullscreen libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen()) elif key.vk == libtcod.KEY_SPACE: end(player_x, player_y, mouse.cx, mouse.cy) elif key.vk == libtcod.KEY_ESCAPE: return True #exit game
def play_game(): global key, mouse player_action = None mouse = libtcod.Mouse() key = libtcod.Key() #main loop while not libtcod.console_is_window_closed(): libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS | libtcod.EVENT_MOUSE, key, mouse) #render the screen 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 target_tile(max_range=None): # TODO: replace this completely with the target_tile function below message("Use the mouse or the keyboard to select a tile...", tcod.blue) # return the position of a tile left-clicked in player's FOV (optionally # in a range), or (None,None) if right-clicked. while True: # render the screen. this erases the inventory and shows the names of # objects under the mouse. render.render_all() tcod.console_flush() tcod.sys_check_for_event(tcod.EVENT_MOUSE | tcod.EVENT_KEY_PRESS, key, mouse) (x, y) = (mouse.cx, mouse.cy) x += render.camera_width y += render.camera_height # accept the target if the player clicked in FOV, and in case a range # is specified, if it's in that range if ( mouse.lbutton_pressed and tcod.map_is_in_fov(terain.map.fov_map, x, y) and (max_range is None or player.distance(x, y) <= max_range) ): return (x, y) elif mouse.rbutton_pressed or key.vk == tcod.KEY_ESCAPE: message("Targetting cancelled") return (None, None) # cancel if the player right-clicked or pressed Escape
def play_game(): global key, mouse player_action= None #Input init mouse = libtcod.Mouse() key = libtcod.Key() while not libtcod.console_is_window_closed(): libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS|libtcod.EVENT_MOUSE, key, mouse) render_all() libtcod.console_flush() for object in objects: object.clear() #handle keys and/or exit player_action = handle_keys() if player_action == 'exit': save_game() break #ai if game_state == 'playing' and player_action != 'didnt-take-turn': for object in objects: if object.ai: object.ai.take_turn()
def draw_things(list_of_maps, map_number): """ This lets the player place things using the mouse by clicking on a tile and drawing over multiple tiles. Unforunately it looks like libtcod 1.5.1 has a bug where getting the (dcx, dcy) values from a mouse drag doesn't work. Or at least I cannot figure out how to get a good list of (dcx, dcy) coordinates from a mouse drag event. """ global key, mouse while True: # Rendering the screen first closes the menu and returns to the map, ready to place something libtcod.console_flush() libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS|libtcod.EVENT_MOUSE,key,mouse) render_all(list_of_maps) xlist = [] ylist = [] if mouse.rbutton_pressed or key.vk == libtcod.KEY_ESCAPE: return (None, None) #cancel if the player right-clicked or pressed Escape if mouse.lbutton: while not mouse.lbutton_pressed: (xlength, ylength) = (mouse.dcx, mouse.dcy) xlist.append(xlength) ylist.append(ylength) print 'xlist and ylist are: ' + str(xlist) + ' ' + str(ylist) # If the mouse button was pressed and dragged, return affected coordinates return (xlist, ylist)
def play_game(): global key, mouse, game_state player_action = None mouse = libtcod.Mouse() key = libtcod.Key() while not libtcod.console_is_window_closed(): libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS|libtcod.EVENT_MOUSE, key, mouse) render_all() libtcod.console_flush() player.player.check_level_up() for object in objects: object.clear() if game_state == 'playing-waiting-for-input' or game_state == 'dead': player_action = handle_keys() #print "player_action: %s" % player_action if player_action == 'exit': save_game() break elif player_action is None: game_state = 'playing' if game_state == 'playing': scheduler.tick()
def play_game(): global key, mouse player_action = None mouse = libtcod.Mouse() key = libtcod.Key() while not libtcod.console_is_window_closed(): libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS|libtcod.EVENT_MOUSE, key, mouse) render_all() libtcod.console_flush() check_level_up() # Erase objects at their old locations for object in objects: object.clear() # Handle keys and exit if needed player_action = handle_keys() # let monsters take their turn and update fighter effects if game_state == 'playing' and player_action != 'didnt-take-turn': for object in objects: if object.ai: object.ai.take_turn() if object.fighter: object.fighter.update_effects() if player_action == 'exit': save_game() break if game_state == 'victory': msgbox('Congratulations, you have beaten ' + GAMENAME+'!\n'+ 'I hope you enjoyed it!', 40) break
def main(): logging.basicConfig(filename='log.txt',level=logging.DEBUG,filemode='w') logging.debug('start of main') #initialize main console font_file = 'data/fonts/terminal10x10_gs_tc.png' libtcod.console_set_custom_font(font_file, libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD) libtcod.console_init_root(SCREEN_W,SCREEN_H,'GolemRL') libtcod.sys_set_fps(LIMIT_FPS) key = libtcod.Key() mouse = libtcod.Mouse() while not libtcod.console_is_window_closed(): libtcod.sys_check_for_event(libtcod.EVENT_ANY,key,mouse) choice = main_menu() if choice == 0: seed = random.randrange(10000) print 'Seed %i'%seed logging.info('Starting new game with seed %i' % seed) game = new_game()#seed) game.play() elif choice == 1: game = load_game() game.play() elif choice == 2: break logging.debug('end of main') return 0
def play_game(): global key, mouse mouse = libtcod.Mouse() key = libtcod.Key() while not libtcod.console_is_window_closed(): libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS | libtcod.EVENT_MOUSE, key, mouse) libtcod.console_set_default_foreground(0, libtcod.white) render_all() libtcod.console_flush() check_level_up() clear_objects() player_action = handle_keys() if player_action == 'exit': save_game() break if game_state == 'playing' and player_action != 'didnt-take-turn': for o in objects: if o.ai: o.ai.take_turn()
def target_tile(max_range=None): """ Return the coordinates of the tile clicked by the player Returns (None, None) if right-click is used instead. NOTE: Challenge yourself by creating a keyboard targeting interface. """ global key, mouse, player while True: # Render the screen, erase the inventory, show object names under the # mouse. libtcod.console_flush() libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS | libtcod.EVENT_MOUSE, key, mouse) render_all() x, y = (mouse.cx, mouse.cy) if ( mouse.lbutton_pressed and in_fov(x, y) and max_range is None or player.distance(x, y) <= max_range ): return (x, y) if mouse.rbutton_pressed or key.vk == libtcod.KEY_ESCAPE: return (None, None)
def play_game(): global player_action player_action = None global key, mouse key = libtcod.Key() mouse = libtcod.Mouse() # Main loop while not libtcod.console_is_window_closed(): libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS|libtcod.EVENT_MOUSE,key,mouse) render_all() # "flush" i.e. present changes to screen libtcod.console_flush() # clear character at last position for object in objects: object.clear() player_action = handle_keys() if player_action == 'exit': save_game() break if game_state == 'playing' and player_action != 'didnt-take-turn': for object in objects: if object.ai: object.ai.take_turn()
def play_game(): settings.player_action = None settings.mouse = libtcod.Mouse() settings.key = libtcod.Key() while not libtcod.console_is_window_closed(): libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS | libtcod.EVENT_MOUSE, settings.key, settings.mouse) render_all() libtcod.console_flush() check_level_up() for object in settings.objects: object.clear() settings.player_action = handle_keys() if settings.player_action == 'exit': save_game() break if settings.game_state == 'playing' and \ settings.player_action != 'didnt-take-turn': for object in settings.objects: if object.ai: object.ai.take_turn()
def play_game(): global key, mouse player_action = None key = libtcod.Key() mouse = libtcod.Mouse() while not libtcod.console_is_window_closed(): #MAIN LOOP libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS|libtcod.EVENT_MOUSE,key,mouse) render_all() #flush console to screen libtcod.console_flush() #clear object positions for object in objects: object.clear() 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 play_game(): player_action = None # mouse stuff Game.mouse = libtcod.Mouse() Game.key = libtcod.Key() (Game.camera_x, Game.camera_y) = (0, 0) while not libtcod.console_is_window_closed(): # render the screen libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS | libtcod.EVENT_MOUSE, Game.key, Game.mouse) # render the screen render_all(Game) libtcod.console_flush() check_level_up(Game) # erase objects from old position, before they move for object in Game.objects: object.clear(Game) # handle keys and exit game if needed player_action = handle_keys() if player_action == "exit": break # give monsters a turn if Game.game_state == data.STATE_PLAYING and player_action != data.STATE_NOACTION: for object in Game.objects: if object.ai: object.ai.take_turn(Game)
def play_game(): player_action = None make_world_map() # main loop while game_state == 'playing': libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS | libtcod.EVENT_MOUSE, key, mouse) # render the screen render_all() libtcod.console_flush() # erase all objects at their old locations, before they move for obj in objects: obj.clear() # handle keys and exit game if needed player_action = handle_keys() if game_state == 'exit': # save_game() quit_game() # let monsters take their turn if game_state == 'combat': for object in objects: if object.in_combat: # combat() pass
def main(): global key, mouse, map_, con R.SCREEN_WIDTH = 100 R.SCREEN_HEIGHT = 80 libtcod.console_set_custom_font("data\ont_big.png",libtcod.FONT_LAYOUT_ASCII_INROW) libtcod.console_init_root(R.SCREEN_WIDTH, R.SCREEN_HEIGHT, "Trader-RL", False) libtcod.sys_set_fps(R.LIMIT_FPS) con = libtcod.console_new(R.SCREEN_WIDTH, R.SCREEN_HEIGHT) map_ = Map(R.MAP_WIDTH,R.MAP_HEIGHT) map_.wind_gen.run_simulation(500) mouse = libtcod.Mouse() key = libtcod.Key() while not libtcod.console_is_window_closed(): libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS|libtcod.EVENT_MOUSE, key, mouse) render() player_action = handle_keys() if player_action == "exit": break handle_mouse()
def target_tile(self): # return the position of a tile left-clicked # in player's FOV (optionally in a range), # or None if right-clicked. #display.message("Click a tile to target.") while True: #libtcod.console_flush() libtcod.sys_check_for_event(libtcod.EVENT_ANY, g.key_event_structure, g.mouse_event_structure) # render the screen. this erases the inventory # and shows the names of objects under the mouse. display.render_all() (x, y) = (g.mouse_event_structure.cx, g.mouse_event_structure.cy) if g.mouse_event_structure.rbutton_pressed \ or g.key_event_structure.vk == libtcod.KEY_ESCAPE: return None # cancel if the player right-clicked or pressed Escape # accept the target if the player clicked in FOV # and in case a range is specified, if it's in that range if g.mouse_event_structure.lbutton_pressed: if libtcod.map_is_in_fov(display.fov_map, x, y) \ and (self.max_range is None or self.owner.distance(x, y) <= self.max_range): return (x, y) else: display.message("Out of range.")
def getKeyEvents(): # Grabs key/mouse events. key = libtcod.Key() mouse = libtcod.Mouse() libtcod.sys_check_for_event(libtcod.EVENT_KEY | libtcod.EVENT_MOUSE, key, mouse) return key, mouse
def choose_location(user, validity_func, radius=None, fail_message=None, guess_target = True, draw_pointer=draw_pointer_func() ): from globals import game_blit, effects, screen, key2direction, world, SCREEN_SIZE, on_screen # Pick random valid target_xy = user.xy if guess_target: valid_candidates = [] for xy in make_rect(Pos(0,0), world.level.size).xy_values(): if xy.distance(user.xy) < radius and validity_func(user, xy): valid_candidates.append(xy) if valid_candidates: target_xy = valid_candidates[rand(0, len(valid_candidates)-1)] while True: game_blit(False) valid = validity_func(user, target_xy) effects.clear() if radius: for xy in world.view.xy_values(): if xy.distance(user.xy) < radius: effects.set_char_background(on_screen(xy), screen.get_char_background(xy)) effects.blit( make_rect(Pos(0,0), SCREEN_SIZE), screen, Pos(0,0), 0.0, 0.7) draw_pointer(screen, target_xy, valid) screen.flush() key, mouse = libtcod.Key(), libtcod.Mouse() libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS | libtcod.EVENT_MOUSE, key, mouse) dir = key2direction(key) new_xy = target_xy if key.vk == libtcod.KEY_ENTER: if valid: return target_xy elif fail_message: fail_message(target_xy) elif dir: new_xy = target_xy + Pos(dir[0], dir[1]) elif key.pressed: return None elif mouse.lbutton_pressed: new_xy = Pos(mouse.cx, mouse.cy) print new_xy if new_xy == target_xy: if valid: return target_xy elif fail_message: fail_message(target_xy) if world.level.map.valid_xy(new_xy): target_xy = new_xy
def main_loop(cmap): while not libtcod.console_is_window_closed(): libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS|libtcod.EVENT_MOUSE, key, mouse) render(cmap) libtcod.console_blit(game_console, 0, 0, screen_w, screen_h, 0, screen_x, screen_y) libtcod.console_set_default_foreground(None,libtcod.grey) libtcod.console_set_default_background(None,libtcod.black) if handle_input(key, mouse): raise SystemExit libtcod.console_flush()
def play_game(): global key, mouse, player_turn mouse = libtcod.Mouse() key = libtcod.Key() start_time = libtcod.sys_elapsed_seconds() while not libtcod.console_is_window_closed(): libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS | libtcod.EVENT_MOUSE, key, mouse) delta_time = libtcod.sys_get_last_frame_length() #render the screen if not local: ##Clear the characters from screen. for object_ in R.world_obj: object_.clear(cam_x, cam_y) for city in cities: for merchant in city.trade_house.caravans_out: merchant.clear(cam_x, cam_y) #handles the keys and exit if needed. player_action = handle_keys() if player_action == "exit": save_game() break if not pause: #and not player_turn: advance_time() #player_turn = True handle_mouse() render_all() else: # for object_ in R.locale_obj: # object_.clear(cam_x,cam_y) # # you.clear(cam_x, cam_y) #handles the keys and exit if needed. player_action = handle_keys() if player_action == "exit": save_game() break handle_mouse() render_local() if R.msg_redraw == True: update_msg_bar()
def target_tile(max_range=None): # Return the position of a tile left-clicked in player's FOV optionally in range, or None,None if right-clicked global key, mouse global fov_recompute, fov_map while True: # Render the screen, this erases the inventory and shows the names of objects under the mouse tcod.console_flush() tcod.sys_check_for_event(tcod.EVENT_KEY_PRESS | tcod.EVENT_MOUSE, key, mouse) render_all() (x, y) = (mouse.cx, mouse.cy) if (mouse.lbutton_pressed and tcod.map_is_in_fov(fov_map, x, y) and (max_range is None or player.distance(x, y) <= max_range)): return (x, y) if mouse.rbutton_pressed or key.vk == tcod.KEY_ESCAPE: return (None, None ) # Cancel if the player right-clicked or pressed Esc
def target_tile(max_range=None): global key, mouse #return the position of a tile left-clicked in player's FOV (optionally in a range), or (None,None) if right-clicked. while True: #render the screen. this erases the inventory and shows the names of objects under the mouse. libtcod.console_flush() libtcod.sys_check_for_event( libtcod.EVENT_KEY_PRESS | libtcod.EVENT_MOUSE, key, mouse) render_all() (x, y) = (mouse.cx, mouse.cy) if mouse.rbutton_pressed or key.vk == libtcod.KEY_ESCAPE: return (None, None ) #cancel if the player right-clicked or pressed Escape #accept the target if the player clicked in FOV, and in case a range is specified, if it's in that range if (mouse.lbutton_pressed and libtcod.map_is_in_fov(fov_map, x, y) and (max_range is None or player.distance(x, y) <= max_range)): return (x, y)
def end_game(player): ''' Shows End Game screen ''' constants = get_constants() con = libtcod.console_new(constants['screen_width'], constants['screen_height']) panel = libtcod.console_new(constants['screen_width'], constants['panel_height']) show_end_menu = True # uses custom background image end_menu_background_image = libtcod.image_load('./art/end_screen.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_end_menu: end_menu(con, end_menu_background_image, player, constants['screen_width'], constants['screen_height']) libtcod.console_flush() action = handle_end_menu(key) exit_game = action.get('exit') fullscreen = action.get('fullscreen') # toggle fullscreen if fullscreen: libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen()) if exit_game: show_end_menu = False else: exit()
def target_tile(max_range=None): # return the position of a tile left-clicked in player's FOV (optionally with a further restricted range), # or (None, None) the user cancels targeting. global key, mouse while True: # Stop showing inventory and continues to render screen while targeting libtcod.console_flush() clear_all() libtcod.sys_check_for_event( libtcod.EVENT_KEY_PRESS | libtcod.EVENT_MOUSE, key, mouse) draw_all() (x, y) = (mouse.cx - STAT_PANEL_WIDTH, mouse.cy) if mouse.lbutton_pressed and libtcod.map_is_in_fov(fov_map, x, y) and \ (max_range is None or player.distance_to_tile(x, y) <= max_range): return (x, y) if mouse.rbutton_pressed or key.vk == libtcod.KEY_ESCAPE: message('Targeting cancelled by player.', color=libtcod.orange) return (None, None)
def target_tile(Game, max_range=None): #return the position of a tile left-clicked in player's FOV (optionally in a range) or (None, None) if right-clicked while True: #render screen. this erases the inv and shows the names of objects under the mouse libtcod.console_flush() libtcod.sys_check_for_event( libtcod.EVENT_KEY_PRESS | libtcod.EVENT_MOUSE, Game.key, Game.mouse) render_all(Game) (x, y) = (Game.mouse.cx, Game.mouse.cy) (x, y) = (Game.camera_x + x, Game.camera_y + y ) #from screen to map coords if (Game.mouse.lbutton_pressed and libtcod.map_is_in_fov(Game.player.fighter.fov, x, y) and (max_range is None or Game.player.distance(x, y) <= max_range)): return (x, y) if Game.mouse.rbutton_pressed or Game.key.vk == libtcod.KEY_ESCAPE: return (None, None)
def play_game(): while not libtcod.console_is_window_closed(): global key, mouse mouse = libtcod.Mouse() key = libtcod.Key() libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS, key, mouse) data.current_areas = data.areas[(data.player.X, data.player.Y, data.player.Z)] render.render_all() for object in data.current_areas.objects: object.clear() libtcod.console_flush() player_action = handle_keys() if player_action == 'exit': break #Exit the game #Let mobs take thier turn. if data.game_state == ( 'playing') and player_action != 'didnt-take-turn': for object in data.current_areas.objects: if object.ai: object.ai.take_turn()
def main(): screen_width = 20 screen_height = 20 libtcod.console_set_custom_font( 'arial10x10.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD) libtcod.console_init_root(screen_width, screen_height, 'TEST RL', False) con = libtcod.console_new(screen_width, screen_height) key = libtcod.Key() mouse = libtcod.Mouse() player = Entity(5, 5, '@', libtcod.red) oracle = Entity(2, 2, '0', libtcod.blue) entities = [player, oracle] while not libtcod.console_is_window_closed(): libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS, key, mouse) render_all(con, entities, screen_width, screen_height) libtcod.console_flush() clear_all(con, entities) input = handle_keys(key) move = input.get('move') exit = input.get('exit') fullscreen = input.get('fullscreen') if move: x, y = move player.move(x, y) if exit: return True if fullscreen: libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen())
def main(): screen_width = 80 screen_height = 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 = [player, npc] libtcod.console_set_custom_font('arial10x10.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD) libtcod.console_init_root(screen_width, screen_height, 'libtcod tutorial revised', False) key = libtcod.Key() mouse = libtcod.Mouse() con = libtcod.console_new(screen_width, screen_height) while not libtcod.console_is_window_closed(): libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS, key, mouse) render_all(con,entities,screen_width,screen_height) libtcod.console_flush() clear_all(con, entities) key = libtcod.console_check_for_keypress() action = handle_keys(key) move = action.get('move') exit = action.get('exit') fullscreen = action.get('fullscreen') if move: dx, dy = move player.move(dx,dy) if exit: return True if fullscreen: libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen())
def targeting(size=1, maxrange=None): global player cursor = Object(player.x, player.y, ' ', 'cursor') while not libtcod.console_is_window_closed(): draw_target(cursor, size, maxrange) render_all() libtcod.console_flush() clear_target(cursor, maxrange, size) libtcod.sys_check_for_event( libtcod.EVENT_KEY_PRESS | libtcod.EVENT_MOUSE, key, mouse) input = key.vk if input in DIRECTIONS: dxy = DIRECTIONS[input] cursor.move(dxy[0], dxy[1], True, maxrange) elif input == libtcod.KEY_ESCAPE: return None elif input == libtcod.KEY_ENTER: clear_all() return Area(cursor.x, cursor.y, size)
def target_tile(con, stats_panel, message_panel, key, mouse, fov_map, Player, objects, max_range=None): while True: libtcod.console_flush() libtcod.sys_check_for_event( libtcod.EVENT_KEY_PRESS | libtcod.EVENT_MOUSE, key, mouse) render_all(con, stats_panel, message_panel, mouse, fov_map, Player, objects) (x, y) = (mouse.cx, mouse.cy) if (mouse.lbutton_pressed and libtcod.map_is_in_fov(fov_map, x, y) and (max_range is None or Player.distance(x, y) <= max_range)): return (x, y) if mouse.rbutton_pressed or key.vk == libtcod.KEY_ESCAPE: return (None, None)
def main(): ## SETUP ## screen_width = 80 screen_height = 65 fps_limit = 10 libtcod.console_set_custom_font( 'arial10x10.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD) libtcod.console_init_root(screen_width, screen_height, 'The Game of Life', False) libtcod.sys_set_fps(fps_limit) con = libtcod.console_new(screen_width, screen_height) key = libtcod.Key() mouse = libtcod.Mouse() map = GameMap(screen_width, screen_height) ## GAME LOOP ## while not libtcod.console_is_window_closed(): ## ENTITY UPDATES AND RENDERING ## entities = map.get_entities() libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS, key, mouse) render_all(con, entities, screen_width, screen_height) libtcod.console_flush() clear_all(con, entities) map.update_tiles() ## CALLS TO INPUT HANDLING ## input = handle_keys(key) exit = input.get('exit') fullscreen = input.get('fullscreen') if exit: return True if fullscreen: libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen())
def handleInput(self): key = rl.Key() # Set up the variables for mouse and key input. mouse = rl.Mouse() rl.sys_check_for_event(rl.EVENT_KEY_PRESS|rl.EVENT_MOUSE,key,mouse) # Update the key and mouse variables if a key or mouse button was pressed. if key.vk == rl.KEY_ENTER or key.vk == rl.KEY_SPACE or key.vk == rl.KEY_KPENTER or mouse.lbutton_pressed: if self.box == None: # Don't do anything if there's no box yet. return None command = None if mouse.lbutton_pressed: # If the mouse was clicked, attempt to retrieve a result. command = self.box.handleClick(mouse) if (key.vk == rl.KEY_ENTER or key.vk == rl.KEY_KPENTER) and command == None: # If a key was pressed and a mouse click did not occur or yield any results: command = self.box.forward() # Retrieve the selected option. if command == "Return to Title": return "TitleScene" return None elif key.vk == rl.KEY_DOWN or key.vk == rl.KEY_KP2: self.box.goDown() # Go down one item. return None elif key.vk == rl.KEY_UP or key.vk == rl.KEY_KP8: self.box.goUp() # Go up one item. return None return None
def handle_keys(self): """returns pointer to function to call""" k = libtcod.Key() m = libtcod.Mouse() for t in range(Player.LIMIT_FPS * Player.MAX_TIMEOUT): if t % 5 == 0: self.redraw_screen(t / Player.LIMIT_FPS) ev = libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS, k, m) if ev and k and k.pressed and chr(k.c) in self.KEYMAP: return self.KEYMAP.get(chr(k.c)) # redraw screen after first second after keypress self.redraw_screen(Player.MAX_TIMEOUT) # call this before going into while loop to make sure no keypresses get dropped ev = libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS, k, m) while True: if k and k.pressed and chr(k.c) in self.KEYMAP: return self.KEYMAP.get(chr(k.c)) k = libtcod.console_wait_for_keypress(True)
def target_tile(max_range=None): # Return the position of a tile left-clicked in the player's FOV # (optionally in a range), or (None, None) if right-clicked global key global mouse while True: # Render the screen. This erases the inventory and shows the names of # objects under the mouse. render_all() libtcod.console_flush() # Get mouse position and click status libtcod.sys_check_for_event( (libtcod.EVENT_KEY_PRESS | libtcod.EVENT_MOUSE), key, mouse) (x, y) = (mouse.cx, mouse.cy) # print('{}:{}'.format(str(x), str(y))) # Accept the taret if the player clicked in FOV and in case a range is # specified, if it's in that range if (mouse.lbutton_pressed and libtcod.map_is_in_fov(fov_map, x, y) and (max_range is None or player.distance(x, y) <= max_range)): return (x, y) if mouse.rbutton_pressed or key.vk == libtcod.KEY_ESCAPE: return (None, None) # Cancel if right-clicked or Escape is pressed
def input(self): libtcod.sys_check_for_event( libtcod.EVENT_KEY_PRESS | libtcod.EVENT_MOUSE, self.key, self.mouse) if self.key.vk == libtcod.KEY_ENTER and self.key.lalt: #Alt+Enter: toggle fullscreen libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen()) elif self.key.vk == libtcod.KEY_ESCAPE: self.state = 0 elif self.key.vk == libtcod.KEY_SPACE: print str(self.mouse.cx) + ' - ' + str(self.mouse.cy) #elif self.key.vk == libtcod.KEY_UP: #self.game_move_map(0,-1) #elif self.key.vk == libtcod.KEY_DOWN: #self.game_move_map(0,1) #elif self.key.vk == libtcod.KEY_LEFT: #self.game_move_map(-1,0) #elif self.key.vk == libtcod.KEY_RIGHT: #self.game_move_map(1,0) if libtcod.console_is_key_pressed(libtcod.KEY_UP): self.game_move_map(0, -1) if libtcod.console_is_key_pressed(libtcod.KEY_DOWN): self.game_move_map(0, 1) if libtcod.console_is_key_pressed(libtcod.KEY_LEFT): self.game_move_map(-1, 0) if libtcod.console_is_key_pressed(libtcod.KEY_RIGHT): self.game_move_map(1, 0) #Debug Purpose------------------------ if self.mouse.mbutton_pressed: print self.window.get_elem_by_mouse()
def play_game(): global key, mouse global camera_x, camera_y player_action = None mouse = libtcod.Mouse() key = libtcod.Key() (camera_x, camera_y) = (0, 0) 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_up_level() # 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 # Monsters take their turns if game_state == 'playing' and player_action != 'didnt-take-turn': for object in objects: if object.ai: object.ai.take_turn()
def play_game(): global key, mouse player_action = None mouse = libtcod.Mouse() key = libtcod.Key() ############# #Main loop # ############# while not libtcod.console_is_window_closed(): libtcod.sys_check_for_event( libtcod.EVENT_KEY_PRESS | libtcod.EVENT_MOUSE, key, mouse) #render the screen 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 play_game(): global key, mouse step = 0 global prev_key prev_key = 'up' global prev_step prev_step = 0 mouse = libtcod.Mouse() key = libtcod.Key() some_happened = False while not libtcod.console_is_window_closed(): step += 1 libtcod.sys_check_for_event( libtcod.EVENT_KEY_PRESS | libtcod.EVENT_MOUSE, key, mouse) if some_happened: render_all() libtcod.console_flush() check_level_up() for object in objects: object.clear() player_action = handle_keys(step) if player_action == 'exit': break some_happened = True if game_state == 'playing' and player_action != 'didnt-take-turn' and player_action != 'nothing_happened': for object in objects: if object.ai: object.ai.take_turn() elif player_action == 'nothing_happened': some_happened = False event_logging('End of game')
def handle_input(): global MOUSE_POS, LAST_MOUSE_POS _mouse = tcod.mouse_get_status() _event = tcod.sys_check_for_event(tcod.EVENT_ANY, KEY, MOUSE) if KEY.c: _key_code = KEY.c else: _key_code = KEY.vk if _event == tcod.KEY_PRESSED: if not _key_code in INPUT: INPUT[_key_code] = 1 else: INPUT[_key_code] += 1 if '--keyout' in sys.argv and INPUT[_key_code] == 1: print KEY.c, KEY.vk, INPUT[_key_code] elif _event == tcod.KEY_RELEASED: INPUT[_key_code] = 0 LAST_MOUSE_POS = MOUSE_POS[:] MOUSE_POS = [int(round(i)) for i in numbers.interp_velocity(LAST_MOUSE_POS, (_mouse.cx, _mouse.cy), .8)] if not MOUSE_POS == LAST_MOUSE_POS: events.trigger_event('mouse_moved', x=MOUSE_POS[0], y=MOUSE_POS[1], dx=MOUSE_POS[0]-LAST_MOUSE_POS[0], dy=MOUSE_POS[1]-LAST_MOUSE_POS[1]) if _mouse.lbutton_pressed: events.trigger_event('mouse_pressed', x=MOUSE_POS[0], y=MOUSE_POS[1], button=1) if _mouse.rbutton_pressed: events.trigger_event('mouse_pressed', x=MOUSE_POS[0], y=MOUSE_POS[1], button=2) if _mouse.lbutton: events.trigger_event('mouse_held', x=MOUSE_POS[0], y=MOUSE_POS[1], button=1) if _mouse.rbutton: events.trigger_event('mouse_held', x=MOUSE_POS[0], y=MOUSE_POS[1], button=2)
def get_user_input(self, x, y): ''' Getting user to type something in ''' mouse = libtcod.Mouse() key = libtcod.Key() player_input = '' key_pressed = '' while not key_pressed == libtcod.KEY_ENTER: # Clear console, print the current iteration of string, and blit self.clear() libtcod.console_print(self.con, x, y, '>> %s_' % player_input) self.blit() libtcod.console_flush() #key = libtcod.console_wait_for_keypress(True) event = libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS | libtcod.EVENT_MOUSE, key, mouse) #key_pressed = get_key(key) if key.vk == libtcod.KEY_CHAR: key_pressed = chr(key.c) else: key_pressed = key.vk # Handle special keypresses if key_pressed == libtcod.KEY_SPACE: key_pressed = ' ' elif key_pressed == libtcod.KEY_BACKSPACE and len(player_input) > 0: key_pressed = '' player_input = player_input[0:len(player_input) - 1] libtcod.console_clear(self.con) libtcod.console_flush() elif key_pressed == libtcod.KEY_ENTER: break # Try to add keypress to thing if isinstance(key_pressed, str): player_input += key_pressed return player_input
def main(): constants = get_constants() #mixer.init(frequency=44100, size=16, channels=2,buffer=4096) #mixer.music.load('A Memory Lost.ogg') #mixer.music.play(loops=0, start=0.0) #mixer.music.set_volume(0.01) libtcod.console_set_custom_font('terminal16x16_gs_ro.png', libtcod.FONT_LAYOUT_ASCII_INROW, 16, 18) libtcod.console_init_root(constants['screen_width'], constants['screen_height'], constants['window_title'], False, libtcod.RENDERER_OPENGL) con = libtcod.console_new(constants['screen_width'], constants['screen_height']) message_panel = libtcod.console_new(constants['message_panel_width'], constants['panel_height']) char_info_panel = libtcod.console_new(constants['char_info_panel_width'], constants['char_info_panel_height']) area_info_panel = libtcod.console_new(constants['area_info_panel_width'], constants['area_info_panel_height']) under_mouse_panel = libtcod.console_new( constants['under_mouse_panel_width'], constants['under_mouse_panel_height']) #load_customfont() player = None entities = [] game_map = None message_log = None game_state = None show_main_menu = True show_load_error_message = False show_controls_menu = 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 games to load', libtcod.darker_blue, 23, 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') controls = action.get('controls') 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_new_game_variables( constants) if player != None: fov_map = initialize_fov(game_map) floor_index = [] entity_index = [] original_entity_index = entity_index fov_index = [] floor_index.append(game_map.tiles) entity_index.append(entities) original_entity_index.append(entities) fov_index.append(fov_map) game_state = GameStates.PLAYERS_TURN show_main_menu = False elif load_saved_game: try: player, entities, game_map, message_log, game_state, floor_index, original_entity_index, \ entity_index, fov_index = load_game() show_main_menu = False except FileNotFoundError: show_load_error_message = True elif controls and show_controls_menu == False: show_controls_menu = True show_main_menu = False elif exit_game: break elif show_controls_menu: libtcod.console_clear(0) controls_menu(con, '', 30, constants['screen_width'], constants['screen_height']) action = handle_controls_menu(key) libtcod.console_flush() back_to_main_menu = action.get('exit') if back_to_main_menu: show_controls_menu = False show_main_menu = True libtcod.console_clear(0) elif show_main_menu == False and show_controls_menu == False: libtcod.console_clear(con) play_game(player, entities, game_map, message_log, game_state, con, message_panel, char_info_panel, area_info_panel, under_mouse_panel, constants, floor_index, original_entity_index, entity_index, fov_index) show_main_menu = True
def play_game(player, entities, game_map, message_log, game_state, con, message_panel, char_info_panel, area_info_panel, under_mouse_panel, constants, floor_index, original_entity_index, entity_index, fov_index): fov_recompute = True fov_map = initialize_fov(game_map) key = libtcod.Key() mouse = libtcod.Mouse() game_state = GameStates.PLAYERS_TURN previous_game_state = game_state mouse_x = mouse.cx old_mouse_x = mouse_x mouse_y = mouse.cy old_mouse_y = mouse_y #attack_animation_x = 0 #attack_animation_y = 0 clean_map = False #attacked = False #animation_time = 200 #animation_distance = 0 targeting_item = None equipment_choice = 0 npc = None item = None while not libtcod.console_is_window_closed(): libtcod.sys_check_for_event( libtcod.EVENT_KEY_PRESS | libtcod.EVENT_MOUSE, key, mouse) """ if animation_time == 0: if attacked: animation_distance += 1 animation_time = 200 if animation_distance == 5: animation_distance = 0 attacked = False """ if clean_map == True: fov_recompute = True clean_map = False if fov_recompute: recompute_fov(fov_map, player.x, player.y, constants['fov_radius'], constants['fov_light_walls'], constants['fov_algorithm']) render_all(con, message_panel, char_info_panel, area_info_panel, under_mouse_panel, entities, player, game_map, fov_map, fov_recompute, message_log, constants['screen_width'], constants['screen_height'], constants['bar_width'], constants['panel_height'], constants['panel_y'], mouse, constants['tiles'], constants['colors'], game_state, npc, targeting_item, item, equipment_choice) fov_recompute = False libtcod.console_flush() clear_all(con, entities, fov_map, game_map) action = handle_keys(key, game_state) mouse_action = handle_mouse(mouse) ############################################ if game_state == GameStates.EQUIPMENT_SCREEN and not action.get( 'exit'): for equipment in action: if equipment: equipment_choice = equipment break ############################################ move = action.get('move') ranged_attack = action.get('ranged_attack') interact = action.get('interact') inspect_item = action.get('inspect_item') wait = action.get('wait') pickup = action.get('pickup') show_inventory = action.get('show_inventory') drop_inventory = action.get('drop_inventory') inventory_index = action.get('inventory_index') take_stairs = action.get('take_stairs') level_up = action.get('level_up') show_character_screen = action.get('show_character_screen') show_equipment_screen = action.get('show_equipment_screen') exit = action.get('exit') fullscreen = action.get('fullscreen') left_click = mouse_action.get('left_click') right_click = mouse_action.get('right_click') player_turn_results = [] if move and game_state == GameStates.PLAYERS_TURN: dx, dy = move destination_x = player.x + dx destination_y = player.y + dy if not game_map.is_blocked(destination_x, destination_y): target = get_blocking_entities_at_location( entities, destination_x, destination_y) if target and not target.invulnerable: attack_results = player.combat_class.attack(target) player_turn_results.extend(attack_results) clean_map = True elif not target: player.move(dx, dy) if player.combat_class.turns_until_rest == 0: pass else: player.combat_class.turns_until_rest -= 1 fov_recompute = True game_state = GameStates.ENEMY_TURN elif move and game_state == GameStates.INTERACT: dx, dy = move destination_x = player.x + dx destination_y = player.y + dy if not game_map.is_blocked(destination_x, destination_y): blocking_target = get_blocking_entities_at_location( entities, destination_x, destination_y) non_blocking_target = get_non_blocking_entities_at_location( entities, destination_x, destination_y) if blocking_target: try: if blocking_target.dialogue.dialogue: npc = blocking_target except (AttributeError): pass if blocking_target.bonfire is not None: message_log.add_message( Message( 'You see a mysterious bonfire. You cannot resist touching it', libtcod.light_violet)) entity_index = blocking_target.bonfire.reset_entities( game_map, original_entity_index, entity_index) game_state = GameStates.PLAYERS_TURN else: message_log.add_message( Message('You see {0}'.format(blocking_target.name), libtcod.white)) elif non_blocking_target: message_log.add_message( Message('You see {0}'.format(non_blocking_target.name), libtcod.white)) else: message_log.add_message( Message('There is nothing to inspect here.', libtcod.white)) elif wait: if player.combat_class.turns_until_rest == 0: pass else: player.combat_class.turns_until_rest -= 1 message = player.combat_class.rest() message_log.add_message(Message(message, libtcod.green)) game_state = GameStates.ENEMY_TURN elif pickup and game_state == GameStates.PLAYERS_TURN: for entity in entities: if entity.item and entity.x == player.x and entity.y == player.y: pickup_results = player.inventory.add_item(entity) player_turn_results.extend(pickup_results) break else: message_log.add_message( Message('There is nothing here to pick up.', libtcod.white)) if show_inventory: previous_game_state = game_state game_state = GameStates.SHOW_INVENTORY if drop_inventory: previous_game_state = game_state game_state = GameStates.DROP_INVENTORY if interact: previous_game_state = GameStates.PLAYERS_TURN game_state = GameStates.INTERACT message_log.add_message(Message('You begin to look around.')) if ranged_attack: if player.equipment.main_hand.equippable.ranged: previous_game_state = GameStates.PLAYERS_TURN game_state = GameStates.TARGETING message_log.add_message(Message('Choose a target to attack.')) else: message_log.add_message( Message('This weapon cannot attack at range.')) if inventory_index is not None and previous_game_state != GameStates.PLAYER_DEAD and \ inventory_index < len(player.inventory.items): item = player.inventory.items[inventory_index] if game_state == GameStates.SHOW_INVENTORY: player_turn_results.extend( player.inventory.use(item, entities=entities, fov_map=fov_map)) elif game_state == GameStates.DROP_INVENTORY: player_turn_results.extend(player.inventory.drop_item(item)) elif game_state == GameStates.CHOOSE_ITEM_TO_INSPECT: previous_game_state = GameStates.CHOOSE_ITEM_TO_INSPECT game_state = GameStates.INSPECT_ITEM message_log.add_message( Message('You inspect the {0}.'.format(item.name))) if take_stairs and game_state == GameStates.PLAYERS_TURN: for entity in entities: if entity.stairs and entity.x == player.x and entity.y == player.y: if entity.name == 'Stairs Down': if len(floor_index) == game_map.dungeon_level: entities = game_map.new_floor( player, message_log, constants) fov_map = initialize_fov(game_map) floor_index.append(game_map.tiles) entity_index.append(entities) original_entity_index.append(entities) fov_index.append(fov_map) fov_recompute = True libtcod.console_clear(con) break elif len(floor_index) > game_map.dungeon_level: # Update the entity index with the floors NEW entity list entity_index[game_map.dungeon_level - 1] = entities entities, player, fov_map = game_map.next_floor( player, entity_index, floor_index, fov_index, message_log, constants) fov_recompute = True libtcod.console_clear(con) break elif entity.name == 'Stairs Up': entity_index[game_map.dungeon_level - 1] = entities entities, player, fov_map = game_map.previous_floor( player, entity_index, floor_index, fov_index, message_log, constants) fov_recompute = True libtcod.console_clear(con) break else: message_log.add_message( Message('There are no stairs here.', libtcod.yellow)) if level_up: if level_up == 'str': player.combat_class.base_strength += 1 elif level_up == 'dex': player.combat_class.base_dexterity += 1 elif level_up == 'sta': player.combat_class.base_stamina += 1 elif level_up == 'int': player.combat_class.base_intelligence += 1 game_state = previous_game_state if show_character_screen: previous_game_state = game_state game_state = GameStates.CHARACTER_SCREEN if show_equipment_screen: previous_game_state = game_state game_state = GameStates.EQUIPMENT_SCREEN if game_state == GameStates.TARGETING: mouse_x = mouse.cx mouse_y = mouse.cy if (old_mouse_y != mouse_y or old_mouse_x != mouse_x) and libtcod.map_is_in_fov( fov_map, mouse_x, mouse_y): fov_recompute = True elif libtcod.map_is_in_fov( fov_map, old_mouse_x, old_mouse_y) and not libtcod.map_is_in_fov( fov_map, mouse_x, mouse_y): clean_map = True old_mouse_x = mouse_x old_mouse_y = mouse_y if left_click and targeting_item != None: target_x, target_y = left_click item_use_results = player.inventory.use(targeting_item, entities=entities, fov_map=fov_map, target_x=target_x, target_y=target_y) player_turn_results.extend(item_use_results) fov_recompute = True elif right_click: player_turn_results.append({'targeting_cancelled': True}) fov_recompute = True elif left_click and targeting_item == None: target_x, target_y = left_click if not game_map.tiles[target_x][target_y].blocked: target = get_blocking_entities_at_location( entities, target_x, target_y) else: message_log.add_message( Message('You can\'t attack that.', libtcod.yellow)) if target and not target.invulnerable: attack_results = player.combat_class.attack(target) player_turn_results.extend(attack_results) fov_recompute = True game_state = GameStates.ENEMY_TURN if game_state == GameStates.SHOW_INVENTORY: if inspect_item: previous_game_state = game_state game_state = GameStates.CHOOSE_ITEM_TO_INSPECT message_log.add_message( Message('Choose an item to inspect.', libtcod.yellow)) if game_state == GameStates.EQUIPMENT_SCREEN: if equipment_choice: previous_game_state = game_state game_state = GameStates.EQUIPMENT_DETAILS if exit: if game_state in (GameStates.SHOW_INVENTORY, GameStates.DROP_INVENTORY, GameStates.CHARACTER_SCREEN, GameStates.INTERACT): if game_state == (GameStates.INTERACT): player_turn_results.append({'interacting_cancelled': True}) game_state = previous_game_state npc = None else: game_state = previous_game_state elif game_state == GameStates.CHOOSE_ITEM_TO_INSPECT: game_state = GameStates.SHOW_INVENTORY previous_game_state = GameStates.PLAYERS_TURN message_log.add_message( Message('Item inspection cancelled.', libtcod.yellow)) elif game_state == GameStates.INSPECT_ITEM: game_state = previous_game_state elif game_state == GameStates.EQUIPMENT_SCREEN: game_state = GameStates.PLAYERS_TURN elif game_state == GameStates.EQUIPMENT_DETAILS: game_state = previous_game_state equipment_choice = False elif game_state == GameStates.TARGETING: player_turn_results.append({'targeting_cancelled': True}) game_state = previous_game_state fov_recompute = True else: libtcod.console_clear(0) save_game(player, entities, game_map, message_log, game_state, floor_index, original_entity_index, entity_index, fov_index) return True if fullscreen: libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen()) for player_turn_result in player_turn_results: message = player_turn_result.get('message') dead_entity = player_turn_result.get('dead') item_added = player_turn_result.get('item_added') item_consumed = player_turn_result.get('consumed') item_dropped = player_turn_result.get('item_dropped') equip = player_turn_result.get('equip') targeting = player_turn_result.get('targeting') targeting_cancelled = player_turn_result.get('targeting_cancelled') xp = player_turn_result.get('xp') interacting_cancelled = player_turn_result.get( 'interacting_cancelled') if message: message_log.add_message(message) if dead_entity: if dead_entity == player: message, game_state = kill_player(dead_entity) else: dead_entity.alive = False message = kill_monster(dead_entity) message_log.add_message(message) if item_added: entities.remove(item_added) game_state = GameStates.ENEMY_TURN if item_consumed: game_state = GameStates.ENEMY_TURN if equip: equip_results = player.equipment.toggle_equip(equip) for equip_result in equip_results: equipped = equip_result.get('equipped') unequipped = equip_result.get('unequipped') if equipped: message_log.add_message( Message('You equipped the {0}.'.format( equipped.name))) if unequipped: message_log.add_message( Message('You unequipped the {0}.'.format( unequipped.name))) game_state = GameStates.ENEMY_TURN if targeting: previous_game_state = GameStates.PLAYERS_TURN game_state = GameStates.TARGETING targeting_item = targeting message_log.add_message(targeting_item.item.targeting_message) if targeting_cancelled: game_state = previous_game_state message_log.add_message(Message('Targeting cancelled.')) if interacting_cancelled: game_state = previous_game_state message_log.add_message(Message('You stop looking around.')) if xp: leveled_up = player.level.add_xp(xp) message_log.add_message( Message('You gain {0} experience points.'.format(xp), libtcod.lighter_yellow)) if leveled_up: message_log.add_message( Message( 'Your skills grow more honed. You reach level {0}'. format(player.level.current_level) + "!", libtcod.yellow)) previous_game_state = game_state game_state = GameStates.LEVEL_UP if item_dropped: entities.append(item_dropped) game_state = GameStates.ENEMY_TURN if game_state == GameStates.ENEMY_TURN: for entity in entities: if entity.ai: if wait: enemy_turn_results = entity.ai.approach_player_on_wait( player, fov_map, game_map, entities) else: enemy_turn_results = entity.ai.take_turn( player, fov_map, game_map, entities) for enemy_turn_result in enemy_turn_results: message = enemy_turn_result.get('message') dead_entity = enemy_turn_result.get('dead') if message: message_log.add_message(message) if dead_entity: if dead_entity == player: message, game_state = kill_player( dead_entity) else: message = kill_monster(dead_entity) message_log.add_message(message) if game_state == GameStates.PLAYER_DEAD: break if game_state == GameStates.PLAYER_DEAD: break else: game_state = GameStates.PLAYERS_TURN
def main(): constants = get_constants() libtcod.console_set_custom_font( 'TiledFont.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD, 32, 10) load_customfont() 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 show_main_help_menu = 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']) if show_main_help_menu: main_menu_help_menu(con, 30, 13, 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') main_help_menu = action.get('main_help_menu') if show_load_error_message and (new_game or load_saved_game or exit_game or main_help_menu): show_load_error_message = False elif show_main_help_menu and (new_game or load_saved_game or exit_game or main_help_menu): show_main_help_menu = 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 main_help_menu: show_main_help_menu = 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 play_game(player, entities, game_map, message_log, game_state, con, panel, constants): fov_recompute = True fov_map = initialize_fov(game_map) key = libtcod.Key() mouse = libtcod.Mouse() if not game_state == GameStates.PLAYER_DEAD: game_state = GameStates.PLAYERS_TURN begin_player_turn = True previous_game_state = game_state targeting_item = None if not game_state == GameStates.PLAYER_DEAD: PLAYERDEADSTATE = False else: PLAYERDEADSTATE = True while not libtcod.console_is_window_closed(): libtcod.sys_check_for_event( libtcod.EVENT_KEY_PRESS | libtcod.EVENT_MOUSE, key, mouse) if fov_recompute: recompute_fov(fov_map, player.x, player.y, constants['fov_radius'], constants['fov_light_walls'], constants['fov_algorithm']) render_all(con, panel, entities, player, game_map, fov_map, fov_recompute, message_log, constants['screen_width'], constants['screen_height'], constants['bar_width'], constants['panel_height'], constants['panel_y'], mouse, constants['colors'], constants['kill_count'], game_state, constants['wall_tile'], constants['floor_tile']) fov_recompute = False libtcod.console_flush() clear_all(con, entities) action = handle_keys(key, game_state) mouse_action = handle_mouse(mouse) move = action.get('move') wait = action.get('wait') pickup = action.get('pickup') show_inventory = action.get('show_inventory') drop_inventory = action.get('drop_inventory') drop_equipment = action.get('drop_equipment') show_equipment_inventory = action.get('show_equipment_inventory') show_bag = action.get('show_bag') inventory_index = action.get('inventory_index') equipment_inventory_index = action.get('equipment_inventory_index') take_stairs = action.get('take_stairs') level_up = action.get('level_up') show_character_screen = action.get('show_character_screen') show_help_menu = action.get('show_help_menu') exit = action.get('exit') exit_quit_menu = action.get('exit_quit_menu') fullscreen = action.get('fullscreen') cast_magic_wand = action.get('cast_magic_wand') shoot_bow = action.get('shoot_bow') drop_menu = action.get('drop_menu') sell_menu = action.get('sell_menu') sell_equipment_menu = action.get('sell_equipment_menu') buy_menu = action.get('buy_menu') buy_equipment_menu = action.get('buy_equipment_menu') shop_menu = action.get('shop_menu') shop_menu_index = action.get('shop_menu_index') shop_equipment_menu_index = action.get('shop_equipment_menu_index') left_click = mouse_action.get('left_click') right_click = mouse_action.get('right_click') player_turn_results = [] if begin_player_turn and game_state == GameStates.PLAYERS_TURN: begin_player_turn = False if player.fighter.status: player_turn_results.extend(player.fighter.status.update()) if move and game_state == GameStates.PLAYERS_TURN: dx, dy = move destination_x = player.x + dx destination_y = player.y + dy if not game_map.is_blocked(destination_x, destination_y): target = get_blocking_entities_at_location( entities, destination_x, destination_y) if target: attack_results = player.fighter.attack(target, constants, entities=entities) #playsound('sounds/attack.m4a', block=False) player_turn_results.extend(attack_results) else: player.move(dx, dy) fov_recompute = True game_state = GameStates.ENEMY_TURN elif wait: game_state = GameStates.ENEMY_TURN elif pickup and game_state == GameStates.PLAYERS_TURN: for entity in entities: if entity.item and ( not entity.equippable ) and entity.x == player.x and entity.y == player.y: pickup_results = player.inventory.add_item(entity) player_turn_results.extend(pickup_results) break elif entity.item and entity.x == player.x and entity.y == player.y: pickup_results = player.equipment_inventory.add_item( entity) player_turn_results.extend(pickup_results) break else: message_log.add_message( Message("There is nothing here to pick up...", libtcod.yellow)) if show_inventory: previous_game_state = game_state game_state = GameStates.SHOW_INVENTORY if show_equipment_inventory: previous_game_state = game_state game_state = GameStates.SHOW_EQUIPMENT_INVENTORY if drop_inventory: previous_game_state = game_state game_state = GameStates.DROP_INVENTORY if drop_equipment: previous_game_state = game_state game_state = GameStates.DROP_EQUIPMENT if show_bag: previous_game_state = game_state game_state = GameStates.SHOW_BAG if drop_menu: previous_game_state = game_state game_state = GameStates.DROP_MENU if inventory_index is not None and previous_game_state != GameStates.PLAYER_DEAD and inventory_index < len( player.inventory.items): item = player.inventory.items[inventory_index] if game_state == GameStates.SHOW_INVENTORY: player_turn_results.extend( player.inventory.use(item, entities=entities, fov_map=fov_map)) elif game_state == GameStates.DROP_INVENTORY: player_turn_results.extend(player.inventory.drop_item(item)) elif game_state == GameStates.SELL_MENU: player_turn_results.extend( player.inventory.sell(item, game_state)) if equipment_inventory_index is not None and previous_game_state != GameStates.PLAYER_DEAD and equipment_inventory_index < len( player.equipment_inventory.items): equip_item = player.equipment_inventory.items[ equipment_inventory_index] if game_state == GameStates.SHOW_EQUIPMENT_INVENTORY: player_turn_results.extend( player.equipment_inventory.use(equip_item)) elif game_state == GameStates.DROP_EQUIPMENT: player_turn_results.extend( player.equipment_inventory.drop_item(equip_item)) elif game_state == GameStates.SELL_EQUIPMENT_MENU: player_turn_results.extend( player.equipment_inventory.sell(equip_item, game_state)) if shop_menu_index is not None and previous_game_state != GameStates.PLAYER_DEAD: item = game_map.shop_items[shop_menu_index] if game_state == GameStates.BUY_MENU: player_turn_results.extend( player.inventory.buy(item, game_state)) if shop_equipment_menu_index is not None and previous_game_state != GameStates.PLAYER_DEAD: item = game_map.shop_equipment_items[shop_equipment_menu_index] if game_state == GameStates.BUY_EQUIPMENT_MENU: player_turn_results.extend( player.equipment_inventory.buy(item, game_state)) if take_stairs and game_state == GameStates.PLAYERS_TURN: for entity in entities: if entity.stairs and entity.x == player.x and entity.y == player.y: entities = game_map.next_floor(player, message_log, constants) fov_map = initialize_fov(game_map) fov_recompute = True libtcod.console_clear(con) break else: message_log.add_message( Message("There are no stairs here...", libtcod.yellow)) if cast_magic_wand and game_state == GameStates.PLAYERS_TURN: wand = player.inventory.search("Magic Wand") staff = player.inventory.search("Wizard Staff") if wand is None and staff is None: message_log.add_message( Message("You cannot cast magic without a magic item!", libtcod.orange)) else: player_turn_results.extend( player.inventory.use(wand, entities=entities, fov_map=fov_map)) game_state = GameStates.ENEMY_TURN if shoot_bow and game_state == GameStates.PLAYERS_TURN: bow = player.inventory.search("Long Bow") arrow = player.inventory.search("Arrow") if bow is None and arrow is None: message_log.add_message( Message( "You don't have anything to shoot with at this time!", libtcod.orange)) elif bow is None and arrow is not None: message_log.add_message( Message("You cannot shoot an arrow without a bow!", libtcod.orange)) elif bow is not None and arrow is None: message_log.add_message( Message("You need arrows to use your bow", libtcod.orange)) else: player_turn_results.extend( player.inventory.use(bow, entities=entities, fov_map=fov_map)) game_state = GameStates.ENEMY_TURN if level_up: if level_up == 'hp': player.fighter.base_max_hp += 20 player.fighter.hp += 20 message_log.add_message( Message("You leveled up your HP!", libtcod.light_cyan)) elif level_up == 'str': player.fighter.base_power += 1 message_log.add_message( Message("You leveled up your ATTACK!", libtcod.light_cyan)) elif level_up == 'def': player.fighter.base_defense += 1 message_log.add_message( Message("You leveled up your DEFENSE!", libtcod.light_cyan)) elif level_up == 'mgk': player.fighter.base_magic += 1 message_log.add_message( Message("You leveled up your MAGIC!", libtcod.light_cyan)) elif level_up == 'mgk_def': player.fighter.base_magic_defense += 1 message_log.add_message( Message("You leveled up your MAGIC RESISTANCE!", libtcod.light_cyan)) game_state = previous_game_state if show_character_screen: previous_game_state = game_state game_state = GameStates.CHARACTER_SCREEN if show_help_menu: previous_game_state = game_state game_state = GameStates.HELP_MENU if sell_menu: previous_game_state = game_state game_state = GameStates.SELL_MENU if sell_equipment_menu: previous_game_state = game_state game_state = GameStates.SELL_EQUIPMENT_MENU if buy_menu: previous_game_state = game_state game_state = GameStates.BUY_MENU if buy_equipment_menu: previous_game_state = game_state game_state = GameStates.BUY_EQUIPMENT_MENU if shop_menu: previous_game_state = game_state game_state = GameStates.SHOP_MENU if game_state == GameStates.TARGETING: if left_click: target_x, target_y = left_click item_use_results = player.inventory.use(targeting_item, entities=entities, fov_map=fov_map, target_x=target_x, target_y=target_y) player_turn_results.extend(item_use_results) elif right_click: player_turn_results.append({'targeting_cancelled': True}) if exit: if game_state in (GameStates.SHOW_INVENTORY, GameStates.DROP_INVENTORY, GameStates.DROP_EQUIPMENT, GameStates.CHARACTER_SCREEN, GameStates.HELP_MENU, GameStates.SHOW_EQUIPMENT_INVENTORY, GameStates.SELL_MENU, GameStates.BUY_MENU, GameStates.SELL_EQUIPMENT_MENU, GameStates.BUY_EQUIPMENT_MENU): game_state = previous_game_state elif game_state == GameStates.TARGETING: player_turn_results.append({'targeting_cancelled': True}) elif game_state == GameStates.SHOW_BAG: if PLAYERDEADSTATE == True: game_state = GameStates.PLAYER_DEAD else: game_state = GameStates.PLAYERS_TURN elif game_state == GameStates.SHOP_MENU: if PLAYERDEADSTATE == True: game_state = GameStates.PLAYER_DEAD else: game_state = GameStates.PLAYERS_TURN elif game_state == GameStates.PLAYERS_TURN: game_state = GameStates.QUIT_MENU elif game_state == GameStates.DROP_MENU: game_state = GameStates.PLAYERS_TURN else: save_game(player, entities, game_map, message_log, game_state) return True if exit_quit_menu: if game_state == GameStates.QUIT_MENU: game_state = previous_game_state if fullscreen: libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen()) for player_turn_result in player_turn_results: message = player_turn_result.get('message') dead_entity = player_turn_result.get('dead') item_added = player_turn_result.get('item_added') equipment_item_added = player_turn_result.get( 'equipment_item_added') item_consumed = player_turn_result.get('consumed') equipment_consumed = player_turn_result.get('equipment_consumed') item_dropped = player_turn_result.get('item_dropped') loot_dropped = player_turn_result.get('loot_dropped') staff_used = player_turn_result.get('staff_used') equip = player_turn_result.get('equip') targeting = player_turn_result.get('targeting') targeting_cancelled = player_turn_result.get('targeting_cancelled') xp = player_turn_result.get('xp') item_bought = player_turn_result.get('item_bought') equipment_bought = player_turn_result.get('equipment_bought') end_turn = player_turn_result.get('end_turn') if message: message_log.add_message(message) if targeting_cancelled: game_state = previous_game_state message_log.add_message(Message('Targeting cancelled')) if xp: leveled_up = player.level.add_xp(xp) message_log.add_message( Message("You gain {0} experience points.".format(xp))) if leveled_up: message_log.add_message( Message( "Your battle prowess grows stronger! You reached level {0}!" .format(player.level.current_level), libtcod.yellow)) previous_game_state = game_state game_state = GameStates.LEVEL_UP if dead_entity: if dead_entity == player: PLAYERDEADSTATE = True message, game_state = kill_player(dead_entity, constants) message_log.add_message(message) else: monster_name = '' monster_name = dead_entity.name message = kill_monster(dead_entity, player, constants) constants['kill_count'] += 1 message_log.add_message(message) while dead_entity.equipment_inventory.items: item = dead_entity.equipment_inventory.items[0] dead_entity.equipment_inventory.loot_item(item) entities.append(item) message_log.add_message( Message( "The {0} dropped the {1}.".format( monster_name, item.name), libtcod.yellow)) while dead_entity.inventory.items: item = dead_entity.inventory.items[0] dead_entity.inventory.loot_item(item) entities.append(item) message_log.add_message( Message( "The {0} dropped the {1}.".format( monster_name, item.name), libtcod.yellow)) if item_added: entities.remove(item_added) game_state = GameStates.ENEMY_TURN if equipment_item_added: entities.remove(equipment_item_added) game_state = GameStates.ENEMY_TURN if item_consumed: game_state = GameStates.ENEMY_TURN if item_bought: game_map.shop_items.remove(item_bought) game_state = GameStates.ENEMY_TURN if equipment_bought: game_map.shop_equipment_items.remove(equipment_bought) game_state = GameStates.ENEMY_TURN if equipment_consumed: game_state = GameStates.ENEMY_TURN if staff_used: game_state = GameStates.ENEMY_TURN if end_turn: game_state = GameStates.ENEMY_TURN if targeting: previous_game_state = GameStates.PLAYERS_TURN game_state = GameStates.TARGETING targeting_item = targeting message_log.add_message(targeting_item.item.targeting_message) if item_dropped: entities.append(item_dropped) game_state = GameStates.ENEMY_TURN if loot_dropped: entities.append(loot_dropped) game_state = GameStates.ENEMY_TURN if equip: equip_results = player.equipment.toggle_equip(equip) for equip_result in equip_results: equipped = equip_result.get('equipped') dequipped = equip_result.get('dequipped') if equipped: message_log.add_message( Message("You equipped the {0}".format( equipped.name))) if dequipped: message_log.add_message( Message("You dequipped the {0}".format( dequipped.name))) game_state = GameStates.ENEMY_TURN if game_state == GameStates.ENEMY_TURN: fov_recompute = True for entity in entities: if entity.ai: if entity.fighter.status: entity.fighter.status.update() enemy_turn_results = entity.ai.take_turn( player, fov_map, game_map, entities, constants) for enemy_turn_result in enemy_turn_results: message = enemy_turn_result.get('message') dead_entity = enemy_turn_result.get('dead') if message: message_log.add_message(message) if dead_entity: if dead_entity == player: PLAYERDEADSTATE = True message, game_state = kill_player( dead_entity, constants) message_log.add_message(message) else: monster_name = '' monster_name = dead_entity.name message = kill_monster(dead_entity, player, constants) constants['kill_count'] += 1 message_log.add_message(message) while dead_entity.equipment_inventory.items: item = dead_entity.equipment_inventory.items[ 0] dead_entity.equipment_inventory.loot_item( item) entities.append(item) message_log.add_message( Message( "The {0} dropped the {1}.".format( monster_name, item.name), libtcod.yellow)) while dead_entity.inventory.items: item = dead_entity.inventory.items[0] dead_entity.inventory.loot_item(item) entities.append(item) message_log.add_message( Message( "The {0} dropped the {1}.".format( monster_name, item.name), libtcod.yellow)) if game_state == GameStates.PLAYER_DEAD: break if game_state == GameStates.PLAYER_DEAD: break else: game_state = GameStates.PLAYERS_TURN begin_player_turn = True
def main(): screen_width = 80 screen_height = 50 bar_width = 20 panel_height = 7 panel_y = screen_height - panel_height message_x = bar_width + 2 message_width = screen_width - bar_width - 2 message_height = panel_height - 1 map_width = 80 map_height = 43 room_max_size = 10 room_min_size = 6 max_rooms = 30 fov_algorithm = 0 fov_light_walls = True fov_radius = 10 max_monsters_per_room = 3 max_items_per_room = 2 colors = { 'dark_wall' : libtcod.Color(0, 0, 100), 'dark_ground' : libtcod.Color(50, 50, 150), 'light_wall' : libtcod.Color(130, 110, 50), 'light_ground' : libtcod.Color(200, 180, 50) } fighter_component = Fighter(hp=30, defense=2, power=5) inventory_component = Inventory(26) player = Entity(0, 0, '@', libtcod.white, 'Player', blocks=True, render_order=RenderOrder.ACTOR, fighter=fighter_component, inventory=inventory_component) entities = [player] libtcod.console_set_custom_font('arial10x10.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD) libtcod.console_init_root(screen_width, screen_height, 'libtcod tutorial revised', False) con = libtcod.console_new(screen_width, screen_height) panel = libtcod.console_new(screen_width, panel_height) game_map = GameMap(map_width, map_height) game_map.make_map(max_rooms, room_min_size, room_max_size, map_width, map_height, player, entities, max_monsters_per_room, max_items_per_room) fov_recompute = True fov_map = initialize_fov(game_map) message_log = MessageLog(message_x, message_width, message_height) key = libtcod.Key() mouse = libtcod.Mouse() game_state = GameStates.PLAYERS_TURN previous_game_state = game_state while not libtcod.console_is_window_closed(): libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS | libtcod.EVENT_MOUSE, key, mouse) if fov_recompute: recompute_fov(fov_map, player.x, player.y, fov_radius, fov_light_walls, fov_algorithm) render_all(con, panel, entities, player, game_map, fov_map, fov_recompute, message_log, screen_width, screen_height, bar_width, panel_height, panel_y, mouse, colors, game_state) fov_recompute = False libtcod.console_flush() clear_all(con, entities) action = handle_keys(key, game_state) move = action.get('move') pickup = action.get('pickup') show_inventory = action.get('show_inventory') drop_inventory = action.get('drop_inventory') inventory_index = action.get('inventory_index') exit = action.get('exit') fullscreen = action.get('fullscreen') player_turn_results = [] if move and game_state == GameStates.PLAYERS_TURN: dx, dy = move destination_x = player.x + dx destination_y = player.y + dy if not game_map.is_blocked(destination_x, destination_y): target = get_blocking_entities_at_location(entities, destination_x, destination_y) if target: attack_results = player.fighter.attack(target) player_turn_results.extend(attack_results) else: player.move(dx, dy) fov_recompute = True game_state = GameStates.ENEMY_TURN elif pickup and game_state == GameStates.PLAYERS_TURN: for entity in entities: if entity.item and entity.x == player.x and entity.y == player.y: pickup_results = player.inventory.add_item(entity) player_turn_results.extend(pickup_results) break else: message_log.add_message(Message('There is nothing to pickup here.', libtcod.yellow)) if show_inventory: previous_game_state = game_state game_state = GameStates.SHOW_INVENTORY if drop_inventory: previous_game_state = game_state game_state = GameStates.DROP_INVENTORY if inventory_index is not None and previous_game_state != GameStates.PLAYER_DEAD and inventory_index < len( player.inventory.items): item = player.inventory.items[inventory_index] if game_state == GameStates.SHOW_INVENTORY: player_turn_results.extend(player.inventory.use(item)) elif game_state == GameStates.DROP_INVENTORY: player_turn_results.extend(player.inventory.drop_item(item)) if exit: if game_state in (GameStates.SHOW_INVENTORY, GameStates.DROP_INVENTORY): game_state = previous_game_state else: return True if fullscreen: libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen()) for player_turn_result in player_turn_results: message = player_turn_result.get('message') dead_entity = player_turn_result.get('dead') item_added = player_turn_result.get('item_added') item_consumed = player_turn_result.get('consumed') item_dropped = player_turn_result.get('item_dropped') if message: message_log.add_message(message) if dead_entity: if dead_entity == player: message, game_state = kill_player(dead_entity) else: message = kill_monster(dead_entity) message_log.add_message(message) if item_added: entities.remove(item_added) game_state = GameStates.ENEMY_TURN if item_consumed: game_state = GameStates.ENEMY_TURN if item_dropped: entities.append(item_dropped) game_state = GameStates.ENEMY_TURN if game_state == GameStates.ENEMY_TURN: for entity in entities: if entity.ai: enemy_turn_results = entity.ai.take_turn(player, fov_map, game_map, entities) for enemy_turn_result in enemy_turn_results: message = enemy_turn_result.get('message') dead_entity = enemy_turn_result.get('dead') if message: message_log.add_message(message) if dead_entity: if dead_entity == player: message, game_state = kill_player(dead_entity) else: message = kill_monster(dead_entity) message_log.add_message(message) if game_state == GameStates.PLAYER_DEAD: break if game_state == GameStates.PLAYER_DEAD: break else: game_state = GameStates.PLAYERS_TURN
message( 'Welcome stranger! Prepare to die hehe XD FILLER FILLER FILLER FILLER FILLER', libtcod.red) mouse = libtcod.Mouse() key = libtcod.Key() ########### # Main Loop ########### 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(game_state_1) libtcod.console_flush() # erase all objects at their old locations, before they move for local_entity in objects: local_entity.clear() # handle keys and exit game if needed player_action = handle_keys(game_state_1) if player_action == 'exit': break if game_state_1.the_game_state == 'playing' and player_action != 'didnt-take-turn': for thing in objects:
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(): constants = get_constants() libtcod.console_set_custom_font( 'arial10x10.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD) libtcod.console_init_root(constants['screen_width'], constants['screen_height'], 'rogue', False) con = libtcod.console_new(constants['screen_width'], constants['screen_height']) panel = libtcod.console_new(constants['screen_width'], constants['panel_height']) player, entities, game_map, message_log, game_state = get_game_variables( constants) fov_recompute = True fov_map = initialize_fov(game_map) key = libtcod.Key() mouse = libtcod.Mouse() previous_game_state = game_state while not libtcod.console_is_window_closed(): libtcod.sys_check_for_event( libtcod.EVENT_KEY_PRESS | libtcod.EVENT_MOUSE, key, mouse) if fov_recompute: recompute_fov(fov_map, player.x, player.y, constants['fov_radius'], constants['fov_light_walls'], constants['fov_algorithm']) render_all(con, panel, entities, player, game_map, fov_map, fov_recompute, message_log, constants['screen_width'], constants['screen_height'], constants['bar_width'], constants['panel_height'], constants['panel_y'], constants['mouse'], constants['colors'], game_state) fov_recompute = False libtcod.console_flush() clear_all(con, entities) action = handle_keys(key, game_state) mouse_action = handle_mouse(mouse) move = action.get('move') pickup = action.get('pickup') show_inventory = action.get('show_inventory') drop_inventory = action.get('drop_inventory') inventory_index = action.get('inventory_index') exit = action.get('exit') fullscreen = action.get('fullscreen') left_click = mouse_action.get('left_click') right_click = mouse_action.get('right_click') player_turn_results = [] if move and game_state == GameStates.PLAYERS_TURN: dx, dy = move destination_x = player.x + dx destination_y = player.y + dy if not game_map.is_blocked(player.x + dx, player.y + dy): target = get_blocking_entities_at_location( entities, destination_x, destination_y) if target: player.fighter.attack(target) attack_results = player.fighter.attack(target) player_turn_results.extend(attack_results) else: player.move(dx, dy) fov_recompute = True game_state = GameStates.ENEMY_TURN elif pickup and game_state == GameStates.PLAYERS_TURN: for entity in entities: if entity.item and entity.x == player.x and entity.y == player.y: pickup_results = player.inventory.add_item(entity) player_turn_results.extend(pickup_results) break else: message_log.add_message( Message('There is nothing here to pick up.', libtcod.yellow)) if show_inventory: previous_game_state = game_state game_state = GameStates.SHOW_INVENTORY if drop_inventory: previous_game_state = game_state game_state = GameStates.DROP_INVENTORY if inventory_index is not None and previous_game_state != GameStates.PLAYER_DEAD and inventory_index < len( player.inventory.items): item = player.inventory.items[inventory_index] if game_state == GameStates.SHOW_INVENTORY: player_turn_results.extend( player.inventory.use(item, entities=entities, fov_map=fov_map)) elif game_state == GameStates.DROP_INVENTORY: player_turn_results.extend(player.inventory.drop_item(item)) if game_state == GameStates.TARGETING: if left_click: target_x, target_y = left_click item_use_results = player.inventory.use(targeting_item, entities=entities, fov_map=fov_map, target_x=target_x, target_y=target_y) player_turn_results.append(item_use_results) elif right_click: player_turn_results.append({'targeting_cancelled': True}) if exit: if game_state in (game_state.SHOW_INVENTORY, game_state.DROP_INVENTORY): game_state = previous_game_state elif game_state == GameStates.TARGETING: player_turn_results.append({'targeting_cancelled': True}) else: return True if fullscreen: libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen()) for player_turn_result in player_turn_results: message = player_turn_result.get('message') dead_entity = player_turn_result.get('dead') item_added = player_turn_result.get('item_added') item_consumed = player_turn_result.get('consumed') item_dropped = player_turn_result.get('item_dropped') targeting = player_turn_result.get('targeting') targeting_cancelled = player_turn_result.get('targeting_cancelled') if message: message_log.add_message(message) if targeting_cancelled: game_state = previous_game_state message_log.add_message(Message('Targeting cancelled')) if dead_entity: if dead_entity == player: message, game_state = kill_player(dead_entity) else: message = kill_monster(dead_entity) message_log.add_message(message) if item_added: entities.remove(item_added) game_state = game_state.ENEMY_TURN if item_consumed: game_state.ENEMY_TURN if targeting: previous_game_state = GameStates.PLAYERS_TURN game_state = GameStates.TARGETING targeting_item = targeting message_log.add_message(targeting_item.item.targeting_message) if item_dropped: entities.append(item_dropped) game_state = GameStates.ENEMY_TURN if game_state == GameStates.ENEMY_TURN: for entity in entities: if entity.ai: enemy_turn_results = entity.ai.take_turn( player, fov_map, game_map, entities) for enemy_turn_result in enemy_turn_results: message = enemy_turn_result.get('message') dead_entity = enemy_turn_result.get('dead') if message: message_log.add_message(message) if dead_entity: if dead_entity == player: message, game_state = kill_player(dead_entity) else: message = kill_monster(dead_entity) message_log.add_message(message) if game_state == GameStates.PLAYER_DEAD: break if game_state == GameStates.PLAYER_DEAD: break else: game_state = GameStates.PLAYERS_TURN
def main(): screen_width = 80 screen_height = 50 map_width = 80 map_height = 45 room_max_size = 10 room_min_size = 6 max_rooms = 30 fov_algorithm = 0 fov_light_walls = True fov_radius = 10 max_monsters_per_room = 3 colors = { 'dark_wall': libtcod.Color(0, 0, 100), 'dark_ground': libtcod.Color(50, 50, 150), 'light_wall': libtcod.Color(130, 110, 50), 'light_ground': libtcod.Color(200, 180, 50) } fighter_component = Fighter(hp=30, defense=2, power=5) player = Entity(0, 0, '@', libtcod.white, "Player", blocks=True, render_order=RenderOrder.ACTOR, fighter=fighter_component) entities = [player] libtcod.console_set_custom_font('arial10x10.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD) libtcod.console_init_root(screen_width, screen_height, 'libtcod tutorial revised', False) con = libtcod.console_new(screen_width, screen_height) game_map = GameMap(map_width, map_height) game_map.make_map(max_rooms, room_min_size, room_max_size, map_width, map_height, player, entities, max_monsters_per_room) fov_recompute = True fov_map = initialize_fov(game_map) key = libtcod.Key() mouse = libtcod.Mouse() game_state = GameStates.PLAYERS_TURN while not libtcod.console_is_window_closed(): libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS, key, mouse) if fov_recompute: recompute_fov(fov_map, player.x, player.y, fov_radius, fov_light_walls, fov_algorithm) render_all(con, entities, player, game_map, fov_map, fov_recompute, screen_width, screen_height, colors) libtcod.console_flush() clear_all(con, entities) action = handle_keys(key) move = action.get('move') exit = action.get('exit') fullscreen = action.get('fullscreen') player_turn_results = [] if move and game_state == GameStates.PLAYERS_TURN: dx, dy = move destination_x = player.x + dx destination_y = player.y + dy if not game_map.is_blocked(destination_x, destination_y): target = get_blocking_entities_at_location(entities, destination_x, destination_y) if target: attack_results = player.fighter.attack(target) player_turn_results.extend(attack_results) else: player.move(dx, dy) fov_recompute = True game_state = GameStates.ENEMY_TURN if exit: return True if fullscreen: libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen()) for player_turn_result in player_turn_results: message = player_turn_result.get('message') dead_entity = player_turn_result.get('dead') if message: print(message) if dead_entity: if dead_entity == player: message, game_state = kill_player(dead_entity) else: message = kill_monster(dead_entity) print(message) if game_state == GameStates.ENEMY_TURN: for entity in entities: if entity.ai: enemy_turn_results = entity.ai.take_turn(player, fov_map, game_map, entities) for enemy_turn_result in enemy_turn_results: message = enemy_turn_result.get('message') dead_entity = enemy_turn_result.get('dead') if message: print(message) if dead_entity: if dead_entity == player: message, game_state = kill_player(dead_entity) else: message = kill_monster(dead_entity) print(message) if game_state == GameStates.PLAYER_DEAD: break if game_state == GameStates.PLAYER_DEAD: break else: game_state = GameStates.PLAYERS_TURN
def main(): # Presets libtcod.sys_set_fps(120) 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 = 20 fov_algorithm = 0 fov_light_walls = True fov_radius = 6 # Dictionary to hold colors we'll be using colors = { 'dark_wall': libtcod.Color(48, 98, 48), 'dark_ground': libtcod.Color(100, 140, 15), 'light_wall': libtcod.Color(110, 110, 48), 'light_ground': libtcod.Color(155, 188, 15), 'unexplored': libtcod.Color(15, 56, 15) } # We declare an npc npc = Entity(int(screen_width / 2 + 2), int(screen_height / 2 - 5), '@', libtcod.grey) # We declare the player player = Entity(int(screen_width / 2), int(screen_height / 2), '@', libtcod.white) # We get all entities in a list so we can iterate them entities = [player, npc] # Define the font and the screen... libtcod.console_set_custom_font( 'arial12x12.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD) libtcod.console_init_root(screen_width, screen_height, 'Roguelike', False) # False because we dont want fullscreen # Main console con = libtcod.console_new(screen_width, screen_height) # We create a map randomly from the available options map_type = randint(0, 1) if map_type == 0: game_map = GameMap(map_width, map_height) game_map.make_map(80, map_width, map_height, player, max_monsters, entities) else: room_num = randint(10, max_rooms) game_map = roomGameMap(map_width, map_height) game_map.make_map(room_num, room_min_size, room_max_size, map_width, map_height, player, entities, 3) # Fov doesn't need to be computed every turn, only if we move. We use # the boolean fov_recompute to handle this fov_recompute = True fov_map = initialize_fov(game_map) # Key for holding key presses and mouse variable key = libtcod.Key() mouse = libtcod.Mouse() # Game Loop while not libtcod.console_is_window_closed(): # Check for keypress libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS, key, mouse) # Calculate fov 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, player) fov_recompute = False libtcod.console_flush() # We overwrite the character before getting the new coordinates, so next time we draw # it will not leave a trace ''' Key handling We look for dictionary entries in action. If any of these is present it will be True. ''' action = handle_keys(key) move = action.get('move') exit = action.get('exit') fullscreen = action.get('fullscreen') if move: dx, dy = move # We check if we can actually move to the tile if not game_map.is_blocked(player.x + dx, player.y + dy): player.move(dx, dy) fov_recompute = True if exit: return True if fullscreen: libtcod.console_set_fullscreen( not libtcod.console_is_fullscreen()) # On and off