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.

324 lines
8.7 KiB

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