pygame.error: video system not initialized

2020-02-06 07:54发布

so I get this error when I try to run my pygame code: pygame.error: video system not initialized

i specify where the wing IDE tells me it is in the code below

import os
import sys
import math
import pygame
import pygame.mixer
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")

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()
sys.exit    
#!/usr/bin/env python

标签: python pygame
4条回答
放我归山
2楼-- · 2020-02-06 08:19

I had this issue recently, and I discovered a strange and unusual bug in the code that I'd written -- only after reading it and re-reading it a dozen times over a 10 minute stretch, trying to launch the file (which looked perfect) a dozen times.

There was pygame.init(). There was screen = pygame.display.set_mode((size)) with the variable size in position to be available in the global namespace.

Turns out it was the main game loop.

# main game loop
while RUNNING == True:
    for tneve in pygame.event.get():
        if tneve.type == QUIT:
            print(tneve)
            RUNNING = False
        loop()
        render()
        CLOCK.tick(FPS)
    cleanup()
# End

What a pain!

P.S. The problem is the one-stop-too-far indentation of everything below RUNNING = False.

查看更多
劳资没心,怎么记你
3楼-- · 2020-02-06 08:20

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.

查看更多
欢心
4楼-- · 2020-02-06 08:29

You have to add:

pygame.init()

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

查看更多
淡お忘
5楼-- · 2020-02-06 08:32

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 :)

查看更多
登录 后发表回答