Preview:
//int segmentHit = -1;  // Initialize segmentHit to -1 indicating no segment was hit initially
for (int k = segmentHit; k < totalSegments - 1; k++) {
        centipede[k][x] = centipede[k + 1][x];
        centipede[k][y] = centipede[k + 1][y];
        centipede[k][exists] = centipede[k + 1][exists];
        centipede[k][direction] = centipede[k + 1][direction];
    }
    // Mark the last segment as not existing
    centipede[totalSegments - 1][exists] = false;
   



if (bullet[exists]) {
        // Check for collision with centipede segments
        for (int j = 0; j < totalSegments; j++) {
            // ... (existing code for collision detection)

            // Check if bullet hits the centipede segment
            if (bulletX >= centipede[j][x] - centipedeCollisionRange &&
                bulletX <= centipede[j][x] + centipedeCollisionRange &&
                bulletY >= centipede[j][y] - centipedeCollisionRange &&
                bulletY <= centipede[j][y] + centipedeCollisionRange &&
                centipede[j][exists]) {

                // Store the index of the hit segment
                segmentHit = j;
                // Split the centipede at the hit segment
               
                bullet[exists] = false;
                break; // Exit the loop after handling collision with one segment
            }
        }
    }
downloadDownload PNG downloadDownload JPEG downloadDownload SVG

Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!

Click to optimize width for Twitter