0
# Core System and Initialization
1
2
Essential pygame initialization, configuration, and core utilities. These functions manage the pygame lifecycle and provide access to system information and error handling.
3
4
## Capabilities
5
6
### System Initialization
7
8
Initialize and uninitialize pygame modules with error tracking and status checking.
9
10
```python { .api }
11
def init() -> tuple[int, int]:
12
"""
13
Initialize all imported pygame modules.
14
15
Returns:
16
tuple[int, int]: (numpass, numfail) - Number of modules successfully/unsuccessfully initialized
17
"""
18
19
def quit() -> None:
20
"""
21
Uninitialize all pygame modules.
22
Call this before your program exits to ensure proper cleanup.
23
"""
24
25
def get_init() -> bool:
26
"""
27
Test if pygame is currently initialized.
28
29
Returns:
30
bool: True if pygame.init() has been called
31
"""
32
```
33
34
### Error Handling
35
36
Access and manage pygame/SDL error information for debugging and error reporting.
37
38
```python { .api }
39
def get_error() -> str:
40
"""
41
Get the current error message from SDL.
42
43
Returns:
44
str: Current error message, empty string if no error
45
"""
46
47
def set_error(message: str) -> None:
48
"""
49
Set the current error message.
50
51
Args:
52
message (str): Error message to set
53
"""
54
```
55
56
### Version Information
57
58
Access version information for pygame and its dependencies.
59
60
```python { .api }
61
def get_sdl_version(linked: bool = True) -> tuple[int, int, int]:
62
"""
63
Get the version number of SDL.
64
65
Args:
66
linked (bool): If True, get version of linked SDL library; if False, get compiled version
67
68
Returns:
69
tuple[int, int, int]: (major, minor, patch) version numbers
70
"""
71
72
def get_sdl_byteorder() -> int:
73
"""
74
Get SDL byte order constant.
75
76
Returns:
77
int: SDL byte order (SDL_LIL_ENDIAN or SDL_BIG_ENDIAN)
78
"""
79
80
# Version module attributes
81
ver: str # Version string (e.g., '2.6.1')
82
vernum: tuple[int, int, int] # Version tuple (e.g., (2, 6, 1))
83
rev: str # Repository revision string
84
SDL: tuple[int, int, int] # SDL version tuple
85
```
86
87
### Cleanup Management
88
89
Register cleanup functions to be called when pygame quits.
90
91
```python { .api }
92
def register_quit(callable) -> None:
93
"""
94
Register a function to be called when pygame.quit() runs.
95
96
Args:
97
callable: Function to call during cleanup (takes no arguments)
98
"""
99
```
100
101
### Module Status
102
103
Check which pygame modules are available and their initialization status.
104
105
```python { .api }
106
# Example module status checking pattern used by all modules
107
import pygame.display
108
pygame.display.get_init() # Returns bool indicating if module is initialized
109
110
# Missing modules are replaced with MissingModule objects
111
# that provide helpful error messages when accessed
112
```
113
114
## Usage Examples
115
116
### Basic Initialization
117
118
```python
119
import pygame
120
import sys
121
122
# Initialize pygame - check for errors
123
num_pass, num_fail = pygame.init()
124
if num_fail > 0:
125
print(f"Warning: {num_fail} pygame modules failed to initialize")
126
127
# Your game code here...
128
129
# Always call quit before exiting
130
pygame.quit()
131
sys.exit()
132
```
133
134
### Error Handling
135
136
```python
137
import pygame
138
139
pygame.init()
140
141
# Check for SDL errors
142
error_msg = pygame.get_error()
143
if error_msg:
144
print(f"SDL Error: {error_msg}")
145
146
# Set custom error (useful for debugging)
147
pygame.set_error("Custom error message")
148
```
149
150
### Version Checking
151
152
```python
153
import pygame
154
155
print(f"Pygame version: {pygame.version.ver}")
156
print(f"SDL version: {pygame.version.SDL}")
157
print(f"Compiled with SDL: {pygame.get_sdl_version(linked=False)}")
158
print(f"Linked with SDL: {pygame.get_sdl_version(linked=True)}")
159
```
160
161
### Cleanup Registration
162
163
```python
164
import pygame
165
166
def cleanup_resources():
167
print("Cleaning up custom resources...")
168
# Your cleanup code here
169
170
pygame.init()
171
pygame.register_quit(cleanup_resources)
172
173
# cleanup_resources will be called automatically when pygame.quit() runs
174
```