2

I am new to dirty rect animation and I am currently trying to store a snapshot of the main display surface window, however I would only like to store the area where my item is going to be blit so the next frame I can call this stored snapshot instead of re blitting the whole background.

I looked at the documentation for Surface.copy() but it doesn't take arguments and I couldn't find anything similar other than pygame.pixelcopy() which from what I understand is not what I am looking for. If Surface.copy() isn't what I am looking for, please let me know of alternatives.

import pygame, time
pygame.init()
screen = pygame.display.set_mode((500, 500))

screen.fill((128, 128, 128))
pygame.display.update()

#immagine a complex pattern being blit to the screen here
pygame.draw.rect(screen, (128, 0, 0), (0, 0, 50, 50))
pygame.draw.rect(screen, (0, 128, 0), (50, 0, 50, 50))
pygame.draw.rect(screen, (0, 0, 128), (200, 0, 50, 50))

#my complex background area that i want to save ()
area_to_save = pygame.Rect(0, 0, 100, 50)

rest_of_background = pygame.Rect(200, 0, 50, 50)

#updating for demo purposes
dirty_rects = [area_to_save, rest_of_background]
for rect in dirty_rects:
    pygame.display.update(rect)
temp_screen = screen.copy()

time.sleep(3)
#after some events happen and I draw the item thats being animated onto the background
item_to_animate = pygame.Rect(35, 10, 30, 30)
pygame.draw.rect(screen, (0, 0, 0), item_to_animate)
pygame.display.update(item_to_animate)

time.sleep(3)
item_to_animate = pygame.Rect(50, 60, 30, 30)
pygame.draw.rect(screen, (0, 0, 0), item_to_animate)
#now that the item has moved, draw back old frame, which draws over the whole surface
screen.blit(temp_screen, (0, 0))
pygame.display.update()

#I understand swapping the drawing of the new item location to after temp_surface blit
#will provide me the desired outcome in this scenario but this is a compressed version of my problem
#so for simplicity sake, is there a way of not saving the whole surface, only those rects defined?

I expect the output of this code to be displaying my background for 3 seconds, then the black square overlaying the patter and then after another 3 seconds, the black square appearing below my pattern.

P.S.:I am new to this site, let me know if I did something wrong please!

Edit: For anyone wondering if this solution (of saving the background before blitting an item over it and then redrawing the saved background before the new item location is blit over) is more efficient than redrawing the whole background and then blitting the item, using a simple square animation over a chequered pattern with redrawing the whole background each time reduced my overall fps by around 50% from 1000 (before redrawing the background) to 500 average. While using dirty rects and this method above I get around 900 fps.

2 Answers 2

0

What you want to do can be achieved by pygame.Surface.blit().

Create a surface with the decided size and blit an area of the screen to this surface. Note, the 3rd argument to bilt is an optional parameter which selects a rectangular area of the source surface:

# create a surface with size 'area_to_save.size'
temp_screen = pygame.Surface(area_to_save.size)

# blit the rectangular area 'area_to_save' from 'screen' to 'temp_screen' at (0, 0) 
temp_screen.blit(screen, (0, 0), area_to_save) 
1
0

You could use subsurface to specify the area you want to copy, and then call copy on the returned Surface.

But note that it may happend that this will not increase the performance of your game at all, since copying a lot of Surfaces around does not come free. Just try and check yourself if it actually better then drawing a background surface every frame.

Also note that you should never use time.sleep in your game. While sleep is blocking your game's process, your game can't process events, so you e.g. can't quit the game in this time. Also, it may happen that your game's window will just not be redrawn if you don't process events by calling pygame.event.get; and once the event queue fills up because you never call pygame.event.get, your game will freeze.

3
  • Thanks for the help, Yes I am aware using time.sleep would be detrimental, I only used it to demonstrate the desired result without actually making a game loop. I will look at subsurface and update my post with my findings. May 3, 2019 at 12:48
  • After doing some research, using a subsurface to blit onto the same surface wont work as pygame automatically locks the subsurface for some reason. See : stackoverflow.com/questions/4117407/… May 3, 2019 at 13:31
  • That's my I said in my answer that you have to copy the subsurface.
    – sloth
    May 3, 2019 at 15:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.