ERROR: pygame.error: video system not initialized

Viewed 34

I'm working through a python book. So far, all of the programs have worked, but now I'm stuck. I typed in the program, and I'm getting this error when I run it. I checked all of the lines several times, and I think everything's right. It is supposed to open a text window and 10 seconds later show a game character. At first, I had an indent error, but I fixed that. Now I'm getting this new error. it starts up and runs for about 0.5 seconds, but it immediately closes and gives this error. I'm using Python 3.8.10 On Windows 10 Pro 64-bit and this is the error: pygame.error: video system not initialized and here is the code:

import pygame
import time
import subprocess
pygame.init()
screen = pygame.display.set_mode((800, 250))
clock = pygame.time.Clock()
font = pygame.font.Font(None, 25)

pygame.time.set_timer(pygame.USEREVENT, 200)

def text_generator(text):
    tmp = ""
    for letter in text:
        tmp += letter
        if letter != " ":
            yield tmp
class DynamicText(object):
    def __init__(self, font, text, pos, autoreset=False):
        self.done = False
        self.font = font
        self.text = text
        self._gen = text_generator(self.text)
        self.pos = pos
        self.autoreset = autoreset
        self.update()

    def reset(self):
        self._gen = text_generator(self.text)
        self.done = False
        self.update()

    def update(self):
        if not self.done:
            try: self.rendered = self.font.render(next(self._gen), True, (0, 128, 0))
            except StopIteration:
                self.done = True
                time.sleep(10)
                subprocess.Popen("python C:\\Users\\david\\Documents\\pythonbook\\pygame1.py 1", shell=True)

    def draw(self, screen):
        screen.blit(self.rendered, self.pos)
text=("Steve has gone on a quest to defeat the Ender Dragon. Will he make it?")

message = DynamicText(font, text, (65, 120), autoreset=True)

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT: break
        if event.type == pygame.USEREVENT: message.update()
        else:
            screen.fill(pygame.color.Color('black'))
            message.draw(screen)
            pygame.display.flip()
            clock.tick(60)
            continue
        break

    pygame.quit()

How can I figure out what the error message means? Or does anyone know?

1 Answers

You quit the game in every frame with pygame.quit(). quit the game after the application loop instead of in the application loop. Also why do you update the game just in the else case? The break statement in case of event.type == pygame.QUIT only breaks the event loop, but not the application loop. Change the control flow of your code:

# application loop
run = True
while run:
    # event loop
    for event in pygame.event.get():
        if event.type == pygame.QUIT: run = False
        if event.type == pygame.USEREVENT: message.update()
        
    # redraw in every frame
    screen.fill(pygame.color.Color('black'))
    message.draw(screen)
    pygame.display.flip()
    clock.tick(60)

# quit after the application loop
pygame.quit()
Related