1

On my pygame I have a problem : the collisions are weird and they work half : on the border they work but on the midddle of the map no (sorry for the color) were that work (yes) and were that not work (no)

here is the portion of code for the deplacement :

    if game.pressed.get(pygame.K_d):
        if lvl.structure[game.player.rect.y // 64 + camY // 64][game.player.rect.x // 64 + 1 + camX // 64] is not 'T':
            game.player.move_right()
            #print(game.player.rect.x)
            if not screen_rect.contains(game.player.rect):
                game.player.rect.x -= HEIGHT -60 
                camX += -HEIGHT

    elif game.pressed.get(pygame.K_q):
        if lvl.structure[game.player.rect.y // 64 + camY // 64][game.player.rect.x // 64 + camX // 64] is not 'T':
            game.player.move_left()
            #print(game.player.rect.x)
            if not screen_rect.contains(game.player.rect):
                game.player.rect.x += HEIGHT - 60
                camX += HEIGHT

    elif game.pressed.get(pygame.K_s):
        print(game.player.rect.y)
        if lvl.structure[game.player.rect.y // 64 + 1 + camY // 64][game.player.rect.x // 64 + camX // 64] is not 'T':
            game.player.move_down()
            if not screen_rect.contains(game.player.rect):
                game.player.rect.y -= WIDHT - 80
                camY -= WIDHT

    elif game.pressed.get(pygame.K_z):
        if lvl.structure[game.player.rect.y // 64 + camY // 64][game.player.rect.x // 64 + camX // 64] is not 'T':
            game.player.move_up()
            #print(game.player.rect.y)
            if not screen_rect.contains(game.player.rect):
                game.player.rect.y += WIDHT - 80
                camY += WIDHT

    lvl.afficher(screen, 0, 0, camX, camY, game.player.rect.x, game.player.rect.y)
    screen.blit(game.player.image, game.player.rect)
    pygame.display.flip()

and for the level :

class Level(pygame.sprite.Sprite):
    def __init__(self):
        self.structure = 0
        self.map = []

    def generer(self):
        with open("niveau.txt", "r") as fichier:
            structure_niveau = []
            for ligne in fichier:
                ligne_niveau = []
                for sprite in ligne:
                    if sprite != '\n':
                        ligne_niveau.append(sprite)
                structure_niveau.append(ligne_niveau)
            self.structure = structure_niveau

    def afficher(self, fenetre, x, y, camX, camY, playerX, playerY):
        tailleSprite = 64
        self.collide = False
        #Camera.__init__(self, x, y)

        grass = pygame.image.load("assets/bloc/grass.png").convert_alpha()

        tree = pygame.image.load("assets/bloc/tree_grass.png").convert_alpha()

        no_texture = pygame.image.load("assets/bloc/no_texture.png").convert_alpha()

        num_ligne = 0
        for ligne in self.structure:
            num_case = 0
            for sprite in ligne:
                x = num_case * tailleSprite + camX
                y = num_ligne * tailleSprite + camY
                sprite_rect = pygame.Rect(x, y, 64, 64)
                screenRect = pygame.Rect(x, y, 1088, 836)
                aroundPlayer = pygame.Rect(playerX, playerY, 46, 64)
                if sprite == 'G':
                    if screenRect.contains(sprite_rect):
                        fenetre.blit(grass, (x, y))

                elif sprite == 'T':
                    if screenRect.contains(sprite_rect):
                        fenetre.blit(tree, (x, y))


                else:
                    if screenRect.contains(sprite_rect):
                        fenetre.blit(no_texture, (x, y))
                    #print(x, y)

                num_case += 1
            num_ligne += 1

Thank you in advance !!!!!

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Nat' code
  • 55
  • 6
  • Do you really mean [`.contains()`](https://www.pygame.org/docs/ref/rect.html#pygame.Rect.contains)? Do you not mean [`.colliderect()`](https://www.pygame.org/docs/ref/rect.html#pygame.Rect.colliderect)? – Rabbid76 Apr 16 '20 at 09:52
  • Sorry but I don't understand – Nat' code Apr 16 '20 at 10:39
  • Try to use `.colliderect()` instead of `.contains()`. Everywhere. – Rabbid76 Apr 16 '20 at 10:41
  • On lvl.structure[game.player.rect.y // 64 + 1 + camY // 64][game.player.rect.x // 64 + camX // 64] is not 'T' ? – Nat' code Apr 16 '20 at 11:18
  • No. You should use the method `colliderect` instead of the method `contains`, everywhere in your code. (I count `contains` 7 times) – Rabbid76 Apr 16 '20 at 11:20
  • Ho thank you @Rabbid76 !! But that doesn't rule the problem of the tree on the image – Nat' code Apr 16 '20 at 16:42

0 Answers0