0

So I'm making a game to test my programming skills and I'm trying to use pygame but when i try and load a level map it says the surface has quit. I have no idea why.

This is the load code:

def load(self):
    print(os.listdir())
    file = input('>>> file name = ')
    try:
        self.Dir = os.getcwd()
        path = os.path.join(self.Dir, "maps", file)
        data = pickle.load(open(path, 'rb'))
    except OSError as error:
        print('Not file ', path)
        print(error)
    else:
        self.grid   = data[0]
        self.blocks = data[1]

Thanks in advance

EDIT: this is the trace back

  Traceback (most recent call last):
  File "F:\PROGRAMS\snow\mapmaker_v2.py", line 169, in <module>
    a.loop()
  File "F:\PROGRAMS\snow\senpy.py", line 95, in loop
    self.blocks.draw(self.screen)
  File "F:\BMDSIT\Portable Python 3.2.5.1\App\lib\site-packages\pygame\sprite.py", line 475, in draw
    self.spritedict[spr] = surface_blit(spr.image, spr.rect)
pygame.error: display Surface quit

EDIT 2: This is the loop code where it happens:

(The load command is in the self.keyboard() function.)

def loop(self):
    print('looping')
    for event in pygame.event.get():               
        if event.type == pygame.QUIT:
            print('quiting :(')
            pygame.display.quit()
            quit('User quit')

    self.screen.fill((237, 237, 237))

    self.keys = pygame.key.get_pressed()
    self.mos = pygame.mouse.get_pressed()

    self.mouse()
    self.keyboard()

    #screen.blit(player,player.pos)
    self.blocks.draw(self.screen)

    self.extraLoop()

    pygame.display.flip()
    print('done looping')
Pyrolle
  • 51
  • 1
  • 6
  • 2
    Can you please include the full traceback in your question as an edit? – roganjosh Feb 28 '17 at 20:46
  • 2
    I suspect your error is happening elsewhere in your code. That error message happens when you try to draw to the screen after the display has been closed, usually by using pygame.quit(). Please post the rest of your code. – Chris Feb 28 '17 at 20:50
  • without see more code it will be tough to figure out. according to this post http://stackoverflow.com/questions/1997710/pygame-error-display-surface-quit-why there are multiple reasons this could happen – The4thIceman Mar 01 '17 at 01:30
  • The problem is I haven't called pygame.quit() or any other quit() function and this only happens after i run my load code – Pyrolle Mar 01 '17 at 09:42

1 Answers1

1

You can't pickle a Surface object.

If you want to pickle an object that contains a Surface, remove it before saving it to disk; and store the name of the image file or a string representation of the Surface instead in the object.

sloth
  • 99,095
  • 21
  • 171
  • 219