0

I am currently working on a small project, which is a platform type game. I had designed the platform sets, in which the player has the ability to jump on the platform and stay on it and can jump of it. However when the player decides to jump off from one platform to another, the program forces the player to jump onto the 'ground' before the platform. Here is the code for collision between Player and Platform:

    def collision(self):
    if knight1.hitbox[1] < self.hit[1] + self.hit[3] and knight1.hitbox[1] + knight1.hitbox[3] > 
        self.hit[1]:
      if knight1.hitbox[0] + knight1.hitbox[2] > self.hit[0] and knight1.hitbox[0] < self.hit[0] + 
         self.hit[2]:
          knight1.jump = True
          knight1.jumpcount = 0
          knight1.jumpheight = 8
          knight1.y = self.y - 85
          self.inAir = True
    if knight1.x + 30 > (self.x + self.width) and self.inAir == True:
        knight1.y = 410
        self.inAir = False
        knight1.jump = True

Any help would be much appreciated.

  • use [pygame.Rect()](https://www.pygame.org/docs/ref/rect.html) to keep position and size and then you can uses [player.rect.colliderect(platform.rect)](https://www.pygame.org/docs/ref/rect.html#pygame.Rect.colliderect) to check collision – furas Jan 11 '20 at 13:20
  • what is `self`? You should have list with all platforms and use `for`-loop to check collision with all elements on list. I don't see any list with platforms in your code - so maybe you check only one platform. OR use [pygame.sprite.Group](https://www.pygame.org/docs/ref/sprite.html#pygame.sprite.Group) to keep all platforms and use [pygame.sprite(player, platforms_group)](https://www.pygame.org/docs/ref/sprite.html#pygame.sprite.spritecollideany) to check collision with all elements in group – furas Jan 11 '20 at 13:27
  • Im using pygame.set.timer to auto generate the platform, also ive used pygame.rect to create like a border for the platform – abcdefg123 Jan 11 '20 at 16:10
  • I don't see where you keep all plaftorms and where you check collision with every platform separatelly - maybe you check collision only with one element. – furas Jan 11 '20 at 16:15
  • Update: I had managed to make it possible for the player to jump from platform to platform, however i the player after jumping from the last platform stays in the air, instead of falling onto the ground. – abcdefg123 Jan 11 '20 at 16:39
  • use `print()` to see values in variables and which part of code is executed. It can be easier then learning how to use debugger. – furas Jan 11 '20 at 17:20

0 Answers0