DEV Community

Cover image for Playing with Pygame #1: Creating a Window
Brett Horstketter
Brett Horstketter

Posted on • Updated on

Playing with Pygame #1: Creating a Window

Code for a basic 500x500 Pygame window, and the window it creates

I have been learning a bit of Python and have wanted to build something graphical and fun. And, something that wasn't a turtle jetting around the screen (as fun as that can be). This blog is really more of a notebook of my learning of Pygame, so beware, I may not know the best/prettiest/most efficient code to implement, well... anything!

In this first post we will look at the very first steps in any basic Pygame project, and by the end we will have a blank Pygame window to impress the more supportive of our friends.

What is Pygame?

Pygame is a set of Python modules tailored to making 2D games. It can be used to make polished, beautiful games and crumby, unpolished little turds.

Since I am just getting started, and by extension so are you, dear reader, I will be focusing exclusively on the later.

you can download Pygame using pip in a command prompt like this:

$ pip install -U pygame --user

Making the Window

The first thing any game needs is a window. This window will be used to display words, images, animations, and even play music!

So without further ado, lets take look at the code:



import pygame
pygame.init()

window = pygame.display.set_mode((500, 500))


Enter fullscreen mode Exit fullscreen mode

When run, this little chunk of code will do a number of things. First, we import pygame so that we can access it's many helpful functions. Then, we start running Pygame by initializing it with pygame.init().

Next, we define a 500 x 500 pixel window by invoking pygame.display.set_mode((width, height)), passing it a tuple with the width and height values both set to 500.

If we try running our Python file at this point, it will in fact open a blank 500 x 500 pixel window.

Yay! You may be telling yourself. We did it!

Well, sorry but not quite. Unfortunately, as you may quickly notice our window immediately closes.

So, what are we missing?

It turns out that in Pygame, we need to have an ongoing loop to really do anything. In this loop, we listen for key presses, update and move images around, and most importantly update the screen with all the changes that have happened in that round of the loop. This is often called a "game loop" or "event loop."

Let's add some more code to our Pygame window:



while True:
    pygame.time.delay(60)


Enter fullscreen mode Exit fullscreen mode

Let's break it down. We create an endless loop, and as we go through our loop, we pause for 1/60th of a second. This will give us a blank Pygame window with sweet 60fps of... nothing. Still though, our window now opens and actually stays open. Progress!

Unfortunately, we run into yet another issue. When we try to close this window, it will stubbornly refuse. In order to get rid of this pesky window, you will have to close the terminal that you ran your script from. Sorry!

We can fix this stubborn behavior by adding an event listener into our game loop. This will give our window the ability to respond to our frantic attempts to close it. Let's take a look at what we have to add to our event loop, just below the delay.



run = True
while run:
    pygame.time.delay(60)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

pygame.quit()


Enter fullscreen mode Exit fullscreen mode

First we need to rework our loop by setting a flag called run that we can use to exit the loop. We also add for event in pygame.even.get(): to look at all the input events that Pygame recieves, i.e. what keys and buttons are being pressed. More specifically, we want to look at the event pygame.QUIT, which is to say the event of clicking the close or 'X' button on our window. Therefore we add if event.type == pygame.QUIT: then stop running, or in snake run = False.

Once out of the loop, we want to neatly close the window, so we call pygame.quit().

Now we have a pygame window that can stay open as long as we want it to!

The complete code should look like this.



import pygame
pygame.init()

window = pygame.display.set_mode((500, 500))
pygame.display.set_caption('pygame_window')

run = True
while run:
    pygame.time.delay(60)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

pygame.quit()


Enter fullscreen mode Exit fullscreen mode

Top comments (0)