MrValdez's Blog

Game Engine Development: Pygame - Part 1

Posted on Nov. 19, 2018

Introduction


Video version

I gave a talk on how to make a game using pygame. It's one hour and can serve as a general overview of this series. You can watch the above and read these articles, or you can read these articles and pretty much ignore the video.

This series will expand on my talk (up until part 4) and will add more explanation than I could give in less than an hour.

Installation step

You need to install pygame via the pip command.

$ pip install pygame

Then we test that pygame is properly installed:

$ python
>>> import pygame
ModuleNotFoundError: No module named 'pygame'

If you received the error above, it means that pygame was not installed properly. Troubleshooting how to fix this is outside the scope of this article.

The code is OS-agnostic. The example codes should work on Windows, Linux (with a graphical UI), and Macintosh.

First step

If you did not get ModuleNotFoundError, we can go to the first step. Open your favorite text editor and type in the following code:

import pygame

resolution = (640, 480)

pygame.init()
screen = pygame.display.set_mode(resolution)

For consistency, this series will name this file "main.py". Feel free to use a different name (except "pygame.py". Don't name it this or python will get confuse on what to import).

Run the program:

$ python main.py

This should make a window of size 640x480. It will disappear if you try to interact with it.

Congratulations, you have taken the first step. This is what we call a black triangle.

The game loop

All games are on an infinite loop. Doesn't matter if you are playing a turned-based game or a real-time game. I can guarantee that it is running on an infinite game loop.

A game loop design allows for many advantages such as simplicity, modularity, and encapsulation

Before entering the game loop, we first setup (or load) the game. We've done that on the code above.

Within a game loop, we have the subroutines for input, update, and draw. Some game engines call them in a different order. But for simplicity when learning, we will be using the above order.

Every iteration (also called a game frame), we check for input from the player, update the game world, then draw the game state.

import pygame

resolution = (640, 480)

pygame.init()
screen = pygame.display.set_mode(resolution)

while True:
    pygame.display.flip()

Run the above program.

$ python main.py

The screen will stay in place now. Try to interact with it by clicking or dragging the window. It will crash. You can fix this with the three finger salute (ctrl-alt-escape) and killing the process.

To understand how the game loop works, it is important for us to start with a crash. By seeing what problems a piece of code fixes, we can understand why its there.

The problem with the current code is that we are ignoring our operating system. Our Operating System is sending our application some message:

HEY, the user clicked on this part. HEY, the user pushed the keyboard. HEY, LISTEN!

And we are irresponsible in not responding to the OS. It thinks that something is wrong. So it tells the user that we are not responding - a crash!

Let's fix that by asking the OS if they have an event for us:

import pygame

resolution = (640, 480)
isRunning = True

pygame.init()
screen = pygame.display.set_mode(resolution)


while isRunning:
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                isRunning = False

        if event.type == pygame.QUIT:
            isRunning  = False

    pygame.display.flip()

pygame.quit()

pygame.event.get() returns a list of events. We can inspect what event is passed by the OS and react to it appropriately. You can look at the list of events here.

The most important effect of the above code is that the OS won't think we've crashed anymore.

Finally, we have code to exit the game loop, let's be responsible programmers and tell the library it's time to cleanup with pygame.quit().


I'll be continuing this in the next post.

In the meantime, here's the repository on the code. Although, I do recommend typing the code instead of copy-pasting.

Categories: gamedev