Building a game using pygame

End Goal
Starting the project
Initializing the game window
Updating the pygame window
Global constants for snake game

Initializing the pygame window

Ranvir Singh · 2 mins read ·
python pygame

Let’s start by initializing the game window so that we can start with the game dev.

Initialize the pygame module

We can start the pygame module just by calling the init function.


Open the game window

With just a few lines of code you can open the pygame window. Just copy and paste the following code into the file that we created in the last part.

import pygame


SCREEN_SIZE: tuple = 800, 600 # width, height

screen = pygame.display.set_mode(SCREEN_SIZE)

is_window_open = True

while is_window_open:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            is_window_open = False
With import pygame, we are importing all the modules of pygame package. pygame.init() is used to initialize all those models.

Then we are defining a tuple that we need to initialize the window. First up is the SCREEN_SIZE which defines the width and height of the screen window.

We set those values using set_mode and fill methods.

You should be able to see the window even with the following code for just a few seconds. But we are going to add a few more lines of code to run the window indefinitely.

is_window_open is used to run that indefinite loop. Inside the loop, we wait for the QUIT event of which occurs when you hit the close button on the created window.

Run the code

After running these few lines of code, we will be able to see the black pygame window and it will stay open till you hit that red close button.

Pygame blank empty window

About Author

Ranvir Singh

Greetings! Ranvir is an Engineering professional with 3+ years of experience in Software development.

Please share your Feedback:

Did you enjoy reading or think it can be improved? Don’t forget to leave your thoughts in the comments section below! If you liked this article, please share it with your friends, and read a few more!