pygame.error: video system not initialized

Question:

I am getting this error whenever I attempt to execute my pygame code:
pygame.error: video system not initialized

from sys import exit
import pygame
from pygame.locals import *

black = 0, 0, 0
white = 255, 255, 255
red = 255, 0, 0
green = 0, 255, 0
blue = 0, 0, 255

screen = screen_width, screen_height = 600, 400

clock = pygame.time.Clock()

pygame.display.set_caption("Physics")

def game_loop():
  fps_cap = 120
  running = True
  while running:
      clock.tick(fps_cap)

      for event in pygame.event.get():  # error is here
          if event.type == pygame.QUIT:
              running = False

      screen.fill(white)

      pygame.display.flip()

  pygame.quit()
  exit()

game_loop()
#!/usr/bin/env python
Asked By: Badfitz66

||

Answers:

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.

Answered By: abarnert

You get an error because you try to set the window title (with set_caption()) but you haven’t created a pygame window, so your screen variable is just a tuple containing the size of your future window.

To create a pygame window, you have to call pygame.display.set_mode(windowSize).

Good luck 🙂

Answered By: Julien Dubois

You have to add:

pygame.init()

Before you quit the display you should stop the while loop.

Answered By: user11425419
  1. You need to initialized pygame using this command pygame.init

    If the problem is not solved then following this step
    
  2. this problem happens when you using a beta version.

  3. so my suggestion is please use the new, old version(If now lunch 3.8 python, you
    need to install python 3.7)

  4. Now goto python terminal and install pygame (pip install pygame)

  5. now the problem is solved…

Answered By: Rajendro Sau
  1. If you doing pygame.init() then solve the problem video system initialized.
    but you get the next error like:

(AttributeError: tuple object has no attribute 'fill') this.


  1. this problem is solving when you doing this
screen = pygame.display.set_mode((600, 400))

but not doing like

screen = screen_width, screen_height = 600, 400

  1. Then the full problem is solved.
Answered By: Rajendro Sau

If you using class for your pygame window don’t use pygame.init() in your class. Use pygame.init() at below libraries.

Answered By: ForceVII

I made some modifications to your code:

import os
import sys
import math
import pygame
import pygame.mixer
from pygame.locals import *

pygame.init()
black = 0, 0, 0
white = 255, 255, 255
red = 255, 0, 0
green = 0, 255, 0
blue = 0, 0, 255

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

clock = pygame.time.Clock()

pygame.display.set_caption("Physics")


while True:
    clock.tick(120)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
    screen.fill(green)

    pygame.display.flip()
Answered By: MauroDev

Changing code to this, avoids that error.
while running:
clock.tick(fps_cap)

for event in pygame.event.get(): #error is here
    if event.type == pygame.QUIT:
        running = False
        pygame.quit()
if running:
     screen.fill(white)
     pygame.display.flip()
Answered By: Ramesh Sridharan

You just need to add

exit()

To stop running code
example :

for event in pygame.event.get(): #error is here
    if event.type == pygame.QUIT:
        running = False
        exit() # Solution
Answered By: ayoub ech-chetyouy

#this will fool the system to think it has video access

import os
import sys
os.environ["SDL_VIDEODRIVER"] = "dummy"
Answered By: Harsh Vasisht

For me, it was a problem because I didn’t set pygame.quit() out of the loop at the end.

Answered By: LaTzy

I’m having this same error, but none of the fixes I see work. Here’s my code:

import pygame 
pygame.init()

FPS = pygame.time.Clock()
HEIGHT = 400
WIDTH = 800

BLACK = (0,0,0)
WHITE = (255,255,255)
RED = (255,0,0)
YELLOW = (255,255,0)
GREEN = (0,255,0)
BLUE = (0,0,255)
PURPLE = (255,0,255)
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('DINO RUN')

class Player:
    def __init__(self, x, y):
        self.image = pygame.Surface((32, 32)) # placeholder image
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y
        self.speed = 5

    def update(self, keys):
        if keys[pygame.K_SPACE]:
            self.rect.y -= self.speed

class PlayerController:
    def __init__(self):
        self.player = Player(50, 50)

    def handle_input(self):
        keys = pygame.key.get_pressed()
        self.player.update(keys)

    def draw(self, screen):
        screen.blit(self.player.image, self.player.rect)


player_controller = PlayerController()
fps = 60

game = True

while game:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            game = False
            pygame.quit()
           
    player_controller.handle_input()
    player_controller.draw(screen)
    screen.fill(WHITE)   
    pygame.draw.line(screen,BLACK,[0,300],[800,300],4)
    pygame.display.update()
    FPS.tick(fps)
Answered By: M4XYW4XY
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.