Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.82 KB | None | 0 0
  1. <doctype HTML>
  2. <head>
  3. <title>
  4. simple snake game
  5. </title>
  6. <style>
  7. canvas {
  8. display: block;
  9. position: absolute;
  10. border: 1px solid #000;
  11. margin: auto;
  12. top: 0;
  13. bottom:0;
  14. right: 0;
  15. left:0;
  16. }
  17. </style>
  18. </head>
  19. <body>
  20. <script>
  21. //constants
  22. var COLS=26, ROWS=26;
  23. //IDs
  24. var EMPTY=0, SNAKE=1, FRUIT=2;
  25. //Directions
  26. var LEFT=0, UP=1, RIGTH=2, DOWN=3;
  27. //keycodes
  28. var KEY_LEFT=37, KEY_UP=38, KEY_RIGHT=39,KEY_DOWN=40;
  29.  
  30. var grid = {
  31. width:null,
  32. height:null,
  33. _grid:null,
  34.  
  35. init:function(d,c,r)
  36. {
  37. this.width =c;
  38. this.height=r;
  39. this._grid=[];
  40.  
  41. for(var x=0;x<c;x++)
  42. {
  43. this._grid.push([]);
  44. for (var y=0;y< r;y++)
  45. {
  46. this._grid[x].push(d);
  47. }
  48. }
  49. },
  50.  
  51. set :function(val,x,y) {
  52. this._grid[x][y] = val;
  53. },
  54. get :function(x,y) {
  55. return this._grid[x][y];
  56. }
  57. }
  58. var snake = {
  59. direction:null,
  60. last: null,
  61. _queue:null,
  62.  
  63. init: function(d,x,y) {
  64. this.direction = d;
  65.  
  66. this._queue = [];
  67. this.insert(x,y);
  68.  
  69. },
  70. insert: function(x,y) {
  71. this._queue.unshift({x:x, y:y});
  72. this.last = this._queue[0];
  73. },
  74. remove: function() {
  75. return this._queue.pop();
  76. }
  77.  
  78. }
  79.  
  80. function setfood() {
  81. var empty = [];
  82. for (var x=0; x < grid.width; x++) {
  83. for(var y=0; y < grid.height; y++) {
  84. if (grid.get(x,y) === EMPTY) {
  85. empty.push({x:x, y:y});
  86. }
  87. }
  88. }
  89. var randpos = empty[Math.round(Math.random()*(empty.length -1))];
  90. grid.set(FRUIT, randpos.x, randpos.y);
  91. }
  92.  
  93. // Game objects
  94. var canvas, ctx, keystate, frames, score;
  95. function main() {
  96. canvas=document.createElement("canvas");
  97. canvas.width=COLS*20;
  98. canvas.height=ROWS*20;
  99. ctx=canvas.getContext("2d");
  100. document.body.appendChild(canvas);
  101.  
  102. ctx.font = "12px Helvatica";
  103.  
  104. frames=0;
  105. keystate={};
  106. document.addEventListener("keydown", function(evt) {
  107. keystate[evt.keycode]= true;
  108. });
  109. document.addEventListener("keyup", function(evt) {
  110. delete keystate[evt.keycode];
  111. });
  112.  
  113. init();
  114. loop();
  115. }
  116. function init() {
  117. score = 0;
  118.  
  119. grid.init(EMPTY, COLS, ROWS);
  120.  
  121. var sp = {x:Math.floor(COLS/2), y:ROWS-1};
  122. snake.init(UP, sp.x, sp.y);
  123. grid.set(SNAKE, sp.x, sp.y);
  124.  
  125. setfood();
  126. }
  127. function loop () {
  128. update();
  129. draw();
  130.  
  131. window.requestAnimationFrame(loop, canvas);
  132. }
  133. function update() {
  134. frames++;
  135.  
  136. if (keystate[KEY_LEFT]&& snake.direction !== RIGHT) snake.direction = LEFT;
  137. if (keystate[KEY_UP]&& snake.direction !== DOWN) snake.direction = UP;
  138. if (keystate[KEY_RIGHT]&& snake.direction !== LEFT) snake.direction = RIGHT;
  139. if (keystate[KEY_DOWN]&& snake.direction !== UP) snake.direction = DOWN;
  140.  
  141. if (frames%5 === 0) {
  142. var nx = snake.last.x;
  143. var ny = snake.last.y;
  144.  
  145. switch (snake.direction) {
  146. case LEFT:
  147. nx--;
  148. break;
  149. case UP:
  150. ny--;
  151. break;
  152. case RIGHT:
  153. nx++;
  154. break;
  155. case DOWN:
  156. ny++;
  157. break;
  158.  
  159. }
  160.  
  161. if (0 > nx || nx > grid.width-1||
  162. 0 > ny || ny > grid.height-1 ||
  163. grid.get(nx, ny) === SNAKE
  164. ){
  165. return init();
  166. }
  167.  
  168. if (grid.get(nx, ny) ===FRUIT){
  169. var tail= {x:nx, y:ny};
  170. score++;
  171. setfood();
  172. }else{
  173. var tail = snake.remove();
  174. grid.set(EMPTY, tail.x, tail.y);
  175. //tail.x = nx;
  176. //tail.y= ny;
  177. }
  178.  
  179. grid.set(SNAKE, tail.x, tail.y);
  180.  
  181. snake.insert(tail.x, tail.y);
  182. }
  183. }
  184. function draw() {
  185. var tw= canvas.width/grid.width;
  186. var th= canvas.height/grid.height;
  187. for (var x=0; x < grid.width; x++) {
  188. for(var y=0; y < grid.height; y++) {
  189. switch (grid.get(x, y)) {
  190. case EMPTY:
  191. ctx.fillStyle = "#fff";
  192. break;
  193. case SNAKE:
  194. ctx.fillStyle = "#0ff";
  195. break;
  196. case FRUIT:
  197. ctx.fillStyle = "#f00";
  198. break;
  199. }
  200. ctx.fillRect(x*tw, y*th, tw, th);
  201. }
  202. }
  203. ctx.fillStyle = "#000";
  204. ctx.fillText("SCORE:" + score,10, canvas.height-10);
  205. }
  206. main();
  207.  
  208.  
  209. </script>
  210. </body>
  211. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement