[英]How do I avoid recursively adding arrays when I need to merge two arrays in a while-loop?
Working with pygame, I am trying to create a larger list out of two smaller lists. 使用pygame,我试图从两个较小的列表中创建一个更大的列表。 This needs to be done during the game loop because those lists contain pygame.Rect
objects which determine collision detection with the character. 这需要在游戏循环期间完成,因为这些列表包含pygame.Rect
对象,这些对象确定与角色的碰撞检测。
charArray.append(guy.rect)
collideArray = newMap.rectArray + charArray
Of course, since this is in the game loop, guy.rect
is recursively added to charArray
, and collideArray
is recursively adding newMap.rectArray
to itself, creating a larger and larger list every frame, which eventually just destroys the framerate. 当然,由于这是在游戏循环中, guy.rect
以递归方式添加到charArray
,而collideArray
以递归方式将newMap.rectArray
添加到自身,每帧创建一个越来越大的列表,最终只会破坏帧速率。
Is there a way I could empty the lists every frame so that they are simply being redefined every frame? 有没有办法可以每帧清空列表,以便每帧都重新定义它们? Or is there some other approach I need to be taking? 或者我需要采取其他方法吗?
Easiest way to "empty" every array at the end of each frame is just to set them as empty at the end of each loop: 在每个帧结尾处“清空”每个数组的最简单方法是在每个循环结束时将它们设置为空:
while game==True:
#your game code goes here
charArray,collideArray = [],[]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.