Use the quit() Method in Pygame

Maxim Maeder May 21, 2022
Use the quit() Method in Pygame

This tutorial will teach you what the pygame.quit() method does and how to use it.

The code displayed here is not the complete code for a valid Pygame window. If you are interested in a bare-bones working framework, consult this article.

Use the pygame.quit() Method

When we want to stop our game and then quit the window, we can use the pygame.quit() method. This method will uninitialize all Pygame modules.

As the documentation states, this function will automatically be called at the end of the program, but it is safe (and advised) to call it multiple times. This method won’t stop the program, just Pygame, but we can let the program run itself out like any normal Python file.

Often, you will see it used this way.

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

As you can see, this code will loop through all the events that occurred in this frame, and if the event is pygame.QUIT, it will call the pygame.quit() method. This event is called when the red x at the top right of the window is pressed.

As you can notice, there is another function, sys.exit(). This function will kill the current process; it is unnecessary, but it is often used to be sure.

Now you know what pygame.quit() does and how to use it.

Author: Maxim Maeder
Maxim Maeder avatar Maxim Maeder avatar

Hi, my name is Maxim Maeder, I am a young programming enthusiast looking to have fun coding and teaching you some things about programming.


Related Article - Pygame Method