Can someone explains this code to me?

Hello,
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.

2 Likes

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
https://inventwithpython.com/pygame/chapter2.html

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

2 Likes

@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