Home > Enterprise >  While Loops in Pygame, not understanding a particular True statement
While Loops in Pygame, not understanding a particular True statement

Time:12-30

I'm currently learning pygame and python in general and something has confused me a little regarding the while True statement. I'm currently just opening up a black window with a pink square on the screen. This is what the below code does.

import pygame
import sys
from pygame import surface

pygame.init()
screen = pygame.display.set_mode((1600, 900))
pygame.display.set_caption('Yohora')

test_surface = pygame.Surface((100, 100))
test_surface.fill('pink')

while True:
  for event in pygame.event.get(): 
    if event.type == pygame.QUIT: 
      pygame.quit() 
      sys.exit()

  screen.blit(test_surface, (200,100)) # Displays pink square
  pygame.display.update()

I wanted to understand this True statement a bit better and understand what exactly it was pointing at.

So I changed it to

while screen == True:

and

while pygame.init() == True:

both runs both opened the window and instantly closed it. However, when I changed it to

while pygame.init() != True:

then the window remained open, just like when I used

while True:

Intuitively, it would have made more sense to me if the opposite were true? I.E., while pygame.init() (i.e., the window is initialized) is True, do the following. However, it's saying, while the window is not initialized (i.e., not true), do the following.

I'm missing something clearly.

CodePudding user response:

pygame.init() does not return True or False. pygame.init() returns a tuple with 2 components. Therefore pygame.init() == True is always False and pygame.init() != True is always True.

CodePudding user response:

The loop while True: is an infinite loop. It will keep running its body until the program is terminated or something inside the loop breaks control out of the loop.

However, to break out of the loop nicely you would require two breaks. One for the inner loop (which processes all pending events) and then one in the outer loop. For the latter, you'd need to set a flag saying that you've seen a 'quit' event. And so it becomes a bit messy.

There is nothing intrinsically wrong with the way it's set up right now. But to demonstrate a more graceful program flow, see this:

while True:
  should_quit = False

  # Process pending events
  for event in pygame.event.get(): 
    if event.type == pygame.QUIT: 
      should_quit = True
      break

  # Are we done?
  if should_quit:
    break

  # Update
  screen.blit(test_surface, (200,100))
  pygame.display.update()

# Do game cleanup (and any of your own cleanup) here
pygame.quit() 

You can see that this pattern adds some clutter to your event loop. Notice also that I removed the sys.exit() call (which is what terminated your program while inside the loop).

If you want to remove the extra break, consider moving the display update step to happen before your event pump, and then you can change the main loop to this:

should_quit = False
while not should_quit:
  # Update
  screen.blit(test_surface, (200,100))
  pygame.display.update()

  # Process events
  for event in pygame.event.get(): 
    if event.type == pygame.QUIT: 
      should_quit = True
      break

# Do game cleanup (and any of your own cleanup) here
pygame.quit() 
  • Related