pygame.error: display Surface quit
In this code:
import sys
import pygame
def run_Game():
# Инициализирует игру
pygame.init()
screen = pygame.display.set_mode(size=(1280, 800))
pygame.display.set_caption("Alien Invasion")
bg_color = (230, 230, 230)
# Основной цикл игры
while True:
# Отслеживание событий клавиатуры и мыши
for event in pygame.event.get():
if event.type == pygame.quit():
sys.exit()
screen.fill(bg_color)
pygame.display.flip()
run_Game()
Line:
screen.fill(bg_color)
Returns an error: pygame. error: display Surface quit
But if you remove it, then:
pygame.display.flip()
Returns the error: pygame. error: video system not initialized
1 answers
Referring to the documentation, pygame.quit()
- Cancels initialization of all pygame modules that were previously initialized. In your case, you should use pygame.QUIT
. here is the event documentation.
Also, referring to the documentation, calling sys. exit() will cause an error SystemExit
, in the case of pygame, it is recommended to create a variable run
, and change its value. This approach will be cleaner and more correct.
As a result, your code should look like this so:
import pygame
def run_Game():
# Инициализирует игру
pygame.init()
screen = pygame.display.set_mode(size=(1280, 800))
pygame.display.set_caption("Alien Invasion")
bg_color = (230, 230, 230)
run = True
# Основной цикл игры
while run:
# Отслеживание событий клавиатуры и мыши
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = not run
screen.fill(bg_color)
pygame.display.flip()
run_Game()
If you think that this option is not suitable for you, here is a solution with sys.exit
:
import sys
import pygame
def run_Game():
# Инициализирует игру
pygame.init()
screen = pygame.display.set_mode(size=(1280, 800))
pygame.display.set_caption("Alien Invasion")
bg_color = (230, 230, 230)
# Основной цикл игры
while True:
# Отслеживание событий клавиатуры и мыши
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit(0)
screen.fill(bg_color)
pygame.display.flip()
run_Game()