PyGameExamplesAndAnswers

StackOverflow            reply.it reply.it


Jump ‘n’ Run and Platformer

Scrolling - Center to player

Related Stack Overflow questions:

Unfortunately, Pygame doesn’t have a built-in solution to this problem. Pygame use pygame.sprite.Sprite objects organized in pygame.sprite.Groups. The attribute .rect of the Sprites is used for drawing the objects as well as for the collision test between objects. There is no built-in feature that can convert object coordinates to screen coordinates before drawing.
As a suggestion for the developers of Pygame: It would be nice to have an optional argument for the camera offset in the method pygame.sprite.Group.draw.

There a different approaches:

At the end a comment about dirty mechanisms and partial screen updates: As soon as the player moves, the entire screen is dirty and needs to be updated. It is therefore questionable whether you should invest resources in partial update mechanisms. These algorithms also take time to run. In highly dynamic scenes, the result of the algorithm is to update all.

Platform collision

Related Stack Overflow questions:

“I thought about testing if the player’s y coordinate is lower that 0, and then teleporting him to y = 0, but that feels like a really non elegant solution”

This is the usual way. The collision stops the player from falling.

“it also makes the player teleport for a frame.”

No it doesn’t. You have to “teleport” the player plyer after the collision detection and before you draw it.

You do the collision detection once in every frame. Once a collision is detected, you know that the player has hit an object at some point while moving since the last frame. Instead of calculating the exact time when the player hits the object and stopping movement, you limit the player’s position by the object.