A Dash of Game Development – 5. Going Object Oriented.


In the previous chapters we finished with the basic outline of a game. However, this simple outline isn’t going to take us too far when it comes to building a complex interactive game. It’s time we added structure to this outline so that we can build it into something that we can actually use to create our game.

For the sake of reference let me copy paste the outline of the game we have from the previous chapter.

InitializeGame

while Running :
        CheckForUserInput
        BuildEventQueue
        if Required.Event in EventQueue :
               TakeAction

        UpdateGameEntities
        ClearTheView
        DrawEntities
        DisplayTheBitmap

CleanupAndClose

Most of our pseudo code can be written in the form of discrete functions. To make the out line more OOP friendly, let’s group the functions into a class. We will call this class a GameManager, since it obviously manages the game. We will add more game management functions to it as we progress, but for now let’s focus on what we know. I hope you have a good handle on Python, because I am assuming at least a basic understanding of Python and OOP.

#!/usr/bin/python

from PySFML import sf

class GameManager():

    def Init(self):
        '''Initiates the game and it's resources. All resource'''
        '''loading can be done here'''
        self.window = sf.RenderWindow(sf.VideoMode(800, 600), "Rapid Game Framework")
        self.running = True
        return True

    def GameLoop(self):
        '''Starts the Game Loop'''
        while self.running:

            # Get the close event
            event = sf.Event()
            while self.window.GetEvent(event):
                if event.Type == sf.Event.Closed:
                    self.running = False

            # Update the game entities
            self.UpdateEntities()

            # Clear the screen.
            self.window.Clear()

            # Draw all entities.
            self.DrawEntities()

            # Display the contents of the bitmap.
            self.window.Display()

        return True

    def Shutdown(self):
        '''Calls Cleanup function and closes the window and exits the game.'''

        # Clean up.
        self.Cleanup()

        # Close our window.
        self.window.Close()
        return True

    def UpdateEntities(self) :
        '''Updates all the game entities'''

    def DrawEntities(self) :
        '''Draws all the game entities.'''

    def Cleanup(self) :
        '''Clean up by freeing all entities'''

if __name__ == '__main__':
    mgr = GameManager()
    if mgr.Init() :
        mgr.GameLoop()
    mgr.Shutdown()

The GameManager is a class that has all the functions from our outline. If you have been following the previous tutorials the function and their names must be self explanatory. The __main__ function of out code creates, initiates, runs the GameManager and finally closes and cleans up the game. That was pretty simple, wasn’t it? The only addition to the above outline was the Shutdown function. I’ll get into it in more detail in the later chapters, but for now let’s just think of it as the last step in our game close process.

The current outline does very little except open and close a window. We will expand it in later chapters. As an exercise you can try adding the scrolling text we had from the previous chapters to this outline. It should be pretty easy.

As you can well see, the code is getting longer. As we continue, the code will get even longer and span across multiple files. Thus henceforth, for brevity, I will be posting only code snippets in posts and mention only function and class names. Be sure you understand the outline and the code mentioned here. We will be using the same function names and the GameManger class from here on. You can always find the entire code for all the examples at the bottom of each chapter.

Downloads


2 responses to “A Dash of Game Development – 5. Going Object Oriented.”

  1. Thank you for these tutorials! Because I found your site I decided to work with pySFML instead of pyGame. I’m interested in working on a game project with you, I don’t have anything particular in mind but I have experience with python.

    My most recent project is http://four2go.gumyum.com, but now I’m trying to learn graphical 2d game design.

    Thanks again, I’ll be sure to stop by often!

    • Well, I will be creating a complete game in this series. You are welcome to join in with your contributions to the game. You can keep it as a separate archive somewhere on your site and provide a link to your work in the comments perhaps?

      Also, you are welcome to comment on anything you feel like.