Python Game Development library providing comprehensive multimedia functionality for creating games and interactive applications.
—
Essential system functions for initializing pygame, managing modules, and handling errors. These functions form the foundation that must be called before using other pygame functionality.
Core functions that initialize and shut down pygame modules. The init() function must be called before using any other pygame functionality.
def init() -> tuple[int, int]:
"""
Initialize all pygame modules.
Returns:
tuple[int, int]: (successful_modules, failed_modules)
"""
def quit() -> None:
"""
Uninitialize all pygame modules.
Call before exiting your program.
"""
def get_init() -> bool:
"""
Check if pygame is initialized.
Returns:
bool: True if pygame has been initialized
"""Register cleanup functions to be called when pygame quits.
def register_quit(callable: callable) -> None:
"""
Register a function to be called when pygame quits.
Parameters:
callable: Function to call during pygame.quit()
"""Functions for getting and setting error messages from pygame operations.
def get_error() -> str:
"""
Get the current error message from pygame/SDL.
Returns:
str: Current error message, empty string if no error
"""
def set_error(msg: str) -> None:
"""
Set the current error message.
Parameters:
msg: Error message to set
"""Functions to get information about the underlying SDL library.
def get_sdl_version() -> tuple[int, int, int]:
"""
Get the version of SDL that pygame is using.
Returns:
tuple[int, int, int]: (major, minor, patch) version numbers
"""
def get_sdl_byteorder() -> int:
"""
Get the byte order of the SDL library.
Returns:
int: Byte order constant (LIL_ENDIAN or BIG_ENDIAN)
"""__version__: str # pygame-ce version stringclass error(RuntimeError):
"""
Main pygame exception class.
Raised by pygame functions when they encounter errors.
"""import pygame
import sys
try:
# Initialize pygame - returns (success_count, fail_count)
success, failed = pygame.init()
print(f"Successfully initialized {success} modules")
if failed > 0:
print(f"Failed to initialize {failed} modules")
# Check if initialization was successful
if not pygame.get_init():
raise pygame.error("pygame failed to initialize")
# Your game code here
# ...
except pygame.error as e:
print(f"pygame error: {e}")
print(f"SDL error: {pygame.get_error()}")
finally:
# Always quit pygame before exiting
pygame.quit()
sys.exit()import pygame
pygame.init()
try:
# Some pygame operation that might fail
surface = pygame.display.set_mode((800, 600))
except pygame.error:
# Get the specific error message
error_msg = pygame.get_error()
print(f"Display initialization failed: {error_msg}")
# Clear the error for next operation
pygame.set_error("")import pygame
def cleanup_resources():
"""Custom cleanup function"""
print("Cleaning up custom resources...")
# Clean up your resources here
pygame.init()
# Register cleanup function to be called during quit
pygame.register_quit(cleanup_resources)
# Your game code here
# ...
# cleanup_resources() will be automatically called
pygame.quit()# Byte order constants
LIL_ENDIAN: int # Little endian byte order
BIG_ENDIAN: int # Big endian byte orderInstall with Tessl CLI
npx tessl i tessl/pypi-pygame-ce