How to Use the Game Classes

The Simplest Game

Here is the absolute minimum to invoke the pygsear game framework:

from pygsear.Game import Game
g = Game()
g.mainloop()

Not very exciting. You can press escape to bring up the configuration screen, change the size of the window, and save your configuration. That's about it. You will of course want to add some code to customize your game.

Initialization

As one of the final steps in the creation of the Game object, the initialize method will be called.

The initialize method is where you should place your code to customize the set up of the game. Like this:

from pygsear.Game import Game
from pygsear.locals import GREEN

class MyFirstGame(Game):
    def initialize(self):
        self.set_background(color=GREEN)

mfg = MyFirstGame()
mfg.mainloop()

The main loop

In most games (and in most programs with a graphical interface) there is one central piece of code which is repeated very quickly while waiting for the player (the user) to tell it what to do.

This central piece of code is often called the event loop since it watches for external events like pressing keys on the keyboard, moving the mouse or clicking the mouse buttons, the passage of time, etc.

In the pygsear Game classes, the main event loop is a method called mainloop.

Once everything for the game is set up (initialized) you should call the mainloop, and that loop will run until the game is over or the player has chosen to quit.

Add more game objects

from pygsear.Game import Game
from pygsear.Drawable import Circle
from pygsear.Event import MOUSEBUTTONDOWN_Event
from pygsear.Util import color

class MyFirstGame(Game):
    def initialize(self):
        green = color('green', 'dark')
        self.set_background(color=green)

        circle = Circle()
        circle.path.set_velocity(vx=5, vy=10)
        self.sprites.add(circle)
        self.circle = circle

        self.events.add(MOUSEBUTTONDOWN_Event(button=1, callback=self.leftclick))

    def leftclick(self, ev):
        if self.circle.rect.collidepoint(ev.pos):
            print 'pop'
            self.quit = 1


mfg = MyFirstGame()
mfg.mainloop()