Simple website with some JavaScript games.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

298 lines
7.7 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. <script>
  2. window.onload = function()
  3. {
  4. var canvas = document.getElementById("canvas");
  5. var c = canvas.getContext('2d');
  6. c.fillStyle= "black";
  7. c.fillRect(0,0, canvas.width, canvas.height);
  8. };
  9. //entire game
  10. function play()
  11. {
  12. var animate = window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || function (callback)
  13. {
  14. window.setTimeout(callback, 1000 / 60)
  15. };
  16. var canvas = document.getElementById("canvas");
  17. var width = 700;
  18. var height = 700;
  19. canvas.style.textAligh = 'center';
  20. var context = canvas.getContext('2d');
  21. var keysDown = {};
  22. //player variables;
  23. var score = 0;
  24. var alive = true;
  25. //usefull function
  26. function getRandomIntInclusive(min, max)
  27. {
  28. min = Math.ceil(min);
  29. max = Math.floor(max);
  30. return Math.floor(Math.random() * (max - min + 1)) + min;
  31. }
  32. //objects
  33. var Player = function(x,y)
  34. {
  35. this.x = 350
  36. this.y = 650;
  37. this.width = 10;
  38. this.height = 10;
  39. this.speed = 4;
  40. this.facing = 40;
  41. this.updateDirection = function(key)
  42. {
  43. facing = key;
  44. }
  45. this.render = function()
  46. {
  47. context.fillStyle = "rgba(255, 255, 255, 1)"; //white
  48. context.fillRect(this.x, this.y, this.width, this.height);
  49. }
  50. this.move = function()
  51. {
  52. for (var key in keysDown)
  53. {
  54. var value = Number(key);
  55. if(value == 37) //left
  56. {
  57. this.x = this.x - this.speed;
  58. }
  59. else if(value == 39)
  60. {
  61. this.x = this.x + this.speed;
  62. }
  63. }
  64. //collisions
  65. for(i = 0; i < bamboos.length; i++)
  66. {
  67. if(bamboos[i] != -1)
  68. {
  69. bb = bamboos[i];
  70. //if(Math.abs(bb.x - p.x) <= bb.width)
  71. if(p.x > bb.x && p.x < bb.x + bb.width || p.x + p.width < bb.x + bb.width && p.x + p.width > bb.x)
  72. {
  73. if(p.y > bb.y && p.y < bb.y + bb.height || p.y + p.height < bb.y + bb.height && p.y + p.height > bb.y)
  74. {
  75. alive = false;
  76. bb = -1;
  77. }
  78. }
  79. }
  80. }
  81. if(this.x < 0)
  82. {
  83. this.x += this.speed;
  84. }
  85. else if(this.x > width)
  86. {
  87. this.x -= this.speed;
  88. }
  89. }
  90. }
  91. //player object
  92. var p = new Player(350, 850);
  93. //object that falls from the sky
  94. var Bamboo = function()
  95. {
  96. this.width = 25;
  97. this.height = 75;
  98. this.x = getRandomIntInclusive(0, width);
  99. this.y = -this.height;
  100. this.speed = 4;
  101. this.render = function()
  102. {
  103. context.fillStyle = "rgba(0, 255, 0 , 1)"; //green
  104. context.fillRect(this.x, this.y, this.width, this.height);
  105. }
  106. this.move = function()
  107. {
  108. this.y += this.speed;
  109. if(this.y> height)
  110. {
  111. //remove bamboo from array
  112. score++;
  113. return true;
  114. }
  115. }
  116. }
  117. var bamboos = [];
  118. //draws all the objects
  119. var render = function()
  120. {
  121. context.fillStyle = "rgba(0, 0,0 ,1)";
  122. context.fillRect(0,0, width, height);
  123. context.fillStyle = "rgba(255, 255,255 ,1)";
  124. context.font = "20px Georgia";
  125. context.fillText("Score: " + score, 10, 25);
  126. p.render();
  127. for(i= 0; i < bamboos.length; i++)
  128. {
  129. if(bamboos[i] != -1)
  130. {
  131. bamboos[i].render();
  132. }
  133. }
  134. //console.log('render was called');
  135. //context.fillStyle = "rgba(0, 128, 0 ,1)"; //green
  136. //context.fillRect(0, 0, 700, 700);
  137. //console.log(context);
  138. }
  139. var update = function()
  140. {
  141. addBamboo();
  142. p.move();
  143. for (i = 0; i< bamboos.length; i++)
  144. {
  145. if(bamboos[i] != -1)
  146. {
  147. if(bamboos[i].move())
  148. {
  149. bamboos[i] = -1;
  150. }
  151. }
  152. }
  153. addBamboo();
  154. }
  155. var count = 0;
  156. var limit = 25;
  157. var addBamboo = function()
  158. {
  159. count++;
  160. if(count == limit)
  161. {
  162. count = 0;
  163. var bnew = true;
  164. for(i = 0; i < bamboos.length; i++)
  165. {
  166. if(bamboos[i] == -1)
  167. {
  168. bamboos[i] = new Bamboo();
  169. bnew = false;
  170. }
  171. }
  172. if(bnew)
  173. {
  174. bamboos.push(new Bamboo());
  175. }
  176. if(limit > 10)
  177. {
  178. limit --;
  179. }
  180. }
  181. }
  182. var tic = function()
  183. {
  184. //console.log('tic was called');
  185. if(alive)
  186. {
  187. update();
  188. render();
  189. }
  190. else
  191. {
  192. context.fillStyle = "rgba(0, 0,0 ,1)";
  193. context.fillRect(0,0, width, height);
  194. context.fillStyle = "rgba(255, 255,255 ,1)";
  195. context.font = "20px Georgia";
  196. context.fillText("You died with a score of: " + score, 250, 325);
  197. }
  198. animate(tic);
  199. }
  200. window.addEventListener("keydown", function (event)
  201. {
  202. if(event.keyCode >=37 && event.keyCode <=40)
  203. {
  204. p.facing = event.keyCode;
  205. }
  206. keysDown[event.keyCode] = true;
  207. });
  208. window.addEventListener("keyup", function (event)
  209. {
  210. delete keysDown[event.keyCode];
  211. });
  212. //tic();
  213. animate(tic);
  214. }
  215. </script>
  216. <div><canvas id="canvas" width="700" height="700"></canvas></div>
  217. <div><input type="submit" name="play" value="Play Game" onclick="this.blur();play()"/></div>