Control documentation

Module for the state control related objects.

class mvctools.control.BaseControl

Base class for the state control.

Its main puprpose is to run the states and handle the transitions between them. It provides for instance a method to register the next state to run. It also allows to stack/unstack states for purpose of menuing.

It owns the data common to all the states such as:
  • self.settings: the game settings
  • self.gamedata: the data shared between the states
  • self.resource: the game resources
These class attributes may be useful to override:
  • settings_class : Class to handle the settings (default is BaseSettings)
  • gamedata_class : Class to handle the settings (default is BaseGamedata)
  • fist_state : Class of the first state to instantiate and run. (default is None)
  • resource_dict : name of the resource folder (default is “resource”)
  • window_title : title of the window (default is “Pygame”)
  • display_fps : display the fps rate in the window title (default is True)
This method may also be useful to override:
  • pre_run : code to run after the video mode is set and before the first state is instantiated
These methods can be called from the states or their mvc:
  • push_current_state : push the current state into the stack
  • register_next_state : register the class of the next state to instantiate and run
Some important points to know about the control creating the next state:
  • The registered state is automatically unregistered when instanciated
  • If no state is registered, the next state is poped from the stack
  • In that case, if the stack is empty, the program ends properly

To launch the game, simply call the method run.

Example:

# Create the main control
class Example(BaseControl):

    window_title = "Example v1.0"
    first_state = SomeState

    def pre_run(self) :
        pygame.mouse.set_visible(False)

# Run the main control
example = Example()
example.run()
full_reload()

Fully reload current state if it is active.

gamedata_class

alias of BaseGamedata

get_fps()

Get the current fps rate setting.

load_next_state()

Load the next state.

Note the following:
  • The registered state is automatically unregistered when instanciated
  • If no state is registered, the next state is poped from the stack
  • In that case, if the stack is empty, the program ends properly
pop_state()

Pop the last-in state from the stack.

pre_run()

Empty method to override.

This code is executed after the video mode is set and before the first state is instantiated.

push_current_state()

Push the current state into the stack.

register_next_state(state)

Register the class of the next state to instantiate and run.

Parameters:state (type) – the class of the next state to instantiate and run
reload()

Reload current state.

run()

Run the game.

static safe_exit()

Exit pygame safely.

Previous topic

Common documentation

Next topic

Controller documentation

This Page