Advertisement
BenjaminPlays

Reduce lag - ANY SITE!

Sep 30th, 2020 (edited)
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. GO TO ANY WEBSITE WITH GOOGLE.. THEN CONTROL + I AND GO TO BOTTOM OF HTML AND PUT THIS IN.
  2.  
  3. $(document).ready(function(){
  4.  
  5. var canvas = $("#canvas")[0];
  6. var ctx = canvas.getContext("2d");
  7. var w = $("#canvas").width();
  8. var h = $("#canvas").height();
  9. var cw = 10;
  10.  
  11. var title = new Image();
  12. var startButton = new Image();
  13. var startButtonSelected1 = new Image();
  14. var startButtonSelected2 = new Image();
  15. var startButtonSelected3 = new Image();
  16. var background = new Image();
  17. var ship = new Image();
  18. var dead_ship = new Image();
  19. var retical = new Image();
  20. var bullet = new Image();
  21. var slug = new Image();
  22. var gunLabel1 = new Image();
  23. var gunLabel2 = new Image();
  24. var gunLabel3 = new Image();
  25. var gunLabel4 = new Image();
  26. var gunLabel5 = new Image();
  27. var gunLabel1Trans = new Image();
  28. var gunLabel2Trans = new Image();
  29. var gunLabel3Trans = new Image();
  30. var currentStartButton = startButton;
  31. var startButtonIncrementor = 0;
  32. var startButtonTimeNow = new Date().getTime();
  33. var startButtonTimeThen = new Date().getTime();
  34. var drawStart = true;
  35. var gun = "Sniper"
  36. var credits = 0;
  37. var dead = 0;
  38. var dead2 = 0;
  39. var dead3 = 0;
  40. var newSpeed = 5;
  41. var bulletSpeed = 8;
  42. var shipX = 0;
  43. var ship2X = 1000;
  44. var ship3X = 0;
  45. var shipY = 250;
  46. var ship2Y = 300;
  47. var ship3Y = 250;
  48. var bulletX = 500;
  49. var bulletY = 550;
  50. var slugAX = 500;
  51. var slugBX = 500;
  52. var slugCX = 500;
  53. var slugAY = 550;
  54. var slugBY = 550;
  55. var slugCY = 550;
  56. var gunType = -1;
  57. var reticalX = 0;
  58. var reticalY = 0;
  59. var reticalX2 = -1;
  60. var reticalY2 = -1;
  61. var drawShipA = true;
  62. var drawShipB = true;
  63. var drawShipC = true;
  64. var drawBullet = false;
  65. var drawSlugs = false;
  66. var now, then = new Date().getTime(), delta;
  67. var sniperSound = new Audio("sniper.wav");
  68. var shotgunSound = new Audio("shotgun.wav");
  69.  
  70. title.src = "images/title1.png";
  71. startButton.src = "images/start_button.png";
  72. startButtonSelected1.src = "images/start_button_selected1.png";
  73. startButtonSelected2.src = "images/start_button_selected2.png";
  74. startButtonSelected3.src = "images/start_button_selected3.png";
  75. background.src = "images/background.png";
  76. ship.src = "images/ship.png";
  77. dead_ship.src = "images/crashed_ship.png";
  78. retical.src = "images/retical.png";
  79. bullet.src = "images/bullet.png";
  80. slug.src = "images/slug.png";
  81. gunLabel1.src = "images/1.png";
  82. gunLabel2.src = "images/2.png";
  83. gunLabel3.src = "images/3.png";
  84. gunLabel4.src = "images/4.png";
  85. gunLabel5.src = "images/5.png";
  86. gunLabel1Trans.src = "images/1_trans.png";
  87. gunLabel2Trans.src = "images/2_trans.png";
  88. gunLabel3Trans.src = "images/3_trans.png";
  89.  
  90. sniperSound.volume = 0.1;
  91.  
  92. function getRandomInt(min, max) {
  93.     return Math.floor(Math.random() * (max - min + 1) + min);
  94. }
  95.  
  96. function findPos(obj) {
  97.     var curleft = 0, curtop = 0;
  98.     if (obj.offsetParent) {
  99.         do {
  100.             curleft += obj.offsetLeft;
  101.             curtop += obj.offsetTop;
  102.         } while (obj = obj.offsetParent);
  103.         return { x: curleft, y: curtop };
  104.     }
  105.     return undefined;
  106. }
  107.  
  108.  
  109. $('#canvas').mousemove(function(e) {
  110.     var pos = findPos(this);
  111.     reticalX = e.pageX - pos.x;
  112.     reticalY = e.pageY - pos.y;
  113. });
  114.  
  115. $('#canvas').click(function(e) {
  116.     var pos = findPos(this);
  117.     reticalX2 = e.pageX - pos.x - 40;
  118.     reticalY2 = e.pageY - pos.y - 40;
  119.     if (gunType == 0){
  120.         if ((Math.abs(reticalX2 - (shipX - 35)) <= 50) && (Math.abs(reticalY2 - (shipY + 35)) <= 50)){
  121.             dead = 1;
  122.             credits += 100}
  123.         if ((Math.abs(reticalX2 - (ship2X - 10)) <= 50) && (Math.abs(reticalY2 - (ship2Y + 10)) <= 50)){
  124.             dead2 = 1;
  125.             credits += 100}
  126.         if ((Math.abs(reticalX2 - (ship3X - 10)) <= 50) && (Math.abs(reticalY2 - (ship3Y + 10)) <= 50)){
  127.             dead3 = 1;
  128.             credits += 100}}
  129.     else if (gunType == 1 && drawBullet == false){
  130.         drawBullet = true;
  131.         bulletX = reticalX2;
  132.         sniperSound.play();}
  133.     else if (gunType == 2 && drawSlugs == false){
  134.         drawSlugs = true;
  135.         slugAX = reticalX2;
  136.         slugBX = reticalX2;
  137.         slugCX = reticalX2;
  138.         shotgunSound.play();}        
  139.     else if (gunType == -1)
  140.         if ((reticalX >= 370 && reticalY >= 220) && (reticalX < 520 && reticalY < 300)) {
  141.             drawStart = false;
  142.             gunType = 0;}
  143. });
  144.  
  145. $(document).keydown(function(e) {
  146.     if (drawStart == false) {
  147.         if (drawBullet == false && drawSlugs == false) {
  148.             switch(e.keyCode) {
  149.                 case 49:
  150.                     gunType = 0;
  151.                     gun = "Sniper";
  152.                     break;
  153.                 case 50:
  154.                     gunType = 1;
  155.                     gun = "Revolver";
  156.                     break;
  157.                 case 51:
  158.                     gunType = 2;
  159.                     gun = "Shotgun";
  160.                     break;}
  161.     }}            
  162.  
  163. });
  164.  
  165.  
  166. function update() {
  167.  
  168.     if ((reticalX >= 370 && reticalY >= 220) && (reticalX < 520 && reticalY < 280)) {
  169.         if ((startButtonTimeNow - startButtonTimeThen) < 500) {
  170.             currentStartButton = startButtonSelected1;
  171.             startButtonTimeNow = new Date().getTime()}
  172.         else if ((startButtonTimeNow - startButtonTimeThen) < 1000) {
  173.             currentStartButton = startButtonSelected2;
  174.             startButtonTimeNow = new Date().getTime();}
  175.         else if ((startButtonTimeNow - startButtonTimeThen) < 1500) {
  176.             currentStartButton = startButtonSelected3;
  177.             startButtonTimeNow = new Date().getTime();}
  178.         else if ((startButtonTimeNow - startButtonTimeThen) < 2000) {
  179.             startButtonTimeNow, startButtonTimeThen = new Date().getTime(), new Date().getTime();}
  180.         else {startButtonTimeNow, startButtonTimeThen = new Date().getTime(), new Date().getTime();}
  181.     }        
  182.  
  183.     else {currentStartButton = startButton;}
  184.  
  185.     if (shipY >= -70 && dead == 0){
  186.         drawShipA = true;
  187.         shipY -= newSpeed;
  188.         shipX += newSpeed;
  189.     }
  190.     else if (dead == 1 && shipY < 600){
  191.         shipY += newSpeed;
  192.         drawShipA = false;
  193.     }            
  194.     else {  
  195.         shipY = getRandomInt(400, 425);
  196.         shipX = getRandomInt(100, 900);
  197.         dead = 0;
  198.         drawShipA = true;
  199.     }
  200.  
  201.     if (ship2Y >= -70 && dead2 == 0){
  202.         drawShipB = true;
  203.         ship2Y -= newSpeed;
  204.         ship2X -= newSpeed;
  205.     }
  206.     else if (dead2 == 1 && ship2Y < 600){
  207.         ship2Y += newSpeed;
  208.         drawShipB = false;
  209.     }
  210.     else {  
  211.         ship2Y = getRandomInt(400, 425);
  212.         ship2X = getRandomInt(100, 900);
  213.         dead2 = 0;
  214.         drawShipB = true;;
  215.     }
  216.  
  217.     if (ship3Y >= -70 && dead3 == 0){
  218.         drawShipC = true;
  219.         ship3Y -= newSpeed;
  220.         ship3X -= newSpeed;
  221.     }
  222.     else if (dead3 == 1 && ship3Y < 600){
  223.         ship3Y += newSpeed;
  224.         drawShipC = false;
  225.     }
  226.     else {  
  227.         ship3Y = getRandomInt(400, 425);
  228.         ship3X = getRandomInt(100, 900);
  229.         dead3 = 0;
  230.         drawShipC = true;
  231.     }
  232.  
  233.     if ((dead + dead2 + dead3 >= 2) && (((Math.abs(shipX - ship2X) + Math.abs(shipY - ship2Y)) + (Math.abs(shipX - ship3X) + Math.abs(shipY - ship3Y)) + (Math.abs(ship3X - ship2X) + Math.abs(ship3Y - ship2Y))) < 1500)) {credits += 50}
  234.  
  235.     if (drawBullet == true){  
  236.         bulletY -= bulletSpeed;
  237.         if ((Math.abs(bulletX - (shipX + 20)) <= 40) && (Math.abs(bulletY - (shipY + 10)) <= 50)){
  238.             dead = 1;
  239.             credits += 100}
  240.         if ((Math.abs(bulletX - (ship2X + 20)) <= 40) && (Math.abs(bulletY - (ship2Y + 10)) <= 50)){
  241.             dead2 = 1;
  242.             credits += 100}
  243.         if ((Math.abs(bulletX - (ship3X + 20)) <= 40) && (Math.abs(bulletY - (ship3Y + 10)) <= 50)){
  244.             dead3 = 1;
  245.             credits += 100}
  246.         if (bulletY <= -130){
  247.             drawBullet = false;
  248.             bulletY = 550;
  249.         } }
  250.  
  251.     if (drawSlugs == true){
  252.         slugAY -= bulletSpeed;
  253.         bulletSpeed = bulletSpeed * 2;
  254.         bulletSpeed = (bulletSpeed - (bulletSpeed % 3))/3
  255.         slugBX -= bulletSpeed;
  256.         slugBY -= bulletSpeed * 2;
  257.         slugCX += bulletSpeed;
  258.         slugCY -= bulletSpeed * 2;
  259.         if ((Math.abs(slugAX - (shipX + 10)) <= 40) && (Math.abs(slugAY - (shipY + 10)) <= 50)){
  260.             dead = 1;
  261.             credits += 100}
  262.         if ((Math.abs(slugAX - (ship2X + 10)) <= 40) && (Math.abs(slugAY - (ship2Y + 10)) <= 50)){
  263.             dead2 = 1;
  264.             credits += 100}
  265.         if ((Math.abs(slugAX - (ship3X + 10)) <= 40) && (Math.abs(slugAY - (ship3Y + 10)) <= 50)){
  266.             dead3 = 1;
  267.             credits += 100}  
  268.          if ((Math.abs(slugBX - (shipX + 10)) <= 40) && (Math.abs(slugBY - (shipY + 10)) <= 50)){
  269.             dead = 1;
  270.             credits += 100}
  271.         if ((Math.abs(slugBX - (ship2X + 10)) <= 40) && (Math.abs(slugBY - (ship2Y + 10)) <= 50)){
  272.             dead2 = 1;
  273.             credits += 100}
  274.         if ((Math.abs(slugBX - (ship3X + 10)) <= 40) && (Math.abs(slugBY - (ship3Y + 10)) <= 50)){
  275.             dead3 = 1;
  276.             credits += 100}            
  277.          if ((Math.abs(slugCX - (shipX + 10)) <= 40) && (Math.abs(slugCY - (shipY + 10)) <= 50)){
  278.             dead = 1;
  279.             credits += 100}
  280.         if ((Math.abs(slugCX - (ship2X + 10)) <= 40) && (Math.abs(slugCY - (ship2Y + 10)) <= 50)){
  281.             dead2 = 1;
  282.             credits += 100}
  283.         if ((Math.abs(slugCX - (ship3X + 10)) <= 40) && (Math.abs(slugCY - (ship3Y + 10)) <= 50)){
  284.             dead3 = 1;
  285.             credits += 100}
  286.         if (slugAY <= -150){
  287.             drawSlugs = false;
  288.             slugAY = 550;
  289.             slugBY = 550;
  290.             slugCY = 550;}}
  291.  
  292. }
  293.  
  294.  
  295. function paint()
  296. {
  297.     now = new Date().getTime();
  298.     delta = now - then;
  299.  
  300.     ctx.drawImage(background,0,0);
  301.  
  302.     if (drawShipA == true){ctx.drawImage(ship, shipX, shipY, 70, 70);}
  303.     else {ctx.drawImage(dead_ship, shipX, shipY, 70, 70);}
  304.  
  305.     if (drawShipB == true){ctx.drawImage(ship, ship2X, ship2Y, 70, 70);}
  306.     else {ctx.drawImage(dead_ship, ship2X, ship2Y, 70, 70);}        
  307.  
  308.     if (drawShipC == true){ctx.drawImage(ship, ship3X, ship3Y, 70, 70);}
  309.     else {ctx.drawImage(dead_ship, ship3X, ship3Y, 70, 70);}            
  310.  
  311.     if (drawBullet == true){
  312.         ctx.drawImage(bullet, bulletX + 30, bulletY, 10,10);}
  313.  
  314.     if (drawSlugs == true){
  315.         ctx.drawImage(slug, slugAX + 20, slugAY, 30, 30);
  316.         ctx.drawImage(slug, slugBX + 20, slugBY, 30, 30);
  317.         ctx.drawImage(slug, slugCX + 20, slugCY, 30, 30);        
  318.     }
  319.  
  320.     bulletSpeed = calcSpeed(delta, 6);
  321.     newSpeed = calcSpeed(delta, 3);    
  322.     then = now;
  323.     ctx.drawImage(retical, reticalX - 40, reticalY - 40, 80, 80);
  324.     ctx.font = "30px sans-serif";
  325.     ctx.strokeStyle = "rgba(255, 165, 0, 1)"
  326.     ctx.strokeText("$" + credits, 15, 30);
  327.     ctx.strokeStyle = "rgba(255, 165, 0, .8)"
  328.     ctx.strokeText(gun, 450, 500);
  329.     ctx.drawImage(gunLabel1, 445, 510, 35, 35);
  330.     ctx.drawImage(gunLabel2, 470, 510, 35, 35);
  331.     ctx.drawImage(gunLabel3, 495, 510, 35, 35);
  332.     switch(gunType) {
  333.         case 0:
  334.             ctx.drawImage(gunLabel1Trans, 445, 510, 35, 35);
  335.             break;
  336.         case 1:
  337.             ctx.drawImage(gunLabel2Trans, 470, 510, 35, 35);
  338.             break;
  339.         case 2:
  340.             ctx.drawImage(gunLabel3Trans, 495, 510, 35, 35);
  341.             break;
  342.             }
  343.     if (drawStart == true) {
  344.         ctx.drawImage(background, 0, 0);
  345.         ctx.drawImage(title, 270, 90, 400, 100);
  346.         ctx.drawImage(currentStartButton, 370, 220, 150, 60);
  347.         ctx.drawImage(retical, reticalX - 40, reticalY - 40, 80, 80);}  
  348.     ctx.strokeRect(0,0,w,background.height);
  349.     }
  350.  
  351. var init = function()
  352.  
  353. {
  354.     requestAnimationFrame(init);
  355.     update();
  356.     paint();
  357. }  
  358.  
  359. var calcSpeed = function(del, speed) {
  360.     return (speed * 60 * del) / 1000;
  361. }
  362.  
  363. init();})
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement