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.

390 lines
9.4 KiB

  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 canvas = document.getElementById("canvasZP");
  13. var c = canvas.getContext('2d');
  14. c.fillStyle= "black";
  15. c.fillRect(0,0,canvas.width,canvas.height);
  16. };
  17. function playZP()
  18. {
  19. var animate = window.requestAnimationFrame ||
  20. window.webkitRequestAnimationFrame ||
  21. window.mozRequestAnimationFrame || function (callback)
  22. {
  23. window.setTimeout(callback, 1000 / 60)
  24. };
  25. var canvas = document.getElementById("canvasZP");
  26. var width = 700;
  27. var height = 700;
  28. canvas.style.textAlign = 'center';
  29. var context = canvas.getContext('2d');
  30. var keysDown = {};
  31. var score = 0;
  32. var dead = false;
  33. //player
  34. var Player = function(x,y)
  35. {
  36. this.x = x;
  37. this.y = y;
  38. this.width = 25;
  39. this.height = 25;
  40. this.speed = 3;
  41. this.facing = 40;
  42. this.updateDirection = function(key)
  43. {
  44. facing = key;
  45. }
  46. this.render = function()
  47. {
  48. context.fillStyle = "rgba(0, 45, 160, 1)"; //green
  49. context.fillRect(this.x, this.y, this.width,this.height);
  50. }
  51. this.move = function()
  52. {
  53. for (var key in keysDown)
  54. {
  55. var value = Number(key);
  56. if (value == 37) //left
  57. {
  58. this.x = this.x - this.speed;
  59. }
  60. else if (value == 39) //right
  61. {
  62. this.x = this.x + this.speed
  63. }
  64. else if(value == 38) //up
  65. {
  66. this.y = this.y - this.speed
  67. }
  68. else if(value == 40) //down
  69. {
  70. this.y = this.y + this.speed
  71. }
  72. else if(value == 32)
  73. {
  74. var added = false;
  75. for(i = 0; i< bullets.length; i++)
  76. {
  77. if(bullets[i] == -1)
  78. {
  79. bullets[i] = new Bullet();
  80. added = true;
  81. break;
  82. }
  83. }
  84. if(added == false)
  85. bullets.push(new Bullet());
  86. }
  87. }
  88. }
  89. }
  90. var p = new Player(350, 350);
  91. //bullet
  92. var Bullet = function()
  93. {
  94. this.x = p.x;
  95. this.y = p.y;
  96. this.width = 10;
  97. this.height = 10;
  98. this.speed = 4;
  99. this.facing = p.facing;
  100. this.move = function()
  101. {
  102. if (this.facing == 37) //left
  103. this.x = this.x - this.speed;
  104. else if (this.facing == 39) //right
  105. this.x = this.x + this.speed
  106. else if(this.facing == 38) //up
  107. this.y = this.y - this.speed
  108. else if(this.facing == 40) //down
  109. this.y = this.y + this.speed
  110. }
  111. this.render = function()
  112. {
  113. context.fillStyle = "rgba(255, 0, 199, 1)"; //green
  114. context.fillRect(this.x, this.y, this.width,this.height);
  115. }
  116. }
  117. var bullets = [];
  118. function getRandomIntInclusive(min, max) {
  119. min = Math.ceil(min);
  120. max = Math.floor(max);
  121. return Math.floor(Math.random() * (max - min + 1)) + min;
  122. }
  123. //zombie
  124. var Zombie = function(x,y)
  125. {
  126. this.x = x;
  127. this.y = y;
  128. this.width = 25;
  129. this.height = 25;
  130. this.speed = 3;
  131. var side = getRandomIntInclusive(0,3);
  132. var mid = getRandomIntInclusive(0,700);
  133. if(side === 1)
  134. {
  135. this.x = mid;
  136. this.y = 0;
  137. }
  138. else if(side === 2)
  139. {
  140. this.x = 700;
  141. this.y = mid;
  142. }
  143. else if(side === 3)
  144. {
  145. this.y = 700;
  146. this.x = mid;
  147. }
  148. else
  149. {
  150. this.x = 0;
  151. this.y = mid;
  152. }
  153. this.render = function()
  154. {
  155. context.fillStyle = "rgba(0, 160, 0, 1)"; //green
  156. context.fillRect(this.x, this.y, this.width,this.height);
  157. }
  158. this.move = function()
  159. {
  160. if(this.x > p.x + this.speed)
  161. this.x -= this.speed;
  162. else if(this.x + this.speed < p.x )
  163. this.x += this.speed;
  164. if(this.y > p.y + this.speed)
  165. this.y -= this.speed;
  166. else if(this.y + this.speed < p.y )
  167. this.y += this.speed;
  168. }
  169. }
  170. var zombies = [];
  171. zombies.push(new Zombie());
  172. var render = function()
  173. {
  174. context.fillStyle = "#000000";
  175. context.fillRect(0, 0, width, height);
  176. context.fillStyle = "rgba(255, 255, 255, 1)";
  177. context.font= "20px Georgia";
  178. context.fillText("Score: " + score,10,25);
  179. for(i = 0; i< zombies.length; i++)
  180. {
  181. if(zombies[i] != -1)
  182. {
  183. zombies[i].render();
  184. }
  185. }
  186. for(i = 0; i< bullets.length; i++)
  187. {
  188. if(bullets[i] != -1)
  189. {
  190. bullets[i].render();
  191. }
  192. }
  193. p.render();
  194. }
  195. var update = function()
  196. {
  197. addZombie();
  198. p.move();
  199. for(i = 0; i< zombies.length; i++)
  200. {
  201. for(z = 0;z < bullets.length; z++)
  202. {
  203. if(zombies[i] != -1 && bullets[z] != -1)
  204. {
  205. if(Math.abs(zombies[i].x - bullets[z].x) < 25 &&
  206. Math.abs(zombies[i].y - bullets[z].y) < 25)
  207. {
  208. score++;
  209. zombies[i] = -1;
  210. bullets[z] = -1;
  211. }
  212. }
  213. }
  214. if(zombies[i] != -1)
  215. {
  216. zombies[i].move();
  217. }
  218. }
  219. for(i = 0; i< bullets.length; i++)
  220. {
  221. //checks to see if goes out of bounds
  222. if(bullets[i].x < 0 || bullets[i].y < 0 ||
  223. bullets[i].x > 700 || bullets[i].y > 700)
  224. {
  225. bullets[i] = -1;
  226. }
  227. if(bullets[i] != -1)
  228. {
  229. bullets[i].move();
  230. }
  231. }
  232. for(i=0; i<zombies.length; i++)
  233. {
  234. if(zombies[i] != -1 &&
  235. Math.abs(p.x - zombies[i].x) < 25 &&
  236. Math.abs(p.y - zombies[i].y) < 25)
  237. {
  238. dead = true;
  239. }
  240. }
  241. }
  242. var counter2 = 0;
  243. var counter = 0;
  244. var rate = 180;
  245. var addZombie = function()
  246. {
  247. counter2++;
  248. counter++;
  249. var added = false;
  250. if(counter >= rate)
  251. {
  252. for(i = 0; i< zombies.length; i++)
  253. {
  254. if(zombies[i] == -1)
  255. {
  256. zombies[i] = new Zombie();
  257. added = true;
  258. break;
  259. }
  260. }
  261. if(added == false)
  262. {
  263. zombies.push(new Zombie());
  264. }
  265. counter = 0;
  266. }
  267. if(counter2 >= 60)
  268. {
  269. if(rate > 30)
  270. {
  271. rate = rate -5;
  272. }
  273. counter2 = 0;
  274. }
  275. }
  276. var sent = false;
  277. var tic = function()
  278. {
  279. if(dead)
  280. {
  281. context.fillStyle = "#000000";
  282. context.fillRect(0, 0, width, height);
  283. context.fillStyle = "rgba(255, 255, 255, 1)"; //green
  284. context.font= "20px Georgia";
  285. context.fillText("You died with a score of " + score,200,340);
  286. if(!sent)
  287. {
  288. document.body.innerHTML += '<form id="dynForm" ' +
  289. 'action="insertScore.php" method="post">' +
  290. '<input type="hidden" name="game_new_score" ' +
  291. 'value=true><input type="hidden" name="game" value=2>' +
  292. '<input type="hidden" name="user_id_score" value=' +
  293. '<?php echo $_SESSION['user_id']; ?>><input ' +
  294. 'type="hidden" name="score_validate" value=' + score + '>' +
  295. '</form>';
  296. document.getElementById("dynForm").submit();
  297. sent = !sent;
  298. }
  299. }
  300. else
  301. {
  302. update();
  303. render()
  304. }
  305. animate(tic);
  306. }
  307. window.addEventListener("keydown", function (event)
  308. {
  309. if(event.keyCode >=37 && event.keyCode <=40)
  310. {
  311. p.facing = event.keyCode;
  312. }
  313. keysDown[event.keyCode] = true;
  314. });
  315. window.addEventListener("keyup", function (event)
  316. {
  317. delete keysDown[event.keyCode];
  318. });
  319. animate(tic);
  320. }
  321. </script>
  322. <div><canvas id="canvasZP" width="700" height="700"></canvas></div>
  323. <div><input type="submit" name="play" value="Play Game" onclick="
  324. this.blur();playZP()"/></div>