Advertisement
Guest User

sneik

a guest
Apr 26th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.24 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Snake</title>
  6. </head>
  7. <body>
  8. <canvas id="cvs" style="background-color: antiquewhite"></canvas>
  9. <script>
  10.  
  11. //-------------------------------------------------------------------------------------
  12. //------ Bugs -------------------------------------------------------------
  13. //-------------------------------------------------------------------------------------
  14.  
  15. //apple spawns partially outside of canvas sometimes.
  16.  
  17. //-------------------------------------------------------------------------------------
  18. //------ Global Variables -------------------------------------------------------------
  19. //-------------------------------------------------------------------------------------
  20.  
  21. // DOM model of canvas
  22. var cvs = null;
  23. // 2D konrext of canvas
  24. var ctx = null;
  25. // Handle to inretval event
  26. var hndInterval = null;
  27. // Snake game grid size
  28. var gridSize = {col: 100, row: 70};
  29. //apple random
  30. let aX;
  31.  
  32. let aY;
  33.  
  34. AppleAte= 0;
  35.  
  36. //apple random defined
  37. function Applereset(){
  38. aX= Math.floor(Math.random() * cvs.width) - apple.width;
  39.  
  40. aY= Math.floor(Math.random() * cvs.height) - apple.height;
  41. }
  42.  
  43. // Apple
  44. var apple=new Image();
  45.  
  46. apple.src="images/apple.png";
  47.  
  48.  
  49.  
  50. //-------------------------------------------------------------------------------------
  51. //------ Classes and Objects-----------------------------------------------------------
  52. //-------------------------------------------------------------------------------------
  53.  
  54. var ESnakeDir = {
  55. Positive: 1,
  56. Negative: -1,
  57. None: 0
  58. };
  59.  
  60. var currentDirLR = ESnakeDir.Positive;
  61. var currentDirUD = ESnakeDir.None;
  62.  
  63. var SnakeElementSize = {
  64. width: 10,
  65. height: 10
  66. };
  67.  
  68.  
  69.  
  70.  
  71.  
  72. //-------------------------------------------------------------------------------------
  73. //------ Functions --------------------------------------------------------------------
  74. //-------------------------------------------------------------------------------------
  75.  
  76. var Snake = {x:500,y:350};
  77.  
  78. // Updates Canvas on interval timeout
  79. function drawCanvas() {
  80. ctx.clearRect(0, 0, cvs.width, cvs.height);
  81. ctx.fillRect(Snake.x,Snake.y,20,20);
  82. Snake.x +=(10 * currentDirLR);
  83. Snake.y +=(10 * currentDirUD);
  84.  
  85.  
  86. ctx.drawImage(apple, aX,aY,30,30);}
  87.  
  88.  
  89. //-------------------------------------------------------------------------------------
  90.  
  91.  
  92. function startGame() {
  93. hndInterval = setInterval(drawCanvas, 150);
  94. Applereset();
  95.  
  96. }
  97.  
  98. //-------------------------------------------------------------------------------------
  99.  
  100. //-------------------------------------------------------------------------------------
  101. //------ Events -----------------------------------------------------------------------
  102. //-------------------------------------------------------------------------------------
  103.  
  104. function keyDown(event) {
  105. switch (event.key) {
  106. case "ArrowUp":
  107. currentDirUD = ESnakeDir.Negative;
  108. currentDirLR = ESnakeDir.None;
  109. break;
  110. case "ArrowLeft":
  111. currentDirLR = ESnakeDir.Negative;
  112. currentDirUD = ESnakeDir.None;
  113. break;
  114. case "ArrowRight":
  115. currentDirLR = ESnakeDir.Positive;
  116. currentDirUD = ESnakeDir.None;
  117. break;
  118. case "ArrowDown":
  119. currentDirUD = ESnakeDir.Positive;
  120. currentDirLR =ESnakeDir.None;
  121. break;
  122. }
  123. }
  124.  
  125. //-------------------------------------------------------------------------------------
  126. //------ Main code --------------------------------------------------------------------
  127. //-------------------------------------------------------------------------------------
  128.  
  129. //This function runs after all HTML elements are created and starts the game
  130. window.onload = function () {
  131. cvs = document.getElementById("cvs");
  132. cvs.width = SnakeElementSize.width * gridSize.col;
  133. cvs.height = SnakeElementSize.height * gridSize.row;
  134. ctx = cvs.getContext('2d');
  135. document.addEventListener("keydown", keyDown, false);
  136. startGame();
  137. }
  138. //-------------------------------------------------------------------------------------
  139.  
  140. </script>
  141. </body>
  142. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement