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)

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:


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




1 Answer 1


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.

  • \$\begingroup\$ 50%? Depending on his algorithm, anywhere from 0 to x%. \$\endgroup\$
    – user39686
    Jan 27, 2014 at 19:33
  • \$\begingroup\$ @Thebluefish Will edit the answer. \$\endgroup\$
    – Kroltan
    Jan 27, 2014 at 19:36
  • \$\begingroup\$ The game code is at the bottom btw and by rectangular collision do you mean: if x=y: #do stuff \$\endgroup\$ Jan 27, 2014 at 19:54
  • \$\begingroup\$ 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 \$\endgroup\$
    – Kroltan
    Jan 27, 2014 at 23:30
  • \$\begingroup\$ 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? \$\endgroup\$ Jan 28, 2014 at 15:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .