pygame.error: video system not initialized
You haven't called pygame.init()
anywhere.
See the basic Intro tutorial, or the specific Import and Initialize tutorial, which explains:
Before you can do much with pygame, you will need to initialize it. The most common way to do this is just make one call.
pygame.init()
This will attempt to initialize all the pygame modules for you. Not all pygame modules need to be initialized, but this will automatically initialize the ones that do. You can also easily initialize each pygame module by hand. For example to only initialize the font module you would just call.
In your particular case, it's probably pygame.display
that's complaining that you called either its set_caption
or its flip
without calling its init
first. But really, as the tutorial says, it's better to just init
everything at the top than to try to figure out exactly what needs to be initialized when.
pygame.error: video system not initialized
You should stop your main loop when you want to exit the game.
My suggestions, either of
- call
exit()
afterpygame.quit()
- set
finish = True
andstart = False
(though due to some indentation issues with your pasted code it's not possible to tell that this would actually work)
You should call pygame.quit()
only when you want to terminate your Python session.
My suggestion: if you want to call game()
more than once, e.g. in an interactive session, you should remove the call to pygame.quit()
inside game()
. This function uninitializes pygame and naturally all attempts to call its functionality will fail then.
why do I get the error: pygame.error: video system not initialized
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()
pygame.display.update()
When the event loop happens, if you close the window, you call pygame.quit(), which quits pygame. Then it tries to do:
pygame.display.update()
But pygame has quit, which is why you are getting the message.
separate the logic out. Events in one function or method, updating in another, and drawing and updating the display in another to avoid this.
Pygame error: video system not initialized, tried everything
Remove the pygame.quit()
calls. If you call pygame.quit()
, the pygame module will deinitialize; and if you keep using it afterwards, it will throw this error.
So instead of
if event.type == pygame.QUIT:
pygame.quit()
just return from the function
if event.type == pygame.QUIT:
return
and stop calling the menu
function recursively.
pygame.error: video system not initialized. pygame.init() already called
Just add sys.exit(0)
and the end and you are done
Here is some info from official document
pygame.quit() uninitialize all pygame modules quit() -> None
Uninitialize all pygame modules that have previously been initialized.
When the Python interpreter shuts down, this method is called
regardless, so your program should not need it, except when it wants
to terminate its pygame resources and continue. It is safe to call
this function more than once: repeated calls have no effect.Note, that pygame.quit()uninitialize all pygame modules will not exit
your program. Consider letting your program end in the same way a
normal python program will end.
here is a sample code for you.
import pygame
import sys
pygame.init()
pygame.display.set_mode((900, 500 ) )
while True :
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit(0)
pygame.error: video system not initialized
You haven't called pygame.init()
anywhere.
See the basic Intro tutorial, or the specific Import and Initialize tutorial, which explains:
Before you can do much with pygame, you will need to initialize it. The most common way to do this is just make one call.
pygame.init()
This will attempt to initialize all the pygame modules for you. Not all pygame modules need to be initialized, but this will automatically initialize the ones that do. You can also easily initialize each pygame module by hand. For example to only initialize the font module you would just call.
In your particular case, it's probably pygame.display
that's complaining that you called either its set_caption
or its flip
without calling its init
first. But really, as the tutorial says, it's better to just init
everything at the top than to try to figure out exactly what needs to be initialized when.
pygame error: video system has not initialized
Your culprit is this line:
if event.type == pygame.quit():
Instead of checking for the QUIT
event, you're calling the pygame.quit()
function the first time your program enters the game loop, and the function uninitializes your pygame
state, which then causes your game to crash the next time you try to access any pygame
state.
What you want is to check if you got the pygame.QUIT
event.
import pygame
pygame.init()
screen = pygame.display.set_mode((800,600))
running = True
while running:
for event in pygame.event.get():
# this should be the event pygame.QUIT
# and not the function pygame.quit()
if event.type == pygame.QUIT:
running = False
Related Topics
How to Scroll Frame Using Mouse Wheel & Adding Horizontal Scrollbar
Convert Rgba Png to Rgb with Pil
Is the Server Bundled with Flask Safe to Use in Production
How to Escape Special Characters of a String with Single Backslashes
What Happens When a Module Is Imported Twice
How to Do Multiple Arguments to Map Function Where One Remains the Same
How to Split Strings into Text and Number
How to Sort a List with Two Keys But One in Reverse Order
How to Escape Strings for SQLite Table/Column Names in Python
Paramiko Ssh Die/Hang with Big Output
Check If Two Unordered Lists Are Equal
Virtualenv --No-Site-Packages and Pip Still Finding Global Packages
Python Enum, When and Where to Use
Is It Ever Useful to Use Python's Input Over Raw_Input
Difference Between Numpy Dot() and Python 3.5+ Matrix Multiplication @