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.

478 lines
13 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
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. //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 = 800;
  27. var height = 800;
  28. canvas.style.textAligh = 'center';
  29. var context = canvas.getContext('2d');
  30. var bullets = [];
  31. var keysDown = {};
  32. //player variables;
  33. var score = 0;
  34. var alive = true;
  35. //usefull function
  36. function getRandomIntInclusive(min, max)
  37. {
  38. min = Math.ceil(min);
  39. max = Math.floor(max);
  40. return Math.floor(Math.random() * (max - min + 1)) + min;
  41. }
  42. //objects
  43. //
  44. //bullet class
  45. var Bullet = function(dir, x, y)
  46. {
  47. //console.log(dir + " " + x + " " + y);
  48. this.dir = dir;
  49. this.x = x;
  50. this.y = y;
  51. this.speed = 5;
  52. this.width = 10;
  53. this.height =10;
  54. this.move = function()
  55. {
  56. //console.log("move");
  57. this.x += this.speed * Math.sin(this.dir);
  58. this.y += this.speed * Math.cos(this.dir);
  59. }
  60. this.render = function()
  61. {
  62. context.fillStyle = "rgba(255, 0, 0 , 1)"; //green
  63. context.beginPath();
  64. context.arc(this.x,this.y,this.width,0,2*Math.PI);
  65. context.fill();
  66. //console.log(this.x);
  67. }
  68. }
  69. //array of bullets
  70. //bullets[0] = new Bullet(Math.PI, 100, 100);
  71. var Player = function(x,y)
  72. {
  73. this.x = width/2;
  74. this.y = height/2;
  75. this.width = 10;
  76. this.height = 10;
  77. this.speed = 4;
  78. this.dir = Math.PI;
  79. this.dirSpeed = 10;
  80. this.render = function()
  81. {
  82. context.fillStyle = "rgba(255, 255, 255, 1)"; //white
  83. context.fillRect(this.x, this.y, this.width, this.height);
  84. }
  85. this.move = function()
  86. {
  87. for (var key in keysDown)
  88. {
  89. var value = Number(key);
  90. if(value == 37) //left
  91. {
  92. this.dir += this.dirSpeed * Math.PI / 360;
  93. }
  94. else if(value == 39)
  95. {
  96. //
  97. this.dir -= this.dirSpeed * Math.PI / 360;
  98. }
  99. else if(value == 38) // up
  100. {
  101. this.x += this.speed * Math.sin(this.dir);
  102. this.y += this.speed * Math.cos(this.dir);
  103. }
  104. else if(value == 40) //down
  105. {
  106. this.x -= this.speed * Math.sin(this.dir);
  107. this.y -= this.speed * Math.cos(this.dir);
  108. }
  109. else if(value == 32) //shoot
  110. {
  111. this.shoot();
  112. }
  113. }
  114. //collisions
  115. for(i = 0; i < astroids.length; i++)
  116. {
  117. if(astroids[i] != -1)
  118. {
  119. bb = astroids[i];
  120. //if(Math.abs(bb.x - p.x) <= bb.width)
  121. 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)
  122. {
  123. 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)
  124. {
  125. alive = false;
  126. bb = -1;
  127. }
  128. }
  129. }
  130. }
  131. if(this.x < 0)
  132. {
  133. this.x += this.speed;
  134. }
  135. else if(this.x > width- this.width)
  136. {
  137. this.x -= this.speed;
  138. }
  139. if(this.y < 0)
  140. {
  141. this.y += this.speed;
  142. }
  143. else if(this.y > height)
  144. {
  145. this.y -= this.speed;
  146. }
  147. }
  148. this.shoot = function()
  149. {
  150. var dimmed = false;
  151. for(i = 0; i> bullets.length; i++)
  152. {
  153. if(bullets[i] == -1)
  154. {
  155. dimmed = true;
  156. bullets[i] = new Bullet(this.dir, this.x, this.y);
  157. }
  158. }
  159. if(dimmed == false)
  160. {
  161. bullets.push(new Bullet(this.dir, this.x, this.y));
  162. }
  163. }
  164. }
  165. //player object
  166. var p = new Player(350, 850);
  167. //object that falls from the sky
  168. var Astroid = function(size)
  169. {
  170. this.width = 15;
  171. this.height = 15;
  172. this.dir = 2 * Math.PI * Math.random()
  173. this.size = size;
  174. var temp = getRandomIntInclusive(0, width);
  175. var temp2 = getRandomIntInclusive(1, 4);
  176. if(temp2 == 1)
  177. {
  178. this.x = temp;
  179. this.y = 0;
  180. }
  181. else if(temp2 == 2)
  182. {
  183. this.x = width;
  184. this.y = temp;
  185. }
  186. else if(temp2 == 3)
  187. {
  188. this.y = height;
  189. this.x = temp;
  190. }
  191. else
  192. {
  193. this.y = temp;
  194. this.x = 0;
  195. }
  196. this.speed = 2;
  197. this.render = function()
  198. {
  199. context.fillStyle = "rgba(255, 255, 255 , 1)"; //green
  200. context.beginPath();
  201. context.arc(this.x,this.y,this.width,0,2*Math.PI);
  202. context.fill();
  203. }
  204. this.move = function()
  205. {
  206. this.y += this.speed * Math.sin(this.dir);
  207. this.x += this.speed *Math.cos(this.dir);
  208. //collision
  209. if(this.y> height)
  210. {
  211. //remove bamboo from array
  212. score++;
  213. return true;
  214. }
  215. }
  216. }
  217. var astroids = [];
  218. //draws all the objects
  219. var render = function()
  220. {
  221. context.fillStyle = "rgba(0, 0,0 ,1)";
  222. context.fillRect(0,0, width, height);
  223. context.fillStyle = "rgba(255, 255,255 ,1)";
  224. context.font = "20px Georgia";
  225. context.fillText("Score: " + score, 10, 25);
  226. p.render();
  227. for(i= 0; i < astroids.length; i++)
  228. {
  229. if(astroids[i] != -1)
  230. {
  231. astroids[i].render();
  232. }
  233. }
  234. for (var i = bullets.length; i--; )
  235. {
  236. if(bullets[i] != -1)
  237. {
  238. bullets[i].render();
  239. }
  240. }
  241. //console.log('render was called');
  242. //context.fillStyle = "rgba(0, 128, 0 ,1)"; //green
  243. //context.fillRect(0, 0, 700, 700);
  244. //console.log(context);
  245. }
  246. var update = function()
  247. {
  248. addAstroid();
  249. p.move();
  250. var halp = bullets.lenght
  251. //console.log(halp);
  252. //for (b = 0; b < bullets.lenght; b++)
  253. //var b;
  254. for (var i = bullets.length; i--; )
  255. {
  256. console.log("b");
  257. if(bullets[i] != -1)
  258. {
  259. if(bullets[i].move())
  260. {
  261. b = -1;
  262. }
  263. }
  264. }
  265. for (i = 0; i< astroids.length; i++)
  266. {
  267. if(astroids[i] != -1)
  268. {
  269. if(astroids[i].move())
  270. {
  271. astroids[i] = -1;
  272. }
  273. }
  274. }
  275. }
  276. var count = 0;
  277. var limit = 25;
  278. var addAstroid = function()
  279. {
  280. count++;
  281. if(count == limit)
  282. {
  283. count = 0;
  284. var bnew = true;
  285. for(i = 0; i < astroids.length; i++)
  286. {
  287. if(astroids[i] == -1)
  288. {
  289. astroids[i] = new Astroid();
  290. bnew = false;
  291. }
  292. }
  293. if(bnew)
  294. {
  295. astroids.push(new Astroid());
  296. }
  297. if(limit > 10)
  298. {
  299. limit --;
  300. }
  301. }
  302. }
  303. var sent;
  304. var tic = function()
  305. {
  306. //console.log('tic was called');
  307. if(alive)
  308. {
  309. update();
  310. render();
  311. }
  312. else
  313. {
  314. if(!sent)
  315. {
  316. context.fillStyle = "rgba(0, 0,0 ,1)";
  317. context.fillRect(0,0, width, height);
  318. context.fillStyle = "rgba(255, 255,255 ,1)";
  319. context.font = "20px Georgia";
  320. context.fillText("You died with a score of: " + score, 250, 325);
  321. //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>';
  322. //document.getElementById("dynForm").submit();
  323. //sent = true;
  324. }
  325. }
  326. animate(tic);
  327. }
  328. window.addEventListener("keydown", function (event)
  329. {
  330. if(event.keyCode >=37 && event.keyCode <=40)
  331. {
  332. p.facing = event.keyCode;
  333. }
  334. keysDown[event.keyCode] = true;
  335. });
  336. window.addEventListener("keyup", function (event)
  337. {
  338. delete keysDown[event.keyCode];
  339. });
  340. //tic();
  341. animate(tic);
  342. }
  343. </script>
  344. <div><canvas id="canvas" width="700" height="700"></canvas></div>
  345. <div><input type="submit" name="play" value="Play Game" onclick="this.blur();playBF()"/></div>