Hint: You can adjust the default video playback speed in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Background and Setup

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.

Comments & Discussion

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

Chris Bailey RP Team on May 1, 2020

Hi @mikesult,

I have seen that myself sometimes, it seems to be a problem with using the virtual environment. This is the simplest route. If you are okay with foregoing the virtual environment for this, you could install pygame to your primary Python install. So without a virtual environment activated you would type python3 -m pip install pygame.

This would allow you to use it for other future game projects also. If you no longer need to use it you could later python3 -m pip uninstall pygame from your main Python setup.

If that does not work then it has to do with SDL not being installed, which is a bit more work. I hope this helps. I will look at if I should remove the suggestion for using the virtual environment in the text below the lesson.

Notes about it here, Getting Started with Pygame

mikesult on May 3, 2020

Hi Chris,

I received the same error when trying to install without the venv. I think I have a conflict with two versions of python. I have both an anaconda installation of 3.7.3 and a regular installation of 3.8.2 As a hobbiest, (sh) it happens. I still have some problem but it is an interesting adventure trying to figure out the problem.

the short story: The 3.8 version was missing the SDL library.

stackoverflow had some of the solution:

stackoverflow.com/questions/45992243/pip-install-pygame-sdl-h-file-not-found

(the long story is a little too long so I’ll omit it but I think I can continue on now.)

Become a Member to join the conversation.