pygame window won't close

Viewed 910

I'm currently following Eric Matthes's Python Crash Course (1st ed.), and am trying to make a pygame program. However, I've been having trouble closing the pygame window, and trying all the mentioned solutions in this other post haven't worked for me, unfortunately. (I'm working with Python 3.7.6, Spider 4.1.4, and pygame 1.9.6.)

My current code looks like this: (I initially followed the book but then modified it many times alone the way)

import sys

import pygame

def run_game():
    
    pygame.init()
    screen = pygame.display.set_mode((1200,800))

    pygame.display.set_caption("Alien Invasion")
    
    bg_color = (230, 230, 230)

    running = True
    
    while running:
        
        for event in pygame.event.get():
            
            if event.type == pygame.QUIT:
                
                running = False
                pygame.quit()
                sys.exit()
                
        screen.fill(bg_color)
        
        pygame.display.flip()
        
run_game() 

Might there be anything I'm missing or doing wrong?

Any help would be really appreciated!

Update:

Python always goes into "not responding" mode when I use pygame: enter image description here

1 Answers

That might help you:

import pygame
import time
(width, height) = (300, 200)
screen = pygame.display.set_mode((width, height))
pygame.display.flip()
pygame.display.set_caption("Hello World")
running = True
try:
   while running:
       for event in pygame.event.get():
           if event.type == pygame.QUIT:
               running = False
   pygame.quit()
except SystemExit:
   pygame.quit()

I got this solution from here: Closing Pygame Window

Related