Partage
  • Partager sur Facebook
  • Partager sur Twitter

Problème collisions pygame

    1 mars 2024 à 22:35:00

    Bonjour, j'ai commencé à coder un petit jeu de plateforme, mais je rencontre un problème avec la détection de collisions, je n'arrive pas à faire tomber le joueur quand il n'est plus sur une plateforme: 

    il y a la map et comment elle est dessinée : 

    map1 = [ 
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    ] 
    for lineIndex in range(len(map1)): 
                for blockIndex in range(len(map1[lineIndex])):
                    blockValue = map1[lineIndex][blockIndex] 
                    x = blockIndex * blockSize
                    y = lineIndex * blockSize
                    rect = pygame.Rect(x,y,blockSize,blockSize)
    
                    if blockValue == 0:
                        pass
                    elif blockValue == 1:
                        pygame.draw.rect(screen, (0, 200, 0), (x, y, blockSize, blockSize))

    et les collisions avec le joueur : 

    if not blockValue == 0 :
                        if (
                            player.x + player.w > x 
                            and player.x < x + blockSize 
                            and player.y + player.h > y 
                            and player.y + player.h < y + 20
                            and player.jump == False
                        ):
                            player.fall = False 
                            player.velocity = 0
                            player.y = y - player.h
                        
                        else:
                            player.fall =True
    
                        
                        if (
                            player.x + player.w > x 
                            and player.x < x + blockSize 
                            and player.y < y + blockSize
                            and player.y > y + blockSize - 20
                        ):
                            player.y = y + blockSize
                            player.jump=False
                            player.fall=True
                            player.velocity = 0
                        
                        elif (
                            player.x + player.w > x 
                            and player.x + player.w < x + player.speed + 2
                            and player.y + player.h > y 
                            and player.y < y + blockSize
                        ):
                            player.x -= player.speed
    
                        elif (
                            player.x < x + blockSize
                            and player.x > x + blockSize - player.speed - 2
                            and player.y + player.h > y 
                            and player.y < y + blockSize
                        ):
                            player.x += player.speed


    C'est la ligne 

    else:
        player.fall =True

    qui pose problème car le joueur ne peux plus sauter du tout.

    Voila voila

    Merci si qqun peut m'aider

    :)



    • Partager sur Facebook
    • Partager sur Twitter
      2 mars 2024 à 17:04:00

      Hahaha , excellent ton pseudo 😂

      Sinon jump et fall c'est pereil, si le perso tombe il peut pas sauter, et s'il tombe plus, alors il peut sauter .

      S'il tombe (fall == True) et s'il n'y a plus de vide sous ses pieds alors fall = False , et si fall == False alors il peut sauter.

      -
      Edité par josmiley 2 mars 2024 à 17:04:23

      • Partager sur Facebook
      • Partager sur Twitter

      Python c'est bon, mangez-en. 

      Problème collisions pygame

      × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
      • Editeur
      • Markdown