Tutorial: Displaying a map in your application

From FIFE development wiki
(Redirected from Tutorial2)
Jump to: navigation, search

Introduction

This tutorial will be aimed on showing how you can really simply load a map for your FIFE based game.

In this tutorial we will use the 'Rio de hola' demo map. A map to use with FIFE can be created with the FIFE editor. It will create an xml file. You can see how a map file looks like, just go on the 'maps' repository in the 'Rio de hola' demo. It imports all the needed object for loading the map, sets the position of all of them, sets the layer, the cameras etc...

So our goal is to write a method to load a map from this xml file and show it for our game.

The game controller

This tutorial is a good opportunity to talk about what we call a 'game controller'. A game controller is a class which handle the game process. It should implements his own listener and use it to handle events from keyboard an mouse to make the progress. Your game controller has to implemented by your application, an example can be find at the previous tutorial : Tutorial1.

Write a basic game controller

Let's try to write a really basic game controller :

class GameController(object):
        '''
        Main game class. It handles all the game process.
        '''
 
        def __init__(self, application, engine, settings):
                self.application = application
                self.engine = engine
                self.model = self.engine.getModel()
                self.settings = settings
 
                self.listener = GameListener(self.engine, self) #game listener
 
        def pump(self)
            '''
            Called every frames by the application.
            '''
 
            pass

As you can see it's really simple. The __init__ method just creates the attributes we will need during the game process and sets up the game listener. The pump method is the method called every frame by the application we just wrote in the previous tutorial.

For now, there is still nothing shown on the screen, but this is normal.

Load a map in the game controller

The interesting part starts here. Now we have to write a method that can load a map (as xml file) and show it.

Let's modify our __init__ a little bit :

        def __init__(self, application, engine, settings):
                self.application = application
                self.engine = engine
                self.model = self.engine.getModel()
                self.settings = settings
 
                self.listener = GameListener(self.engine, self) #game listener
 
                #new stuffs
                self.filename = None
                self.map = None

You will understand the purpose of these two things later.

Now take a look at what could be our loadMap method :

        def loadMap(self, filename):
                '''
                Load a map from a xml file.
                '''
 
                self.filename = filename
 
                self.map = loadMapFile(os.path.join('maps', filename + '.xml'), self.engine, extensions = {'lights': True})

The loadMapFile method is provided by FIFE engine in the loaders module :

from fife.extensions.loaders import loadMapFile

Thanks to this function, we don't need to do anything.

Note : in your future games, you will sometimes want to load different maps during the game process. So in this case, we will need to add a new method in our game controller to clean a previous map and all the created objects :

        def reset(self):
                '''
                Reset the current map informations.
                '''
 
                if self.map:
                        self.engine.getModel().deleteMap(self.map)
 
                retval = self.engine.getModel().deleteObjects()
 
                self.map = None
                self.filename = None

This method is straight-forward : we delete the map by removing it from the engine model and do the same with the objects. After that we can safely load a new map.

A good idea would be to add the call to reset() in the loadMap() method :

        def loadMap(self, filename):
                '''
                Load a map from a xml file.
                '''
 
                self.reset()
 
                self.filename = filename
 
                self.map = loadMapFile(os.path.join('maps', filename + '.xml'), self.engine, extensions = {'lights': True})

In the next tutorial we will deal with map layers, cameras, listeners... and try to make things more interactive :-)