State

class game_state.State

The State class which works as an individual screen.

Attributes:
state_name: str

The name of the state. Has to be unique among other states.

Added in version 1.1.

window: pygame.Surface

Deprecated since version 2.3.0:

To add class attributes to your own state system, make a base state (with your custom attributes) and make all your states inherit from it. Check the official guide for more info.

Added in version 1.0.

The main game window.

manager: StateManager

The manager to which the state is binded to.

Added in version 1.0.

classmethod __init_subclass__(*, state_name=None, eager_load=False, lazy_load=False)

Arguments you can pass while subclassing the State.

Parameters:
  • state_name (Optional[str]) –

    The name of the state. If no state_name is passed, it uses the identifier’s name.

    Added in version 1.1.

    class Game(State, state_name="GameState"): ...
    

  • eager_load (bool) –

    Automatically marks this class to be loaded eagerly.

    Added in version 2.2.

    class MainMenu(State, eager_load=True): ...
    

  • lazy_load (bool) –

    Automatically marks this class to be loaded lazily.

    Added in version 2.2.

    class PauseMenu(State, lazy_load=True): ...
    

Return type:

None

Warning

You cannot set eager_load and lazy_load both to True. You can only enable one (or none) of them.

on_setup()

This listener is only called once while being loaded into the StateManager. This is also called when reloading the State.

Deprecated since version 2.3.0:

Replaced by on_load() as it’s more explicit about it’s function and allows you to handle state reloads separately.

Added in version 2.0.

Warning

This method need not be called manually.

Return type:

None

on_load(reload)

Called when the state is loaded into the StateManager.

This listener is invoked both during the initial load of the state and when the state is reloaded.

Added in version 2.3.0.

Warning

This method need not be called manually.

Parameters:

reload (bool) –

A bool indicating whether the state is being loaded for the first time (False) or reloaded (True).

Return type:

None

on_unload(reload)

Called when the state is being unloaded from the StateManager.

This listener is invoked both during the initial load of the state and when the state is reloaded.

Added in version 2.3.0.

Warning

This method need not be called manually.

Parameters:

reload (bool) –

A bool indicating whether the state is being unloaded for the first time (False) or reloaded (True).

Return type:

None

on_enter(previous_state)

This listener is called once when a state has been switched and is entering the current state.

Added in version 2.0.

Warning

This method need not be called manually.

Parameters:

previous_state (Optional[State]) –

The state that was running previously. If there are no previous states, None is passed

Return type:

None

on_leave(next_state)

This listener is called once when the state has been switched and is exiting the current one.

Added in version 2.0.

Warning

This method need not be called manually.

Parameters:

next_state (State) –

The next state that is going to be applied.

Return type:

None

process_event(event)

To be called when an event needs to be processed.

Deprecated since version 2.3.1:

Add your own process_event in your subclasses. This is to prevent any type checking issues.

Changed in version 2.3:

Changed the type of event from pygame.Event to typing.Any

Added in version 2.0.

Note

This method needs to be called manually.

Parameters:

event (Any) –

The event object you want to consume.

Return type:

None

process_update(*args)

The main game loop method to be executed through the StateManager.

Deprecated since version 2.3.1:

Add your own process_update in your subclasses. This is to prevent any type checking issues.

Added in version 2.0.

Note

This method needs to be called manually.

Parameters:

*args (Any) –

The arguments to be passed on to the update counter.

Return type:

None