Advertisement
Guest User

Untitled

a guest
Apr 20th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1. // Get the canvas from HTML file
  2. const canvas = document.getElementById("cvs");
  3. const canvas_context = canvas.getContext("2d");
  4.  
  5. // Add event listener for keys when pressed down
  6. document.addEventListener("keydown", direction);
  7.  
  8. // Set the background
  9. var background = new Image();
  10. background.src = "img/background.png";
  11.  
  12. // Set apple image
  13. var appleImg = new Image();
  14. appleImg.src = "img/apple.png";
  15.  
  16. // Create box unit
  17. const box = 25;
  18.  
  19. // Create snake
  20. var snake = [];
  21.  
  22. // Set the snake's starting position
  23. snake[0] = {
  24. x : 10 * box,
  25. y : 9 * box
  26. }
  27.  
  28. // Create apple
  29. let apple = {
  30. x : Math.floor(Math.random() * 20 + 1) * box,
  31. y : Math.floor(Math.random() * 16 + 1) * box
  32. }
  33.  
  34. // Create score
  35. var score = 0;
  36.  
  37. // Create direction variable;
  38. var dir;
  39.  
  40. // Call function update every 100 ms (= 10 FPS)
  41. var game = setInterval(update, 100);
  42.  
  43. function update() {
  44.  
  45. // Draw background
  46. canvas_context.drawImage(background, 0, 0, background.width, background.height, 0, 0, canvas.width, canvas.height);
  47.  
  48. // Draw snake
  49. for(let i = 0; i < snake.length; i++){
  50.  
  51. if(i == 0)
  52. canvas_context.fillStyle = "white";
  53. else
  54. canvas_context.fillStyle = "green";
  55.  
  56. canvas_context.fillRect(snake[i].x, snake[i].y, box, box);
  57.  
  58. canvas_context.strokeStyle = "black";
  59. canvas_context.strokeRect(snake[i].x, snake[i].y, box, box);
  60. }
  61.  
  62. // Draw the apple
  63. canvas_context.drawImage(appleImg, apple.x, apple.y, 25, 25 * appleImg.height / appleImg.width);
  64.  
  65. // Old snake head's position
  66. let snakeX = snake[0].x;
  67. let snakeY = snake[0].y;
  68.  
  69. // Control direction
  70. // "%" and nested if is used for walking through walls portal effect
  71. if(dir == "LEFT"){
  72. if(snakeX == box)
  73. snakeX = 20 * box;
  74. else
  75. snakeX = snakeX - box;
  76. }
  77. else if(dir == "RIGHT")
  78. snakeX = box + snakeX % (20 * box);
  79. else if(dir == "UP"){
  80. if(snakeY == box)
  81. snakeY = 16 * box;
  82. else
  83. snakeY = snakeY - box;
  84. }
  85. else if(dir == "DOWN")
  86. snakeY = box + snakeY % (16 * box);
  87.  
  88. // Case that snake eats the apple
  89. if(snakeX == apple.x && snakeY == apple.y){
  90. // Increase score
  91. score++;
  92. // Randomize apple's position
  93. apple.x = (Math.floor(Math.random() * 20 + 1) * box);
  94. apple.y = Math.floor(Math.random() * 16 + 1) * box ;
  95.  
  96. } else {
  97. // Remove snake's tail
  98. snake.pop();
  99. }
  100.  
  101. // (OPTIONAL) Wall collision detection - end game
  102. //if(snakeX < 1 * box || snakeX > 20 * box || snakeY > 16 * box || snakeY < 1 * box)
  103. //clearInterval(game);
  104.  
  105. for(let i = 1; i < snake.length; i++){
  106. if(snakeX == snake[i].x && snakeY == snake[i].y)
  107. clearInterval(game);
  108. }
  109.  
  110. // Add snake's new head
  111. let newHead = {
  112. x : snakeX,
  113. y : snakeY
  114. }
  115.  
  116. snake.unshift(newHead);
  117.  
  118.  
  119. }
  120.  
  121.  
  122. var flag = false;
  123. function direction(event){
  124. switch(event.keyCode){
  125. case 37:
  126. if(dir !== "RIGHT" && flag == false ) {
  127.  
  128. dir = "LEFT";
  129. flag = true;
  130. setTimeout(function() { reset_flag(); }, 100);
  131. console.log(dir);
  132. }
  133.  
  134. break;
  135. case 38:
  136. if(dir !== "DOWN" && flag == false){
  137. dir = "UP";
  138. flag = true;
  139. setTimeout(function() { reset_flag(); }, 100);
  140. console.log(dir);
  141. }
  142.  
  143. break;
  144. case 39:
  145. if(dir !== "LEFT" && flag == false){
  146. dir = "RIGHT";
  147. flag = true;
  148. setTimeout(function() { reset_flag(); }, 100);
  149. console.log(dir);
  150. }
  151. break;
  152. case 40:
  153. if(dir !== "UP" && flag == false){
  154. dir = "DOWN";
  155. flag = true;
  156. setTimeout(function() { reset_flag(); }, 100);
  157. console.log(dir);
  158. }
  159. break;
  160. }
  161. }
  162.  
  163. function reset_flag(){
  164. flag = false;
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement