2016-08-01 389 views
0

您好我需要Wing IDE 101的帮助,因为当我尝试运行我的代码时,实际的程序工作正常。但之后,我退出了它,外壳给了我这个错误:pygame.display.flip - 给出这个错误:“pygame.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 

所以我在想,如果这是一个永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() 
+0

检查'pygame.init()':'numpass,numfail = pygame.init()'的返回值。 'numfail'的价值是什么? –

+0

你使用的是什么版本的python。当我在python 3.5中运行你的代码时,它工作正常。除非你关闭窗口并得到一个'OpenGL.error.GLError:GLError'。 –

+0

+ Mr。 Python我正在使用Python 3.5.2,我也尝试过2.7。它与Wing101有关吗? – Hyungjun

回答

0

上关闭应用程序的错误无关,与永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) 

但通过调用sys.exit()一起pygame.quit(),被抑制的错误。

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) 
+0

不错的解决方案,但这种sorta使我的python启动程序无响应,我需要强制退出IDLE和Python启动器。无论如何,感谢解决方案^^ – Hyungjun

+0

如果这是一个问题,这是另一个我刚刚测试的解决方案,可能会更好。如果可以稍微重构游戏循环,则可以在event.type == pygame.QUIT为true时退出while循环。之后(在while循环之下),你可以调用pygame.quit(),这应该工作。我在测试应用程序时从未使用过IDLE,所以我没有意识到启动程序会因此而变得没有响应。如果有帮助,我可以将其添加到上述解决方案中。 – CodeSurgeon

相关问题