tuxemon.tools module

Do not import platform-specific libraries such as pygame. Graphics/audio operations should go to their own modules.

As the game library is developed and matures, move these into larger modules if more appropriate. Ideally this should be kept small.

class tuxemon.tools.NamedTupleProtocol(*args, **kwargs)[source]

Bases: Protocol

Protocol for arbitrary NamedTuple objects.

tuxemon.tools.assert_never(value)[source]

Assertion for exhaustive checking of a variable.

Parameters:

value (NoReturn) – The value that will be checked for exhaustiveness.

Return type:

NoReturn

tuxemon.tools.calc_dialog_rect(screen_rect)[source]

Return a rect that is the area for a dialog box on the screen.

Note

This only works with Pygame rects, as it modifies the attributes.

Parameters:

screen_rect (pygame.rect.Rect) – Rectangle of the screen.

Returns:

Rectangle for a dialog.

Return type:

pygame.rect.Rect

tuxemon.tools.cast_dataclass_parameters(self)[source]

Takes a dataclass object and casts its __init__ values to the correct type

Return type:

None

tuxemon.tools.cast_value(i)[source]
Parameters:

i (Tuple[Tuple[Type[Any] | None | Sequence[Type[Any] | None], str], Any]) –

Return type:

Any

tuxemon.tools.copy_dict_with_keys(source, keys)[source]

Return new dict using only the keys/value from keys.

If key from keys is not present no error is raised.

Parameters:
  • source (Mapping[str, TVar]) – Original mapping.

  • keys (Iterable[str]) – Allowed keys in the output mapping.

Returns:

New mapping with the keys restricted to those in keys.

Return type:

Mapping[str, TVar]

tuxemon.tools.get_cell_coordinates(rect, point, size)[source]

Find the cell of size, within rect, that point occupies.

Parameters:
  • rect (ReadOnlyRect) –

  • point (Tuple[int, int]) –

  • size (Tuple[int, int]) –

Return type:

Tuple[int, int]

tuxemon.tools.get_types_tuple(param_type)[source]
Parameters:

param_type (Type[Any] | None) –

Return type:

Sequence[Type[Any] | None]

tuxemon.tools.number_or_variable(session, value)[source]

Returns a numeric game variable by its name.

If value is already a number, convert from string to float and return that.

Parameters:
  • session (Session) – Session object, that contains the requested variable.

  • value (str) – Name of the requested variable or string with numerical value.

Returns:

Numerical value contained in the string or in the variable referenced by that name.

Raises:
  • ValueError – If value is not a number but no numeric variable with

  • that name can be retrieved.

Return type:

float

tuxemon.tools.open_choice_dialog(session, menu, escape_key_exits=False)[source]

Open a dialog choice with the standard window size.

Parameters:
  • session (Session) – Game session.

  • menu (Sequence[Tuple[str, str, Callable[[], None]]]) – Optional menu object.

  • escape_key_exits (bool) –

Returns:

The pushed dialog choice state.

Return type:

State

tuxemon.tools.open_dialog(session, text, avatar=None, menu=None)[source]

Open a dialog with the standard window size.

Parameters:
  • session (Session) – Game session.

  • text (Sequence[str]) – List of strings.

  • avatar (Optional[Sprite]) – Optional avatar sprite.

  • menu (Optional[Sequence[Tuple[str, str, Callable[[], None]]]]) – Optional menu object.

Returns:

The pushed dialog state.

Return type:

State

tuxemon.tools.round_to_divisible(x, base=16)[source]

Rounds a number to a divisible base.

This is used to round collision areas that aren’t defined well. This function assists in making sure collisions work if the map creator didn’t set the collision areas to round numbers.

Parameters:
  • x (float) – The number we want to round.

  • base (int) – The base that we want our number to be divisible by. By default this is 16.

Returns:

Rounded number that is divisible by base.

Return type:

int

tuxemon.tools.scale(number)[source]

Scale an integer by the configured scale factor.

Parameter:

number: Integer to scale.

Returns:

Scaled integer.

Parameters:

number (int) –

Return type:

int

tuxemon.tools.scale_sequence(sequence)[source]

Scale a sequence of integers by the configured scale factor.

Parameters:

sequence (TVarSequence) – Sequence to scale.

Returns:

Scaled sequence.

Return type:

TVarSequence

tuxemon.tools.show_item_result_as_dialog(session, item, result)[source]

Show generic dialog if item was used or not.

Parameters:
  • session (Session) – Game session.

  • item (Item) – Item object.

  • result (Mapping[str, Any]) – A dict with a success key indicating success or failure.

Return type:

None

tuxemon.tools.transform_resource_filename(*filename)[source]

Appends the resource folder name to a filename.

Parameters:

filename (str) – Relative path of a resource.

Returns:

The absolute path of the resource.

Return type:

str

tuxemon.tools.vector2_to_tile_pos(vector)[source]
Parameters:

vector (Vector2) –

Return type:

Tuple[int, int]