leafleafleafDocy banner shape 01Docy banner shape 02Man illustrationFlower illustration

Python Pygame Window Creation and Closing

Estimated reading: 5 minutes 14 views

When we draw graphics with the turtle module, we need a window. To draw with Pygame, you also need to create a window.

Python Pygame window creation

Let’s create the simplest window first. The code is as follows.

import pygame

Running this code, you can see a blank window filled with black, as shown in the image below.

The establishment of Python Pygame window

Line 1 of the code is a simple import statement that imports the pygame module so that we can use the functions in that module in our program. In fact, all of the functions that Pygame provides for graphics, sound, and other functions are located in the pygame module.

The second line of code is to call the pygame.init() function. We call pygame.init() every time we want to use Pygame, and it always comes after importing pygame, and before calling any other Pygame functions.

Line 3 calls pygame.display.set_mode([500,400]), creates a display window 500 pixels wide and 400 pixels high, and returns a pygame.Surface object for the window, which is then stored in in a variable named windowSurface.

A Surface object is an object that represents a rectangular 2D image. We can change the pixels of the Surface object by calling the Pygame drawing function, and then display it on the screen.

Note that the Surface object returned by pygame.display.set_mode() is called the display Surface. When the pygame.display.update() function is called, any content previously drawn to the display Surface object will be displayed on the window.

Drawing on a Surface object (which only exists in computer memory) is much faster than drawing a Surface object to the computer screen. This is because modifying computer memory is much faster than modifying pixels on a monitor.

Python programs often draw multiple different content into a Surface object. In this iteration of the game loop, once everything on a Surface object has been drawn to the display Surface object, the display Surface object is drawn to the screen.

Python Pygame window closes

After running the code, you may find a problem – the window cannot be closed. Why is this?

This is because the programs created by Pygame need to detect the user’s actions and respond according to the user’s actions. So Pygame has an event loop that constantly checks what the user is doing, like pressing the keyboard, moving the mouse, or closing a window. Since we haven’t added this event loop, the window cannot be closed.

Below, we’ve added a few lines of code to the program (the highlighted lines below) that let it monitor the user’s actions. In this way, when the user clicks the close button, the window will be closed, the code is as follows.

import pygame
while Running: 
    for event in pygame.event.get(): 
        if event.type == pygame.QUIT: 
            Running =False 

First we create a variable Running and set it to True. Then to keep the Pygame event loop running, we use a while loop. When Running is True, the loop will continue. The only way to exit the loop is if the variable Running is False.

Then, a for loop is added to the while loop, which iterates over the list of Event objects returned by pygame.event.get(). The Event object has a member variable called type that tells what type of event the object represents; Pygame has a constant variable for each possible type in the pygame.locals module. The program checks whether the type of the Event object is equal to the constant pygame.QUIT, if it is, then we know that a QUIT event was generated.

The pygame module generates the QUIT event when the user closes the program’s window or when the computer closes the window and attempts to terminate all running programs. When the program detects an event of type QUIT, it sets the variable Running to False. At this point, the while loop is exited.

Then, call the pygame.quit() function, which is a function corresponding to init(), which needs to be called before exiting the program. Only then can Pygame be exited and the program terminated.

Running the above code, we will see a working Pygame window that can be closed when the user clicks the close window button.

Tip: We usually use capital letters to define constant variables, to indicate that the content of this type of variable will not be easily changed, which is a conventional way. Although the value of these constant variables can actually still be changed.Anytime the user performs an action such as pressing a keyboard or moving the mouse over the program’s window, the Pygame library creates a pygame.event.Event object to record the action, or “event”. To find out what event happened, we can call the pygame.event.get() function, which returns a list of pygame.event.Event objects.

This list of Event objects contains all events that have occurred since the last time the pygame.event.get() function was called. Typically, we use a for loop to iterate through the list of Event objects. In each iteration of this for loop, the next event object in the list of Event objects is obtained.

The list of Event objects returned by the pygame.event.get() function will be sorted according to the order in which the events occurred. If the user clicks the mouse and presses a keyboard key, the Event object for the mouse click will be item 1 in the list, and the Event object for the keyboard key will be item 2. If no events have occurred, then pygame.event.get() will return an empty list.

Leave a Comment