When is pygame.init() needed?

Viewed 4506

I am studying pygame and in the vast majority of tutorials it is said that one should run pygame.init() before doing anything. I was doing one particular tutorial and typing out the code as one does and noticed that in example after example there is no pygame.init() and no other explicit initialisation of any modules.

For instance, the following works (for me, at least) without any issues:

import pygame

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

while True:

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

Just wondering how to find out exactly what is initialised when pygame.init() is run and what is initialised when pygame.init() is not run.

2 Answers

You can check the number of modules by:

numpass, numfail = pygame.init()

pygame.init() will initialize for example the joystick, font or mixer modules. The scrap module will require a separate init. And the midi module will need a separate init and import.

pygame.init()

print(pygame.font.get_init())  # True
print(pygame.joystick.get_init())  # True
print(pygame.scrap.get_init())  # False
print(pygame.midi.get_init())  # AttributeError

For midi:

import pygame.midi

pygame.midi.init()
print(pygame.midi.get_init())  # True

For scrap:

pygame.scrap.init()
print(pygame.scrap.get_init())  # True
Related