What is PyGame and where did it come from? In this lesson, you’ll explore some of the background of pygame
and how to set it up on your system.
pygame
is a Python wrapper for the SDL library, which stands for Simple DirectMedia Layer. SDL provides cross-platform access to your system’s underlying multimedia hardware components, such as sound, video, mouse, keyboard, and joystick.
pygame
started life as a replacement for the stalled PySDL project. The cross-platform nature of both SDL and pygame
means you can write games and rich multimedia Python programs for every platform that supports them!
For this tutorial, the demonstrations shown will be done on a Macintosh computer, and the Python environment is setup with a virtual environment. To create a virtual environment on macOS or Windows and activate it, you would use these commands from your terminal:
$ mkdir PyGame
$ cd PyGame
$ python3 -m venv game_env
$ # macOS virtual environment activation
$ source game_env/bin/activate
$ # Windows virtual environment activation
$ game_env/Scripts/activate
To install pygame
into your virtual environment or your platform, use the pip
command:
(game_env) $ python3 -m pip install pygame
You can verify the install by loading one of the examples that comes with the library:
(game_env) $ python3 -m pygame.examples.aliens
If a game window appears, then pygame
is installed properly! If you run into problems, then the GettingStarted guide outlines some known issues and caveats for all platforms.
mikesult on May 1, 2020
Hi Christopher,
I ran into an error trying to install pygame For context: I’m on a macbook pro. I was able to setup the venv and when I entered ‘python’ to check the version it returned “Python 3.8.2 …’
Then after I activated the virtual env I tried to install pygame with
python3 -m pip install pygame
I received a very long error message (over 200 lines) which I didn’t understand but around line 212 of the error message I did see this part which seems like it could be the source of the problem
” In file included from src_c/pygame.h:32: src_c/_pygame.h:216:10: fatal error: ‘SDL.h’ file not found #include <SDL.h> “
Any ideas as to how to solve this problem?
I know that in the past I’ve run some pygame examples I’ve found on the internet but it wasn’t in a venv.
BTW, I’m enjoying the Real Python podcast you are hosting.
Mike Sult