Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading