On the documentation page for pygame.display.update()
, it says you can pass a rect into the method to update part of the screen. However, all of the examples I see just pass an existing rect from an image or shape in the program. How can I tell it to update an area on the screen directly? For example, when drawing a rectangle, I could use a rect argument of (100,200,30,40)
. This would draw a rectangle with a top at 200, a left at 100, a width of 30, and a height of 40. How can I pass a similar argument into pygame.display.update()
? I tried pygame.display.update((100,200,30,40))
, but this updates the entire window.
Asked
Active
Viewed 6,898 times
3

Johnny Dollard
- 708
- 3
- 11
- 26
-
@MichealO'Dwyer Of course I have. I'm not asking you to write my code - I just need to know how to pass an argument to a very specific command. `pygame.display.update((100,200,30,40))` is failing to only update only the area I'm trying to update, so I assume I am not passing the rect correctly. – Johnny Dollard Feb 10 '18 at 01:38
-
@MichealO'Dwyer I understand that you don't want to answer questions from people who don't do their work, but I feel that it should be obvious from the specificity of my question that I have been working on this for a while and have narrowed the problem down to a single line of code. That said, I have added a specific example to the question that describes my problem more precisely, so it is easier to answer. – Johnny Dollard Feb 10 '18 at 02:06
-
1I saw that you edited it to include an attempt :) I would've answered but there is a perfect answer below already. – Micheal O'Dwyer Feb 10 '18 at 17:18
1 Answers
6
Just define a rect and pass it to pygame.display.update()
to update only this specific region of the display. You can also pass a list of rects.
import random
import pygame as pg
from pygame.math import Vector2
# A simple sprite, just to have something moving on the screen.
class Ball(pg.sprite.Sprite):
def __init__(self, screen_rect):
super().__init__()
radius = random.randrange(5, 31)
self.image = pg.Surface((radius*2, radius*2), pg.SRCALPHA)
pg.draw.circle(self.image, pg.Color('dodgerblue1'), (radius, radius), radius)
pg.draw.circle(self.image, pg.Color('dodgerblue3'), (radius, radius), radius-2)
self.rect = self.image.get_rect(center=screen_rect.center)
self.vel = Vector2(random.uniform(-2, 2), random.uniform(-2, 2))
self.pos = Vector2(self.rect.center)
self.screen_rect = screen_rect
self.lifetime = 350
def update(self):
self.pos += self.vel
self.rect.center = self.pos
self.lifetime -= 1
if not self.screen_rect.contains(self.rect) or self.lifetime <= 0:
self.kill()
def main():
screen = pg.display.set_mode((800, 600))
screen.fill((20, 40, 70))
pg.display.update()
screen_rect = screen.get_rect()
clock = pg.time.Clock()
all_sprites = pg.sprite.Group()
# Pass this rect to `pg.display.update` to update only this area.
update_rect = pg.Rect(50, 50, 500, 400)
done = False
while not done:
for event in pg.event.get():
if event.type == pg.QUIT:
done = True
all_sprites.add(Ball(screen_rect))
all_sprites.update()
screen.fill((20, 50, 90))
all_sprites.draw(screen)
# Update only the area that we specified with the `update_rect`.
pg.display.update(update_rect)
clock.tick(60)
if __name__ == '__main__':
pg.init()
main()
pg.quit()

skrx
- 19,980
- 5
- 34
- 48
-
Hi. I tried your exact code with pygame 2.0.0 (SDL 2.0.12, python 3.7.8) and it does not work the way it looks in your video. It updates the complete screen. No Cliping effect like in your video. Is it a version problem? – Hothie Nov 25 '20 at 15:52