-1

Trying to design and call open a basic customized pygame window that pops up right after the program starts. The window I'm producing gets minimized by default instead of just opening. It's also not updating the color, and it immediately crashes when I open the tab that it's in.

# I'm running Windows 10 with Spyder (Python 3.9), if that matters
# This is the entire code:

import pygame

WIDTH, HEIGHT = 900, 500
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Space Shooter Friends (Version 1.0)")

# Color presets
WHITE = (255,255,255)
BLACK = (0,0,0)

# Event loop
def main():
   run = True
    
   while run:
       for event in pygame.event.get():
           if event.type == pygame.QUIT:
               run = False
       WIN.fill(WHITE) 
       pygame.display.update()
   pygame.quit()

# Ensure the game only executes in the file named: space_shooter.py
if __name__ == "__space_shooter__":
   main()   

For context, I'm a beginner level student just trying to generate a basic white display for now, with dimensions: 9ooW x 500H, and centered ideally so that the pygame window's center is superimposed onto the center of my computer screen. I want this window to pop up as soon as the program starts running, and it should stay open for an indefinite amount of time, or until exited from with the X icon.

It seems to be producing the window right away as intended, but it places it into a minimized tab instead of an opened window display for some reason. The window pops open if I click on the tab, but it's filled in with black regardless of what values I insert into WIN.fill(R,B,G) as arguments. Also, the program immediately becomes non-responsive with the message: (Not responding) next to the game's title (at the top of the pygame window, not in the Spyder terminal).

Seems like it's not able to update for some reason, possibly causing it to crash, but I'm not really sure. I'm not getting any runtime or syntax errors from Python, but I do get a "Python is not responding" message from Windows in the pygame window as well as whenever I try to close the window using the X icon. Any help is much appreciated!

Rabbid76
  • 202,892
  • 27
  • 131
  • 174

1 Answers1

1

The problem is in the following line:

if __name__ == "__space_shooter__":

The __name__ variable will not contain the file name of the current script. If the script is ran directly, it will contain "__main__". If the script is imported by another script, it will contain the file name of that other script.

In order to check the script's file name, which you according to the comment wanted to do, you have to use the __file__ variable. The only problem is that the __file__ variable does not only containt the file name, but also the file path and the file extension. That's why I would use the str.endswith function, which checks whether a certain string ends with a given string. There are also some more complicate and reliable ways, but I will leave it to this:

if __file__.endswith("space_shooter.py"):

However, it is more common to check whether the current file is being ran directly instead of being imported from another file. This allows other files to import and use the functions and classes present in the file, without that everything in the file is ran too.

For this, you have to use the __name__ variable:

if __name__ == "__main__":

As said above, the __name__ variable will contain "__main__" when the file is ran directly. Thus we can compare __name__ with it to know whether the file is ran directly or not.

For more information on the __name__ variable, there is more explanation and a useful example.

The_spider
  • 1,202
  • 1
  • 8
  • 18