🐍 Python Python problem.. How can i fix this type of problem?

Bearmy

Established
ERROR: for event in pygame.event.get():
pygame.error: video system not initialized

THIS IS CODE:
import pygame

from pygame.locals import *

from OpenGL.GL import *
from OpenGL.GLU import *

vertices = ((1, -1, -1),(1, 1, -1),(-1, 1, -1),(-1, -1, -1),(1, -1, 1),(1, 1, 1),(-1, -1, 1),(-1, 1, 1))

edges = ((0,1),(0,3),(0,4), (2,1),(2,3),(2,7),(6,3),(6,4),(6,7),(5,1),(5,4),(5,7))

def Cube():
glBegin(GL_POINTS)
for edge in edges:
for vertex in edge:
glVertex3fv(vertices[vertex])
glEnd()

def main():
pygame.init()
display = (800,600)
pygame.display.set_mode(display, DOUBLEBUF|OPENGL)

gluPerspective(45, (display[0]/display[1]), 0.1, 50.0)

glTranslatef(0.0,0.0, -5)

glRotatef(0, 0, 0, 0)

while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
glRotatef(1, 3, 1, 1)
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
pygame.display.flip()
pygame.time.wait(15)

main ()
 
The error you are encountering, pygame.error: video system not initialized, is occurring because the initialization and setup of the Pygame display window and OpenGL context are not correctly placed in your code. Here are a few issues and suggestions to fix this problem:

1. Indentation Issue: The main() function should include the event handling loop and rendering logic within the function definition. Currently, the event handling loop is outside the main() function, and the rendering code is not inside any loop.

2. Initialization and Display Setup: You should move the Pygame initialization, display setup, and OpenGL settings inside the main() function so that they are executed when the program starts. Ensure that these setup steps are properly indented under the main() function.

3. Rendering Logic: The rendering logic for drawing the cube and handling events should be inside the main loop of the program. Make sure that the Cube() function is called to draw the cube within the rendering loop.

4. Correcting the Rotation: The rotation logic using glRotatef() should be within the rendering loop after handling events.

Here is an updated version of your code with the necessary corrections applied:

Python:
import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *

vertices = ((1, -1, -1),(1, 1, -1),(-1, 1, -1),(-1, -1, -1),(1, -1, 1),(1, 1, 1),(-1, -1, 1),(-1, 1, 1))
edges = ((0,1),(0,3),(0,4),(2,1),(2,3),(2,7),(6,3),(6,4),(6,7),(5,1),(5,4),(5,7))

def Cube():
    glBegin(GL_LINES)
    for edge in edges:
        for vertex in edge:
            glVertex3fv(vertices[vertex])
    glEnd()

def main():
    pygame.init()
    display = (800,600)
    pygame.display.set_mode(display, DOUBLEBUF|OPENGL)

    gluPerspective(45, (display[0]/display[1]), 0.1, 50.0)

    glTranslatef(0.0,0.0, -5)

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

        glRotatef(1, 3, 1, 1)

        glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
        Cube()
        pygame.display.flip()
        pygame.time.wait(15)

if __name__ == "__main__":
    main()

With these changes, your code should now properly initialize the Pygame display and OpenGL context, handle events, and render the rotating cube as intended.
 

About this Thread

  • 1
    Replies
  • 413
    Views
  • 1
    Participants
Last reply from:
Unknown user

Trending Topics

Online now

Members online
1,024
Guests online
1,131
Total visitors
2,155

Forum statistics

Threads
2,272,806
Posts
28,945,415
Members
1,236,418
Latest member
yesitsme
Back
Top