Basic Pygame game structure

Prerequisites

Look at the previous posts

We use python and Pygame, and this tutorial focuses mostly on using Pygame.

The game should look like this when done

Encouragement: Don’t let Others discourage you. We all hear it. If you listen you will never achieve your goal.

Repository

Github and BitBucket both provide instructions

Create a empty repository online and then follows the instructions given by GitHub to create locally and set remote to your empty GitHub repository.

I suggest you look at the git post (if it doesn’t exist here, then try Git, Github etc. They have plenty of info). Create a repository with a “README.md” and a python/VSCode “.gitignore”.

Clone the Git

Using CMD or a terminal go to the folder you want to create your python pygame project folder in

Create your python project

substitute with your GitHub

git clone git@github.com:<your-name>/pygame-worm.git

we have our project - lets fix our folder to be able to run the code. From the new folder (pygame-worm) run this:

  • python -m venv .venv

On windows (Linux Mac read up on activating the virtual environment)

  • .venv\Scripts\activate

And finally add pygame (to the virtual environment for our project)

  • python -m pip install pygame

EDIT: above URL is old - to clone the repository for your project use

git clone https://bitbucket.org/gameexperiments/pygame-worm.git

After creating and activating your “VENV”, install pygame - this puts it in the virtual environment

python -m pip install pygame

Finally

to finish off the environment create a file called “.gitignore” in the folder and add the following

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

#
__pycache__

#
.vscode

These are files and folders we do not want.

You may also want to add an “assets” folder to it depending on your project.

The first few are common venv folder names. “.venv” is what we use.

pycache first appears when you create and include additional python files in your project.

.vscode is a folder that VSCode may or may not create, depending what you are doing with your code.

Set up and use VSCode

If VSCode is installed, from the terminal type

code .

That will open VSCode and the project folder

Check that the following extensions are installed

  • Python
  • git graph (optional - source control graph is a new thing)

if not then install and restart VSCode (close and open again) to get things working correctly.

VSCode should detect the virtual environment and activate it. It may say something like this

Now your environment is ready!

Create a game file

In VSCode select new file (first icon above the blue box in the image below) and create game.py

And paste the following code into game.py

# the pygame imports to make it usable in our game
import pygame
from pygame.locals import *


# Initialize the pygame code
pygame.init()


# set window size
size = width, height = (800, 600)
screen = pygame.display.set_mode(size)
# update the display to see what we set
pygame.display.update()

# control variable
game_running = True


# GAME LOOP. game_running is the control variable
while game_running:
    # event listeners 
    # i.e. listen to key-presses or windows closing etc.
    for event in pygame.event.get():
        if event.type == QUIT:
            # collapse the app
            game_running = False


    pygame.display.update()


# quit the pygame window
pygame.quit()

The above code is commented so you can understand.

Comments are useful for learning/teaching. It is better to write clear and clean code than to comment. Comments usually mean that your code isn’t easy to understand, but they are useful for a number of reasons. If you write the code like documentation then use the ‘///’ triple forward slash so you can generate documentation

Run it,

it will produce an empty window.

You should see a window that you can exit.

The Quit looks for the event to quit.

The game loop is what makes the game possible. It keeps checking for events and updating the screen with the current info until you quit the screen.

The display update draws the window.

We set the window size as 800 by 600.

Can’t exit the game? Close the window … No … kill it ctrl-alt-del - select task manager and click end task, or simply restart the computer. We will fix this and add pressing the ESC key once we set up input.