這檢查是否某些點位於一個矩形內,並且每當它運行時,它都會使我的程序減慢很多。我們如何改變它以提高效率?如何加快此功能?
def draw_grid(self, box):
for element in self.map_layout.all_map_objects:
if element not in self.build_grid and box.area.collidepoint(element.checkpoint):
self.build_grid.append(element)
elif not box.area.collidepoint(element.checkpoint):
if element in self.build_grid:
self.build_grid.remove(element)
'build_grid'的順序是否重要? – mgilson
@mgilson完全沒有! –
然後,我肯定會考慮將'self.build_grid'更改爲集合(只要它包含的元素是可散列的) – mgilson