0
\$\begingroup\$

The following (abridged) bit of code is inside the main loop. What I expected to happen was the entity's position to update due to velocity, then update again if it's touching a static object before drawing rects and updating the display, but when run, an entity approaching a static will always visibly intersect with the static for one frame before moving back out, like it's running ent/stc.load() (a method for drawing rects) and pygame.display.update() multiple times during each loop.

pygame.Surface.fill(window,(100,0,0)) for stc in statics: stc.load() for ent in entities: ent.vel[0]+=ent.acc[0] ent.pos[0]+=ent.vel[0] for stc in statics: if ent.rect.colliderect(stc): if ent.vel[0]>0: ent.acc[0]=0 ent.vel[0]=0 ent.pos[0]=stc.rect.left-ent.box[0] ent.load() clock.tick(60) pygame.display.update()
\$\endgroup\$
  • \$\begingroup\$ which values the ent.box list contains? width and height? \$\endgroup\$ – 648trindade Aug 9 at 15:44
0
\$\begingroup\$

You should check collisions for a next_pos before moving to it, so you will prevent undesired intersections between objects. The intersection is corrected in the next frame, then it will be drawn at the end of the frame it happens, so it's better approach to prevent intersections/ovelaps.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.