Hi I need help on Wing IDE 101 because when I try to run my code, the actual program works fine. But after I exit out of it, the shell gives me this error:
Traceback (most recent call last):
File "/Users/kimh2/Desktop/project_1/projectcode_1.py", line 225, in <module>
main()
File "/Users/kimh2/Desktop/project_1/projectcode_1.py", line 223, in <module>
pygame.display.flip()
pygame.error: video system not initialized
So I was wondering if this is a Wing IDE issue or if there is something wrong with my code. Here is my code:
import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
import random
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)
)
surfaces = (
(0,1,2,3),
(3,2,7,6),
(6,7,5,4),
(4,5,1,0),
(1,5,7,2),
(4,0,3,6)
)
colors = (
(1,0,0),
(0,1,0),
(0,0,1),
(0,1,0),
(1,1,1),
(0,1,1),
(1,0,0),
(0,1,0),
(0,0,1),
(1,0,0),
(1,1,1),
(0,1,1),
)
##ground_vertices = (
## (-10, -1.1, 20),
## (10, -1.1, 20),
## (-10, -1.1, -300),
## (10, -1.1, -300),
## )
##
##
##def ground():
## glBegin(GL_QUADS)
## for vertex in ground_vertices:
## glColor3fv((0,0.5,0.5))
## glVertex3fv(vertex)
##
## glEnd()
def set_vertices(max_distance, min_distance = -20, camera_x = 0, camera_y = 0):
camera_x = -1*int(camera_x)
camera_y = -1*int(camera_y)
x_value_change = random.randrange(camera_x-75,camera_x+75)
y_value_change = random.randrange(camera_y-75,camera_y+75)
z_value_change = random.randrange(-1*max_distance,min_distance)
new_vertices = []
for vert in vertices:
new_vert = []
new_x = vert[0] + x_value_change
new_y = vert[1] + y_value_change
new_z = vert[2] + z_value_change
new_vert.append(new_x)
new_vert.append(new_y)
new_vert.append(new_z)
new_vertices.append(new_vert)
return new_vertices
def Cube(vertices):
glBegin(GL_QUADS)
for surface in surfaces:
x = 0
for vertex in surface:
x+=1
glColor3fv(colors[x])
glVertex3fv(vertices[vertex])
glEnd()
glBegin(GL_LINES)
for edge in edges:
for vertex in edge:
glVertex3fv(vertices[vertex])
glEnd()
def main():
pygame.init()
pygame.mixer.init()
display = (800,600)
pygame.display.set_mode(display, DOUBLEBUF|OPENGL)
pygame.display.set_caption("Cubez!")
max_distance = 120
gluPerspective(45, (display[0]/display[1]), 0.1, max_distance)
glTranslatef(0,0, -40)
x_move = 0
y_move = 0
cur_x = 0
cur_y = 0
game_speed = 1
dir_speed = 1
cube_dict = {}
for x in range(50):
cube_dict[x] =set_vertices(max_distance)
#glRotatef(25, 2, 1, 0)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_move = dir_speed
if event.key == pygame.K_RIGHT:
x_move = -1*dir_speed
if event.key == pygame.K_UP:
y_move = -1*dir_speed
if event.key == pygame.K_DOWN:
y_move = dir_speed
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
x_move = 0
if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
y_move = 0
## if event.type == pygame.MOUSEBUTTONDOWN:
## if event.button == 4:
## glTranslatef(0,0,1.0)
##
## if event.button == 5:
## glTranslatef(0,0,-1.0)
x = glGetDoublev(GL_MODELVIEW_MATRIX)
camera_x = x[3][0]
camera_y = x[3][1]
camera_z = x[3][2]
cur_x += x_move
cur_y += y_move
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
glTranslatef(x_move,y_move,game_speed)
#ground()
for each_cube in cube_dict:
Cube(cube_dict[each_cube])
for each_cube in cube_dict:
if camera_z <= cube_dict[each_cube][0][2]:
new_max = int(-1*(camera_z-(max_distance*2)))
cube_dict[each_cube] = set_vertices(new_max,int(camera_z-max_distance), cur_x, cur_y)
pygame.display.flip()
main()
pygame.quit()
The error on closing the application has nothing to do with Wing IDE but rather with using OpenGL alongside with pygame, which does not handle OpenGL contexts very well. A fix that I use while working with pygame + pyopengl is the use the sys module to exit the app instead.
The following code reproduces the same error that your code gets on closing the window:
from OpenGL.GL import *
from pygame.locals import *
import pygame
if __name__ == "__main__":
width, height = 550, 400
pygame.init()
pygame.display.set_mode((width, height), DOUBLEBUF|OPENGL)
clock = pygame.time.Clock()
glClear(GL_COLOR_BUFFER_BIT)
glClear(GL_DEPTH_BUFFER_BIT)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
glOrtho(0, width, 0, height, -1, 1)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
glColor(1, 0, 0)
glBegin(GL_TRIANGLES)
glVertex(0, 0, 0)
glVertex(275, 400, 0)
glVertex(550, 0, 0)
glEnd()
pygame.display.flip()
clock.tick_busy_loop(60)
But by calling sys.exit() alongside pygame.quit(), the error is suppressed.
from OpenGL.GL import *
from pygame.locals import *
import pygame
import sys#add this!
if __name__ == "__main__":
width, height = 550, 400
pygame.init()
pygame.display.set_mode((width, height), DOUBLEBUF|OPENGL)
clock = pygame.time.Clock()
glClear(GL_COLOR_BUFFER_BIT)
glClear(GL_DEPTH_BUFFER_BIT)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
glOrtho(0, width, 0, height, -1, 1)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()#add this!
glColor(1, 0, 0)
glBegin(GL_TRIANGLES)
glVertex(0, 0, 0)
glVertex(275, 400, 0)
glVertex(550, 0, 0)
glEnd()
pygame.display.flip()
clock.tick_busy_loop(60)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.