0

recently I watch a youtube video learing pygame for 90 min I write the exactly code that they guide on the video

import pygame

WIDTH, HEIGHT = 900, 500
WIN = pygame.display.set_mode((WIDTH,HEIGHT))

def main() :
    run = True
    while run :

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

    pygame.quit()   
if __name__ == "__name__":
    main()

but the result end up with a flash screen and the program just close it self. Come with a line

PS C:\Users\Windows 10 Pro\Desktop\textpython> & "C:/python/New folder/python.exe" "c:/Users/Windows 10 Pro/Desktop/textpython/import pygame.py" pygame 2.1.3.dev8 (SDL 2.0.22, Python 3.11.0) Hello from the pygame community. https://www.pygame.org/contribute.html PS C:\Users\Windows 10 Pro\Desktop\textpython>

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Need_help
  • 31
  • 4

1 Answers1

1

You code in main() is never started really because of this block:

if __name__ == "__name__":
    main()

So all you see is effect of pygame.display.set_mode() and then program terminates. Proper condition should look like this:

if __name__ == "__main__":
    main()

Also you should move pygame.display.set_mode() to your main().

In future, you should run your code with debugger and see the flow and then peek the value of variables.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141