简体   繁体   中英

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. This needs to be done during the game loop because those lists contain pygame.Rect objects which determine collision detection with the character.

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.

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 = [],[]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM