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.

376 lines
9.8 KiB

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