0
votes

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')
1
Can you please include the full traceback in your question as an edit? - roganjosh
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
without see more code it will be tough to figure out. according to this post stackoverflow.com/questions/1997710/… there are multiple reasons this could happen - The4thIceman
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

1 Answers

1
votes

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.