This section is about migrating from Pygame to Pygame2 and explains the smaller and bigger changes, which you will encounter, if you are already used to Pygame.
In Pygame2, the whole module and package layout changed. While Pygame used a ‘get all from module pygame‘ approach, Pygame2 forces you to explicitly import the required bits and pieces.
While in Pygame, you simply do a
import pygame # Import anything
in Pygame2 you have to
import pygame2 # Import the Pygame2 core
import pygame2.sdl # Import the SDL core wrapper
import pygame2.sdl.video # Import the SDL video system
import pygame2.sdlmixer # Import the SDL_mixer wrapper
...
Each Pygame2 package and module forces you to explicitly import it. While this sounds like a lot of typing work all over the place, it also lets you easily choose and deploy only the minimum of the required dependencies for your application and keeps your imports free of superfluous stuff.
Similar to the explicit imports, you also have to explicitly initialize the modules, where necessary. In Pygame, a
pygame.init ()
...
pygame.quit ()
is enough to initialize nearly all parts and shut them down at the end of the program. In Pygame2, you have to explicitly initialize the modules (not all of them, of course. Only the majority of the SDL-related ones and pygame2.freetype).
pygame2.sdl.video.init ()
pygame2.sdl.audio.init ()
pygame2.sdlmixer.init ()
...
pygame2.sdl.video.quit ()
pygame2.sdl.audio.quit ()
pygame2.sdlmixer.quit ()
Note
The pygame2.sdl modules can also be initialized using pygame2.sdl.init() and the appropriate flags.
Some modules might contain an init() and quit() method, which do not use anything (such as e.g. pygame2.openal). Those are mostly for future usage and might be required to be invoked in later version of Pygame2.
The following subsections will provide you a comparision of which Pygame module, class, method or function can be found where in Pygame2 now.
The module is not (yet) ported to Pygame2.
pygame2.sdl.cdrom.CD.cur_track and pygame2.sdl.cdrom.CD.cur_frame
pygame2.sdl.cdrom.CD.cur_track will return a pygame2.sdl.cdrom.CDTrack object with additional information about the track length, type, etc.
pygame2.sdl.cdrom.CD.tracks returns a list of pygame2.sdl.cdrom.CDTrack object, which provide additional information about the tracks.
pygame2.sdl.cdrom.CDTrack.offset
pygame2.sdl.cdrom.CDTrack.offset will return the track offset in frames.
No notable changes apply here, except that there is no replacement for pygame.Color.set_length().
pygame.cursors can be found under pygame2.sdl.cursors.
There is no 1:1 replacement for pygame.display.
Instead you have to use a mixture of pygame2.sdl.video, pygame2.sdl.wm and pygame2.sdl.gl
pygame2.sdl.event.get_app_state()
(pygame2.sdl.event.get_app_state () & pygame2.sdl.constants.APPACTIVE) == pygame2.sdl.constants.APPACTIVE
pygame.draw can be found under pygame2.sdlext.draw.
pygame2.sdl.event.get_app_state()
(pygame2.sdl.event.get_app_state () & (pygame2.sdl.constants.APPMOUSEFOCUS | pygame2.sdl.constants.APPINPUTFOCUS) ==
(pygame2.sdl.constants.APPMOUSEFOCUS | pygame2.sdl.constants.APPINPUTFOCUS)
pygame.font separates into two different modules in Pygame2, pygame2.font for font-file related tasks and pygame2.sdlttf for the SDL_ttf wrapper.
(pygame2.sdlttf.Font.style & pygame2.sdlttf.constants.STYLE_UNDERLINE) ==
pygame2.sdlttf.constants.STYLE_UNDERLINE
(pygame2.sdlttf.Font.style & pygame2.sdlttf.constants.STYLE_BOLD) ==
pygame2.sdlttf.constants.STYLE_BOLD`
(pygame2.sdlttf.Font.style & pygame2.sdlttf.constants.STYLE_ITALIC) ==
pygame2.sdlttf.constants.STYLE_ITALIC
pygame.gfxdraw can be found under pygame2.sdlgfx.
pygame.image separates into two different modules in Pygame2, pygame2.sdl.image for BMP loading and pygame2.sdlimage for the SDL_image wrapper.
pygame2.sdl.event.get_app_state()
(pygame2.sdl.event.get_app_state () & (pygame2.sdl.constants.APPINPUTFOCUS) == pygame2.sdl.constants.APPINPUTFOCUS
Constants are put into the corresponding constants module of the package, they belong to, e.g. pygame2.sdl.constants, pygame2.sdlttf.constants, etc.
pygame.mask can be found under pygame2.mask.
pygame.midi can be found under pygame2.midi.
pygame.mixer.music is handled by the pygame2.sdlmixer.Music class within the pygame2.sdlmixer module.
pygame2.sdl.event.get_app_state()
(pygame2.sdl.event.get_app_state () & (pygame2.sdl.constants.APPMOUSEFOCUS) == pygame2.sdl.constants.APPMOUSEFOCUS
The module is not (yet) ported to Pygame2.
pygame.PixelArray can be found under pygame2.sdlext.PixelArray.
No notable changes apply here.
pygame.scrap can be found under pygame2.sdlext.scrap.
pygame.sndarray can be found under pygame2.sdlmixer.sndarray.
No notable changes apply here.
pygame.surfarray can be found under pygame2.sdlext.surfarray.