Getting Started with Pygame

Getting Started with Pygame

To get started with Pygame, you need to install Pygame library in your system.

Pygame requires Python, so make sure that you have Python 3.7.7 or greater, because it is much friendlier to beginners and it also runs faster than the previous versions.

Using pip tool

The best way to install Pygame in your computer is by using the pip tool. Python uses pip to install packages. Pip is installed automatically while installing Python.

Write the following code in your terminal:

python3 -m pip install -U pygame --user

We are using “- -user” flag to tell pip to install pygame in the home directory rather than installing it globally.

To see if it works, run the following command:

python3 -m pygame.examples.aliens

If something like this appears, you are ready to code. Now you can directly go to tutorials.

More info on installation

If the above method did not worked, then you can try some alternative methods below.

Wheels are available for x86 and x64 architectures on Linux and Windows, and for x64 on Mac. If pip doesn’t find a wheel for your platform, it will try to compile pygame from source (see below).

pygame requires a “newer” pip. If pygame starts compiling from source and fails, first try upgrading pip.

Windows installation:

Make sure you have installed python 3.6 or greater with the “Add python to PATH” option selected. This means that python and pip will work for you from the command line.

If you have the above information satisfied then run the code given below on your command prompt.

py -m pip install -U pygame --user

Now try,

py -m pygame.examples.aliens

If it works then you can now move to tutorials.

Error that might occur:

‘PermissionError: [WinError 5] Access is denied’

If this error occurs then, try opening your command prompt with Administrator privileges by right-clicking on the Command Prompt icon and click on ‘Run as Administrator’.

Note: This does not seem to work for Python 3.9, so use Python 3.7. Comment if any other error occurs.

Mac installation:

Recent versions of Mac OS X require pygame 2

If the above method did not work for you, then maybe you are using recent version of Mac OS X. Try running this code in your terminal:

python3 -m pip install -U pygame --user

If there are problem with ‘ then maybe that’s because of virtualenv.

More details here: details on the virtualenv issue. It does work if you create a venv with python -m venv however.

Instructions if you want to use an virtualenv (some people have trouble with the window losing focus when inside a venv).

# Create a virtualenv called 'anenv' and use it.
python3 -m virtualenv anenv
. ./anenv/bin/activate
# venvdotapp helps the python be a Mac 'app'. So the pygame window can get focus.
python -m pip install venvdotapp
venvdotapp
python -m pip install pygame

# Now try running this command
python -m pygame.examples.aliens

That’s just needed on Mac if you use virtualenv.

Anaconda Mac:

Try using,

#instead of using python, try using
pythonw
# Notice that 'w' in the end

If any error occurs, please read the MacCompile Instructions.

Unix Binary Packages:

Many Linux and BSD distributions have their own packages of Pygame. These may have an older version of Pygame than the wheels, but have been carefully tested with other packages in that distribution.

Debian/Ubuntu/Mint:

Try running the following commands.

sudo apt-get install python3-pygame
python3 -m pygame.examples.aliens

Fedora/Red Hat:

Try running the following commands.

sudo yum install python3-pygame
python3 -m pygame.examples.aliens

OpenSUSE:

Try running the following command.

sudo zypper install python3-pygame

Arch/Manjaro:

Try running the following commands.

sudo pamac install python-pygame
python -m pygame.examples.aliens

Installing from the Source:

Compilation

Compiling and installing pygame is handled by Python’s distutils. Pygame also comes with some scripts to automatically configure the flags needed to build pygame. Use the “setup.py” script to start the installation.

The first time you run the setup script, it will call the “config.py” script. This will build a “Setup” file which stores all the information needed to compile. The “config.py” will do a good job of detecting what dependencies are available and where they are located. If it isn’t perfect, it is easy to build your own, or edit the created “Setup” text file. This “Setup” file is a simple Makefile-like text file. It defines variables needed to use each dependency, and then enables all the pygame modules with found dependencies. If you have trouble compiling, you should be able to easily fix any problems inside the “Setup” file.

Running the “setup.py” script will call distutils to build and install the pygame package. Distutils actually supports a wide variety of compile and install options. running “python setup.py help” will start to show you the different options available. You can change many things like install locations, compiler to use, and more. Calling the “setup.py” script with no arguments and it will just ask you if you want the default flags needed to compile and install.

Some of the .c files are generated by Cython from .pyx files. Running “setup.py cython” will update them.

Windows Compiling info:

