Home

How to Quit PyGame

 

As a simple example of closing the window of pygame, I wrote the following script in Listing 1

 

Listing 1.

 

import sys

import pygame

 

class MyWindow:

    def __init__(self):

        pygame.init()

        self.screen = pygame.display.set_mode((600,400))

 

    def run(self):

        while True:

            for event in pygame.event.get():

                if event.type == pygame.QUIT:

                    pygame.quit()

                    print(pygame.__version__)

                    print(sys.version_info)

                    sys.exit()

            pygame.display.flip()

 

       

if __name__ == '__main__':

    mw = MyWindow()

    mw.run()

 

 

Run the script in Listing 1, a window like Figure 1 will be shown.

Figure 1

 

Click the Windows close button, then the window closed.

On the terminal window, the following messages were displayed on my PC.

 

pygame 2.5.2 (SDL 2.28.3, Python 3.12.1)

Hello from the pygame community. https://www.pygame.org/contribute.html

2.5.2

sys.version_info(major=3, minor=12, micro=1, releaselevel='final', serial=0)

 

 

Home