Wrote a snake for two players in Js wanted to know how to write a game stop when the head of one of the snakes touches the tail of the other?

  • 0
    const canvas = document.getElementById("game-field"); /* announce the game-field */
    const ctx = canvas.getContext("2d"); /* seting the spatial dimension */
    
            /* VARRIABLES */
    
    const gameField = new Image();
    gameField.src = "img/game-field.jpg";
    
    const foodImg = new Image();
    foodImg.src = "img/food.png";
    
    let box = 32;
    
    let score = 0;
    
    let food = { /* writing a random spawn of food */
        x: Math.floor((Math.random() * 42 + 1)) * box, 
        y: Math.floor((Math.random() * 17 + 4)) * box,
    };
    
    let snake = []; /* set the snake spawn */
    snake[0] = {
        x: 9 * box,
        y: 10 * box
    };
    
    document.addEventListener("keydown", direction);
    
    let dir;
    
            /* END /VARRIABLES */
    
    function direction(event) {
        if (event.keyCode == 65 && dir != "right")
            dir = "left";
        else if(event.keyCode == 87 && dir != "down")
            dir = "up";
        else if(event.keyCode == 68  && dir != "left")
            dir = "right";
        else if(event.keyCode == 83  && dir != "up")
            dir = "down";
    }
    
    function eatTail(head, arr) { /* responding for eating of tail */
        for(let i = 0; i < arr.length; i++) {
            if(head.x == arr[i].x && head.y == arr[i].y)
                clearInterval(game);
        }
    }
    
    function eatSecondTail(secondHead, secondArr) { /* responding for eating of tail */
        for(let j = 0; j < secondArr.length; j++) {
            if(secondHead.X == secondArr[j].X && secondHead.Y == secondArr[j].Y)
                clearInterval(game);
        }
    }
    
            /* SECOND PLAYER */
            /* SECOND PLAYER */
            /* SECOND PLAYER */
    
    
            /* VARRIABLES 2*/
    
            let secondScore = 0;
    
            let secondSnake = []; /* set the snake spawn */
            secondSnake[0] = {
                X: 35 * box,
                Y: 10 * box
            };
    
            let secondDir;
        
                /* END /VARRIABLES 2 */
        
            document.addEventListener("keydown", secondDirection);
    
            function secondDirection(secondEvent) {
                if (secondEvent.keyCode == 37 && secondDir != "right")
                    secondDir = "left";
                else if(secondEvent.keyCode == 38 && secondDir != "down")
                    secondDir = "up";
                else if(secondEvent.keyCode == 39  && secondDir != "left")
                    secondDir = "right";
                else if(secondEvent.keyCode == 40  && secondDir != "up")
                    secondDir = "down";
            }
    
    function drawGame() { /* responding for image drawing */
        ctx.drawImage(gameField, 0, 0);
    
        ctx.drawImage(foodImg, food.x, food.y);
    
        for(let i = 0; i < snake.length; i++) { /* draws a head and tail */
            ctx.fillStyle = i == 0 ? "#3498DB" : "#2874A6";
            ctx.fillRect(snake[i].x, snake[i].y, box, box);
        }
    
            ctx.fillStyle = "#3498DB"; /* Score */
            ctx.font = "63px Century Gothic";
            ctx.fillText(score, box * 9, box * 1.97);
    
            let snakeX = snake[0].x;
            let snakeY = snake[0].y;
    
            if (snakeX == food.x && snakeY == food.y) { /* accrual a score */
                score++;
            }
    
            if(snakeX == food.x && snakeY == food.y) { /* set the spawn of food */
                food = {
                    x: Math.floor((Math.random() * 42 + 1)) * box,
                    y: Math.floor((Math.random() * 17 + 4)) * box,
                };
            } else {
                snake.pop();
            }
    
    
            if(snakeX < box || snakeX > box * 44 /* set the border of game-field */
                 || snakeY < 4 * box || snakeY > box * 20)
                 clearInterval(game);
    
                 
    
             /* prescribing the mobility of the snake */
                    if(dir == "left") snakeX -= box;
                    if(dir == "right") snakeX += box;
                    if(dir == "up") snakeY -= box;
                    if(dir == "down") snakeY += box;
    
    
                    /* PRESCRIBING EATING SNAKES TAIL */
                
                
            let newHead = { /* prescribing the head of snake */
                x: snakeX,
                y: snakeY
            };
    
            eatTail(newHead, snake);
    
            snake.unshift(newHead);
        
    
            /* SECOND PLAYER */
            /* SECOND PLAYER */
            /* SECOND PLAYER */
    
    
            for(let j = 0; j < secondSnake.length; j++) { /* draws a head and tail */
                ctx.fillStyle = j == 0 ? "yellow" : "orange";
                
                ctx.fillRect(secondSnake[j].X, secondSnake[j].Y, box, box);
            }
        
                ctx.fillStyle = "yellow"; /* Score */
                ctx.font = "63px Century Gothic";
                ctx.fillText(secondScore, box * 35, box * 1.97);
        
                let secondSnakeX = secondSnake[0].X;
                let secondSnakeY = secondSnake[0].Y;
        
                if (secondSnakeX == food.x && secondSnakeY == food.y) { /* accrual a score */
                    secondScore++;
                }
        
                if(secondSnakeX == food.x && secondSnakeY == food.y) { /* set the spawn of food */
                    food = {
                        x: Math.floor((Math.random() * 42 + 1)) * box,
                        y: Math.floor((Math.random() * 17 + 4)) * box,
                    };
                } else {
                    secondSnake.pop();
                }
        
                if(secondSnakeX < box || secondSnakeX > box * 44 /* set the border of game-field */
                     || secondSnakeY < 4 * box || secondSnakeY > box * 20)
                     clearInterval(game);
        
                 /* prescribing the mobility of the snake */
                        if(secondDir == "left") secondSnakeX -= box;
                        if(secondDir == "right") secondSnakeX += box;
                        if(secondDir == "up") secondSnakeY -= box;
                        if(secondDir == "down") secondSnakeY += box;
        
                    
                let newSecondHead = { /* prescribing the head of snake */
                    X: secondSnakeX,
                    Y: secondSnakeY
                };
        
                eatSecondTail(newSecondHead, secondSnake);
        
                secondSnake.unshift(newSecondHead);
    
                if (snakeX == secondSnakeX && snakeY == secondSnakeY) {
                    clearInterval(game);
                }
    
                if (secondArr.X == snakeX && secondArr.Y == snakeY) {
                    clearInterval(game)
                }
    
        }
    
        let game = setInterval(drawGame, 100); /* update the timed in 100 miliseconds */




    JavaScript Anonymous, Dec 9, 2020

  • 0 Answers
Your Answer
To place the code, please use CodePen or similar tool. Thanks you!