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.

464 lines
12 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
  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 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. //
  43. //bullet class
  44. var Bullet = function(dir, x, y)
  45. {
  46. console.log(dir + " " + x + " " + y);
  47. this.dir = dir;
  48. this.x = x;
  49. this.y = y;
  50. this.speed = 5;
  51. this.width = 10;
  52. this.height =10;
  53. this.move = function()
  54. {
  55. sonsole.log("move");
  56. this.x += this.speed * Math.sin(this.dir);
  57. this.y += this.speed * Math.cos(this.dir);
  58. }
  59. this.render = function()
  60. {
  61. context.fillStyle = "rgba(255, 255, 255 , 1)"; //green
  62. context.beginPath();
  63. context.arc(this.x,this.y,this.width,0,2*Math.PI);
  64. context.fill();
  65. console.log(this.x);
  66. }
  67. }
  68. //array of bullets
  69. var bullets = [];
  70. bullets.push(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.render = function()
  80. {
  81. context.fillStyle = "rgba(255, 255, 255, 1)"; //white
  82. context.fillRect(this.x, this.y, this.width, this.height);
  83. }
  84. this.move = function()
  85. {
  86. for (var key in keysDown)
  87. {
  88. var value = Number(key);
  89. if(value == 37) //left
  90. {
  91. //one degree
  92. this.dir += Math.PI / 360;
  93. }
  94. else if(value == 39)
  95. {
  96. //one degree
  97. this.dir -= 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.size; 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 (i = 0; i < bullets.lenght; 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. for (i = 0; i< astroids.length; i++)
  251. {
  252. if(astroids[i] != -1)
  253. {
  254. if(astroids[i].move())
  255. {
  256. astroids[i] = -1;
  257. }
  258. }
  259. }
  260. console.log(bullets.lenght);
  261. for (i = 0; i < bullets.lenght; i++)
  262. {
  263. if(bullets[i] != -1)
  264. {
  265. if(bullets[i].move())
  266. {
  267. bullets[i] = -1;
  268. }
  269. }
  270. }
  271. }
  272. var count = 0;
  273. var limit = 25;
  274. var addAstroid = function()
  275. {
  276. count++;
  277. if(count == limit)
  278. {
  279. count = 0;
  280. var bnew = true;
  281. for(i = 0; i < astroids.length; i++)
  282. {
  283. if(astroids[i] == -1)
  284. {
  285. astroids[i] = new Astroid();
  286. bnew = false;
  287. }
  288. }
  289. if(bnew)
  290. {
  291. astroids.push(new Astroid());
  292. }
  293. if(limit > 10)
  294. {
  295. limit --;
  296. }
  297. }
  298. }
  299. var sent;
  300. var tic = function()
  301. {
  302. //console.log('tic was called');
  303. if(alive)
  304. {
  305. update();
  306. render();
  307. }
  308. else
  309. {
  310. if(!sent)
  311. {
  312. context.fillStyle = "rgba(0, 0,0 ,1)";
  313. context.fillRect(0,0, width, height);
  314. context.fillStyle = "rgba(255, 255,255 ,1)";
  315. context.font = "20px Georgia";
  316. context.fillText("You died with a score of: " + score, 250, 325);
  317. //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>';
  318. //document.getElementById("dynForm").submit();
  319. //sent = true;
  320. }
  321. }
  322. animate(tic);
  323. }
  324. window.addEventListener("keydown", function (event)
  325. {
  326. if(event.keyCode >=37 && event.keyCode <=40)
  327. {
  328. p.facing = event.keyCode;
  329. }
  330. keysDown[event.keyCode] = true;
  331. });
  332. window.addEventListener("keyup", function (event)
  333. {
  334. delete keysDown[event.keyCode];
  335. });
  336. //tic();
  337. animate(tic);
  338. }
  339. </script>
  340. <div><canvas id="canvas" width="700" height="700"></canvas></div>
  341. <div><input type="submit" name="play" value="Play Game" onclick="this.blur();playBF()"/></div>