Can someone explains this code to me?

I’ve been watching a pygame tutorial on YouTube and there’s a part of the code that I don’t get. I don’t understand what is making the game window running, the only thing I see here is that when “quit” is clicked, the boolean is set to false. Screenshot_15

At the top of that screenshot is running = True, which runs when that file loads (so likely when the game loads).

Then, while running: evaluates to true and the loop initiates.

1 Like

I understand that the loop iterates because the variable above is set to true but I don’t understand how does this loop affect the rest of the game. It only has a code to set the variable to false when “quit” is clicked.

@veljkocukic There is a loop running listening for events.
It checks the event type.
If the event type is equal to QUIT which is probably a person pressing the letter q on the keyboard.
Once the correct event type is found running is changed to False.


I get that but I only see the code for finding quit event, I don’t understand how does this effect the game in other aspects: Screenshot_16

Hi @veljkocukic. That is the main game loop listening to user events or action. You can read more from

That is, in my opinion, a very good introductory site for pygame.


@veljkocukic It breaks out of the loop and probably closes the window. That is why they called it QUIT.

That code, by itself, will not affect the game in other aspects. But within that loop you would write the rest of the game logic code as well.

1 Like