pygame.display.flip - 给出了这个错误:" pygame.error:视频系统未初始化"

时间:2016-08-01 04:54:32

标签: python python-3.x pygame pyopengl

您好我在Wing IDE 101上需要帮助,因为当我尝试运行我的代码时,实际程序运行正常。但是在我退出之后,shell给了我这个错误:

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

所以我想知道这是否是一个Wing IDE问题,或者我的代码是否有问题。这是我的代码:

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

1 个答案:

答案 0 :(得分:0)

关闭应用程序时的错误与Wing IDE无关,而是使用OpenGL和pygame,后者不能很好地处理OpenGL上下文。我在使用pygame + pyopengl时使用的修复是使用sys模块来退出应用程序。

以下代码再现了代码在关闭窗口时出现的相同错误:

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)

但是通过在pygame.quit()旁边调用sys.exit(),可以抑制错误。

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)