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.

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