user2921888
user2921888

Reputation: 25

Weird bug in pygame

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

Upvotes: 0

Views: 381

Answers (1)

jsk
jsk

Reputation: 197

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.

Upvotes: 1

Related Questions