Utilities¶
Technical overview. Game Boy integrations need values that cross the boundary between the emulated hardware and the host application, such as button events, BCD-encoded numbers, and errors describing invalid hardware access.
Using the API. pyboy.utils provides WindowEvent values for PyBoy.send_input, BCD conversion helpers, and the exception classes
raised by the public API. The complete set of event names is listed below; use
the high-level button methods when they are sufficient.
- class pyboy.utils.WindowEvent(event)[source]¶
Bases:
objectNamespace of integer constants representing supported input and emulator events.
Pass one of these constants to
PyBoy.send_input:>>> from pyboy.utils import WindowEvent >>> pyboy.send_input(WindowEvent.PAUSE)
Just for button presses, it might be easier to use:
pyboy.PyBoy.button,pyboy.PyBoy.button_pressandpyboy.PyBoy.button_release.See WindowEvent options for the complete list of supported events.
- pyboy.utils.dec_to_bcd(value, byte_width=1, byteorder='little')[source]¶
Converts a decimal value to Binary Coded Decimal (BCD).
- Parameters:
value (int) – The integer value to convert.
byte_width (int) – The number of bytes to consider for each value.
byteorder (str) – The endian type to use. This is only used for 16-bit values and higher. See int.from_bytes for more details.
Example:
>>> from pyboy.utils import dec_to_bcd >>> f"{dec_to_bcd(30):08b}" '00110000' >>> f"{dec_to_bcd(32):08b}" '00110010'
- Returns:
The BCD equivalent of the decimal value.
- Return type:
- pyboy.utils.bcd_to_dec(value, byte_width=1, byteorder='little')[source]¶
Converts a Binary Coded Decimal (BCD) value to decimal.
- Parameters:
value (int) – The BCD value to convert.
byte_width (int) – The number of bytes to consider for each value.
byteorder (str) – The endian type to use. This is only used for 16-bit values and higher. See int.to_bytes for more details.
Example:
>>> from pyboy.utils import bcd_to_dec >>> bcd_to_dec(0b00110000) 30 >>> bcd_to_dec(0b00110010) 32
- Returns:
The decimal equivalent of the BCD value.
- Return type:
- exception pyboy.utils.PyBoyException[source]¶
Bases:
ExceptionCustom exception class for PyBoy-related errors.
This exception is raised for errors specific to the PyBoy emulator.
- exception pyboy.utils.PyBoyInternalError[source]¶
Bases:
PyBoyExceptionException raised for internal errors in the PyBoy emulator.
This exception is used to indicate errors that occur within the PyBoy emulator that are not expected to be encountered by the end user.
- exception pyboy.utils.PyBoyAssertException[source]¶
Bases:
PyBoyExceptionException is used to indicate assertion errors that occur within the PyBoy emulator.
- exception pyboy.utils.PyBoyOutOfBoundsException[source]¶
Bases:
PyBoyExceptionException raised for errors when an operation exceeds array or integer bounds in PyBoy.
- exception pyboy.utils.PyBoyNotImplementedException[source]¶
Bases:
PyBoyExceptionException raised for methods that are not yet implemented in a base class.
- exception pyboy.utils.PyBoyInvalidInputException[source]¶
Bases:
PyBoyExceptionException is used to indicate that an invalid input has been provided to a PyBoy function or method.
- exception pyboy.utils.PyBoyInvalidOperationException[source]¶
Bases:
PyBoyExceptionException raised when the user requests an invalid operation, which cannot be performed.
- exception pyboy.utils.PyBoyDependencyError[source]¶
Bases:
PyBoyExceptionException raised for errors related to missing dependencies in PyBoy.
- exception pyboy.utils.PyBoyFeatureDisabledError[source]¶
Bases:
PyBoyExceptionException raised when the user requests a feature that has been disabled.
- exception pyboy.utils.AccessError[source]¶
Bases:
PyBoyExceptionBase class that replaces an optional feature. Whenever this object is accessed, it raises a predefined exception. I.e. on missing dependencies etc.
- exception pyboy.utils.PillowImportError[source]¶
Bases:
AccessError,PyBoyDependencyErrorException raised when the Pillow library is not found.
- exception pyboy.utils.SoundEnabledError[source]¶
Bases:
AccessError,PyBoyFeatureDisabledErrorCompatibility exception for disabled sound access. Current sound accessors raise
PyBoyFeatureDisabledError.
WindowEvent options¶
The internal events are included for completeness, but applications should use the public events when sending input.