1

I've written a small game in pygame. I'm attempting to implement a feature where after the user presses a key, the game begins saving every frame as an image until the key is pressed again. Obviously this would be detrimental to the framerate of the game if it was saved by the main thread, so currently I'm attempting to make it work using a frame buffer and a second thread as follows:

framebuffer = deque([])
class frameSaver:
    def __init__(self):
        self._running = True
    def terminate(self):
        self._running = False
    def run(self):
        global framebuffer
        while self._running:
            while len(framebuffer) > 0:
                frame, framename = framebuffer.popleft()
                pygame.image.save(frame, 'capture\\'+str(framename)+'.png')

The main loop in my program appends a tuple containing a copy of the pygame display surface and the current frame number to the frame buffer following every frame update.

I had assumed this would make my code faster because saving an image would be a mostly io bound task, but instead it made my games fps drop into the single digits. My suspicion is that the culprit is either the GIL or something about pygame itself, so I was wondering if anyone either knew of another way to save a pygame surface as an image, or a better way to do this.

0 Answers0