snake game HTML & JavaScript


SUBMITTED BY: thetruebeef

DATE: Nov. 15, 2018, 6:16 p.m.

FORMAT: Text only

SIZE: 9.5 kB

HITS: 269

  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  5. <title>Feed the Snake v 1.1 beta</title>
  6. <style>
  7. body
  8. {
  9. background:#000;
  10. color:#FFF;
  11. }
  12. canvas
  13. {
  14. background:#FFF;
  15. }
  16. #controls
  17. {
  18. position:absolute;
  19. top:0;
  20. right:0;
  21. margin:10px;
  22. }
  23. </style>
  24. <script type="text/javascript">
  25. var snake = window.snake || {};
  26. function launchFullscreen(element) {
  27. if(element.requestFullscreen) {
  28. element.requestFullscreen();
  29. } else if(element.mozRequestFullScreen) {
  30. element.mozRequestFullScreen();
  31. } else if(element.webkitRequestFullscreen) {
  32. element.webkitRequestFullscreen();
  33. } else if(element.msRequestFullscreen) {
  34. element.msRequestFullscreen();
  35. }
  36. }
  37. window.onload = function(){
  38. document.addEventListener("fullscreenchange", function(){snake.game.adjust();});
  39. document.addEventListener("webkitfullscreenchange", function(){snake.game.adjust();});
  40. document.addEventListener("mozfullscreenchange", function(){snake.game.adjust();});
  41. document.addEventListener("MSFullscreenChange", function(){snake.game.adjust();});
  42. snake.game = (function()
  43. {
  44. var canvas = document.getElementById('canvas');
  45. var ctx = canvas.getContext('2d');
  46. var status=false;
  47. var score = 0;
  48. var old_direction = 'right';
  49. var direction = 'right';
  50. var block = 10;
  51. var score = 0;
  52. var refresh_rate = 250;
  53. var pos = [[5,1],[4,1],[3,1],[2,1],[1,1]];
  54. var scoreboard = document.getElementById('scoreboard');
  55. var control = document.getElementById('controls');
  56. var keys = {
  57. 37 : 'left',
  58. 38 : 'up',
  59. 39 : 'right',
  60. 40 : 'down'
  61. };
  62. function adjust()
  63. {
  64. if (document.fullscreenElement || document.webkitFullscreenElement || document.mozFullScreenElement || document.msFullscreenElement )
  65. {
  66. canvas.width=window.innerWidth;
  67. canvas.height=window.innerHeight;
  68. control.style.display='none';
  69. }
  70. else
  71. {
  72. canvas.width=850;
  73. canvas.height=600;
  74. control.style.display='inline';
  75. }
  76. }
  77. var food = [Math.round(Math.random(4)*(canvas.width - 10)), Math.round(Math.random(4)*(canvas.height - 10)),];
  78. function todraw()
  79. {
  80. for(var i = 0; i < pos.length; i++)
  81. {
  82. draw(pos[i]);
  83. }
  84. }
  85. function giveLife()
  86. {
  87. var nextPosition = pos[0].slice();
  88. switch(old_direction)
  89. {
  90. case 'right':
  91. nextPosition[0] += 1;
  92. break;
  93. case 'left':
  94. nextPosition[0] -= 1;
  95. break;
  96. case 'up':
  97. nextPosition[1] -= 1;
  98. break;
  99. case 'down':
  100. nextPosition[1] += 1;
  101. break;
  102. }
  103. pos.unshift(nextPosition);
  104. pos.pop();
  105. }
  106. function grow()
  107. {
  108. var nextPosition = pos[0].slice();
  109. switch(old_direction)
  110. {
  111. case 'right':
  112. nextPosition[0] += 1;
  113. break;
  114. case 'left':
  115. nextPosition[0] -= 1;
  116. break;
  117. case 'up':
  118. nextPosition[1] -= 1;
  119. break;
  120. case 'down':
  121. nextPosition[1] += 1;
  122. break;
  123. }
  124. pos.unshift(nextPosition);
  125. }
  126. function loop()
  127. {
  128. ctx.clearRect(0,0,canvas.width,canvas.height);
  129. todraw();
  130. giveLife();
  131. feed();
  132. if(is_catched(pos[0][0]*block,pos[0][1]*block,block,block,food[0],food[1],10,10))
  133. {
  134. score += 10;
  135. createfood();
  136. scoreboard.innerHTML = score;
  137. grow();
  138. if(refresh_rate > 100)
  139. {
  140. refresh_rate -=5;
  141. }
  142. }
  143. snake.game.status = setTimeout(function() { loop(); },refresh_rate);
  144. }
  145. window.onkeydown = function(event){
  146. direction = keys[event.keyCode];
  147. if(direction)
  148. {
  149. setWay(direction);
  150. event.preventDefault();
  151. }
  152. };
  153. function setWay(direction)
  154. {
  155. switch(direction)
  156. {
  157. case 'left':
  158. if(old_direction!='right')
  159. {
  160. old_direction = direction;
  161. }
  162. break;
  163. case 'right':
  164. if(old_direction!='left')
  165. {
  166. old_direction = direction;
  167. }
  168. break;
  169. case 'up':
  170. if(old_direction!='down')
  171. {
  172. old_direction = direction;
  173. }
  174. break;
  175. case 'down':
  176. if(old_direction!='up')
  177. {
  178. old_direction = direction;
  179. }
  180. break;
  181. }
  182. }
  183. function feed()
  184. {
  185. ctx.beginPath();
  186. ctx.fillStyle = "#ff0000";
  187. ctx.fillRect(food[0],food[1],10,10);
  188. ctx.fill();
  189. ctx.closePath();
  190. }
  191. function createfood()
  192. {
  193. food = [Math.round(Math.random(4)*850), Math.round(Math.random(4)*600)];
  194. }
  195. function is_catched(ax,ay,awidth,aheight,bx,by,bwidth,bheight) {
  196. return !(
  197. ((ay + aheight) < (by)) ||
  198. (ay > (by + bheight)) ||
  199. ((ax + awidth) < bx) ||
  200. (ax > (bx + bwidth))
  201. );
  202. }
  203. function draw(pos)
  204. {
  205. var x = pos[0] * block;
  206. var y = pos[1] * block;
  207. if(x >= canvas.width || x <= 0 || y >= canvas.height || y<= 0)
  208. {
  209. document.getElementById('pause').disabled='true';
  210. snake.game.status=false;
  211. ctx.clearRect(0,0,canvas.width,canvas.height);
  212. ctx.font='40px san-serif';
  213. ctx.fillText('Game Over',300,250);
  214. ctx.font = '20px san-serif';
  215. ctx.fillStyle='#000000';
  216. ctx.fillText('To Play again Refresh the page or click the Restarts button',200,300);
  217. throw ('Game Over');
  218. }
  219. else
  220. {
  221. ctx.beginPath();
  222. ctx.fillStyle='#000000';
  223. ctx.fillRect(x,y,block,block);
  224. ctx.closePath();
  225. }
  226. }
  227. function pause(elem)
  228. {
  229. if(snake.game.status)
  230. {
  231. clearTimeout(snake.game.status);
  232. snake.game.status=false;
  233. elem.value='Play'
  234. }
  235. else
  236. {
  237. loop();
  238. elem.value='Pause';
  239. }
  240. }
  241. function begin()
  242. {
  243. loop();
  244. }
  245. function restart()
  246. {
  247. location.reload();
  248. }
  249. function start()
  250. {
  251. ctx.fillStyle='#000000';
  252. ctx.fillRect(0,0,canvas.width,canvas.height);
  253. ctx.fillStyle='#ffffff';
  254. ctx.font='40px helvatica';
  255. ctx.fillText('Vignesh',370,140);
  256. ctx.font='20px san-serif';
  257. ctx.fillText('presents',395,190);
  258. ctx.font='italic 60px san-serif';
  259. ctx.fillText('Feed The Snake',240,280);
  260. var img = new Image();
  261. img.onload = function()
  262. {
  263. ctx.drawImage(img,300,300,200,200);
  264. ctx.fillRect(410,330,10,10);
  265. }
  266. img.src ='snake.png';
  267. }
  268. function fullscreen()
  269. {
  270. launchFullscreen(canvas);
  271. }
  272. return {
  273. pause: pause,
  274. restart : restart,
  275. start : start,
  276. begin: begin,
  277. fullscreen : fullscreen,
  278. adjust : adjust,
  279. };
  280. })();
  281. snake.game.start();
  282. }
  283. </script>
  284. </head>
  285. <body>
  286. <canvas width="850" height="600" id="canvas" style="border:1px solid #333;" onclick="snake.game.begin();">
  287. </canvas>
  288. <div id="controls" style="float:right; text-align:center;">
  289. <input type="button" id="pause" value="Play" onClick="snake.game.pause(this);" accesskey="p">
  290. <input type="button" id="restart" value="Restart" onClick="snake.game.restart();">
  291. <br/><br/>
  292. <input type="button" id="fullscreen" value="Play Fullscreen" onClick="snake.game.fullscreen();">
  293. <br/><br/>
  294. <div style="font-size:24px;">
  295. Score :
  296. <span id="scoreboard">0</span>
  297. </div>
  298. </div>
  299. </body>
  300. </html>

comments powered by Disqus