How to take screenshot of certain part of screen in Pygame

后端 未结 4 1393
感动是毒
感动是毒 2021-01-04 23:26

Is there a way I can take a screenshot of the right half of my pygame window?

I\'m making a game using pygame and I need to take a snapshot of the screen but not the

4条回答
  •  醉梦人生
    2021-01-05 00:26

    import pygame
    import sys
    
    
    screen = pygame.display.set_mode((400, 500))
    clock = pygame.time.Clock()
    
    
    def grab(x, y, w, h):
        "Grab a part of the screen"
        # get the dimension of the surface
        rect = pygame.Rect(x, y, w, h)
        # copy the part of the screen
        sub = screen.subsurface(rect)
        # create another surface with dimensions
        # This is done to unlock the screen surface
        screenshot = pygame.Surface((w, h))
        screenshot.blit(sub, (0, 0))
        return screenshot
    
    
    def blit(part, x, y):
        screen.blit(part, (x, y))
    
    
    def quit():
        pygame.quit()
        sys.exit()
    
    
    def start():
        # shows half the screen
        blit(back, 0, 0)
        # and the other half copied
        sub = grab(50, 0, 75, 250)
        blit(sub, 200, 0)
        while True:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    quit()
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_ESCAPE:
                        quit()
            pygame.display.update()
            clock.tick(60)
    
    
    back = pygame.image.load("img\\back.png")
    
    start()
    

提交回复
热议问题