gale.game module

This file contains the implementation of the class Game: the window, virtual-resolution scaling, and game loop (a variable-rate update/render pair driven by real elapsed time, plus a fixed_update that steps at a constant rate regardless of frame rate) every gale game is built on top of. Every constructor argument is optional and falls back to gale.conf.settings (see gale.conf) when omitted, so a project’s settings.py is enough to configure it without passing anything explicitly.

Importing this module calls pygame.init().

Author: Alejandro Mujica (aledrums@gmail.com)

class gale.game.Game(title=None, window_width=None, window_height=None, virtual_width=None, virtual_height=None, fps=None, fixed_timestep=None, *args, **kwargs)[source]

Bases: InputListener

Base class to implemente a game by using pygame.

This class handles the window to show the game an a virtual screen with the resolution that you want to emulate. This also handles timer and the game loop.

Usage example:

class MyGame(Game):
def init(self) -> None:

# Set your own initial configuration of the game. self.player = Player() self.world = World()

def on_input(self, input_id: str, input_data: InputData) -> None:

# Make your action when an input is detected. if input_id == “quit” and input_data.pressed:

self.quit()

def update(self, dt: float) -> None:

# Update of all your game elements here. # dt is the elapsed time in secconds. self.world.update(dt) self.player.update(dt) self.player.interact_with(self.world)

def render(self, surface: pygame.Surface) -> None:

# Render all of your game elements on the virtual # screen surface. self.world.render(surface) self.player.render(surface)

game = MyGame(title=’Title of my game’) game.exec()

fixed_update() is optional and only needed for logic that must advance by the same amount of time every call, independent of the frame rate (e.g. driving gale.physics.World.fixed_update() directly instead of its own internal accumulator, or a networked simulation tick):

class MyGame(Game):
def init(self) -> None:

self.world = World()

def fixed_update(self) -> None:

self.world.fixed_update()

Parameters:
  • title (str | None)

  • window_width (int | None)

  • window_height (int | None)

  • virtual_width (int | None)

  • virtual_height (int | None)

  • fps (int | None)

  • fixed_timestep (float | None)

  • args (Tuple[Any])

  • kwargs (Dict[str, Any])

init()[source]

Empty. This should be implemented by the extension class.

Return type:

None

on_input(input_id, input_data)[source]

Empty. This should be implemented by the extension class.

Parameters:
  • input_id (str)

  • input_data (InputData)

Return type:

None

update(dt)[source]

Empty. This should be implemented by the extension class.

Parameters:

dt (float) – Time elapsed (in seconds) since the last time this function has been executed.

Return type:

None

fixed_update()[source]

Empty. Override for deterministic, frame-rate-independent logic (e.g. driving a gale.physics.World, or anything else that needs to advance by the same amount of time on every call regardless of how fast frames are rendering): unlike update(), this runs zero, one, or several times per frame so that it always advances the game by exactly fixed_timestep seconds each time it’s called.

Return type:

None

render(surface)[source]

Empty. This should be implemented by the extension class.

Parameters:
  • render_surface – The surface where you should render all of the game elements on. Its dimensions are virtual_width x virtual_height.

  • surface (Surface)

Return type:

None

exec()[source]

Execute the game loop.

Return type:

None

quit()[source]

Mark the game to exit and stop it from receiving further input, undoing the registration __init__ made with InputHandler.

Return type:

None