PySDL2 for Pygamers

Care to move to a newer SDL with your Pygame knowledge? Then you should know one thing or two about PySDL2 before hacking code, since it is completely different from Pygame. Do not let that fact scare you away, the basics with graphics and sound are still the same (as they are fundamental), but you will not find many similarities to the Pygame API within PySDL2.


More details, examples, etc.

Technical differences

Pygame is implemented as a mixture of Python, C and Assembler code, wrapping 3rd party libraries with CPython API interfaces. PySDL2 in contrast is written in pure Python, using ctypes to interface with the C interfaces of 3rd party libraries.

API differences


pygame sdl2
init() sdl2.SDL_Init() where appropriate
quit() sdl2.SDL_Quit() where appropriate
error No equivalent
get_error() sdl2.SDL_GetError()
set_error() sdl2.SDL_SetError()
get_sdl_version() sdl2.SDL_GetVersion()
get_sdl_byteorder() sdl2.SDL_BYTEORDER
register_quit() No equivalent planned
encode_string() No equivalent planned
encode_file_path() No equivalent planned

sdl2.ext.init() initializes only the video subsystem. By comparison, pygame.init() initializes all Pygame submodules (which includes initializing other SDL subsystems).


PySDL2 does not feature any CD-ROM related interfaces. They were removed in SDL2 and PySDL2 does not provide its own facilities.


You can find a similar class in sdl2.ext.Color. It does not feature a set_length() or correct_gamma() method, though.


PySDL2 does not feature any pre-defined cursor settings at the moment.


pygame.display sdl2
init() sdl2.ext.init()
quit() sdl2.ext.quit()
get_init() sdl2.SDL_WasInit()
set_mode() sdl2.ext.Window
get_surface() sdl2.ext.Window.get_surface()
flip() sdl2.ext.Window.refresh()
update() sdl2.ext.Window.refresh()
get_driver() sdl2.SDL_GetCurrentVideoDriver()
Info No equivalent
get_wm_info() sdl2.SDL_GetWindowWMInfo()
list_modes() sdl2.SDL_GetNumDisplayModes()
mode_ok() sdl2.SDL_GetClosestDisplayMode()
gl_get_attribute() sdl2.SDL_GL_GetAttribute()
gl_set_attribute() sdl2.SDL_GL_SetAttribute()
get_active() No equivalent
iconify() sdl2.ext.Window.minimize()
toggle_fullscreen() sdl2.SDL_SetWindowFullscreen()
set_gamma() sdl2.SDL_SetWindowBrightness()
set_gamma_ramp() sdl2.SDL_SetWindowGammaRamp.()
set_icon() sdl2.SDL_SetWindowIcon()
set_caption() sdl2.ext.Window.title
get_caption() sdl2.ext.Window.title
set_palette() sdl2.SDL_SetSurfacePalette()


Drawing primitives can be accessed through either the sdl2.SDL_RenderDraw*() and sdl2.SDL_RenderFill*() functions or the more powerful sdl2.sdlgfx module,


pygame.event sdl2
pump() sdl2.SDL_PumpEvents()
get() sdl2.SDL_PollEvent() or sdl2.ext.get_events()
poll() sdl2.SDL_PollEvent()
wait() sdl2.SDL_WaitEvent()
peek() sdl2.SDL_PeepEvents()
clear() sdl2.SDL_FlushEvents()
event_name() No equivalent
set_blocked() sdl2.SDL_EventState()
get_blocked() sdl2.SDL_EventState()
set_allowed() sdl2.SDL_EventState()
set_grab() sdl2.SDL_SetWindowGrab()
get_grab() sdl2.SDL_GetWindowGrab()
post() sdl2.SDL_PeepEvents()
Event sdl2.SDL_Event


pygame.font sdl2
init() sdl2.sdlttf.TTF_Init()
quit() sdl2.sdlttf.TTF_Quit()
get_init() sdl2.sdlttf.TTF_WasInit()
get_default_font() No equivalent planned [1]
get_fonts() No equivalent planned [1]
match_font() No equivalent planned [1]
SysFont No equivalent planned [1]
Font No equivalent planned [1]


PySDL2 does not feature direct FreeType support.


PySDL2 offers SDL_gfx support through the sdl2.sdlgfx module.


pygame.image sdl2
load() sdl2.sdlimage.IMG_Load(), sdl2.ext.load_image()
save() sdl2.surface.SDL_SaveBMP(), sdl2.sdlimage.IMG_SavePNG()
get_extended() sdl2.sdlimage.IMG_isBMP() et al.
tostring() No equivalent yet
fromstring() No equivalent yet
frombuffer() No equivalent yet


pygame.joystick sdl2
init() sdl2.SDL_Init()
quit() sdl2.SDL_Quit()
get_init() sdl2.SDL_WasInit()
get_count() sdl2.joystick.SDL_NumJoysticks()
Joystick() sdl2.joystick.SDL_Joystick and related functions


pygame.key sdl2
get_focused() sdl2.keyboard.SDL_GetKeyboardFocus()
get_pressed() sdl2.keyboard.SDL_GetKeyboardState()
get_mods() sdl2.keyboard.SDL_GetModState()
set_mods() sdl2.keyboard.SDL_SetModState()
set_repeat() Based on the OS/WM settings, no equivalent
get_repeat() Based on the OS/WM settings, no equivalent
name() sdl2.keyboard.SDL_GetKeyName()


