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.

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