Advertisement
Guest User

Untitled

a guest
Mar 18th, 2023
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 3.95 KB | Source Code | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7.     <title>Document</title>
  8.     <style>
  9.         html,body{
  10.             height:100%;
  11.             width:100%;
  12.             display: flex;
  13.             align-items: center;
  14.             justify-content: center;
  15.             padding:0px;
  16.             margin:0px;
  17.         }
  18.         .display{
  19.             height:50%;
  20.             aspect-ratio: 1/1;
  21.             display: flex;
  22.             flex-wrap: wrap;
  23.             padding:0px;
  24.             margin:0px;
  25.             border:2px solid #000000;
  26.         }
  27.         .part{
  28.             padding:0px;
  29.             margin:0px;
  30.             display: flex;
  31.             align-items: center;
  32.             justify-content: center;
  33.         }
  34.     </style>
  35. </head>
  36. <body>
  37.     <div class="display" id="display"></div>
  38.     <script>
  39.         let snake = [];
  40.         const verticalfov = 2;
  41.         const horisontalfov = 2;
  42.         let foodPosition = [-1,-1];
  43.         for (y = 0 ; y < 1+2*verticalfov ; y++){
  44.            for (x = 0 ; x < 1+2*horisontalfov ; x++){
  45.                var div = document.createElement('div');
  46.                div.id = "partNo"+x+"/"+y;div.className = "part";
  47.                document.getElementById("display").appendChild(div);
  48.                document.getElementById("partNo"+x+"/"+y).style.height = 100/(1+2*verticalfov)+"%";
  49.                document.getElementById("partNo"+x+"/"+y).style.width = 100/(1+2*horisontalfov)+"%";
  50.                console.log("generated div for coordinates x:" + x + " y:" + y);
  51.            }
  52.        }
  53.        snake.push([horisontalfov,verticalfov]);
  54.        console.log("current snake x y and status "+snake);
  55.        for ( y = 1 ; y < 4 ; y++ ){
  56.            console.log("current y "+y)
  57.            snake.push([horisontalfov,verticalfov+y]);
  58.            console.log("current snake x y and status "+snake);
  59.        }
  60.        setSnakeValuesAndRender(true);
  61.        function setSnakeValuesAndRender(SnakeGrowth){
  62.            let snakeTemplate = [];
  63.            for (i = 0 ; i < snake.length ; i ++){
  64.                snakeTemplate.push(snake[i]);
  65.            }
  66.            snake [0] = [0,0];
  67.            console.log(snake);
  68.            console.log(snakeTemplate);
  69.            for( let currentlyUpdatedSnakePart = 1 ; currentlyUpdatedSnakePart < snake.length ; currentlyUpdatedSnakePart++){
  70.                console.log("Currently Updated Snake Part "+currentlyUpdatedSnakePart+" and it's values x y and status "+snake[currentlyUpdatedSnakePart]);
  71.                snake[currentlyUpdatedSnakePart] = snakeTemplate[currentlyUpdatedSnakePart - 1];
  72.                console.log("Post Update Currently Updated Snake Part "+currentlyUpdatedSnakePart+" and it's values x y and status "+snake[currentlyUpdatedSnakePart]);
  73.                document.getElementById("partNo"+snake[currentlyUpdatedSnakePart][0]+"/"+snake[currentlyUpdatedSnakePart][1]).style.backgroundColor = "lime";
  74.            }
  75.        }
  76.        for (let i = 0 ; i < (2+2*horisontalfov)*(2+2*verticalfov) ; i++){
  77.            randomizeFoodLocation();
  78.        }
  79.        randomizeFoodLocation()
  80.        function randomizeFoodLocation(){
  81.            let x = 0,y = 0;
  82.            for (i=0;i==0;){
  83.                x =Math.floor(Math.random() * (1+2*horisontalfov));
  84.                y =Math.floor(Math.random() * (1+2*verticalfov));
  85.                for (j=1;j<snake.length;j++){
  86.                    if (x!=snake[j][0] && y!=snake[j][1]){
  87.                        i++;
  88.                    }else{}
  89.                    console.log(snake[j])
  90.                }
  91.            }
  92.            foodPosition[0] = x;
  93.            foodPosition[1] = y;
  94.            console.log(foodPosition);
  95.            document.getElementById("partNo"+(foodPosition[0])+"/"+(foodPosition[1])).style.backgroundColor = "red";
  96.        }
  97.    </script>
  98. </body>
  99. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement