Pygame how to fix 'trailing pixels'?
In the image the red trail is a trail that pygame is creating when I have a bounding rectangle added around sprites. The sprite also does it and the simplest solution was to just clear the surface to black after each redraw. However attempting to do so on the entire main surface is not such a good idea. How can I fix this?
Solution 1:
Normally you will do:
def draw():
# fill screen with solid color.
# draw, and flip/update screen.
But, you can update just dirty portions of the screen. See pygame.display.update()
:
pygame.display.update()
Update portions of the screen for software displaysupdate(rectangle=None) -> None
update(rectangle_list) -> None
This function is like an optimized version of pygame.display.flip() for software displays. It allows only a portion of the screen to updated, instead of the entire area. If no argument is passed it updates the entire Surface area like pygame.display.flip().
You can pass the function a single rectangle, or a sequence of rectangles. It is more efficient to pass many rectangles at once than to call update multiple times with single or a partial list of rectangles. If passing a sequence of rectangles it is safe to include None values in the list, which will be skipped.
It's best to use it in combination with RenderUpdates
, which is a Sprite.Group
:
pygame.sprite.RenderUpdates
Group sub-class that tracks dirty updates.RenderUpdates(*sprites) -> RenderUpdates
This class is derived from pygame.sprite.Group(). It has an extended draw() method that tracks the changed areas of the screen.
Solution 2:
Just have a black rectangle and blit that overtop of where your sprite was on the previous frame and that should get rid of it. Just remember to do this before you blit your sprite or your new sprite will be partly blacked out.