Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to make collision to not iterable platform?

Player has ability to stand on iterable platforms, but he can’t stand on the not iterable platform – Platform4. How to correct this problem?

class Player(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()
        pygame.sprite.Sprite.__init__(self)
       # [...]
    def update(self):
        hits_4 = pygame.sprite.spritecollide(player, platform4, False)
        if hits_4:
             self.pos.y = hits_4[0].rect.top + 1 
             self.vel.y = 0

class Platform4(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()
        self.image = platform_images4
        self.image.set_colorkey(WHITE1)
        self.rect = self.image.get_rect()
        self.rect.centerx = 300
        self.rect.centery = 500

    def update(self):
        self.rect.move_ip(-1, 0)
        if self.rect.right <= 0:
            self.kill()

platform4 = Platform4()
all_sprites = pygame.sprite.Group()
all_sprites.add(player, fire, platform4)

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

platform4 is a pygame.sprite.Sprite object. For the collision of 2 Sprite objets you have to use pygame.sprite.collide_rect:

hits_4 = pygame.sprite.spritecollide(player, platform4, False)

hits_4 = pygame.sprite.collide_rect(player, platform4)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading