Controller documentation

Module containing the base controller class.

class mvctools.controller.BaseController(state, model)

Base controller class for the MVC pattern implementation.

Shouldn’t be instanciated manually but subclassed then registered in a State class.

A subclass of BaseController may override the following method:
  • init: called at initialization (default: do nothing)
  • is_quit_event: define what a quit event is (default: pygame.QUIT and Alt+f4)
  • handle_event: process a given event (default: do nothing)
An instance has the following attributes:
  • self.state: the state that uses the controller
  • self.control: the game that uses the controller
  • self.model: the model associated with the controller
handle_event(event)

Empty method to override.

Parameters:event (Event) – a pygame event
Returns:bool – True to stop the current state, False (or None) otherwise.

An overwritten version of this method should regiser actions to the model.

Example:

def handle_event(event):
    if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_UP:
            return self.model.register_up() # OR
            # return self.register("up")

Note: It is probably a bad idea to return directly True since stopping the current state should be the model decision.

init()

Empty method to override if needed.

Called at initialization.

is_quit_event(event)

Define what a quit event is. Include pygame.Quit and Alt+F4.

Can be overriden to include more quit events.

Parameters:event (Event) – a pygame event
Returns:bool – True if event is a quit event
register(name, *args, **kwargs)

Convenience function to register an action to the model.

Parameters:
  • name (str) – name of the action to register
  • args (list) – arguments to pass to the model method
  • kwargs (dict) – keywords arguments to pass to the model method
Returns:

bool – True to indicate that the model wants to stop the current state, False otherwise.

Previous topic

Control documentation

Next topic

Gamedata documentation

This Page