Before we jump into building a game, we will need to setup PyGame. Since PyGame requires Python, we will need to ensure it’s installed first. This tutorial series assumes you will be using Python 3. You can download and install Python 3 from the official website.
If you’re running Linux, ensure the python3-pip package is installed.
Once you have Python installed, we can setup PyGame. Open your command prompt or terminal and run:
pip3 install pygame
This command should install everything we need to get started with PyGame. If you’re having issues with the installation, I would recommend taking a look at the Getting Started guide on the PyGame website.
Creating a Project Directory and File
Next, create a folder for your project. Find the location where you want to store your project, and make a folder there. Name the folder anything you like. Enter the folder, then create a new file. I’m going to name mine game.py, though you can name yours whatever you wish. Make sure the file extension is .py though. The .py file extension indicates a Python file. This is important when we go to run the script. For the purposes of this series, I’ll refer to this main file as game.py. Open our game.py script with your favorite text/code editor. Personally, I love Visual Studio Code for easy debugging and code completion. If you’re using another editor, I’ll explain how to run your project via the command line. Once you’ve opened the game.py file, add the following:
import pygame pygame.init()
Running the Game
To run the game, ensure you’re in our project directory in the command line. Then, type python3 game.py. The game should then compile to bytecode and be interpreted. It’s worth noting if you’re using Visual Studio Code, you can go to Debug > Start Debugging to use the Python debugger instead of running the script with Python via the terminal.
At this point, we have a basic PyGame set up! If you found this guide useful, you would probably find the rest of this series helpful too. You can receive news about our latest tutorials and courses we publish, you can subscribe to our newsletter. If you found this tutorial helpful, and would like to help us out, sharing this guide on your favorite social media platform would be appreciated.Tags: guide, Python, setup
Categorised in: PyGame Basics
This post was written by Jared York