Advertisement
Guest User

Buggy Code

a guest
Apr 26th, 2020
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.75 KB | None | 0 0
  1. <!DOCTYPE html>
  2.  
  3. <html>
  4.  
  5. <head>
  6.  
  7. <title>Doom</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 ammo = 1000;
  25. var ammomax = 30;
  26.  
  27.  
  28. let mouse = {
  29. x: canvas.width / 2,
  30. y: canvas.height / 2
  31. }
  32.  
  33. var keyState = {};
  34. var keyTime = {};
  35.  
  36. window.addEventListener('keydown',function(e){
  37.  
  38. keyState[e.keyCode || e.which] = true;
  39. keyTime[e.keyCode || e.which] = new Date();
  40.  
  41. },true);
  42.  
  43. setInterval(function() {
  44. var now = new Date();
  45. for(key in keyTime) {
  46. if(now - keyTime[key] >= 0500 && document.hasFocus() == false) {
  47. // run the release code for this key
  48. keyState[key] = false;
  49. }
  50. }
  51. }, 1000);
  52.  
  53. window.addEventListener('keyup',function(e){
  54.  
  55. keyState[e.keyCode || e.which] = false;
  56.  
  57. },true);
  58.  
  59. window.addEventListener("mousemove", function(e){
  60. mouse.x = event.clientX;
  61. mouse.y = event.clientY;
  62. })
  63.  
  64. addEventListener('resize', () => {
  65. canvas.width = innerWidth;
  66. canvas.height = innerHeight;
  67.  
  68. init();
  69. })
  70.  
  71.  
  72.  
  73. function bullet(Ix, Iy, width, height, color, speed, dx, dy) {
  74. bullets.push(this);
  75. this.Ix = Ix;
  76. this.Iy = Iy;
  77. this.width = width;
  78. this.height = height;
  79. this.x = this.Ix;
  80. this.y = this.Iy;
  81. this.target;
  82.  
  83. if(dx == null && dy == null) {
  84.  
  85. this.dx = (mouse.x - this.Ix - 0.5*this.width) / 100;
  86. this.dy = (mouse.y - this.Iy - 0.5*this.height) / 100;
  87.  
  88. var length = Math.sqrt(this.dx * this.dx + this.dy * this.dy);
  89. this.dx /= length;
  90. this.dy /= length;
  91. this.dx = this.dx * speed;
  92. this.dy = this.dy * speed;
  93.  
  94.  
  95. } else {
  96. this.dx = dx;
  97. this.dy = dy;
  98.  
  99. }
  100.  
  101.  
  102.  
  103.  
  104.  
  105. this.draw = function() {
  106.  
  107. if(this.x < canvas.width && this.x > -this.width && this.y < canvas.height && this.y > -this.height) {
  108. this.x += this.dx;
  109. this.y += this.dy;
  110.  
  111. b.fillStyle = color;
  112. b.fillRect(this.x, this.y, width, height);
  113. }
  114.  
  115.  
  116. /*
  117. if(posX > this.x - Bsize && posX < this.x + this.width && posY > this.y - Bsize && posY < this.y + this.height) {
  118.  
  119.  
  120.  
  121.  
  122. }
  123. */
  124. }
  125.  
  126. }
  127. let bullets = [];
  128.  
  129. function gun(speed, reload, iammo, width, height, color) {
  130.  
  131. }
  132.  
  133. function player(Ix, Iy, width, height, color, dx, dy) {
  134. players.push(this);
  135. this.x = Ix;
  136. this.y = Iy;
  137. this.width = width;
  138. this.height = height;
  139. this.dx = dx;
  140. this.dy = dy;
  141. this.upI = 38;
  142. this.downI = 40;
  143. this.leftI = 37;
  144. this.rightI = 39;
  145. this.slot1 = "empty";
  146. this.slot2 = "empty";
  147. this.slot3 = "empty";
  148. this.reloading;
  149. this.draw = function() {
  150. b.fillStyle = color;
  151. b.fillRect(this.x, this.y, this.width, this.height);
  152. }
  153. this.remap = function(up, left, down, right) {
  154. this.upI = up;
  155. this.leftI = left;
  156. this.downI = down;
  157. this.rightI = right;
  158. }
  159. this.fire = function() {
  160. /*
  161. if(slot1 == "empty" && slot2 == "empty" && slot3 == "empty") {
  162.  
  163. } else if(slot1 == "empty" && slot2 != "empty") {
  164. slot1 = slot2;
  165. slot2 = "empty";
  166. } else if(slot1 == "empty" && slot2 != "empty") {
  167. slot1 = slot2;
  168. slot2 = "empty";
  169. }
  170. */
  171.  
  172. addEventListener("click", () => {
  173. if(this.reloading != true && document.hasFocus()) {
  174. click = true;
  175. }
  176. });
  177.  
  178. if(this.reloading != true && click == true && ammo > 0) {
  179. if(bullets.length > 20) {
  180. bullets = [];
  181.  
  182. }
  183.  
  184. //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);
  185. //bullet that moves with player
  186. var ndog = new bullet(this.x + 0.5*this.width - 0.5*width, this.y + 0.5*this.height - 0.5*height, width, height, "SteelBlue", 5);
  187.  
  188. ammo -= 1;
  189. click = false;
  190. this.reloading = true;
  191. setTimeout(() => { this.reloading = false; }, 0100);
  192. }
  193.  
  194. for(var i = 0; i < bullets.length; i++) {
  195. bullets[i].draw();
  196. }
  197.  
  198.  
  199.  
  200.  
  201. }
  202.  
  203. }
  204. let players = [];
  205.  
  206.  
  207.  
  208.  
  209.  
  210. function gameLoop() {
  211.  
  212.  
  213. for(var i = 0; i < players.length; i++) {
  214.  
  215. if(keyState[players[i].upI]) {
  216. players[i].y = players[i].y - players[i].dy;
  217. }
  218. if(keyState[players[i].downI]) {
  219. players[i].y = players[i].y + players[i].dy;
  220. }
  221. if(keyState[players[i].leftI]) {
  222. players[i].x = players[i].x - players[i].dx;
  223. }
  224. if(keyState[players[i].rightI]) {
  225. players[i].x = players[i].x + players[i].dx;
  226. }
  227.  
  228. /*
  229. if(keyState[players[i].upI]) {
  230. b.save();
  231. b.translate(player.x - canvas.width / 2, player.y - canvas.height / 2);
  232. b.fillStyle = "#f03a47";
  233. b.fillRect(0, 0, canvas.width, canvas.height);
  234. b.restore();
  235. }
  236. */
  237.  
  238. }
  239. if(keyState[73]) {
  240. inform = true;
  241. } else {
  242. inform = false;
  243. }
  244.  
  245.  
  246. setTimeout(gameLoop, 10);
  247. }
  248.  
  249.  
  250.  
  251.  
  252. let p1;
  253. let p2;
  254.  
  255. function init() {
  256. b.fillStyle = "#f03a47";
  257. b.fillRect(0, 0, canvas.width, canvas.height);
  258. p1 = new player(0.5*canvas.width, 0.5*canvas.height, 20, 20, "white", 5, 5);
  259. p2 = new player(0.5*canvas.width, 0.5*canvas.height, 20, 20, "white", 5, 5);
  260. p1.remap(87, 65, 83, 68);
  261. bullets = [];
  262.  
  263.  
  264. }
  265. //resizeGame();
  266. init();
  267.  
  268. setInterval(function () {
  269.  
  270. b.save();
  271. b.translate(-p1.x + canvas.width / 2, -p1.y + canvas.height / 2);
  272. b.clearRect(p1.x - (canvas.width/2), p1.y - (canvas.height/2), canvas.width, canvas.height);
  273. b.fillStyle = "#f03a47";
  274. b.fillRect(0, 0, canvas.width, canvas.height);
  275. if(document.hasFocus() == false) {
  276. var keyState = {};
  277. }
  278.  
  279.  
  280.  
  281.  
  282.  
  283.  
  284.  
  285.  
  286.  
  287.  
  288.  
  289.  
  290. p1.draw();
  291.  
  292. p1.fire();
  293.  
  294.  
  295. b.restore();
  296.  
  297.  
  298.  
  299.  
  300. if(inform == true) {
  301. b.textAlign = "left";
  302. b.font = "20px Arial";
  303. b.fillStyle = "White";
  304. var RposX = Math.round(p1.x);
  305. var RposY = Math.round(p1.y);
  306. b.fillText("Canvas Proportions: " + canvas.width + ", " + canvas.height, 10, 60);
  307. b.fillText("P1 Position: " + RposX + ", " + RposY, 50, 90);
  308.  
  309. }
  310. //Ammo Tracker
  311. var tsize = canvas.width / 50;
  312. b.textAlign = "right";
  313. b.font = tsize + "px Arial";
  314. b.fillStyle = "White";
  315. b.fillText(ammo + " / " + ammomax, canvas.width - 0*tsize, canvas.height - 0.1*tsize);
  316.  
  317.  
  318. //p1.draw();
  319. //p1.fire();
  320.  
  321.  
  322.  
  323. }, 20);
  324.  
  325.  
  326.  
  327.  
  328. gameLoop();
  329. </script>
  330.  
  331. </body>
  332.  
  333. <style>
  334.  
  335. html, body {
  336.  
  337. width: 100%;
  338.  
  339. height: 100%;
  340.  
  341. margin: 0;
  342.  
  343. overflow: hidden;
  344.  
  345. }
  346.  
  347. #Canvy {
  348.  
  349.  
  350.  
  351. background-color: #000000;
  352.  
  353. }
  354.  
  355. body {
  356.  
  357. background-color: Black;
  358.  
  359.  
  360. }
  361. body {
  362. -webkit-touch-callout: none; /* iOS Safari */
  363. -webkit-user-select: none; /* Safari */
  364. -khtml-user-select: none; /* Konqueror HTML */
  365. -moz-user-select: none; /* Old versions of Firefox */
  366. -ms-user-select: none; /* Internet Explorer/Edge */
  367. user-select: none; /* Non-prefixed version, currently
  368. supported by Chrome, Opera and Firefox */
  369. }
  370.  
  371. </style>
  372.  
  373. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement