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.

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