Python Game Development library providing comprehensive multimedia functionality for creating games and interactive applications.
—
Comprehensive input handling for keyboard, mouse, and game controllers. Provides both real-time state checking and event-based input processing.
Real-time keyboard state checking and configuration functions.
def get_focused() -> bool:
"""
Check if display is receiving keyboard input.
Returns:
bool: True if window has keyboard focus
"""
def get_pressed() -> dict[int, bool]:
"""
Get state of all keyboard buttons.
Returns:
dict[int, bool]: Mapping of key constants to pressed state
"""
def get_just_pressed() -> dict[int, bool]:
"""
Get keys that were just pressed since last call.
Returns:
dict[int, bool]: Mapping of key constants to just-pressed state
"""
def get_just_released() -> dict[int, bool]:
"""
Get keys that were just released since last call.
Returns:
dict[int, bool]: Mapping of key constants to just-released state
"""
def get_mods() -> int:
"""
Get currently pressed modifier keys.
Returns:
int: Bitmask of modifier key constants
"""
def set_mods(int) -> None:
"""
Set modifier key state.
Parameters:
int: Bitmask of modifier keys to set as pressed
"""
def set_repeat(delay: int = 0, interval: int = 0) -> None:
"""
Set key repeat rate.
Parameters:
delay: Milliseconds before first repeat (0 to disable)
interval: Milliseconds between repeats
"""
def get_repeat() -> tuple[int, int]:
"""
Get key repeat settings.
Returns:
tuple[int, int]: (delay, interval) in milliseconds
"""
def name(key: int) -> str:
"""
Get name of key from key constant.
Parameters:
key: Key constant
Returns:
str: Human-readable key name
"""
def key_code(name: str) -> int:
"""
Get key constant from key name.
Parameters:
name: Key name string
Returns:
int: Key constant
"""Functions for handling Unicode text input for text fields and chat systems.
def start_text_input() -> None:
"""
Start Unicode text input.
Enables TEXTINPUT events and on-screen keyboards on mobile.
"""
def stop_text_input() -> None:
"""
Stop Unicode text input.
Disables TEXTINPUT events.
"""
def set_text_input_rect(rect: Rect) -> None:
"""
Set rectangle for text input composition.
Parameters:
rect: Rectangle where text composition occurs
"""Real-time mouse state checking and position functions.
def get_pressed(num_buttons: int = 3) -> tuple[bool, ...]:
"""
Get mouse button states.
Parameters:
num_buttons: Number of buttons to check
Returns:
tuple[bool, ...]: Button states (left, middle, right, ...)
"""
def get_just_pressed() -> tuple[bool, bool, bool]:
"""
Get mouse buttons that were just pressed.
Returns:
tuple[bool, bool, bool]: (left, middle, right) just-pressed states
"""
def get_just_released() -> tuple[bool, bool, bool]:
"""
Get mouse buttons that were just released.
Returns:
tuple[bool, bool, bool]: (left, middle, right) just-released states
"""
def get_pos() -> tuple[int, int]:
"""
Get mouse cursor position.
Returns:
tuple[int, int]: (x, y) position relative to window
"""
def get_rel() -> tuple[int, int]:
"""
Get relative mouse movement since last call.
Returns:
tuple[int, int]: (x, y) movement in pixels
"""
def set_pos(pos: tuple[int, int]) -> None:
"""
Set mouse cursor position.
Parameters:
pos: (x, y) position to set
"""
def set_visible(bool) -> bool:
"""
Set mouse cursor visibility.
Parameters:
bool: True to show cursor, False to hide
Returns:
bool: Previous visibility state
"""
def get_visible() -> bool:
"""
Get mouse cursor visibility.
Returns:
bool: True if cursor is visible
"""
def get_focused() -> bool:
"""
Check if display is receiving mouse input.
Returns:
bool: True if window has mouse focus
"""
def set_cursor(cursor) -> None:
"""
Set mouse cursor appearance.
Parameters:
cursor: Cursor object or system cursor constant
"""
def get_cursor() -> object:
"""
Get current mouse cursor.
Returns:
object: Current cursor object
"""
def set_relative_mode(enable: bool) -> None:
"""
Enable/disable relative mouse mode.
In relative mode, cursor is hidden and confined to window,
providing unlimited mouse movement.
Parameters:
enable: True to enable relative mode
"""
def get_relative_mode() -> bool:
"""
Get relative mouse mode state.
Returns:
bool: True if relative mode is enabled
"""Comprehensive game controller and joystick support.
def init() -> None:
"""Initialize joystick module."""
def quit() -> None:
"""Uninitialize joystick module."""
def get_init() -> bool:
"""
Check if joystick module is initialized.
Returns:
bool: True if joystick module is initialized
"""
def get_count() -> int:
"""
Get number of joysticks connected.
Returns:
int: Number of connected joysticks
"""
class Joystick:
def __init__(self, id: int):
"""
Initialize joystick object.
Parameters:
id: Joystick device ID (0 to get_count()-1)
"""
def init(self) -> None:
"""Initialize the joystick for use."""
def quit(self) -> None:
"""Uninitialize the joystick."""
def get_init(self) -> bool:
"""
Check if joystick is initialized.
Returns:
bool: True if joystick is initialized
"""
def get_id(self) -> int:
"""
Get joystick ID.
Returns:
int: Joystick ID
"""
def get_name(self) -> str:
"""
Get joystick name.
Returns:
str: Joystick device name
"""
def get_numaxes(self) -> int:
"""
Get number of axes.
Returns:
int: Number of axes on joystick
"""
def get_axis(self, axis_number: int) -> float:
"""
Get axis value.
Parameters:
axis_number: Axis index
Returns:
float: Axis value from -1.0 to 1.0
"""
def get_numbuttons(self) -> int:
"""
Get number of buttons.
Returns:
int: Number of buttons on joystick
"""
def get_button(self, button: int) -> bool:
"""
Get button state.
Parameters:
button: Button index
Returns:
bool: True if button is pressed
"""
def get_numballs(self) -> int:
"""
Get number of trackballs.
Returns:
int: Number of trackballs on joystick
"""
def get_ball(self, ball_number: int) -> tuple[int, int]:
"""
Get trackball movement.
Parameters:
ball_number: Trackball index
Returns:
tuple[int, int]: (x, y) movement since last call
"""
def get_numhats(self) -> int:
"""
Get number of hat controls.
Returns:
int: Number of hats on joystick
"""
def get_hat(self, hat_number: int) -> tuple[int, int]:
"""
Get hat position.
Parameters:
hat_number: Hat index
Returns:
tuple[int, int]: Hat position (-1, 0, 1 for each axis)
"""import pygame
pygame.init()
screen = pygame.display.set_mode((800, 600))
clock = pygame.time.Clock()
running = True
while running:
# Handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
print(f"Key pressed: {pygame.key.name(event.key)}")
# Check real-time key states
keys = pygame.key.get_pressed()
# Movement with arrow keys
if keys[pygame.K_LEFT]:
print("Moving left")
if keys[pygame.K_RIGHT]:
print("Moving right")
if keys[pygame.K_UP]:
print("Moving up")
if keys[pygame.K_DOWN]:
print("Moving down")
# Check for key combinations
if keys[pygame.K_LCTRL] and keys[pygame.K_s]:
print("Ctrl+S pressed!")
pygame.display.flip()
clock.tick(60)
pygame.quit()import pygame
pygame.init()
screen = pygame.display.set_mode((800, 600))
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1: # Left click
print(f"Left click at {event.pos}")
elif event.button == 3: # Right click
print(f"Right click at {event.pos}")
elif event.type == pygame.MOUSEMOTION:
print(f"Mouse moved to {event.pos}, relative: {event.rel}")
# Check real-time mouse state
mouse_buttons = pygame.mouse.get_pressed()
mouse_pos = pygame.mouse.get_pos()
if mouse_buttons[0]: # Left button held
print(f"Left button held at {mouse_pos}")
pygame.display.flip()
pygame.quit()import pygame
pygame.init()
pygame.joystick.init()
# Check for connected joysticks
joystick_count = pygame.joystick.get_count()
print(f"Found {joystick_count} joysticks")
joysticks = []
for i in range(joystick_count):
joystick = pygame.joystick.Joystick(i)
joystick.init()
joysticks.append(joystick)
print(f"Joystick {i}: {joystick.get_name()}")
screen = pygame.display.set_mode((800, 600))
clock = pygame.time.Clock()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.JOYBUTTONDOWN:
print(f"Controller {event.joy} button {event.button} pressed")
elif event.type == pygame.JOYAXISMOTION:
print(f"Controller {event.joy} axis {event.axis}: {event.value}")
elif event.type == pygame.JOYHATMOTION:
print(f"Controller {event.joy} hat {event.hat}: {event.value}")
# Check real-time joystick states
for i, joystick in enumerate(joysticks):
# Check axes (usually left stick X/Y, right stick X/Y, triggers)
num_axes = joystick.get_numaxes()
for axis in range(num_axes):
value = joystick.get_axis(axis)
if abs(value) > 0.1: # Dead zone
print(f"Joystick {i} axis {axis}: {value}")
# Check buttons
num_buttons = joystick.get_numbuttons()
for button in range(num_buttons):
if joystick.get_button(button):
print(f"Joystick {i} button {button} pressed")
pygame.display.flip()
clock.tick(60)
pygame.quit()import pygame
pygame.init()
screen = pygame.display.set_mode((800, 600))
font = pygame.font.Font(None, 36)
# Start text input
pygame.key.start_text_input()
text = ""
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_BACKSPACE:
text = text[:-1]
elif event.key == pygame.K_RETURN:
print(f"Entered text: {text}")
text = ""
elif event.type == pygame.TEXTINPUT:
text += event.text
# Draw text input
screen.fill((255, 255, 255))
text_surface = font.render(text, True, (0, 0, 0))
screen.blit(text_surface, (10, 10))
pygame.display.flip()
pygame.key.stop_text_input()
pygame.quit()Install with Tessl CLI
npx tessl i tessl/pypi-pygame-ce