Pygame - Overview



Pygame is a popular Python library used for developing video games. It is free, open source and cross-platform wrapper around Simple DirectMedia Library (SDL). Abstraction of SDL functions provided by Pygame makes development of multi-media applications using Python very easy.

Originally developed by Peter Shinners, Lenard Lindstrom, René Dudfield and others in Oct 2000, latest version of Pygame is 2.0.1, released in Dec 2020. In addition to SDL functionality, Pygame also provides other features such as vector maths, collision detection, camera and MIDI support etc. Pygame applications can be used on Android based mobile phones also.

Environment Setup

Easiest way to install Pygame on any machine is by using PIP installer that comes with standard Python distribution. Ensure that you have latest version of pip. It is recommended to install Pygame in a new virtual environment using following command −

pip3 install pygame

For Raspberri Pi, Pygame is pre-installed in raspbian Linus distribution. As far as installation on MacOS is concerned, newer versions require Pygame 2.0. Prebuilt binary packages are available for many flavours of Linux. They can be installed using respective package manager utilities.

For Ubuntu, following is the command −

sudo apt-get install pygame

For Fedora, the following is the command −

sudo yum install pygame

It is also possible to compile Pygame’s source code (available at https://github.com/pygame/pygame) and install on desired operating system with the help of respective C/C++ compiler and Python’s setuptools library.

To verify if Pygame has been successfully installed, try and import pygame package and check its version.

(pygmenv) C:\pygmenv>python
Python 3.7.4 (tags/v3.7.4:e09359112e, Jul 8 2019, 20:34:20) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pygame
pygame 2.0.1 (SDL 2.0.14, Python 3.7.4) 
Hello from the pygame community. https://www.pygame.org/contribute.html

Pygame library consists of following modules −

pygame._sdl2.touch to work with touch input
pygame.camera camera use
pygame.cdrom audio cdrom control
pygame.cursors cursor resources
pygame.display to control the display window and screen
pygame.draw drawing shapes
pygame.event interacting with events and queues
pygame.examples module of example programs
pygame.fastevent interacting with events and queues from multiple threads.
pygame.font loading and rendering fonts
pygame.freetype loading and rendering computer fonts
pygame.gfxdraw drawing shapes
pygame.image image transfer
pygame.joystick interacting with joysticks, gamepads, and trackballs.
pygame.key to work with the keyboard
pygame.locals pygame constants
pygame.mask image masks.
pygame.math vector classes
pygame.midi interacting with midi input and output.
pygame.mixer loading and playing sounds
pygame.mixer.music controlling streamed audio
pygame.mouse to work with the mouse
pygame.pixelcopy general pixel array copying
pygame.scrap clipboard support.
pygame.sndarray accessing sound sample data
pygame.sprite basic game object classes
pygame.surfarray accessing surface pixel data using array interfaces
pygame.tests unit test suite package
pygame.time monitoring time
pygame.transform to transform surfaces
Advertisements