# Pygame collision detection less frequent when objects are increased

I currently experiencing an issue in pygame where whenever i increase the number of objects e.g. platforms, rocks

for a in range(150):
rock = Rock(0,0)


OR incresing the range which they spawn in:

rock.rect.x = random.randrange(-200,30000)
rock.rect.y = random.randrange(80,500)
rock_list.add(rock)
all_sprites_list.add(rock)


LINE 232

(which i really need to do for an ENDLESS mode) collision detection between the player and all the objects that have have been changed will become less frequent, so the character will just go through th objects as they weren't even there... you can see the character fall through the objects, it falls through in front of the object and not behind. All game code is below, I have tried a number of different techniques and values but I have found that these numbers and ranges work best.

Collision Detection code: FOR PLATFORM

medium_collide = pygame.sprite.spritecollide(player, medium_list, False)
if medium_collide:
player.rect.y-=2


FOR ROCK

    rock_collide = pygame.sprite.spritecollide(player, rock_list, True)
if rock_collide:
lives-=1
if rock_collide:
player.rect.y = 100
player.rect.x = 50


!!!!!GAME CODE BELOW!!!!!

GAME CODE

## 1 Answer

Can't say much without seeing your whole code, but I guess you're just testing collision for all objects in the game. Instead, do a AABB (rectangular) collision test to see if objects are near enough to matter (usually if they are on-screen). If they are, then do your more complex collision detection. Note that this will actually degrade performance extremely if you have all objects of the game on-screen at the same time, and you'll need to adjust this pre-check to fit your game.

• 50%? Depending on his algorithm, anywhere from 0 to x%. – user39686 Jan 27 '14 at 19:33
• @Thebluefish Will edit the answer. – Kroltan Jan 27 '14 at 19:36
• The game code is at the bottom btw and by rectangular collision do you mean: if x=y: #do stuff – user2815879 Jan 27 '14 at 19:54
• No, I mean something like if player.x < object.x or player.y < object.y or player.x+player.width > object.x or player.y+player.height > object.y: do stuff – Kroltan Jan 27 '14 at 23:30
• How would i spawn say 20 objects and then if their x position is greater than screenwidth, reset at x=0? I have no idea how to control individual objects? – user2815879 Jan 28 '14 at 15:01