Error: video system not initialized when using pygame - python

I keep having the error whenever I try to run this code:
import pygame
pygame.init()
print(pygame.display.Info())
WIDTH, HEIGHT = 900, 500
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("First Game")
def main():
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.quit:
run = False
pygame.quit()
if __name__ == "__main__":
main()
I've tried adding pygame.init() but it didn't help.

You have a couple of problems here. The immediate cause of the error is that your call to pygame.quit() isn't inside the main function, so it gets called immediately when the program starts. You need to fix that:
import pygame
pygame.init()
print(pygame.display.Info())
WIDTH, HEIGHT = 900, 500
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("First Game")
def main():
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.quit:
run = False
pygame.quit()
if __name__ == "__main__":
main()
This will allow your program to run...but it's never going to quit. You're comparing event.type == pygame.quit, but pygame.quit is a function; this comparison will always be False. You need to compare it to pygame.QUIT, which is an integer value that will match event.type as expected:
import pygame
pygame.init()
print(pygame.display.Info())
WIDTH, HEIGHT = 900, 500
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("First Game")
def main():
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()
if __name__ == "__main__":
main()

Related

PyGame background color is not changing

I cannot find, what is wrong with background, even with debugger it is not doing nothing
#importing
from turtle import width
import pygame
#resolution
pygame.display.set_caption("Tanks")
WIDTH, HEIGHT = 900, 500
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
#background color (my problem)
def draw_window():
WIN.fill((255, 0, 0))
pygame.display.update()
#main functions
def main():
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()
#game start
if __name__ == "__main__":
main()
You have to call draw_window in the application loop:
#importing
import pygame
#resolution
pygame.display.set_caption("Tanks")
WIDTH, HEIGHT = 900, 500
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
clock = pygame.time.Clock()
#background color (my problem)
def draw_window():
WIN.fill((255, 0, 0))
pygame.display.update()
#main functions
def main():
run = True
while run:
clock.tick(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
draw_window()
pygame.quit()
#game start
if __name__ == "__main__":
main()
The typical PyGame application loop has to:
limit the frames per second to limit CPU usage with pygame.time.Clock.tick
handle the events by calling either pygame.event.pump() or pygame.event.get().
update the game states and positions of objects dependent on the input events and time (respectively frames)
clear the entire display or draw the background
draw the entire scene (blit all the objects)
update the display by calling either pygame.display.update() or pygame.display.flip()
When you write a function, it is just some instructions on memory, it doesn't run unless you call it. This is the case. You write a set of instructions, draw_window, but you never call it. What you need to do is before the game loop, call draw_window. So the code would be something like this.
#importing
from turtle import width
import pygame
#resolution
pygame.display.set_caption("Tanks")
WIDTH, HEIGHT = 900, 500
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
#background color (my problem)
def draw_window():
WIN.fill((255, 0, 0))
pygame.display.update()
#main functions
def main():
run = True
while run:
draw_window()
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()
#game start :)
if __name__ == "__main__":
main()
This way the result will be a red window which is rgb(0, 0).

pygame instantly quits window

Why is the window instantly closing?
I am trying to follow this tutorial so far everything is a coppy but it doesnt work
https://www.youtube.com/watch?v=jO6qQDNa2UY
there is lines under
pygame.QUIT():
run = False
import pygame
WIDTH, HEIGHT = 900, 500
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
def main():
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT():
run = False
pygame.quit()
if __name__ == "__main_":
main()
Not a python guy, but I think the last line should be
if __name__ == "__main__":
main()
BlackBeans is on the correct track about the typo in your "if" statement. Also, though, there needs to be a "pygame.init()" call at the beginning of the program as well. Plus "pygame.QUIT()" actually should be "pygame.QUIT". With those bits of information, the following code snippet presents a black screen until the window is closed.
import pygame
pygame.init() # This is needed
WIDTH, HEIGHT = 900, 500
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
def main():
print("Entered main")
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT: # Omit the "()"
print("Quitting")
run = False
pygame.display.update()
pygame.quit()
if __name__ == "__main__":
main()
Give that a go to see if you progress further in your game development.

Pygame - window opens but fill() doesn't work and no events detected

I'm creating a simple pygame program (PyGame 1.9.6 on Python 3.7), but some of the code inside my while loop doesn't seem to work. When I run the program, the window opens, but the screen doesn't fill with black, nor does the window close when I press "x"
import pygame
# pygame setup
pygame.init()
# Open a window on the screen
width, height = 600, 600
screen = pygame.display.set_mode((width, height))
def main():
running = True
clock = pygame.time.Clock()
BLACK = (0,0,0)
while running:
clock.tick(5) # number of loops per second
print("tick")
screen.fill(BLACK)
for event in pygame.event.get():
print("event detected")
if event == pygame.QUIT:
running = False
pygame.display.update()
main()
In the console, "tick" appears like normal and "event detected" appears after pressing any keys or mouse click. I don't get any errors when I run it.
If event == pygame.QUIT: should be if event.type == pygame.QUIT:
I usually use that event like this:
if event.type == pygame.QUIT:
pygame.quit()
quit()
Just as #Telan said
if event==pygame.QUIT:
should be
if event.type==pygame.QUIT:
However to properly shutdown pygame, pygame.quit() is important to shutdown pygame modules which is the reverse of pygame.init()
While sys.exit() is used to properly shutdown the main python program.
from sys import exit
import pygame
if event.type == pygame.QUIT:
pygame.quit()
exit()
Full code is shown below. Enjoy!
import pygame
from sys import exit
# pygame setup
pygame.init()
# Open a window on the screen
width, height = 600, 600
screen = pygame.display.set_mode((width, height))
def main():
clock = pygame.time.Clock()
BLACK = (0, 0, 0)
while True:
clock.tick(5) # number of loops per second
print("tick")
screen.fill(BLACK)
for event in pygame.event.get():
print("event detected")
if event.type == pygame.QUIT:
pygame.quit()
exit()
pygame.display.update()
if __name__ == "__main__":
main()

Pygame window closes right after

import pygame
WIDTH, HEIGHT = 1100, 600
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Game")
WHITE = (255, 255, 255)
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()
if __name__ == "__GAME!__":
main()
When I run this code, the pygame window closes right after while using Virtual Studio Code. While using Jupyter notebook it runs but goes unresponsive after 2-3 seconds. Both of them do not display any errors though.
I'm not sure why you're using
if __name__ == "__GAME!__"
But if I change this to
if __name__ == "__main__"
Then it works.

Every time I run my game I get pygame.error: video system not initialized

import pygame
import Selection
import Round
import Winner
import Fighting
pygame.init()
screen = pygame.display.set_mode((640, 500))
def main():
process = 0
clock = pygame.time.Clock()
keepGoing = True
while keepGoing:
clock.tick(30)
for event in pygame.event.get():
if event.type == pygame.QUIT:
keepGoing = False
if process == 0:
Selection.main()
process += 1
elif process == 1:
Fighting.main()
process += 1
pygame.quit()
if __name__ == "__main__":
main()
The error that appears is
for event in pygame.event.get():
pygame.error: video system not initialized
It runs through the program and gets to
Selection.main()
and calls that program which runs fine but when that closes this program starts back up at
clock.tick(30)
but then stops at
for event in pygame.event.get()
and throws that error. The
import Round
import Winner
Are not doing anything yet because those to programs are not written yet.
I ran your code and narrowed it down to the essentials and couldn't reproduce. Are you sure you put the pygame.quit() in the right indentation level?
I can replicate the same exception if I do this (notice pygame.quit is not indented). This would kill the pygame object and would cause the exception to be thrown upon first entering into the run loop. I labeled the execution order from 1-10 so it will be clear why it throws an exception.
import pygame #1
pygame.init() #2
screen = pygame.display.set_mode((640, 500)) #3
def main(): #7
running = True #8
while(running == True): #9
for event in pygame.event.get(): #10 pygame doesn't exist and was killed in 4.
if event.type == pygame.QUIT:
running = False
pygame.quit() #4
if __name__ == "__main__": #5
main() #6
The exception goes away when I move the pygame initialization and screen object creation inside main, as well as the pygame.quit call like so:
import pygame
def main():
pygame.init()
screen = pygame.display.set_mode((640, 500))
running = True
while(running == True):
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()
if __name__ == "__main__":
main()
This also works:
import pygame
pygame.init()
screen = pygame.display.set_mode((640, 500))
def main():
keepGoing = True
while keepGoing:
for event in pygame.event.get():
if event.type == pygame.QUIT:
keepGoing = False
pygame.quit()
if __name__ == "__main__":
main()

Categories

Resources