Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Not sure that anybody would help but please try your best! (Python Pygame)

+4 votes
asked Jan 11, 2023 by codenxn (1,350 points)

So I'm learning python pygame. There is a problem called "pygame.error: video system not initialized". Here is my code:

import pygame

pygame.init()

screen = pygame.display.set_mode((800, 600))

running = True

while running:

        for event in pygame.event.get(): #There is the error

                if event.type == pygame.quit()

                        running = False.

I know nobody would help but at least I tried. I  search thru the internet and still didn't found the error. I even found somebody on stack overflow, who asked the same question as me, everybody had a different answer and none of them where working. I would really appreciate your help! If you can't help, thanks for your attention tho.

1 Answer

0 votes
answered Jan 12, 2023 by Lorik Bajgora (140 points)

The error message "pygame.error: video system not initialized" is typically caused by not initializing the pygame library before trying to use it. In your code, you have called pygame.init(), which should initialize the library and allow you to use it.

The problem may be caused by a problem with the pygame.event.get() line. Specifically, it seems that you are missing the colon at the end of the if event.type == pygame.quit() line. This means that the code inside the if block will never be executed, and the running loop will never exit.

You should also be comparing event.type to pygame.QUIT, not calling pygame.quit(). This means you should replace if event.type == pygame.quit() with if event.type == pygame.QUIT:

Here's the corrected code:

import pygame pygame.init() screen = pygame.display.set_mode((800, 600)) running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False

It's always good practice to close the game window to avoid getting any window handle errors, So you can also use pygame.quit() after the while loop ends to close the window and free up resources.

pygame.quit()

Try running this corrected code, and let me know if you still encounter any issues.

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...