Advertisement
Guest User

Untitled

a guest
May 27th, 2020
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 27.12 KB | None | 0 0
  1. <!DOCTYPE html>
  2.  
  3. <html>
  4.  
  5. <head>
  6.  
  7. <title>2D Shooter</title>
  8.  
  9. </head>
  10. <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
  11. <body>
  12.  
  13. <canvas id="Canvy"></canvas>
  14.  
  15. <script>
  16. var canvas = document.getElementById("Canvy");
  17. var b = canvas.getContext("2d");
  18. b.canvas.width = window.innerWidth;
  19. b.canvas.height = window.innerHeight;
  20. document.body.appendChild(canvas);
  21.  
  22. var inform = false;
  23. var click = false;
  24. var offsetX;
  25. var offsetY;
  26. var scalewidth = window.innerWidth / 1920;
  27. var scaleheight = window.innerHeight / 969;
  28. var paused = false;
  29. var mouseDown = 0;
  30. var timeDilation = 1;
  31. var maxFreeze = 100;
  32. var freeze = 100;
  33. var freezeTime = false;
  34. var cantFreeze = false;
  35.  
  36.  
  37. let mouse = {
  38. x: canvas.width / 2,
  39. y: canvas.height / 2
  40. }
  41.  
  42. var keyState = {};
  43. var keyTime = {};
  44.  
  45. window.addEventListener('keydown',function(e){
  46.  
  47. keyState[e.keyCode || e.which] = true;
  48. keyTime[e.keyCode || e.which] = new Date();
  49.  
  50. },true);
  51.  
  52. setInterval(function() {
  53. var now = new Date();
  54. for(key in keyTime) {
  55. if(now - keyTime[key] >= 0500 && document.hasFocus() == false) {
  56. // run the release code for this key
  57. keyState[key] = false;
  58. }
  59. }
  60. }, 1000);
  61.  
  62. window.addEventListener('keyup',function(e){
  63.  
  64. keyState[e.keyCode || e.which] = false;
  65.  
  66. },true);
  67.  
  68. window.addEventListener("mousemove", function(e){
  69. mouse.x = event.clientX;
  70. mouse.y = event.clientY;
  71. })
  72.  
  73. document.body.onmousedown = function() {
  74. mouseDown = 1;
  75. }
  76. document.body.onmouseup = function() {
  77. mouseDown = 0;
  78. }
  79.  
  80. window.addEventListener("keydown", function(e){
  81.  
  82. if(e.keyCode == 67) {
  83. var newcode = prompt("Enter Code here");
  84. eval(newcode);
  85. }
  86. });
  87.  
  88. addEventListener('resize', () => {
  89. canvas.width = innerWidth;
  90. canvas.height = innerHeight;
  91.  
  92. init();
  93. })
  94.  
  95.  
  96.  
  97. function bullet(Ix, Iy, width, height, color, speed, origin, power, dx, dy) {
  98. bullets.push(this);
  99. this.Ix = Ix;
  100. this.Iy = Iy;
  101. this.width = width;
  102. this.height = height;
  103. this.x = this.Ix;
  104. this.y = this.Iy;
  105. this.origin = origin;
  106. this.power = power;
  107.  
  108. if(dx == null && dy == null) {
  109.  
  110. if(this.origin == "player") {
  111. this.dx = (mouse.x - this.Ix - offsetX) / 100;
  112. this.dy = (mouse.y - this.Iy - offsetY) / 100;
  113. } else {
  114. //Determine which player is closest
  115. var dist = [];
  116. for(var i = 0; i < players.length; i++) {
  117. //Find distance for each and push it into array
  118. dist.push(Math.hypot(this.x + 0.5*this.width - players[i].x + 0.5*players[i].width, this.y + 0.5*this.height - players[i].y + 0.5*players[i].height))
  119. }
  120. //Find value of minimum distance
  121. Array.min = function(array) {
  122. return Math.min.apply(Math, array);
  123. }
  124. var mindist = Array.min(dist);
  125. //find Index value of minimum distance and use it to find that player
  126. var a = dist.indexOf(mindist);
  127. //console.log("Array is: " + dist);
  128. //console.log("Min Distance is " + mindist);
  129. //console.log("Index is " + a);
  130. this.dx = (players[a].x - this.Ix) / 100;
  131. this.dy = (players[a].y - this.Iy) / 100;
  132. }
  133.  
  134.  
  135. //this.dx = (mouse.x - 0.5*canvas.width) / 100;
  136. //this.dy = (mouse.y - 0.5*canvas.height) / 100;
  137.  
  138. var length = Math.sqrt(this.dx * this.dx + this.dy * this.dy);
  139. this.dx /= length;
  140. this.dy /= length;
  141. this.dx = this.dx * speed;
  142. this.dy = this.dy * speed;
  143.  
  144.  
  145. } else {
  146. this.dx = dx;
  147. this.dy = dy;
  148.  
  149. }
  150.  
  151.  
  152.  
  153.  
  154.  
  155. this.draw = function() {
  156.  
  157. if(this.x < canvas.width - offsetX && this.x > -this.width - offsetX && this.y < canvas.height - offsetY && this.y > -this.height - offsetY) {
  158. //this.x += this.dx;
  159. //this.y += this.dy;
  160.  
  161. for(var i = 0; i < players.length; i++) {
  162. if(this.origin != "player" && players[i].x > this.x - players[i].width && players[i].x < this.x + this.width && players[i].y > this.y - players[i].height && players[i].y < this.y + this.height) {
  163.  
  164. players[i].health -= this.power;
  165. if(players[i].health <= 0) {
  166. bullets = [];
  167. players[i].x = 800;
  168. players[i].y = 400;
  169. players[i].health = players[i].healthI;
  170. }
  171. var item = this;
  172.  
  173. bullets = bullets.filter(bullets => bullets !== item);
  174. }
  175.  
  176. }
  177.  
  178. b.fillStyle = color;
  179. b.fillRect(this.x, this.y, width, height);
  180. } else {
  181. bullets = bullets.filter(bullets => bullets !== this);
  182. }
  183.  
  184.  
  185. /*
  186. if(posX > this.x - Bsize && posX < this.x + this.width && posY > this.y - Bsize && posY < this.y + this.height) {
  187.  
  188.  
  189.  
  190.  
  191. }
  192. */
  193. }
  194.  
  195. this.move = function() {
  196. this.x += this.dx * timeDilation;
  197. this.y += this.dy * timeDilation;
  198. }
  199.  
  200. }
  201. let bullets = [];
  202.  
  203. function gun(speed, reload, iammo, width, height, color) {
  204.  
  205. }
  206.  
  207. function player(Ix, Iy, width, height, color, speed) {
  208. players.push(this);
  209. this.x = Ix;
  210. this.y = Iy;
  211. this.width = width;
  212. this.height = height;
  213. this.speed = speed;
  214. this.upI = 38;
  215. this.downI = 40;
  216. this.leftI = 37;
  217. this.rightI = 39;
  218. this.slot1 = "empty";
  219. this.slot2 = "empty";
  220. this.slot3 = "empty";
  221. this.reloading;
  222. this.health = 100;
  223. this.healthI = 100;
  224. this.draw = function() {
  225. b.fillStyle = color;
  226. b.fillRect(this.x, this.y, this.width, this.height);
  227. }
  228. this.healthbar = function(x,y,width,height,name) {
  229. this.name = name;
  230. var tsize = canvas.width / 50;
  231. b.textAlign = "left";
  232. b.font = tsize + "px Arial";
  233. b.fillStyle = "White";
  234. b.fillText(name + ": ", x, y + 0.04*canvas.height);
  235. b.fillStyle = "SlateGray";
  236. b.fillRect(x + 0.1*canvas.width, y, width, height);
  237. b.fillStyle = color;
  238. b.fillRect(x + 0.1*canvas.width, y, width * (this.health / this.healthI), height);
  239.  
  240.  
  241. }
  242. this.remap = function(up, left, down, right) {
  243. this.upI = up;
  244. this.leftI = left;
  245. this.downI = down;
  246. this.rightI = right;
  247. }
  248. this.fire = function(speed, firerate, widthf, heightf, power, color) {
  249. /*
  250. if(slot1 == "empty" && slot2 == "empty" && slot3 == "empty") {
  251.  
  252. } else if(slot1 == "empty" && slot2 != "empty") {
  253. slot1 = slot2;
  254. slot2 = "empty";
  255. } else if(slot1 == "empty" && slot2 != "empty") {
  256. slot1 = slot2;
  257. slot2 = "empty";
  258. }
  259. */
  260.  
  261.  
  262.  
  263. addEventListener("click", () => {
  264. if(this.reloading != true && document.hasFocus()) {
  265. click = true;
  266.  
  267. }
  268. });
  269.  
  270. if(mouseDown > 0) {
  271. click = true;
  272. }
  273.  
  274. if(this.reloading != true && click == true) {
  275.  
  276.  
  277. //var ndog = new bullet(0.5*canvas.width + 0.5*this.width - 0.5*width, 0.5*canvas.height + 0.5*this.height - 0.5*height, width, height, "SteelBlue", 10);
  278. //bullet that moves with player
  279.  
  280.  
  281. var ndog = new bullet(this.x + 0.5*this.width - 0.25*width, this.y + 0.5*this.height - 0.25*height, widthf, heightf, color, speed, "player", power);
  282.  
  283.  
  284. click = false;
  285. this.reloading = true;
  286. setTimeout(() => { this.reloading = false; }, firerate*1000);
  287. }
  288.  
  289. for(var i = 0; i < bullets.length; i++) {
  290. if(bullets[i].origin == "player") {
  291. bullets[i].draw();
  292. }
  293.  
  294. }
  295.  
  296.  
  297. if(this.reloading == true) {
  298.  
  299. }
  300.  
  301. }
  302.  
  303. }
  304. let players = [];
  305.  
  306. function enemy(x, y, width, height, color, dx, dy) {
  307.  
  308. enemies.push(this);
  309.  
  310. this.x = x;
  311. this.y = y;
  312. this.dx = dx;
  313. this.dy = dy;
  314. this.width = width;
  315. this.height = height;
  316. this.onplat = false;
  317. this.Ix = x;
  318. this.Iy = y;
  319. this.Idx = dx;
  320. this.Idy = dy;
  321.  
  322. this.health = 100;
  323. this.healthI = this.health;
  324.  
  325. this.reloading;
  326.  
  327. this.startup = true;
  328. this.startuptime = 0;
  329.  
  330. this.overlaping;
  331.  
  332. this.Q = 2;
  333. this.count = 0;
  334.  
  335. //b.fillStyle = color;
  336.  
  337. //b.fillRect(this.x, this.y, width, height);
  338.  
  339. this.draw = function(color2) {
  340. if(this.health > 0) {
  341. if(color2 == null) {
  342. b.fillStyle = color;
  343. b.fillRect(this.x, this.y, width, height);
  344. } else {
  345. var grd = b.createRadialGradient(this.x + 0.5*this.width, this.y + 0.5*this.height, 0.1*this.width, this.x + 0.6*this.width, this.y + 0.6*this.height, 0.7*this.width);
  346. grd.addColorStop(0, color2);
  347. grd.addColorStop(1, color);
  348.  
  349. // Fill with gradient
  350. b.fillStyle = grd;
  351. b.fillRect(this.x, this.y, width, height);
  352. }
  353. for(var i = 0; i < bullets.length; i++) {
  354. if(bullets[i].origin == "player" && bullets[i].x > this.x - bullets[i].width && bullets[i].x < this.x + this.width && bullets[i].y > this.y - bullets[i].height && bullets[i].y < this.y + this.height) {
  355.  
  356. this.health -= bullets[i].power;
  357. bullets[i].y = Math.pow(10,10);
  358. }
  359. }
  360. //Health Bar
  361. b.fillStyle = "Black";
  362. b.fillRect(this.x, this.y - 20, this.width, 10);
  363. b.fillStyle = "Red";
  364. b.fillRect(this.x, this.y - 20, this.width * (this.health / this.healthI), 10);
  365. } else {
  366. this.y = Math.pow(10,10);
  367. }
  368.  
  369.  
  370.  
  371. }
  372.  
  373. this.cd = function(power) {
  374.  
  375. for(var i = 0; i < players.length; i++) {
  376. if(players[i].x > this.x - players[i].width && players[i].x < this.x + this.width && players[i].y > this.y - players[i].height && players[i].y < this.y + this.height) {
  377. players[i].health -= power;
  378. this.overlaping = true;
  379. if(players[i].health <= 0) {
  380. bullets = [];
  381. players[i].x = 800;
  382. players[i].y = 400;
  383. players[i].health = players[i].healthI;
  384.  
  385. }
  386.  
  387. } else {
  388. if(this.overlaping == true) {
  389. this.overlaping = false;
  390. }
  391. }
  392. }
  393.  
  394. }
  395.  
  396. this.move = function(direction, mspeed, minrange, maxrange) {
  397. //Determine which player is closest
  398. this.direction = direction;
  399. this.mspeed = mspeed;
  400. this.minrange = minrange;
  401. this.maxrange = maxrange;
  402. var dist = [];
  403. for(var i = 0; i < players.length; i++) {
  404. //Find distance for each and push it into array
  405. dist.push(Math.hypot(this.x + 0.5*this.width - players[i].x + 0.5*players[i].width, this.y + 0.5*this.height - players[i].y + 0.5*players[i].height))
  406. }
  407. //Find value of minimum distance
  408. Array.min = function(array) {
  409. return Math.min.apply(Math, array);
  410. }
  411. this.mindist = Array.min(dist);
  412. //find Index value of minimum distance and use it to find that player
  413. var a = dist.indexOf(this.mindist);
  414.  
  415. this.dx = (players[a].x - this.x) / 100;
  416. this.dy = (players[a].y - this.y) / 100;
  417. var length = Math.sqrt(this.dx * this.dx + this.dy * this.dy);
  418. this.dx /= length;
  419. this.dy /= length;
  420. this.dx = this.dx * mspeed;
  421. this.dy = this.dy * mspeed;
  422. if(direction == "towards" && this.mindist > this.minrange && this.mindist < this.maxrange && this.overlaping != true) {
  423. this.x += this.dx * timeDilation;
  424. this.y += this.dy * timeDilation;
  425. }
  426. if(direction == "away" && this.mindist > this.minrange && this.mindist < this.maxrange && this.overlaping != true) {
  427. this.x -= this.dx * timeDilation;
  428. this.y -= this.dy * timeDilation;
  429. }
  430. /*
  431. if(direction == "away and zig-zag" && this.mindist > this.minrange && this.mindist < this.maxrange && this.overlaping != true) {
  432. this.x -= this.dx * timeDilation;
  433. this.y -= this.dy * timeDilation;
  434.  
  435. if(this.reloading == false) {
  436.  
  437. this.count += 1;
  438.  
  439.  
  440. }as
  441. if(this.count > 1) {
  442. this.Q = -this.Q;
  443. this.count = 0;
  444. }
  445. //setTimeout(() => { }, 2000);
  446.  
  447. }
  448. */
  449. }
  450.  
  451. this.fire = function(speed, firerate, widthf, heightf, color, dx, dy) {
  452. this.startT;
  453. this.nowT;
  454. if(this.health > 0) {
  455. if(this.startup == true) {
  456. setTimeout(() => { this.startup = false; /*this.startuptime = 0;*/ }, (this.startuptime + firerate )*1000 / timeDilation);
  457. }
  458. if(this.reloading != true && this.startup == false) {
  459. if(paused == false && document.hasFocus()) {
  460. var ndog = new bullet(this.x + 0.5*this.width - 0.5*widthf, this.y + 0.5*this.height - 0.5*heightf, widthf, heightf, color, speed, "enemy", 30, dx, dy);
  461. }
  462. this.reloading = true;
  463.  
  464. this.startT = new Date();
  465. //setTimeout(() => { this.reloading = false; }, (firerate / timeDilation)*1000);
  466. }
  467. for(var i = 0; i < bullets.length; i++) {
  468.  
  469. if(bullets[i].origin == "enemy") {
  470. bullets[i].draw();
  471. }
  472.  
  473.  
  474. }
  475. this.nowT = new Date();
  476. if(this.nowT - this.startT >= firerate / timeDilation * 1000) {
  477. this.reloading = false;
  478.  
  479. }
  480.  
  481. }
  482.  
  483.  
  484.  
  485.  
  486.  
  487. }
  488.  
  489. this.fourfire = function(speed, firerate, widthf, heightf, color) {
  490. if(this.startup == true) {
  491. setTimeout(() => { this.startup = false; /*this.startuptime = 0;*/ }, (this.startuptime + firerate)*1000);
  492. }
  493. if(this.reloading != true && this.startup == false) {
  494. if(paused == false && document.hasFocus()) {
  495. var bull = new bullet(this.x + 0.5*this.width - 0.5*widthf, this.y + 0.5*this.height - 0.5*heightf, widthf, heightf, color, 0, speed, 0);
  496. var bull2 = new bullet(this.x + 0.5*this.width - 0.5*widthf, this.y + 0.5*this.height - 0.5*heightf, widthf, heightf, color, 0, -speed, 0);
  497. var bull3 = new bullet(this.x + 0.5*this.width - 0.5*widthf, this.y + 0.5*this.height - 0.5*heightf, widthf, heightf, color, 0, 0, speed);
  498. var bull4 = new bullet(this.x + 0.5*this.width - 0.5*widthf, this.y + 0.5*this.height - 0.5*heightf, widthf, heightf, color, 0, 0, -speed);
  499.  
  500. }
  501.  
  502. this.reloading = true;
  503. setTimeout(() => { this.reloading = false; }, firerate*1000);
  504. }
  505. for(var i = 0; i < bullet.list.length; i++) {
  506. bullet.list[i].draw();
  507.  
  508.  
  509. }
  510.  
  511. //decal on shooter
  512. var size = 0.25*this.width;
  513. var t = new platform(this.x + 0.5*this.width - 0.5*size, this.y, size, size, color);
  514. var l = new platform(this.x, this.y + 0.5*this.height - 0.5*size, size, size, color);
  515. var b = new platform(this.x + 0.5*this.width - 0.5*size, this.y + this.height - size, size, size, color);
  516. var r = new platform(this.x + this.width - size, this.y + 0.5*this.height - 0.5*size, size, size, color);
  517. }
  518.  
  519. this.fourDfire = function(speed, firerate, widthf, heightf, color) {
  520. if(this.startup == true) {
  521. setTimeout(() => { this.startup = false; /*this.startuptime = 0;*/ }, (this.startuptime + firerate)*1000);
  522. }
  523. if(this.reloading != true && this.startup == false) {
  524. if(paused == false && document.hasFocus()) {
  525. var bull = new bullet(this.x + 0.5*this.width - 0.5*widthf, this.y + 0.5*this.height - 0.5*heightf, widthf, heightf, color, 0, speed, speed);
  526. var bull2 = new bullet(this.x + 0.5*this.width - 0.5*widthf, this.y + 0.5*this.height - 0.5*heightf, widthf, heightf, color, 0, speed, -speed);
  527. var bull3 = new bullet(this.x + 0.5*this.width - 0.5*widthf, this.y + 0.5*this.height - 0.5*heightf, widthf, heightf, color, 0, -speed, speed);
  528. var bull4 = new bullet(this.x + 0.5*this.width - 0.5*widthf, this.y + 0.5*this.height - 0.5*heightf, widthf, heightf, color, 0, -speed, -speed);
  529.  
  530. }
  531.  
  532. this.reloading = true;
  533. setTimeout(() => { this.reloading = false; }, firerate*1000);
  534. }
  535. for(var i = 0; i < bullet.list.length; i++) {
  536. bullet.list[i].draw();
  537.  
  538.  
  539. }
  540.  
  541. //decal on shooter
  542. var size = 0.25*this.width;
  543. var tl = new platform(this.x, this.y, size, size, color);
  544. var tr = new platform(this.x + this.width - size, this.y, size, size, color);
  545. var bl = new platform(this.x, this.y + this.height - size, size, size, color);
  546. var br = new platform(this.x + this.width - size, this.y + this.height - size, size, size, color);
  547. }
  548.  
  549. this.eightfire = function(speed, firerate, widthf, heightf, color) {
  550. if(this.startup == true) {
  551. setTimeout(() => { this.startup = false; /*this.startuptime = 0;*/ }, (this.startuptime + firerate)*1000);
  552. }
  553. if(this.reloading != true && this.startup == false) {
  554. if(paused == false && document.hasFocus()) {
  555. var bull = new bullet(this.x + 0.5*this.width - 0.5*widthf, this.y + 0.5*this.height - 0.5*heightf, widthf, heightf, color, 0, "enemy", 30, speed, 0);
  556. var bull2 = new bullet(this.x + 0.5*this.width - 0.5*widthf, this.y + 0.5*this.height - 0.5*heightf, widthf, heightf, color, 0, "enemy", 30, -speed, 0);
  557. var bull3 = new bullet(this.x + 0.5*this.width - 0.5*widthf, this.y + 0.5*this.height - 0.5*heightf, widthf, heightf, color, 0, "enemy", 30, 0, speed);
  558. var bull4 = new bullet(this.x + 0.5*this.width - 0.5*widthf, this.y + 0.5*this.height - 0.5*heightf, widthf, heightf, color, 0, "enemy", 30, 0, -speed);
  559. var bull5 = new bullet(this.x + 0.5*this.width - 0.5*widthf, this.y + 0.5*this.height - 0.5*heightf, widthf, heightf, color, 0, "enemy", 30, speed, speed);
  560. var bull6 = new bullet(this.x + 0.5*this.width - 0.5*widthf, this.y + 0.5*this.height - 0.5*heightf, widthf, heightf, color, 0, "enemy", 30, speed, -speed);
  561. var bull7 = new bullet(this.x + 0.5*this.width - 0.5*widthf, this.y + 0.5*this.height - 0.5*heightf, widthf, heightf, color, 0, "enemy", 30, -speed, speed);
  562. var bull8 = new bullet(this.x + 0.5*this.width - 0.5*widthf, this.y + 0.5*this.height - 0.5*heightf, widthf, heightf, color, 0, "enemy", 30, -speed, -speed);
  563.  
  564. }
  565.  
  566. this.reloading = true;
  567. setTimeout(() => { this.reloading = false; }, firerate*1000);
  568. }
  569. for(var i = 0; i < bullets.length; i++) {
  570. if(bullets[i].origin == "enemy") {
  571. bullets[i].draw();
  572. }
  573.  
  574.  
  575. }
  576.  
  577. //decal on shooter
  578. var size = 0.25*this.width;
  579. /*
  580. var t = new platform(this.x + 0.5*this.width - 0.5*size, this.y, size, size, color);
  581. var l = new platform(this.x, this.y + 0.5*this.height - 0.5*size, size, size, color);
  582. var b = new platform(this.x + 0.5*this.width - 0.5*size, this.y + this.height - size, size, size, color);
  583. var r = new platform(this.x + this.width - size, this.y + 0.5*this.height - 0.5*size, size, size, color);
  584. var tl = new platform(this.x, this.y, size, size, color);
  585. var tr = new platform(this.x + this.width - size, this.y, size, size, color);
  586. var bl = new platform(this.x, this.y + this.height - size, size, size, color);
  587. var br = new platform(this.x + this.width - size, this.y + this.height - size, size, size, color);
  588. */
  589. }
  590.  
  591. this.horzfire = function(speed, firerate, widthf, heightf, color) {
  592. if(this.startup == true) {
  593. setTimeout(() => { this.startup = false; /*this.startuptime = 0;*/ }, (this.startuptime + firerate)*1000);
  594. }
  595. if(this.reloading != true && this.startup == false) {
  596. if(paused == false && document.hasFocus()) {
  597. var bull = new bullet(this.x + 0.5*this.width - 0.5*widthf, this.y + 0.5*this.height - 0.5*heightf, widthf, heightf, color, 0, speed, 0);
  598. var bull2 = new bullet(this.x + 0.5*this.width - 0.5*widthf, this.y + 0.5*this.height - 0.5*heightf, widthf, heightf, color, 0, -speed, 0);
  599.  
  600. }
  601.  
  602.  
  603. this.reloading = true;
  604. setTimeout(() => { this.reloading = false; }, firerate*1000);
  605. }
  606. for(var i = 0; i < bullet.list.length; i++) {
  607. bullet.list[i].draw();
  608.  
  609.  
  610. }
  611. //decal on shooter
  612. var size = 0.25*this.width;
  613. var l = new platform(this.x, this.y + 0.5*this.height - 0.5*size, size, size, color);
  614. var r = new platform(this.x + this.width - size, this.y + 0.5*this.height - 0.5*size, size, size, color);
  615. }
  616.  
  617.  
  618. }
  619. let enemies = [];
  620.  
  621. function bar(x,y,width,height,name, color, numer, denom) {
  622. this.name = name;
  623. var tsize = canvas.width / 50;
  624. b.textAlign = "left";
  625. b.font = tsize + "px Arial";
  626. b.fillStyle = "White";
  627. b.fillText(name + ": ", x, y + 0.04*canvas.height);
  628. b.fillStyle = "SlateGray";
  629. b.fillRect(x + 0.1*canvas.width, y, width, height);
  630. b.fillStyle = color;
  631. b.fillRect(x + 0.1*canvas.width, y, width * (numer / denom), height);
  632.  
  633.  
  634. }
  635.  
  636. function gameLoop() {
  637.  
  638.  
  639. for(var i = 0; i < players.length; i++) {
  640.  
  641. if(keyState[players[i].upI]) {
  642. players[i].y = players[i].y - players[i].speed;
  643. }
  644. if(keyState[players[i].downI]) {
  645. players[i].y = players[i].y + players[i].speed;
  646. }
  647. if(keyState[players[i].leftI]) {
  648. players[i].x = players[i].x - players[i].speed;
  649. }
  650. if(keyState[players[i].rightI]) {
  651. players[i].x = players[i].x + players[i].speed;
  652. }
  653.  
  654. /*
  655. if(keyState[players[i].upI]) {
  656. b.save();
  657. b.translate(player.x - canvas.width / 2, player.y - canvas.height / 2);
  658. b.fillStyle = "#f03a47";
  659. b.fillRect(0, 0, canvas.width, canvas.height);
  660. b.restore();
  661. }
  662. */
  663.  
  664. }
  665. if(keyState[73]) {
  666. inform = true;
  667. } else {
  668. inform = false;
  669. }
  670.  
  671. if(keyState[32] && cantFreeze == false) {
  672. if(freeze > 0) {
  673. freezeTime = true;
  674. timeDilation = 0.1;
  675. freeze -= 0.5;
  676. } else {
  677. cantFreeze = true;
  678.  
  679. }
  680.  
  681. } else {
  682. freezeTime = false;
  683. timeDilation = 1;
  684. if(freeze < maxFreeze) {
  685. freeze += 0.1;
  686. }
  687. if(cantFreeze == true) {
  688. if(Math.floor(freeze) == maxFreeze) {
  689. cantFreeze = false;
  690. }
  691. }
  692. }
  693.  
  694.  
  695. setTimeout(gameLoop, 10);
  696. }
  697.  
  698.  
  699.  
  700.  
  701. let p1;
  702. let p2;
  703. let ndog;
  704. let e1;
  705. let e2;
  706. let e3;
  707. let e4;
  708. let e5;
  709. let e6;
  710. let e7;
  711. let e8;
  712. let r1;
  713.  
  714. function init() {
  715. b.fillStyle = "#f03a47";
  716. b.fillRect(-canvas.width, 0, canvas.width, canvas.height);
  717. p1 = new player(800, 400, 50, 50, "RoyalBlue", 5);
  718. //p2 = new player(800, 400, 50, 50, "Tomato", 5);
  719. e1 = new enemy(300, 200, 50, 50, "DarkRed");
  720. e2 = new enemy(-300, 700, 50, 50, "DarkRed");
  721. e3 = new enemy(-900, 200, 200, 200, "DarkRed");
  722. e3.health = 300;
  723. e3.healthI = 300;
  724.  
  725. e4 = new enemy(-2300, 100, 50, 50, "DarkRed");
  726. e5 = new enemy(-2300, 700, 50, 50, "DarkRed");
  727. e6 = new enemy(-3000, 300, 50, 50, "DarkRed");
  728. e7 = new enemy(-3000, 500, 50, 50, "DarkRed");
  729. e8 = new enemy(-3900, 200, 200, 200, "DarkRed");
  730. e8.health = 400;
  731. e8.healthI = 400;
  732. p1.remap(87, 65, 83, 68);
  733. bullets = [];
  734.  
  735.  
  736. }
  737.  
  738. init();
  739.  
  740. setInterval(function () {
  741. b.save();
  742.  
  743. offsetX = -p1.x + canvas.width / 2;
  744. offsetY = -p1.y + canvas.height / 2;
  745.  
  746. b.translate(offsetX, offsetY);
  747. //b.scale(scalewidth, scaleheight);
  748.  
  749. b.clearRect(p1.x - (canvas.width/2), p1.y - (canvas.height/2), canvas.width, canvas.height);
  750.  
  751. b.fillStyle = "#66CDAA";
  752. b.fillRect(-1000, 0, 2000, 800);
  753. b.fillRect(-2000, 200, 1000, 400);
  754. b.fillRect(-4000, 0, 2000, 800);
  755.  
  756.  
  757.  
  758.  
  759.  
  760.  
  761.  
  762.  
  763.  
  764.  
  765. p1.draw();
  766. p1.fire(20, 0.1, 30, 30, 10, "RoyalBlue");
  767. //p2.draw();
  768. //p2.fire(20, 0.1, 30, 30, 10, "Tomato");
  769. e1.draw();
  770. e1.fire(5,1, 50, 50, "Red");
  771. e1.move("away and zig-zag", 2, 0, 700);
  772. //console.log(e1.dx * timeDilation * e1.Q);
  773. e1.cd(1);
  774. e2.draw();
  775. e2.fire(5,1, 50, 50, "Red");
  776.  
  777. e3.draw();
  778. e3.fire(20,1, 50, 50, "Red");
  779. e3.move("away", 2, 100, 900);
  780.  
  781. //r1.draw();
  782.  
  783. e4.draw();
  784. e4.fire(7,1, 50, 50, "Red");
  785. e4.move("towards", 5, 0, 700);
  786. e5.draw();
  787. e5.fire(7,1, 50, 50, "Red");
  788. e5.move("towards", 5, 0, 700);
  789. e6.draw();
  790. e6.fire(5,1, 50, 50, "Red");
  791. e7.draw();
  792. e7.fire(5,1, 50, 50, "Red");
  793. e8.draw();
  794. e8.fire(10,0.4, 50, 50, "Red");
  795.  
  796. //p2.draw();
  797. //p2.fire();
  798.  
  799.  
  800. b.restore();
  801.  
  802.  
  803.  
  804.  
  805. if(inform == true) {
  806. b.textAlign = "left";
  807. b.font = "20px Arial";
  808. b.fillStyle = "White";
  809. var RposX = Math.round(p1.x);
  810. var RposY = Math.round(p1.y);
  811. b.fillText("Canvas Proportions: " + canvas.width + ", " + canvas.height, 10, 90);
  812. b.fillText("P1 Position: " + RposX + ", " + RposY, 50, 120);
  813. b.fillText(bullets, 50, 150);
  814. b.fillText(freeze, 50, 180);
  815.  
  816. }
  817.  
  818. p1.healthbar(0.01*canvas.width, 0.01*canvas.height, 0.3*canvas.width, 0.05*canvas.height, "P1 Health");
  819. //p2.healthbar(0.5*canvas.width, 0.01*canvas.height, 0.3*canvas.width, 0.05*canvas.height, "P2 Health");
  820.  
  821. //Freeze Bar
  822. var Freeze = new bar(0.5*canvas.width, 0.01*canvas.height, 0.3*canvas.width, 0.05*canvas.height, "Freeze", "PaleTurquoise", freeze, maxFreeze);
  823.  
  824.  
  825.  
  826. for(var i = 0; i < players.length; i++) {
  827. if(players[i].health < players[i].healthI && mouseDown < 1 && click == false) {
  828. players[i].health += 0.1;
  829.  
  830. }
  831. }
  832.  
  833.  
  834.  
  835.  
  836. var Tx;
  837. var Ty;
  838. var Twidth;
  839. var Theight;
  840. //checking if Bullets are overlaping
  841. for(var i = 0; i < bullets.length; i++) {
  842. Tx = bullets[i].x;
  843. Ty = bullets[i].y;
  844. Twidth = bullets[i].width;
  845. Theight = bullets[i].height;
  846. //console.log(Tx);
  847. for(var j = 0; j < bullets.length; j++) {
  848. //posX > x - Bsize && posX < x + width && posY > y - Bsize && posY < y + width
  849.  
  850. if(j != i && Tx > bullets[j].x - Twidth && Tx < bullets[j].x + bullets[j].width && Ty > bullets[j].y - Theight && Ty < bullets[j].y + bullets[j].width && bullets[i].origin != bullets[j].origin) {
  851. var item = bullets[j];
  852. var item2 = bullets[i];
  853. //bullets = bullets.filter(bullets => bullets !== item);
  854. //bullets = bullets.filter(bullets => bullets !== item2);
  855.  
  856. //bullets = [];
  857. }
  858. }
  859. //Bullets move
  860. bullets[i].move();
  861. }
  862.  
  863.  
  864.  
  865. }, 20);
  866.  
  867.  
  868.  
  869.  
  870. gameLoop();
  871.  
  872. </script>
  873.  
  874. </body>
  875.  
  876. <style>
  877.  
  878. html, body {
  879.  
  880. width: 100%;
  881.  
  882. height: 100%;
  883.  
  884. margin: 0;
  885.  
  886. overflow: hidden;
  887.  
  888. }
  889.  
  890. #Canvy {
  891.  
  892.  
  893.  
  894. background-color: #000000;
  895.  
  896. }
  897.  
  898. body {
  899.  
  900. background-color: Black;
  901.  
  902.  
  903. }
  904. body {
  905. -webkit-touch-callout: none; /* iOS Safari */
  906. -webkit-user-select: none; /* Safari */
  907. -khtml-user-select: none; /* Konqueror HTML */
  908. -moz-user-select: none; /* Old versions of Firefox */
  909. -ms-user-select: none; /* Internet Explorer/Edge */
  910. user-select: none; /* Non-prefixed version, currently
  911. supported by Chrome, Opera and Firefox */
  912. }
  913.  
  914. </style>
  915.  
  916. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement