Start New Discussion within our Software Development Community

Hey guys, I'm making a platform game similar to Super Mario in Pygame and I want to add platforms i.e. I have a background picture that has platforms drawn on it. I then want to make it so he can only go down as far as the platform, and as the platform goes up he has to go up.

Is there any way I can do this like:

If player at co-ords(12,45) to (20,45) then:
player movement not lower than (0,45)

or something to that effect. This is the only way I can think of doing this.
Any help would be greatly appreciated :)

In the code block where the movement of the player gets updated and applied, just run a simple check through it for something like this:

Get the position that the player would be at based on the movement they just pressed a key for, and then, if it's allowable, move the player's character, otherwise, don't.

If the player's character is a pygame.surface object, then you can use the get_pos() function to get their coordinates. Then figure out what the new coordinates would be with the movement they're wanting to do. Check to see if these new coords are allowed or not, and if they are, move the character surface to them and re-blit, etc. You'll most likely need to be taking the character's surface's dimensions into account here too.

And if it helps explain the above, here's some pseudo-code:

# within the segment controlling the player's movement:
# delta_x  and  delta_y  are the proposed changes in x and y (respectively)

cur_x, cur_y = player_surface.get_pos()

new_x = cur_x + delta_x
new_y = cur_y + delta_y

# for my example, I won't allow the player to be within
# 12 - 20 for the x, and 45 for the y
if new_x not in range(12, 21):
    if new_y not in range(45, 46):
        # allow player movement here
# otherwise, nothing will happen, i.e. they won't move.

That's sort of a skeleton anyways...

Hope that helped!

This article has been dead for over six months. Start a new discussion instead.