Chapter 2 of 1225 min
Set up your project
In this chapter you will:
- install the tools every Python developer uses
- download the starter project and install pygame
- run your first file
Install three tools
- Python 3.10 or newer from python.org. On Windows, tick Add python.exe to PATH on the first screen of the installer.
- VS Code from code.visualstudio.com. Once it's open, install the Python extension from the Extensions panel.
- Git from git-scm.com. Git saves snapshots of your code, so you can always go back, and later put your game on GitHub.
Download the starter project
Open a terminal (on Windows, PowerShell; on a Mac, Terminal), go to the folder where you keep your work, and run:
git clone https://github.com/jrcodex-learning/pixel-run.gitIn VS Code, choose File → Open Folder and open the new pixel-run folder. Then open a terminal inside VS Code with Terminal → New Terminal. From now on, type commands there.
The folder holds just a README.md and a .gitignore (a list of files Git should never save). There is no code yet. You'll write all of it.
Make a virtual environment
A virtual environment is a private set of Python packages for one project, so what you install here can't break another project. Make one called venv:
python -m venv venvThen switch it on. On Windows:
venv\Scripts\activateOn macOS or Linux:
source venv/bin/activateThe start of your terminal line now shows (venv). Switch it on again each time you open a new terminal.
Call it venv exactly, without a dot in front. In chapter 11 the tool that puts your game online skips a folder with that name; any other name would get packed into your game. On Windows, if python isn't found, try py. If PowerShell says running scripts is disabled, run Set-ExecutionPolicy -Scope CurrentUser RemoteSigned once, then try again.
Install pygame
pip install pygame-cepygame-ce is the actively maintained edition of pygame (ce stands for community edition). You still write import pygame in your code.
Check it works by running a little game that comes with it:
python -m pygame.examples.aliensA window opens with a small shooting game (arrow keys and space). Close it when you've had a look.
Run your first file
Create a file called main.py in the project folder:
import pygame
print("Ready to build Pixel Run!")Run it:
python main.pyYou should see
pygame-ce 2.5.8 (SDL 2.32.10, Python 3.12.1)
Ready to build Pixel Run!pygame prints its own version line when it's imported. Your numbers may differ; that's fine.
Save your progress
Tell Git who you are (only once, ever):
git config --global user.name "Your Name"
git config --global user.email "you@example.com"Then save a snapshot, called a commit:
git add .
git commit -m "Chapter 2: set up"You'll commit at the end of every chapter. By the end, your Git history shows the whole game growing, step by step.
Stuck, or want to compare? The finished code for this chapter is tagged ch02: run git diff ch02, or see it on GitHub.