A_N_T_I_C_L_O_N
Mantiqiy savolar yozamiz javob beramaiz
10 soat 46 daqiqa 6 soniyadan keyin yozdi:
Diqqat savol u yashash uchun uyidan vohli chiqib ketadi yasash uchun kopgina konsertlar beradi va ohirgi konserti fojiyali yakun topadi topganlar shaxsiy xabarga yozsin #1 deb
1 kun 10 soat 25 daqiqa 21 soniyadan keyin yozdi:
U savol emas lekin hamma unga javob beradi #2 deb yozila javobi
1 kun 10 soat 25 daqiqa 54 soniyadan keyin yozdi:
  1. <!DOCTYPE html>
  2. html, body {
  3. height: 100%;
  4. margin: 0;
  5. }
  6. body {
  7. background: black;
  8. display: flex;
  9. align-items: center;
  10. justify-content: center;
  11. }
  12. canvas {
  13. border: 1px solid white;
  14. }
  15. </style>
  16. </head>
  17. <canvas width="400" height="400" id="game"></canvas>
  18. var canvas = document.getElementById('game');
  19. var context = canvas.getContext('2d');
  20. var grid = 16;
  21. var count = 0;
  22.  
  23. var snake = {
  24. x: 160,
  25. y: 160,
  26.  
  27. // snake velocity. moves one grid length every frame in either the x or y direction
  28. dx: grid,
  29. dy: 0,
  30.  
  31. // keep track of all grids the snake body occupies
  32. cells: [],
  33.  
  34. // length of the snake. grows when eating an apple
  35. maxCells: 4
  36. };
  37. var apple = {
  38. x: 320,
  39. y: 320
  40. };
  41. // get random whole numbers in a specific range
  42. function getRandomInt(min, max) {
  43. return Math.floor(Math.random() * (max - min)) + min;
  44. }
  45. // game loop
  46. function loop() {
  47. requestAnimationFrame(loop);
  48. // slow game loop to 15 fps instead of 60 (60/15 = 4)
  49. if (++count < 4) {
  50. return;
  51. }
  52. count = 0;
  53. context.clearRect(0,0,canvas.width,canvas.height);
  54. // move snake by it's velocity
  55. snake.x += snake.dx;
  56. snake.y += snake.dy;
  57. // wrap snake position horizontally on edge of screen
  58. if (snake.x < 0) {
  59. snake.x = canvas.width - grid;
  60. }
  61. else if (snake.x >= canvas.width) {
  62. snake.x = 0;
  63. }
  64.  
  65. // wrap snake position vertically on edge of screen
  66. if (snake.y < 0) {
  67. snake.y = canvas.height - grid;
  68. }
  69. else if (snake.y >= canvas.height) {
  70. snake.y = 0;
  71. }
  72. // keep track of where snake has been. front of the array is always the head
  73. snake.cells.unshift({x: snake.x, y: snake.y});
  74. // remove cells as we move away from them
  75. if (snake.cells.length > snake.maxCells) {
  76. snake.cells.pop();
  77. }
  78. // draw apple
  79. context.fillStyle = 'red';
  80. context.fillRect(apple.x, apple.y, grid-1, grid-1);
  81. // draw snake one cell at a time
  82. context.fillStyle = 'green';
  83. snake.cells.forEach(function(cell, index) {
  84.  
  85. // drawing 1 px smaller than the grid creates a grid effect in the snake body so you can see how long it is
  86. context.fillRect(cell.x, cell.y, grid-1, grid-1);
  87. // snake ate apple
  88. if (cell.x === apple.x && cell.y === apple.y) {
  89. snake.maxCells++;
  90. // canvas is 400x400 which is 25x25 grids
  91. apple.x = getRandomInt(0, 25) * grid;
  92. apple.y = getRandomInt(0, 25) * grid;
  93. }
  94. // check collision with all cells after this one (modified bubble sort)
  95. for (var i = index + 1; i < snake.cells.length; i++) {
  96.  
  97. // snake occupies same space as a body part. reset game
  98. if (cell.x === snake.cells[i].x && cell.y === snake.cells[i].y) {
  99. snake.x = 160;
  100. snake.y = 160;
  101. snake.cells = [];
  102. snake.maxCells = 4;
  103. snake.dx = grid;
  104. snake.dy = 0;
  105. apple.x = getRandomInt(0, 25) * grid;
  106. apple.y = getRandomInt(0, 25) * grid;
  107. }
  108. }
  109. });
  110. }
  111. // listen to keyboard events to move the snake
  112. document.addEventListener('keydown', function(e) {
  113. // prevent snake from backtracking on itself by checking that it's
  114. // not already moving on the same axis (pressing left while moving
  115. // left won't do anything, and pressing right while moving left
  116. // shouldn't let you collide with your own body)
  117. // left arrow key
  118. if (e.which === 37 && snake.dx === 0) {
  119. snake.dx = -grid;
  120. snake.dy = 0;
  121. }
  122. // up arrow key
  123. else if (e.which === 38 && snake.dy === 0) {
  124. snake.dy = -grid;
  125. snake.dx = 0;
  126. }
  127. // right arrow key
  128. else if (e.which === 39 && snake.dx === 0) {
  129. snake.dx = grid;
  130. snake.dy = 0;
  131. }
  132. // down arrow key
  133. else if (e.which === 40 && snake.dy === 0) {
  134. snake.dy = grid;
  135. snake.dx = 0;
  136. }
  137. });
  138. // start the game
  139. requestAnimationFrame(loop);
  140. </script>
  141. </body>
  142. </html>