Multiplayer Java Script game
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.

389 lines
9.8 KiB

  1. <script src="../server/server.js"></script>
  2. <script>
  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("canvasZP");
  14. var c = canvas.getContext('2d');
  15. c.fillStyle= "black";
  16. c.fillRect(0,0,canvas.width,canvas.height);
  17. };
  18. function playZP()
  19. {
  20. var animate = window.requestAnimationFrame || 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. {
  86. bullets.push(new Bullet());
  87. }
  88. }
  89. }
  90. }
  91. }
  92. var p = new Player(350, 350);
  93. //bullet
  94. var Bullet = function()
  95. {
  96. this.x = p.x;
  97. this.y = p.y;
  98. this.width = 10;
  99. this.height = 10;
  100. this.speed = 4;
  101. this.facing = p.facing;
  102. this.move = function()
  103. {
  104. if (this.facing == 37) //left
  105. {
  106. this.x = this.x - this.speed;
  107. }
  108. else if (this.facing == 39) //right
  109. {
  110. this.x = this.x + this.speed
  111. }
  112. else if(this.facing == 38) //up
  113. {
  114. this.y = this.y - this.speed
  115. }
  116. else if(this.facing == 40) //down
  117. {
  118. this.y = this.y + this.speed
  119. }
  120. }
  121. this.render = function()
  122. {
  123. context.fillStyle = "rgba(255, 0, 199, 1)"; //green
  124. context.fillRect(this.x, this.y, this.width,this.height);
  125. }
  126. }
  127. var bullets = [];
  128. function getRandomIntInclusive(min, max) {
  129. min = Math.ceil(min);
  130. max = Math.floor(max);
  131. return Math.floor(Math.random() * (max - min + 1)) + min;
  132. }
  133. //zombie
  134. var Zombie = function(x,y)
  135. {
  136. this.x = x;
  137. this.y = y;
  138. this.width = 25;
  139. this.height = 25;
  140. this.speed = 3;
  141. var side = getRandomIntInclusive(0,3);
  142. var mid = getRandomIntInclusive(0,700);
  143. if(side === 1)
  144. {
  145. this.x = mid;
  146. this.y = 0;
  147. }
  148. else if(side === 2)
  149. {
  150. this.x = 700;
  151. this.y = mid;
  152. }
  153. else if(side === 3)
  154. {
  155. this.y = 700;
  156. this.x = mid;
  157. }
  158. else
  159. {
  160. this.x = 0;
  161. this.y = mid;
  162. }
  163. this.render = function()
  164. {
  165. context.fillStyle = "rgba(0, 160, 0, 1)"; //green
  166. context.fillRect(this.x, this.y, this.width,this.height);
  167. }
  168. this.move = function()
  169. {
  170. if(this.x > p.x + this.speed)
  171. this.x -= this.speed;
  172. else if(this.x + this.speed < p.x )
  173. this.x += this.speed;
  174. if(this.y > p.y + this.speed)
  175. this.y -= this.speed;
  176. else if(this.y + this.speed < p.y )
  177. this.y += this.speed;
  178. }
  179. }
  180. var zombies = [];
  181. zombies.push(new Zombie());
  182. var render = function()
  183. {
  184. context.fillStyle = "#000000";
  185. context.fillRect(0, 0, width, height);
  186. context.fillStyle = "rgba(255, 255, 255, 1)";
  187. context.font= "20px Georgia";
  188. context.fillText("Score: " + score,10,25);
  189. for(i = 0; i< zombies.length; i++)
  190. {
  191. if(zombies[i] != -1)
  192. {
  193. zombies[i].render();
  194. }
  195. }
  196. for(i = 0; i< bullets.length; i++)
  197. {
  198. if(bullets[i] != -1)
  199. {
  200. bullets[i].render();
  201. }
  202. }
  203. p.render();
  204. }
  205. var update = function()
  206. {
  207. addZombie();
  208. p.move();
  209. for(i = 0; i< zombies.length; i++)
  210. {
  211. for(z = 0;z < bullets.length; z++)
  212. {
  213. if(zombies[i] != -1 && bullets[z] != -1)
  214. {
  215. if(Math.abs(zombies[i].x - bullets[z].x) < 25 &&
  216. Math.abs(zombies[i].y - bullets[z].y) < 25)
  217. {
  218. score++;
  219. zombies[i] = -1;
  220. bullets[z] = -1;
  221. }
  222. }
  223. }
  224. if(zombies[i] != -1)
  225. {
  226. zombies[i].move();
  227. }
  228. }
  229. for(i = 0; i< bullets.length; i++)
  230. {
  231. //checks to see if goes out of bounds
  232. if(bullets[i].x < 0 || bullets[i].y < 0 || bullets[i].x > 700 || bullets[i].y > 700)
  233. {
  234. bullets[i] = -1;
  235. }
  236. if(bullets[i] != -1)
  237. {
  238. bullets[i].move();
  239. }
  240. }
  241. for(i=0; i<zombies.length; i++)
  242. {
  243. if(zombies[i] != -1 && Math.abs(p.x - zombies[i].x) < 25 &&
  244. Math.abs(p.y - zombies[i].y) < 25)
  245. {
  246. dead = true;
  247. }
  248. }
  249. }
  250. var counter2 = 0;
  251. var counter = 0;
  252. var rate = 180;
  253. var addZombie = function()
  254. {
  255. counter2++;
  256. counter++;
  257. var added = false;
  258. if(counter >= rate)
  259. {
  260. for(i = 0; i< zombies.length; i++)
  261. {
  262. if(zombies[i] == -1)
  263. {
  264. zombies[i] = new Zombie();
  265. added = true;
  266. break;
  267. }
  268. }
  269. if(added == false)
  270. {
  271. zombies.push(new Zombie());
  272. }
  273. counter = 0;
  274. }
  275. if(counter2 >= 60)
  276. {
  277. if(rate > 30)
  278. {
  279. rate = rate -5;
  280. }
  281. counter2 = 0;
  282. }
  283. }
  284. var sent = false;
  285. var tic = function()
  286. {
  287. if(dead)
  288. {
  289. context.fillStyle = "#000000";
  290. context.fillRect(0, 0, width, height);
  291. context.fillStyle = "rgba(255, 255, 255, 1)"; //green
  292. context.font= "20px Georgia";
  293. context.fillText("You died with a score of " + score,200,340);
  294. }
  295. else
  296. {
  297. update();
  298. render()
  299. }
  300. animate(tic);
  301. }
  302. window.addEventListener("keydown", function (event)
  303. {
  304. if(event.keyCode >=37 && event.keyCode <=40)
  305. {
  306. p.facing = event.keyCode;
  307. }
  308. keysDown[event.keyCode] = true;
  309. });
  310. window.addEventListener("keyup", function (event)
  311. {
  312. delete keysDown[event.keyCode];
  313. });
  314. animate(tic);
  315. }
  316. </script>
  317. <div><canvas id="canvasZP" width="700" height="700"></canvas></div>
  318. <div><input type="submit" name="play" value="Play Game" onclick="this.blur();playZP()"/></div>