Skip to main content

This will be both a continuation and a starting post. It will be the beginning of a series of blogs creating a Pygame project to create the Game Of Life. So, it will be also a continuation on my last post about the Game of Life. But first of all, I will take the chance to learn a new tool for environment management in Python. Therefore, in this first post we will set up the directory and environment of our Python project with Poetry.

Why environments?

Well, it may be the case that you, as a developer, are working on several projects at the same time. Not all projects require the same Python modules. In fact, it may be the case that different projects produce conflicts with each other.

Therefore, it is not possible to pip install your way to victory and have everything in your local computer. That is where environments come in.

By setting up a clean environment for each project, you can separate the modules that you install in each of them. So, when you start working on your ML project you can activate the environment with Pytorch, numpy and whatever. And when you are working on your other project working on analysing science experiments you can boot up your Scipy environment. The good thing is that the environments are always independent from one another and will not generate conflicts.

Poetry on Windows

Yes, sadly, my work laptop runs on Windows, so I will show the steps I followed to install and use Poetry on Windows.

First of all, open up a PowerShell and run the following command

(Invoke-WebRequest -Uri -UseBasicParsing).Content | py -

Then, add the following %APPDATA%\Python\Scripts to your PATH variable to make sure that windows knows what to do when the poetry command is executed. To check that everything has been installed properly, you can execute

poetry --version

and it should run without errors and return the poetry version that you have installed. Finally, just as a sanity measure, you can make sure that your poetry version is fully up to date.

poetry self update

Creating the Game of Life project environment

Now that we have poetry up and running, we can start our Game of Life project enviroment. First we can let poetry create the directory that we will use.

poetry new game-of-life

This will create the game-of-life directory with some predetermined files in there. We can go into that directory and add the Pygame package that we will need.

cd game-of-life
poetry add pygame

You will notice that poetry installs the module and updates the project.toml file by itself. It now contains the dependency for Pygame as well:

python = "^3.11"
pygame = "^2.4.0"

In order to see if everything worked fine, we can simply copy and paste an example taken from the Pygame docs page and run it. I created the file

# Example file showing a circle moving on screen
import pygame

# pygame setup
screen = pygame.display.set_mode((1280, 720))
clock = pygame.time.Clock()
running = True
dt = 0

player_pos = pygame.Vector2(screen.get_width() / 2, screen.get_height() / 2)

while running:
    # poll for events
    # pygame.QUIT event means the user clicked X to close your window
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    # fill the screen with a color to wipe away anything from last frame
    screen.fill("purple"), "red", player_pos, 40)

    keys = pygame.key.get_pressed()
    if keys[pygame.K_w]:
        player_pos.y -= 300 * dt
    if keys[pygame.K_s]:
        player_pos.y += 300 * dt
    if keys[pygame.K_a]:
        player_pos.x -= 300 * dt
    if keys[pygame.K_d]:
        player_pos.x += 300 * dt

    # flip() the display to put your work on screen

    # limits FPS to 60
    # dt is delta time in seconds since last frame, used for framerate-
    # independent physics.
    dt = clock.tick(60) / 1000


Now we activate the environment and run the script.

poetry shell
python ./game-of-life/

and it should open a purple screen with a red circle that we can move with the WASD keys.

Perfect, everything is set up now and we can start working on our new amazing Game of Life project.

See the rest of the blogs on my experience creating the Game of Life game in Python: