Search code examples
pythonvideo-capturepygame

How to capture pygame screen?


How can I capture and save a sequence of images or a video of a pygame screen?
Basically I want to share my game video on youtube. Also, want to make a tutorial.

The game is rendered mainly in a loop:

def main():
    while True:
        GetInput()
        Move()
        Shift()
        Draw()

With the Draw() function doing all the blit() and stuff before doing the pygame.display.flip()


Solution

  • Use pygame.image.save on your screen surface:

    window = pygame.display.set_mode(...)
    
    ...
    
    pygame.image.save(window, "screenshot.jpeg")
    

    Note that this will slow down your program tremendously. If it is time-based, you may wish to fake the framerate when doing a capture.