What is the best way to redraw a Pygame window?

2019-06-09 15:49发布

问题:

All basic Pygame examples I've seen just redraw the background over everything and then the sprites over that. This should cause performance issues later on (because you're adding at least 2 new images to the window for every frame rate tick without ever removing them). Is there a better way?

回答1:

If updating the entire screen is fast enough, this is already the "best" way, since it's the simplest.

If drawing to the screen is a performance bottleneck, then it can be better to just update only the parts of the screen that are changed. You probably want to use pygame's DirtySprite and LayeredDirty classes. LayeredDirty's draw function returns a list of Rect instances that describe the parts of the screen that needs updating, and you can just pass this list to pygame.display.update.



标签: pygame