Advertisement
Guest User

yea

a guest
Apr 25th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.90 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" width="1000" height="700"></canvas>
  9. <script>
  10. //-------------------------------------------------------------------------------------
  11. //------ Global Variables -------------------------------------------------------------
  12. //-------------------------------------------------------------------------------------
  13.  
  14. // DOM model of canvas
  15. var cvs = null;
  16. // 2D konrext of canvas
  17. var ctx = null;
  18. // Handle to inretval event
  19. var hndInterval = null;
  20. // Snake game grid size
  21. var gridSize = {col: 100, row: 70};
  22. // Counts number of frames
  23. var frameCounter = 0;
  24.  
  25. var cvsH = cvs.height;
  26. var cvsW = cvs.width;
  27.  
  28. //-------------------------------------------------------------------------------------
  29. //------ Classes and Objects-----------------------------------------------------------
  30. //-------------------------------------------------------------------------------------
  31.  
  32. var ESnakeDir = {
  33. Positive: 1,
  34. Negative: -1,
  35. None: 0
  36. };
  37.  
  38. var currentDirLR = ESnakeDir.Positive;
  39. var currentDirUD = ESnakeDir.None;
  40.  
  41. var SnakeElementSize = {
  42. width: 10,
  43. height: 10
  44. };
  45.  
  46.  
  47. //-------------------------------------------------------------------------------------
  48. //------ Functions --------------------------------------------------------------------
  49. //-------------------------------------------------------------------------------------
  50.  
  51. var pos = {x:500,y:350};
  52.  
  53. // Updates Canvas on interval timeout
  54. function drawCanvas() {
  55. ctx.clearRect(0, 0, cvs.width, cvs.height);
  56. ctx.fillRect(pos.x,pos.y,10,10);
  57. pos.x +=(10 * currentDirLR);
  58. pos.y +=(10 * currentDirUD);
  59.  
  60. frameCounter++;
  61. }
  62.  
  63. function reloadGameAfterHit() {
  64. if (pos.x < 0 || pos.y < 0 || pos.x >= cvsW / SnakeElementSize.width || pos.y >= cvsH / SnakeElementSize.height) {
  65. location.reload()
  66. }
  67. }
  68.  
  69.  
  70. //-------------------------------------------------------------------------------------
  71.  
  72. function startGame() {
  73. hndInterval = setInterval(drawCanvas, 50);
  74. reloadGameAfterHit();
  75. }
  76.  
  77. //-------------------------------------------------------------------------------------
  78.  
  79. //-------------------------------------------------------------------------------------
  80. //------ Events -----------------------------------------------------------------------
  81. //-------------------------------------------------------------------------------------
  82.  
  83. function keyDown(event) {
  84. switch (event.key) {
  85. case "ArrowUp":
  86. currentDirUD = ESnakeDir.Negative;
  87. currentDirLR = ESnakeDir.None;
  88. break;
  89. case "ArrowLeft":
  90. currentDirLR = ESnakeDir.Negative;
  91. currentDirUD = ESnakeDir.None;
  92. break;
  93. case "ArrowRight":
  94. currentDirLR = ESnakeDir.Positive;
  95. currentDirUD = ESnakeDir.None;
  96. break;
  97. case "ArrowDown":
  98. currentDirUD = ESnakeDir.Positive;
  99. currentDirLR =ESnakeDir.None;
  100. break;
  101. }
  102. }
  103.  
  104. //-------------------------------------------------------------------------------------
  105. //------ Main code --------------------------------------------------------------------
  106. //-------------------------------------------------------------------------------------
  107.  
  108. //This function runs after all HTML elements are created and starts the game
  109. window.onload = function () {
  110. cvs = document.getElementById("cvs");
  111. cvs.width = SnakeElementSize.width * gridSize.col;
  112. cvs.height = SnakeElementSize.height * gridSize.row;
  113. ctx = cvs.getContext('2d');
  114. document.addEventListener("keydown", keyDown, false);
  115. startGame();
  116. };
  117. //-------------------------------------------------------------------------------------
  118.  
  119. </script>
  120. </body>
  121. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement