Pygame display.update() refreshes the whole width of window instead of requested Rect only

Advertisements

I’m discovering Pygame and stuck on some problematic behavior that I can’t figure out.

I am trying to refresh a 80px square in the center of the window with the code below, but the whole line turns blue instead of the square only as expected. Is this normal behavior, and can someone help me understand why this is happening please?

Using pygame 2.1.3 / python 3.11.1 / macOS 13.1

import pygame
pygame.init()
screen = pygame.display.set_mode((400, 400))
screen.fill((255,0,0))
pygame.display.update(((160,160,80,80)))

>Solution :

The documentation for pygame.display.update() does not clearly specify that, however the rectangle is not intended to limit the screen update to that area, but at least to update that area in the most efficient way. What method is most efficient may depend on the system. Note that the window is organized in lines, so it may be more efficient to update a complete set of contiguous lines than to update many line sections separately.

Use pygame.Surface.set_clip() to restrict the modifieable surface to a certain area:

screen.set_clip((160,160,80,80))
screen.fill((255,0,0))
pygame.display.update()

Leave a ReplyCancel reply