[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Drawing efficiency question



On 2/03/22 12:02 pm, Irv Kalb wrote:> I'm wondering if my code to check if an element is within the viewable area, is actually doing more work than not bothering to check at all.

If the drawing of each object is just a blit call, then your checks are
probably just duplicating what pygame is already doing itself much more
efficiently.

The only way to find out for sure is to try it, which it sounds like
you've already done and found it only makes a small difference.

To do better you'll need some kind of spatial index to quickly find
objects that might be in view. A simple way would be to divide the
world into a grid of cells, each one about the size of the window,
and maintain a collection of objects in each cell. Then you can
draw just the objects in cells that overlap the window.

--
Greg