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.

318 lines
8.5 KiB

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