snake


SUBMITTED BY: wesleyslash41

DATE: July 22, 2024, 11:49 a.m.

FORMAT: Text only

SIZE: 6.2 kB

HITS: 143

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Jogo da Cobrinha</title>
  7. <link rel="stylesheet" href="style.css">
  8. <style type="text/css" id="dcoder_stylesheet">body {
  9. display: flex;
  10. flex-direction: column;
  11. align-items: center;
  12. justify-content: center;
  13. height: 100vh;
  14. margin: 0;
  15. background-color: #000;
  16. color: #fff;
  17. font-family: Arial, sans-serif;
  18. }
  19. h1 {
  20. margin-bottom: 20px;
  21. }
  22. canvas {
  23. border: 1px solid #fff;
  24. }
  25. #controls {
  26. display: flex;
  27. flex-direction: column;
  28. align-items: center;
  29. margin-top: 20px;
  30. }
  31. .control-row {
  32. display: flex;
  33. justify-content: center;
  34. }
  35. button {
  36. width: 50px;
  37. height: 50px;
  38. margin: 5px;
  39. font-size: 18px;
  40. background-color: #444;
  41. color: #fff;
  42. border: none;
  43. border-radius: 5px;
  44. cursor: pointer;
  45. }
  46. button:hover {
  47. background-color: #666;
  48. }
  49. #inputContainer {
  50. display: flex;
  51. flex-direction: column;
  52. align-items: center;
  53. }
  54. #inputContainer label {
  55. margin-bottom: 10px;
  56. font-size: 20px;
  57. }
  58. #inputContainer input {
  59. margin-bottom: 10px;
  60. padding: 5px;
  61. font-size: 18px;
  62. }</style></head>
  63. <body>
  64. <h1>Jogo da Cobrinha</h1>
  65. <div id="inputContainer">
  66. <label for="playerName">Nome do Jogador:</label>
  67. <input type="text" id="playerName">
  68. <button onclick="startGame()">Iniciar Jogo</button>
  69. </div>
  70. <canvas id="gameCanvas" width="400" height="400" style="display: none;"></canvas>
  71. <div id="controls" style="display: none;">
  72. <div class="control-row">
  73. <button onclick="setDirection('UP')">8</button>
  74. </div>
  75. <div class="control-row">
  76. <button onclick="setDirection('LEFT')">4</button>
  77. <button onclick="setDirection('DOWN')">2</button>
  78. <button onclick="setDirection('RIGHT')">6</button>
  79. </div>
  80. </div>
  81. <script src="script.js"></script>
  82. <script type="text/javascript" id="dcoder_script">const canvas = document.getElementById("gameCanvas");
  83. const ctx = canvas.getContext("2d");
  84. const box = 20;
  85. const rows = canvas.height / box;
  86. const cols = canvas.width / box;
  87. let snake;
  88. let food;
  89. let score;
  90. let d;
  91. let game;
  92. let playerName;
  93. let highScore = localStorage.getItem("highScore") || 0;
  94. let highScorePlayer = localStorage.getItem("highScorePlayer") || "N/A";
  95. function startGame() {
  96. playerName = document.getElementById("playerName").value || "Jogador";
  97. document.getElementById("inputContainer").style.display = "none";
  98. canvas.style.display = "block";
  99. document.getElementById("controls").style.display = "block";
  100. init();
  101. }
  102. function init() {
  103. snake = [];
  104. snake[0] = { x: Math.floor(cols / 2) * box, y: Math.floor(rows / 2) * box };
  105. food = {
  106. x: Math.floor(Math.random() * cols) * box,
  107. y: Math.floor(Math.random() * rows) * box
  108. };
  109. score = 0;
  110. d = "RIGHT"; // Movimento inicial da cobra
  111. if (game) clearInterval(game);
  112. game = setInterval(draw, 100);
  113. }
  114. document.addEventListener("keydown", direction);
  115. function direction(event) {
  116. switch (event.keyCode) {
  117. case 37: // seta para esquerda
  118. case 100: // número 4 no teclado numérico
  119. if (d != "RIGHT") d = "LEFT";
  120. break;
  121. case 38: // seta para cima
  122. case 104: // número 8 no teclado numérico
  123. if (d != "DOWN") d = "UP";
  124. break;
  125. case 39: // seta para direita
  126. case 102: // número 6 no teclado numérico
  127. if (d != "LEFT") d = "RIGHT";
  128. break;
  129. case 40: // seta para baixo
  130. case 98: // número 2 no teclado numérico
  131. if (d != "UP") d = "DOWN";
  132. break;
  133. }
  134. }
  135. function setDirection(newDirection) {
  136. if (newDirection === 'LEFT' && d !== 'RIGHT') d = 'LEFT';
  137. if (newDirection === 'UP' && d !== 'DOWN') d = 'UP';
  138. if (newDirection === 'RIGHT' && d !== 'LEFT') d = 'RIGHT';
  139. if (newDirection === 'DOWN' && d !== 'UP') d = 'DOWN';
  140. }
  141. function collision(head, array) {
  142. for (let i = 0; i < array.length; i++) {
  143. if (head.x == array[i].x && head.y == array[i].y) {
  144. return true;
  145. }
  146. }
  147. return false;
  148. }
  149. function draw() {
  150. ctx.clearRect(0, 0, canvas.width, canvas.height);
  151. for (let i = 0; i < snake.length; i++) {
  152. ctx.fillStyle = (i == 0) ? "green" : "white";
  153. ctx.fillRect(snake[i].x, snake[i].y, box, box);
  154. ctx.strokeStyle = "red";
  155. ctx.strokeRect(snake[i].x, snake[i].y, box, box);
  156. }
  157. ctx.fillStyle = "red";
  158. ctx.fillRect(food.x, food.y, box, box);
  159. let snakeX = snake[0].x;
  160. let snakeY = snake[0].y;
  161. if (d == "LEFT") snakeX -= box;
  162. if (d == "UP") snakeY -= box;
  163. if (d == "RIGHT") snakeX += box;
  164. if (d == "DOWN") snakeY += box;
  165. if (snakeX == food.x && snakeY == food.y) {
  166. score++;
  167. food = {
  168. x: Math.floor(Math.random() * cols) * box,
  169. y: Math.floor(Math.random() * rows) * box
  170. };
  171. } else {
  172. snake.pop();
  173. }
  174. let newHead = {
  175. x: snakeX,
  176. y: snakeY
  177. };
  178. if (snakeX < 0 || snakeY < 0 || snakeX >= canvas.width || snakeY >= canvas.height || collision(newHead, snake)) {
  179. // Salvar a pontuação mais alta
  180. if (score > highScore) {
  181. highScore = score;
  182. highScorePlayer = playerName;
  183. localStorage.setItem("highScore", highScore);
  184. localStorage.setItem("highScorePlayer", highScorePlayer);
  185. }
  186. init(); // Reinicia o jogo
  187. } else {
  188. snake.unshift(newHead);
  189. }
  190. ctx.fillStyle = "white";
  191. ctx.font = "20px Changa one";
  192. ctx.fillText("Jogador: " + playerName, 2 * box, 1.2 * box);
  193. ctx.fillText("Recorde: " + highScorePlayer + " (" + highScore + ")", 2 * box, 2.2 * box);
  194. ctx.font = "45px Changa one";
  195. ctx.fillText(score, 2 * box, 3.2 * box);
  196. }
  197. init();</script></body></html>

comments powered by Disqus