Question

I open a pygame window and draw some stuff on it with pyopengl calls something like this:

def run(facets, SCREEN_SIZE=(800, 600)):
  pygame.init()
  screen = pygame.display.set_mode(SCREEN_SIZE, HWSURFACE|OPENGL|DOUBLEBUF)

  glViewport(0, 0, SCREEN_SIZE[0], SCREEN_SIZE[1])
  glMatrixMode(GL_PROJECTION)
  glLoadIdentity()
  gluPerspective(60.0, float(SCREEN_SIZE[0])/SCREEN_SIZE[1], .1, 1000.)
  glMatrixMode(GL_MODELVIEW)
  glLoadIdentity()

  clock = pygame.time.Clock()

  glMaterial(GL_FRONT, GL_AMBIENT, (0.1, 0.1, 0.1, 1.0))
  glMaterial(GL_FRONT, GL_DIFFUSE, (1.0, 1.0, 1.0, 1.0))

  pygame.mouse.set_visible(False)
  pygame.event.set_grab(True)

  while True:
    for event in pygame.event.get():
      if event.type == QUIT:
        pygame.quit()
        pygame.display.quit()
        print "returning..."
        return
      if event.type == KEYUP and event.key == K_ESCAPE:
        pygame.quit()
        pygame.display.quit()
        print "returning..."
        return

    pygame.mouse.set_visible(False)

    # Clear the screen, and z-buffer
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

    # other game loop code here, e.g. rendering the facets
    # and moving the viewport around.

I've left in pretty much all the pygame and pyopengl calls I make, in case any of them are relevant.

what I want to happen: to be running a session in bpython/IDLE/interactive python prompt, to call run() and look at something, hit ESC and have it close the window and return me to my prompt.

what actually happens: run my interactive session, build data structures, call run(), look at stuff, hit ESC and the window closes, but then immediately reopens. only way to get out is to kill the python process (which not only kills my interactive session, but in bpython leaves my shell window inoperable.) Note, I also do see the result of print "returning..." in the now defunct shell.

I've seen these questions:

But they only make me rather paranoid: is pygame really so resource-leaky that you have to quit python to get it to tidy up its mess?

Also, what dark magic is causing my return calls to not return?

Finally, can anyone tell me how to make this work the way I want? even utterly evil unsupported hacks are fine, for now, as I just need to debug my other code using this 3D renderer to see what I'm doing!

Was it helpful?

Solution

Answer is: always post all of your code!

This was an indentation error in the calling code, which was calling run() over and over. pygame windows appear to be perfectly happy exiting to an interactive prompt.

I would also have seen this if I'd had the patience to press ESC 9 times.

OTHER TIPS

I always use

sys.exit()

just remember to import sys

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top