Search code examples
pythonpygamepartialareablit

Pygame blit area parameter?


I am currently trying to blit a part of an already drawn image again. For this I read one can use the optional third area parameter when using blit. I do not know why but that parameter is acting weird for me.

From what I understand the area parameter is (pos_x, pos_y, width, height).

My screen is 1280x1024, so if I want to have the part I want to redraw start in the center I would pass something like (640, 512, 200, 200) and a 200x200 part of the image should be drawn starting from the center.

However, for me the image is always drawn in the top left, and the first two numbers in the area parameter change the brightness of the part being drawn.

My Code:

image = pygame.image.load("image.jpg")
image = pygame.transform.scale(image, (self.display.get_width(), self.display.get_height()))
self.display.blit(image, (0,0))
# Drawing something here which has to be overwritten, but cannot redraw everything.
self.display.blit(image, (0,0), (640,512,200,200))

The second blit just draws a 200x200 image at the top left of my screen, which is brighter than the first time the image is drawn.

Kind regards


Solution

  • You're passing (0, 0) as the blit destination (the second argument), so your surface appears in the top left corner of the display. If you want to draw it elsewhere, for example at (200, 300) you have to pass these coords as the second argument:

    self.display.blit(image, (200, 300), (640,512,200,200))