Grey
Grey

Reputation: 45

Trying to understand pygame

I am having trouble trying to grasp a couple of things inside the py game code, firstly why are there two types of quit or are these the same? for example:

pygame.QUIT 
pygame.quit()

Also I can't grasp this small piece code fully:

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

I understand the first line of code, but I don't understand event.type?, is .type a function inside of pygame or python in general? what does it do?

Upvotes: 1

Views: 163

Answers (3)

Ultimate Gobblement
Ultimate Gobblement

Reputation: 1879

 pygame.QUIT

Is an enumeration for an input that signals for the program to quit

pygame.quit()

Is a function call to unload pygame modules. According to the docs it won't actually quit the game: http://www.pygame.org/docs/ref/pygame.html#pygame.quit You could use sys.exit(0) for that.

This loop here:

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

Checks each event, if one of them has the value pygame.QUIT then some exit code should follow.

Upvotes: 2

Ikke
Ikke

Reputation: 101231

The manual for pygame.quit() says:

Uninitialize all pygame modules that have previously been initialized. When the Python interpreter shuts down, this method is called regardless, so your program should not need it, except when it wants to terminate its pygame resources and continue.

So you should normally call this method yourself.

pygame.QUIT is a so called event type. Pygame uses events to tell you something happened. The code checks each events in the queue and checks if a QUIT event happened.

event.type is something from pygame, and it tells you what type of event it is. By comparing it to pygame.QUIT, you can check if the quit() method has been called, and take steps to shutdown the game.

Upvotes: -1

gdanezis
gdanezis

Reputation: 629

pygame.QUIT is simply a constant, while pygame.quit() is a function within the pygame module that uninitializes it. The fragment of code:

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

is part of the typical control loop of a pygame program. The method pygame.event.get() returns the next event. If the type of event (an attribute of the event object) is pygame.QUIT then the application should do what is necessary to exit, including possibly calling pygame.quit().

Upvotes: 1

Related Questions