Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.64 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="pt">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Snake</title>
  6. <script >
  7. document.addEventListener("DOMContentLoaded", ()=>{
  8.  
  9. const canvas = document.getElementById("Game");
  10. const canvas2d = canvas.getContext("2d");
  11.  
  12. const box = 32;
  13.  
  14. /*carregar as imagens*/
  15. const groundImg = new Image();
  16. groundImg.src = "snake/img/ground.png";
  17. const foodImg = new Image();
  18. foodImg.src = "snake/img/food.png";
  19.  
  20. /*carregar o audio*/
  21. const deadAud = new Audio("snake/audio/dead.mp3")
  22. const eatAud = new Audio("snake/audio/down.mp3")
  23. const downAud = new Audio("snake/audio/eat.mp3")
  24. const leftAud = new Audio("snake/audio/left.mp3")
  25. const rightAud = new Audio("snake/audio/right.mp3")
  26. const upAud = new Audio("snake/audio/up.mp3")
  27.  
  28. let food = {
  29. x:Math.floor(Math.random() * 17 + 1) * box,
  30. y:Math.floor(Math.random() * 15 + 3) * box
  31. };
  32.  
  33. const snake = [];
  34.  
  35. snake[0] = {
  36. x: 10 * box,
  37. y:9 * box
  38. };
  39.  
  40. let score = 0;
  41. let direction;
  42.  
  43. const setDirection = (event) => {
  44.  
  45. if(event.keyCode === 37 && direction !== "right"){
  46. direction = "left";
  47. }
  48. else if (event.keyCode === 38 && direction !== "down"){
  49. direction = "up";
  50. }
  51. else if (event.keyCode === 39 && direction !== "left"){
  52. direction = "right";
  53. }
  54. else if (event.keyCode === 40 && direction !== "up"){
  55. direction = "down";
  56. }
  57. }
  58. document.addEventListener("keydown", setDirection);
  59.  
  60. const collisionChecker = (snake) => {
  61. for(let i = 1; i < snake.length; i++){
  62. if(snake[0].x === snake[i].x && snake[0].y === snake[i].y){
  63. return true;
  64. }
  65. }
  66.  
  67. return false;
  68. }
  69.  
  70. const game = setInterval(()=>{
  71. canvas2d.drawImage(groundImg,0 ,0);
  72. canvas2d.drawImage(foodImg,food.x,food.y);
  73.  
  74. for(let i = 0; i < snake.length; i++){
  75. canvas2d.fillStyle = (i === 0 ) ? "#0F0" : "green";
  76.  
  77. canvas2d.fillRect(snake[i].x, snake[i].y,box,box);
  78.  
  79. canvas2d.strokeStyle = "#F00";
  80. canvas2d.strokeRect(snake[i].x, snake[i].y,box,box);
  81. }
  82.  
  83. /*guardar a cabeça atual*/
  84. let snakeX = snake[0].x;
  85. let snakeY = snake[0].y;
  86.  
  87. /*verificar se conseguiu comer*/
  88. if(snakeX === food.x && snakeY === food.y){
  89. score++;
  90.  
  91. eatAud.play();
  92.  
  93. food = {
  94. x:Math.floor(Math.random() * 17 + 1) * box,
  95. y:Math.floor(Math.random() * 15 + 3) * box
  96. };
  97. }
  98. else {
  99. snake.pop();
  100. }
  101.  
  102. if (direction === "left"){
  103. snakeX -= box; /* snake[0].x = snake[0].x - box; <=> snake[0].x -= box;*/
  104. }
  105. else if(direction === "up"){
  106. snakeY -= box;
  107. }
  108. else if(direction === "down"){
  109. snakeY += box;
  110. }
  111. else if(direction === "right"){
  112. snakeX += box;
  113. }
  114.  
  115. /*verificar se houve Game Over*/
  116. if(snakeX < box ||
  117. snakeX > 17 * box ||
  118. snakeY < 3 * box ||
  119. snakeY > 17 * box ||
  120. collisionChecker(snake)
  121. ) {
  122. clearInterval(game);
  123. deadAud.play();
  124.  
  125. setTimeout (()=>{alert("take the L noob!");},10)
  126. window.location.reload();
  127. }
  128.  
  129. const newHead = {
  130. x: snakeX,
  131. y:snakeY
  132. };
  133.  
  134. snake.unshift(newHead);
  135.  
  136. canvas2d.fillStyle = "#FFF";
  137. canvas2d.font = "45px Calibri";
  138. canvas2d.fillText(score, 2 * box,1.6 * box);
  139.  
  140.  
  141.  
  142.  
  143. },100);
  144. });
  145. </script>
  146. </head>
  147. <body>
  148. <div>
  149. <canvas id="Game" width="608" height="608"></canvas>
  150. </div>
  151. </body>
  152. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement