0

Hi there I am currently working on a side on-platformer. I am experiencing a weird issue when I jump from one platform and to another it works fine. However whenever I jump from one and hit the bottom of another one my character sprite goes flying off the screen upwards. I feel that this is due to not having correctly programmed the collision between the player and the platforms, because I don't know how. Heres what ive done at the moment:

    collide = pygame.sprite.spritecollide(player, platform_list, False)
    if collide:
        player.rect.y-=1

If anyone can suggest a better way for collision detection between player and platforms please say it, thanks. And somehow this bug allows the player to get past the screen boundaries which I've set up for the 4 corners of the screen (lines 322 and 212), however these barriers normally

Heres my full game code:

http://pastebin.com/cae4u5NR

1 Answer 1

1

When programming something graphical the y-coordinate is reversed so the y-coordinate value would be higher under the platform and lower over the platform.

Your current code would move the player up inside the platform which would activate the collision again and push it even further up inside the platform.

You should instead say:

    collide = pygame.sprite.spritecollide(player, platform_list, False)
    if collide:
        player.rect.y += 1

Here the thing to notice is the change from "-= 1" to "+= 1".

This would make the player sprite move down instead of going up inside the platform.

I also have this example of collision detection lying, I implemented the method into my own object oriented program once but I have lost my own program. I can't remember who owns this program as I found it a long time ago, I hope it will help you to see from what I would say a fair way of doing it.

Sign up to request clarification or add additional context in comments.

4 Comments

then how would you distinguish collision between the 4 corners of the platforms because surely you'd need different outcomes for different corners.
@user2921888 You check what direction the different objects are moving, that is what it does in the code I linked. The platforms aren't moving but it is checking the players direction and will act on that.
ok I think it worked but after doing that my jump has gone all weird: if event.type == pygame.KEYDOWN: if collide and event.key == pygame._UP: player.change_y -= 5 if player.change_y == 7: player.change_y = False is making the character jump upwards off the screen
@user2921888 Try looking at the code I sent, it contains a functioning jump and collision detection which works well, so I would recommend you looking at it since it was that code that helped me learning how to do collision. Can't really remember in my head how to do it without trying to write something myself though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.