Complete Roguelike Tutorial, using python+libtcod, part 10

From RogueBasin
Revision as of 20:12, 3 April 2011 by Jotaf (talk | contribs) (page created)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

This is part of a series of tutorials; the main page can be found here.



Main menu and saving


Tidy initialization

Now that our game is bursting with gameplay potential, we can think about those little things that diehard fans will surely miss during their long playing sessions. One of the most important is, of course, a save/load mechanism! This way they can go to sleep and dream about your scary monsters between play sessions.

To choose between continuing a previous game or starting a new one we need a main menu. But wait -- our initialization logic and game loop are tightly bound, so they're not really prepared for these tasks. To avoid code duplication, we need to break them down into meaningful blocks (functions). We can then put them together to change between the menu and the game, start new games or load them, and even go to new dungeon levels. It's much easier than it sounds, so fear not!

Take a look at your initialization and game loop code, after all the functions. I can identify 4 blocks:


  • System initialization (code between console_set_custom_font and panel = ...).
  • Setting up a new game (everything else except for the game loop and the FOV map creation).
  • Creating the FOV map.
  • Starting the game loop.


The reason why I chose this separation is because I think they're the minimal blocks needed to do all the tasks. The system initialization must be done just once. Thinking out loud, here are outlines of all the tasks:


  • Create a new game: set up the game, create FOV map, start game loop. (This is what we have now.)
  • Load game: load data (we won't deal with this block now), create FOV map, start game loop.
  • Advance level: set up new level (we won't deal with this block now), create FOV map. (The game loop is already running and will just continue.)


Hopefully that will make at least some sense, it's not very detailed but gives us something to aim at.

The system initialization code will stay right where it is, it's the first thing the script executes. Now grab the rest of the code and put it in a new function; except for FOV map creation (and fov_recompute = True), the lines player_action = None and objects = [player], which will go elsewhere:


def new_game():
    global player, inventory, game_msgs, game_state
    
    #create object representing the player
    fighter_component = Fighter(hp=30, defense=2, power=5, death_function=player_death)
    player = Object(0, 0, '@', 'player', libtcod.white, blocks=True, fighter=fighter_component)

    #generate map (at this point it's not drawn to the screen)
    make_map()

    game_state = 'playing'
    inventory = []

    #create the list of game messages and their colors, starts empty
    game_msgs = []

    #a warm welcoming message!
    message('Welcome stranger! Prepare to perish in the Tombs of the Ancient Kings.', libtcod.red)


We have to declare some variables as global, since we're assigning them inside a function now. The FOV map creation code goes in its own function, declaring some globals as well:


def initialize_fov():
    global fov_recompute, fov_map
    fov_recompute = True
    
    #create the FOV map, according to the generated map
    fov_map = libtcod.map_new(MAP_WIDTH, MAP_HEIGHT)
    for y in range(MAP_HEIGHT):
        for x in range(MAP_WIDTH):
            libtcod.map_set_properties(fov_map, x, y, not map[x][y].blocked, not map[x][y].block_sight)


Finally, the game loop and the player_action = None line belong to their own function now:


def play_game():
    player_action = None
    
    while not libtcod.console_is_window_closed():
        #render the screen
        render_all()
        
        libtcod.console_flush()

        #erase all objects at their old locations, before they move
        for object in objects:
            object.clear()
        
        #handle keys and exit game if needed
        player_action = handle_keys()
        if player_action == 'exit':
            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()


That was a lot of shuffling stuff around! Ok, just a couple of tiny changes left. The new_game function should initialize FOV right after creating the map:


    initialize_fov()


And, since the objects' list represents objects on the map, it makes sense to initialize it on map creation, so put it in make_map:


def make_map():
    global map, objects
    
    #the list of objects with just the player
    objects = [player]


Finally, after system initialization you need to call the new functions to start playing:


new_game()
play_game()


That's it! The code is much tidier. If you don't care much about that, though, and prefer visible changes -- then the next section is just for you!


The main menu