You can compile pygame on windows with mingw (gcc for windows) and also with visual studio. Up to date details can be found here: CompileWindows

Unix Compiling Info:

Compiling from linux shouldn’t give you any problems. One thing you must keep in mind is that most linux RPM packages separate the actual library from the “dev” files needed to compile. To build you will need to make sure the packages like “SDL-dev” are installed.You can check to see if SDL is ready to be built from by running the command sdl-config and seeing if it is found. If the sdl-config script is not on the path (or you have more than one?) Set the environment variable SDL_CONFIG to its location.

Sometimes you will have the SDL libraries installed in once location and the other SDL libraries in another. This tricks the pygame config scripts, but you can help it out by setting the environment LOCALBASE to a path prefix where the other libraries are. The common cause for this is SDL installed in /usr and other SDL libs installed in /usr/local. The command for this situation is “LOCALBASE=/usr/local python setup.py install”.

Mac OS X Compiling Info:

Instructions for compiling on Mac OS X can be found here: MacCompile

if you have anaconda installed in your system, then please deactivate base by running the following command:

conda deactivate

then run the following command:

python3 -m pygame.examples.aliens

Sources of Information:

Pygame Wiki

Thanks for reading

Keep learning


Also Read:

  • Simple Music Player Using Python
    Hello friends, do you know that there are many ways to make music players from simple to advanced levels? Here, we are presenting you with two very very basic music player in python. One of them is a simple program and the other one is a GUI program. For the simple program, we have used…
  • First game window in Pygame and Python
    Hello people, this is going to be our first tutorial in which we will learn how to create our first game window in Pygame and Python. So, the first step is to open VS Code and create a python file inside your directory. 1. Click on Create File: 2. Name your file with .py extension…
  • What is Pygame? Full Introduction
    Gaming has been fun for all of us since childhood. We all enjoy playing games. But almost all the games that we have played yet were made by someone else. But you know what would be really cool? Creating your own game. Don’t worry if you don’t know how to make one. We are here…
  • Getting Started with Pygame
    To get started with Pygame, you need to install Pygame library in your system. Pygame requires Python, so make sure that you have Python 3.7.7 or greater, because it is much friendlier to beginners and it also runs faster than the previous versions. Using pip tool The best way to install Pygame in your computer is…
  • Customize the Pygame Window|Python
    In the previous article, we learned how to create a game window using pygame and python. But it was definitely not what we all wanted. Of course, we need to Customize the Pygame Window by adding a background color, change the title, and adding an icon. So in this article, we will learn how to…
  • Display Images with Pygame|Python
    Now comes the most exciting part. In this article, we will be Display Images with Pygame. So, one of the most important things that you need to move further in this tutorial is of-course images. To download free icons, we can use Flaticon. Flaticon offers a wide catalog of free icons. When you perform a search, there will…
  • Moving an object in PyGame|Python
    Now as we have learned how to add images, we can finally bring the mechanics/movement to our game elements using pygame. So, the aim of this article is to make you learn how to make PyGame elements moving or Moving an object in PyGame. All in all, we will learn how to move an image…
  • Add Background Music and text to PyGame
    So, far we have learned how to add images and mechanics to our game. In this article, we will be learning to add Background Music and text to PyGame. So, do the following changes in the final code that we got in our previous article after adding movement. Adding text to PyGame window: Now, adding…
  • Car Race Game in PyGame Python: From Scratch
    Check the video below to know about our Car Race Game in PyGame and Python we are going to make. If you don’t know Pygame, then you should not worry, we will guide you from scratch. You just need some basic Python skills, other than Python, we will guide you with everything. Introduction to Car…
  • Overview of our First Game Project
    So, congratulations, after learning all the basic and essential concepts of our pygame series, you are finally here to get the Overview of our First Game Project In this article, we will get to know about what game we are gonna make in the next article. But before that, in this article, we will try…
  • Python Complete Setup for Pygame
    Now, it’s time to install the tools that we will use to write programs. So, we will be learning Python Complete Setup for Pygame in this article. Let’s start. 1. Installing Python first. First, we need to go to the official site of python: https://www.python.org/ Now we need to go to the downloads page of…
  • Complete Game In PyGame and Python
    Now, it’s time to create our fully featured Complete Game In PyGame and Python with all the knowledge that we have gained from this series of articles. If you have seen the overview of our game, then maybe you have already made it. If yes then congrats, or you can follow along. To create this…

Share:

Author: Ayush Purawr