Cross-platform library for developing multimedia applications and video games in Python built on top of SDL
Essential pygame initialization, configuration, and core utilities. These functions manage the pygame lifecycle and provide access to system information and error handling.
Initialize and uninitialize pygame modules with error tracking and status checking.
def init() -> tuple[int, int]:
"""
Initialize all imported pygame modules.
Returns:
tuple[int, int]: (numpass, numfail) - Number of modules successfully/unsuccessfully initialized
"""
def quit() -> None:
"""
Uninitialize all pygame modules.
Call this before your program exits to ensure proper cleanup.
"""
def get_init() -> bool:
"""
Test if pygame is currently initialized.
Returns:
bool: True if pygame.init() has been called
"""Access and manage pygame/SDL error information for debugging and error reporting.
def get_error() -> str:
"""
Get the current error message from SDL.
Returns:
str: Current error message, empty string if no error
"""
def set_error(message: str) -> None:
"""
Set the current error message.
Args:
message (str): Error message to set
"""Access version information for pygame and its dependencies.
def get_sdl_version(linked: bool = True) -> tuple[int, int, int]:
"""
Get the version number of SDL.
Args:
linked (bool): If True, get version of linked SDL library; if False, get compiled version
Returns:
tuple[int, int, int]: (major, minor, patch) version numbers
"""
def get_sdl_byteorder() -> int:
"""
Get SDL byte order constant.
Returns:
int: SDL byte order (SDL_LIL_ENDIAN or SDL_BIG_ENDIAN)
"""
# Version module attributes
ver: str # Version string (e.g., '2.6.1')
vernum: tuple[int, int, int] # Version tuple (e.g., (2, 6, 1))
rev: str # Repository revision string
SDL: tuple[int, int, int] # SDL version tupleRegister cleanup functions to be called when pygame quits.
def register_quit(callable) -> None:
"""
Register a function to be called when pygame.quit() runs.
Args:
callable: Function to call during cleanup (takes no arguments)
"""Check which pygame modules are available and their initialization status.
# Example module status checking pattern used by all modules
import pygame.display
pygame.display.get_init() # Returns bool indicating if module is initialized
# Missing modules are replaced with MissingModule objects
# that provide helpful error messages when accessedimport pygame
import sys
# Initialize pygame - check for errors
num_pass, num_fail = pygame.init()
if num_fail > 0:
print(f"Warning: {num_fail} pygame modules failed to initialize")
# Your game code here...
# Always call quit before exiting
pygame.quit()
sys.exit()import pygame
pygame.init()
# Check for SDL errors
error_msg = pygame.get_error()
if error_msg:
print(f"SDL Error: {error_msg}")
# Set custom error (useful for debugging)
pygame.set_error("Custom error message")import pygame
print(f"Pygame version: {pygame.version.ver}")
print(f"SDL version: {pygame.version.SDL}")
print(f"Compiled with SDL: {pygame.get_sdl_version(linked=False)}")
print(f"Linked with SDL: {pygame.get_sdl_version(linked=True)}")import pygame
def cleanup_resources():
print("Cleaning up custom resources...")
# Your cleanup code here
pygame.init()
pygame.register_quit(cleanup_resources)
# cleanup_resources will be called automatically when pygame.quit() runs