Hindi, asked by nit57, 8 months ago

वर्ण और वमिाला
क वर्ण के कल कितने मेढ?? उनके साम

Answers

Answered by SpammerTraining
0

 &lt;!DOCTYPE html&gt;</p><p>&lt;html&gt;</p><p>&lt;head&gt;</p><p>  &lt;title&gt;&lt;/title&gt;</p><p>  &lt;style&gt;</p><p>  html, body {</p><p>    height: 100%;</p><p>    margin: 0;</p><p>  }</p><p>  body {</p><p>    background: black;</p><p>    display: flex;</p><p>    align-items: center;</p><p>    justify-content: center;</p><p>  }</p><p>  canvas {</p><p>    border: 1px solid white;</p><p>  }</p><p>  &lt;/style&gt;</p><p>&lt;/head&gt;</p><p>&lt;body&gt;</p><p>&lt;canvas width="400" height="400" id="game"&gt;&lt;/canvas&gt;</p><p>&lt;script&gt;</p><p>var canvas = document.getElementById('game');</p><p>var context = canvas.getContext('2d');</p><p>var grid = 16;</p><p>var count = 0;</p><p>  </p><p>var snake = {</p><p>  x: 160,</p><p>  y: 160,</p><p>  </p><p>  // snake velocity. moves one grid length every frame in either the x or y direction</p><p>  dx: grid,</p><p>  dy: 0,</p><p>  </p><p>  // keep track of all grids the snake body occupies</p><p>  cells: [],</p><p>  </p><p>  // length of the snake. grows when eating an apple</p><p>  maxCells: 4</p><p>};</p><p>var apple = {</p><p>  x: 320,</p><p>  y: 320</p><p>};</p><p>// get random whole numbers in a specific range</p><p>// @see https://stackoverflow.com/a/1527820/2124254</p><p>function getRandomInt(min, max) {</p><p>  return Math.floor(Math.random() * (max - min)) + min;</p><p>}</p><p>// game loop</p><p>function loop() {</p><p>  requestAnimationFrame(loop);</p><p>  // slow game loop to 15 fps instead of 60 (60/15 = 4)</p><p>  if (++count &lt; 4) {</p><p>    return;</p><p>  }</p><p>  count = 0;</p><p>  context.clearRect(0,0,canvas.width,canvas.height);</p><p>  // move snake by it's velocity</p><p>  snake.x += snake.dx;</p><p>  snake.y += snake.dy;</p><p>  // wrap snake position horizontally on edge of screen</p><p>  if (snake.x &lt; 0) {</p><p>    snake.x = canvas.width - grid;</p><p>  }</p><p>  else if (snake.x &gt;= canvas.width) {</p><p>    snake.x = 0;</p><p>  }</p><p>  </p><p>  // wrap snake position vertically on edge of screen</p><p>  if (snake.y &lt; 0) {</p><p>    snake.y = canvas.height - grid;</p><p>  }</p><p>  else if (snake.y &gt;= canvas.height) {</p><p>    snake.y = 0;</p><p>  }</p><p>  // keep track of where snake has been. front of the array is always the head</p><p>  snake.cells.unshift({x: snake.x, y: snake.y});</p><p>  // remove cells as we move away from them</p><p>  if (snake.cells.length &gt; snake.maxCells) {</p><p>    snake.cells.pop();</p><p>  }</p><p>  // draw apple</p><p>  context.fillStyle = 'red';</p><p>  context.fillRect(apple.x, apple.y, grid-1, grid-1);</p><p>  // draw snake one cell at a time</p><p>  context.fillStyle = 'green';</p><p>  snake.cells.forEach(function(cell, index) {</p><p>    </p><p>    // drawing 1 px smaller than the grid creates a grid effect in the snake body so you can see how long it is</p><p>    context.fillRect(cell.x, cell.y, grid-1, grid-1);  </p><p>    // snake ate apple</p><p>    if (cell.x === apple.x &amp;&amp; cell.y === apple.y) {</p><p>      snake.maxCells++;</p><p>      // canvas is 400x400 which is 25x25 grids </p><p>      apple.x = getRandomInt(0, 25) * grid;</p><p>      apple.y = getRandomInt(0, 25) * grid;</p><p>    }</p><p>    // check collision with all cells after this one (modified bubble sort)</p><p>    for (var i = index + 1; i &lt; snake.cells.length; i++) {</p><p>      </p><p>      // snake occupies same space as a body part. reset game</p><p>      if (cell.x === snake.cells[i].x &amp;&amp; cell.y === snake.cells[i].y) {</p><p>        snake.x = 160;</p><p>        snake.y = 160;</p><p>        snake.cells = [];</p><p>        snake.maxCells = 4;</p><p>        snake.dx = grid;</p><p>        snake.dy = 0;</p><p>        apple.x = getRandomInt(0, 25) * grid;</p><p>        apple.y = getRandomInt(0, 25) * grid;</p><p>      }</p><p>    }</p><p>  });</p><p>}</p><p>// listen to keyboard events to move the snake</p><p>document.addEventListener('keydown', function(e) {</p><p>  // prevent snake from backtracking on itself by checking that it's </p><p>  // not already moving on the same axis (pressing left while moving</p><p>  // left won't do anything, and pressing right while moving left</p><p>  // shouldn't let you collide with your own body)</p><p>  </p><p>  // left arrow key</p><p>  if (e.which === 37 &amp;&amp; snake.dx === 0) {</p><p>    snake.dx = -grid;</p><p>    snake.dy = 0;</p><p>  }</p><p>  // up arrow key</p><p>  else if (e.which === 38 &amp;&amp; snake.dy === 0) {</p><p>    snake.dy = -grid;</p><p>    snake.dx = 0;</p><p>  }</p><p>  // right arrow key</p><p>  else if (e.which === 39 &amp;&amp; snake.dx === 0) {</p><p>    snake.dx = grid;</p><p>    snake.dy = 0;</p><p>  }</p><p>  // down arrow key</p><p>  else if (e.which === 40 &amp;&amp; snake.dy === 0) {</p><p>    snake.dy = grid;</p><p>    snake.dx = 0;</p><p>  }</p><p>});</p><p>// start the game</p><p>requestAnimationFrame(loop);</p><p>&lt;/script&gt;</p><p>&lt;/body&gt;</p><p>&lt;/html&gt;</p><p>

Similar questions