Constants in PySDL2 are spread across the different packages and modules, depending on where they originate from.

See pygame.mixer.


pygame.mouse sdl2
get_pressed() sdl2.mouse.SDL_GetMouseState()
get_pos() sdl2.mouse.SDL_GetMouseState()
get_rel() sdl2.mouse.SDL_GetRelativeMouseState()
set_pos() sdl2.mouse.SDL_WarpMouseInWindow()
set_visible() sdl2.mouse.SDL_ShowCursor()
get_focused() sdl2.mouse.SDL_GetMouseFocus()
set_cursor() sdl2.mouse.SDL_GetCursor()
get_cursor() sdl2.mouse.SDL_SetCursor()

No such module is planned for PySDL2.


You can work with YUV overlays by using the sdl2.render module with sdl2.render.SDL_Texture objects.


You can access pixel data of sprites and surfaces directly via the sdl2.ext.PixelView class. It does not feature comparison or extractions methods.


No such functionality is available for PySDL2. Rectangles are represented via sdl2.rect.SDL_Rect for low-level SDL2 wrappers or 4-value tuples.


PySDL2 offers basic text-based clipboard access via the sdl2.clipboard module. A feature-rich clipboard API as for Pygame does not exist yet.


No such module is available for PySDL2 yet.


PySDL2 uses a different approach of rendering and managing sprite objects via a component-based system and the sdl2.ext.Sprite class. A sprite module as for Pygame is not planned.


pygame.Surface sdl2
blit() sdl2.surface.SDL_BlitSurface(), sdl2.ext.SpriteRenderSystem
convert() sdl2.surface.SDL_ConvertSurface()
convert_alpha() sdl2.surface.SDL_ConvertSurface()
copy() sdl2.surface.SDL_ConvertSurface()
fill() sdl2.surface.SDL_FillRect(), sdl2.surface.SDL_FillRects(), sdl2.ext.fill()
scroll() No equivalent planned
set_colorkey() sdl2.surface.SDL_SetColorKey()
get_colorkey() sdl2.surface.SDL_GetColorKey()
set_alpha() sdl2.surface.SDL_SetSurfaceAlphaMod()
get_alpha() sdl2.surface.SDL_GetSurfaceAlphaMod()
lock() sdl2.surface.SDL_LockSurface()
unlock() sdl2.surface.SDL_UnlockSurface()
mustlock() sdl2.surface.SDL_MUSTLOCK()
get_locked() sdl2.surface.SDL_Surface.locked
get_locks() No equivalent planned
get_at() Direct access to the pixels for surfaces can be achieved via the sdl2.ext.PixelView class
set_at() Direct access to the pixels for surfaces can be achieved via the sdl2.ext.PixelView class
get_at_mapped() No equivalent planned
get_palette() via sdl2.surface.SDL_Surface.format and the sdl2.pixels.SDL_PixelFormat.palette attribute
get_palette_at() sdl2.pixels.SDL_Palette.colors[offset]
set_palette() sdl2.surface.SDL_SetSurfacePalette()
set_palette_at() sdl2.pixels.SDL_Palette.colors[offset]
map_rgb() sdl2.pixels.SDL_MapRGB()
unmap_rgb() sdl2.pixels.SDL_GetRGB()
set_clip() sdl2.surface.SDL_SetClipRect()
get_clip() sdl2.surface.SDL_GetClipRect()
subsurface() sdl2.ext.subsurface()
get_parent() No equivalent yet
get_abs_parent() As for get_parent
get_offset() As for get_parent
get_abs_offset() As for get_parent
get_size() sdl2.ext.Sprite.size, sdl2.surface.SDL_Surface.w, sdl2.surface.SDL_Surface.h
get_width() sdl2.ext.Sprite.size[0], sdl2.surface.SDL_Surface.w,
get_height() sdl2.ext.Sprite.size[1], sdl2.surface.SDL_Surface.h
get_rect() No equivalent planned
get_bitsize() sdl2.pixels.SDL_PixelFormat.BitsPerPixel
get_bytesize() sdl2.pixels.SDL_PixelFormat.BytesPerPixel
get_flags() sdl2.surface.SDL_Surface.flags
get_pitch() sdl2.surface.SDL_Surface.pitch
get_masks() sdl2.pixels.SDL_PixelFormat.Rmask, …
get_shifts() sdl2.pixels.SDL_PixelFormat.Rshift, …
get_losses() sdl2.pixels.SDL_PixelFormat.Rloss, …
get_bounding_rect() No equivalent planned
get_view() sdl2.ext.PixelView
get_buffer() sdl2.ext.PixelView or sdl2.surface.SDL_Surface.pixels


2D and 3D pixel access can be achieved via the sdl2.ext.PixelView class in environments without numpy. Simplified numpy-array creation with direct pixel access (similar to pygame.surfarray.pixels2d() and pygame.surfarray.pixels3d()) is available via sdl2.ext.pixels2d() and sdl2.ext.pixels3d().


pygame.time sdl2
get_ticks() sdl2.timer.SDL_GetTicks()
wait() sdl2.timer.SDL_Delay()
delay() sdl2.timer.SDL_Delay()
Clock No equivalent planned


The are no transformation helpers in PySDL2 at moment. Those might be implemented later on via numpy helpers, the Python Imaging Library or other 3rd party packages.


pygame.version sdl2
ver sdl2.__version__
vernum sdl2.version_info


[1](1, 2, 3, 4, 5) Check for an easy to use system font detection module