SHARE
TWEET

Untitled

a guest Mar 24th, 2019 111 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var px=200, py=300, ex=600, ey=300, pr=0, er=0, php = 20, ehp = 20;
  2. var nb = 0;
  3. var bx = [], by = [], dx = [], dy = [], color = [];
  4. var pspeed = 5, bspeed = 15, r = 10;
  5.  
  6. function d(x1, y1, x2, y2){
  7.     return Math.sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));
  8. }
  9.  
  10. function shoot(x, y, tx, ty, c){
  11.     bx[nb] = x;
  12.     by[nb] = y;
  13.     let dist = d(x, y, tx, ty);
  14.     dx[nb] = (tx-x)*bspeed/dist;
  15.     dy[nb] = (ty-y)*bspeed/dist;
  16.     color[nb] = c;
  17.     ++nb;
  18. }
  19.  
  20. function move_enemy(){
  21.  
  22.     if (er == 0){
  23.         er = r;
  24.         shoot(ex, ey, px, py, "red");
  25.     }
  26. }
  27.  
  28. function rem_bullet(i){
  29.     nb--;
  30.     bx[i] = bx[nb];
  31.     by[i] = by[nb];
  32.     dx[i] = dx[nb];
  33.     dy[i] = dy[nb];
  34.     color[i] = color[nb];
  35. }
  36.  
  37. function update() {
  38.     if (php <= 0 || ehp <= 0) return;
  39.     if (pr > 0) --pr;
  40.     if (er > 0) --er;
  41.     if (isKeyPressed[65]) px -= pspeed;
  42.     if (isKeyPressed[68]) px += pspeed;
  43.     if (isKeyPressed[87]) py -= pspeed;
  44.     if (isKeyPressed[83]) py += pspeed;
  45.     move_enemy();
  46.     for (let i=0; i<nb; ++i){
  47.         bx[i] += dx[i];
  48.         by[i] += dy[i];
  49.         if (color[i] == 'red' && areColliding(px, py, 30, 30, bx[i], by[i], 5, 5)){
  50.             php--;
  51.             rem_bullet(i);
  52.             --i;
  53.             continue;
  54.         }
  55.         if (color[i] == 'blue' && areColliding(ex, ey, 30, 30, bx[i], by[i], 5, 5)){
  56.             ehp--;
  57.             rem_bullet(i);
  58.             --i;
  59.             continue;
  60.         }
  61.     }
  62. }
  63. function draw() {
  64.     context.fillStyle = "blue";
  65.     context.fillRect(px, py, 30, 30);
  66.     context.fillStyle = "red";
  67.     context.fillRect(ex, ey, 30, 30);
  68.     for (let i=0; i<nb; ++i){
  69.         context.fillStyle = color[i];
  70.         context.fillRect(bx[i], by[i], 5, 5);
  71.     }
  72. };
  73. function keyup(key) {
  74.     // Show the pressed keycode in the console
  75.     console.log("Pressed", key);
  76. };
  77. function mouseup() {
  78.     if (pr == 0){
  79.         pr = r;
  80.         shoot(px, py, mouseX, mouseY, "blue");
  81.     }
  82. };
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top