Advertisement
valiamaximova1

Tankove game

Jun 15th, 2019
340
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.04 KB | None | 0 0
  1. enum Direction {
  2.   UP,
  3.     RIGHT,
  4.     DOWN,
  5.     LEFT
  6. }
  7.  
  8. class Bullet {
  9.   public float x;
  10.   public float y;
  11.   public int size;
  12.   public int dirX;
  13.   public int dirY;
  14.   public float speed;
  15.   public Tank origin;
  16.   public boolean toDelete;
  17.  
  18.   public Bullet(float x, float y, Tank origin, int dirX, int dirY) {
  19.     speed = 7;
  20.     this.x = x;
  21.     this.y = y;
  22.     this.origin = origin;
  23.     size = bulletSize;
  24.     this.dirX = dirX;
  25.     this.dirY = dirY;
  26.     toDelete = false;
  27.   }
  28. }
  29.  
  30. class Tank {
  31.   public float x;
  32.   public float y;
  33.   public int hp;
  34.   public int dmg;
  35.   public int size;
  36.   public float speed;
  37.   public int dirX;
  38.   public int dirY;
  39.   public Direction shootDir;
  40.   public boolean shoot;
  41.   public float startX;
  42.   public float startY;
  43.   public int startHP;
  44.  
  45.  
  46.   public Tank(boolean p1, int hp, int dmg, int size, float speed) {
  47.     if (p1) {
  48.       x = 0 + 10;
  49.     } else {
  50.       x = width - size - 10;
  51.     }
  52.     y = hudHeight + (height-hudHeight)/2;
  53.     startX = x;
  54.     startY = y;
  55.     this.hp = hp;
  56.     startHP = hp;
  57.     this.dmg = dmg;
  58.     this.size = size;
  59.     this.speed = speed;
  60.     dirX = 0;
  61.     dirY = 0;
  62.     shootDir = Direction.UP;
  63.     shoot = false;
  64.   }
  65. }
  66.  
  67. Tank p1;
  68. Tank p2;
  69. int bulletSize = 4;
  70. float barrelSize = 5;
  71. ArrayList<Bullet> bulletList = new ArrayList<Bullet>();
  72. int hudHeight = 100;
  73. int p1Points = 0;
  74. int p2Points = 0;
  75.  
  76. void setup() {
  77.   size(600, 600);
  78.   p1 = new Tank(true, 3, 1, 20, 5);
  79.   p2 = new Tank(false, 3, 1, 20, 5);
  80. }
  81.  
  82. void draw() {
  83.   background(0);
  84.   updateTank(p1);
  85.   updateTank(p2);
  86.  
  87.   updateBullets();
  88.  
  89.   drawTank(p1);
  90.   drawTank(p2);
  91.  
  92.   drawBullets();
  93.  
  94.   drawHUD();
  95. }
  96.  
  97. void takeDamage(Tank t, Bullet b) {
  98.   t.hp -= b.origin.dmg;
  99.   if (t.hp <= 0) {
  100.     newRound();
  101.   }
  102. }
  103.  
  104. void drawHUD() {
  105.   fill(255);
  106.   stroke(0, 0, 255);
  107.   rect(0, 0, width, hudHeight);
  108.   fill(255, 0, 0);
  109.   rect(10, 10, 100, 80);
  110.   float p1HPLength = ((float)p1.hp / (float)p1.startHP) * 100;
  111.   fill(0, 255, 0);
  112.   rect(10, 10, p1HPLength, 80);
  113.  
  114.   fill(255, 0, 0);
  115.   rect(width-110, 10, 100, 80);
  116.   float p2HPLength = ((float)p2.hp / (float)p2.startHP) * 100;
  117.   fill(0, 255, 0);
  118.   rect(width-110, 10, p2HPLength, 80);
  119.  
  120.   textSize(32);
  121.   fill(0, 0, 255);
  122.   text(p1Points, 238, 60);
  123.   fill(0, 0, 255);
  124.   text(p2Points, 328, 60);
  125. }
  126.  
  127. void updateTank(Tank t) {
  128.   t.x += t.dirX * t.speed;
  129.   t.y += t.dirY * t.speed;
  130.   if (t.x + t.size > width) {
  131.     t.x = width - t.size;
  132.   }
  133.   if (t.x < 0) {
  134.     t.x = 0;
  135.   }
  136.   if (t.y + t.size > height) {
  137.     t.y = height - t.size;
  138.   }
  139.   if (t.y < hudHeight) {
  140.     t.y = hudHeight;
  141.   }
  142.  
  143.   shootBullet(t);
  144. }
  145.  
  146. void keyPressed() {
  147.   if (key == 'w') {
  148.     p1.dirY = -1;
  149.     p1.shootDir = Direction.UP;
  150.     p1.dirX = 0;
  151.   } else if (key == 's') {
  152.     p1.dirY = 1;
  153.     p1.shootDir = Direction.DOWN;
  154.     p1.dirX = 0;
  155.   } else if (key == 'a') {
  156.     p1.dirX = -1;
  157.     p1.shootDir = Direction.LEFT;
  158.     p1.dirY = 0;
  159.   } else if (key == 'd') {
  160.     p1.dirX = 1;
  161.     p1.shootDir = Direction.RIGHT;
  162.     p1.dirY = 0;
  163.   } else if (key == 'c') {
  164.     p1.shoot = true;
  165.   }
  166.  
  167.   if (key == CODED) {
  168.     if (keyCode == UP) {
  169.       p2.dirY = -1;
  170.       p2.shootDir = Direction.UP;
  171.       p2.dirX = 0;
  172.     } else if (keyCode == DOWN) {
  173.       p2.dirY = 1;
  174.       p2.shootDir = Direction.DOWN;
  175.       p2.dirX = 0;
  176.     } else if (keyCode == LEFT) {
  177.       p2.dirX = -1;
  178.       p2.shootDir = Direction.LEFT;
  179.       p2.dirY = 0;
  180.     } else if (keyCode == RIGHT) {
  181.       p2.dirX = 1;
  182.       p2.shootDir = Direction.RIGHT;
  183.       p2.dirY = 0;
  184.     }
  185.   }
  186.   if (key == 'p') {
  187.     p2.shoot = true;
  188.   }
  189. }
  190.  
  191. void keyReleased() {
  192.   if (key == 'w') {
  193.     if (p1.dirY == -1) {
  194.       p1.dirY = 0;
  195.     }
  196.   } else if (key == 's') {
  197.     if (p1.dirY == 1) {
  198.       p1.dirY = 0;
  199.     }
  200.   } else if (key == 'a') {
  201.     if (p1.dirX == -1) {
  202.       p1.dirX = 0;
  203.     }
  204.   } else if (key == 'd') {
  205.     if (p1.dirX == 1) {
  206.       p1.dirX = 0;
  207.     }
  208.   }
  209.  
  210.   if (key == CODED) {
  211.     if (keyCode == UP) {
  212.       if (p2.dirY == -1) {
  213.         p2.dirY = 0;
  214.       }
  215.     } else if (keyCode == DOWN) {
  216.       if (p2.dirY == 1) {
  217.         p2.dirY = 0;
  218.       }
  219.     } else if (keyCode == LEFT) {
  220.       if (p2.dirX == -1) {
  221.         p2.dirX = 0;
  222.       }
  223.     } else if (keyCode == RIGHT) {
  224.       if (p2.dirX == 1) {
  225.         p2.dirX = 0;
  226.       }
  227.     }
  228.   }
  229. }
  230.  
  231. void newRound() {
  232.   if(p1.hp <= 0) {
  233.     p2Points++;
  234.   }
  235.   else {
  236.     p1Points++;
  237.   }
  238.   p1.hp = p1.startHP;
  239.   p1.x = p1.startX;
  240.   p1.y = p1.startY;
  241.   p1.shootDir = Direction.UP;
  242.   p2.hp = p2.startHP;
  243.   p2.x = p2.startX;
  244.   p2.y = p2.startY;
  245.   p2.shootDir = Direction.UP;
  246.   bulletList.clear();
  247. }
  248.  
  249. void updateBullets() {
  250.   int i = 0;
  251.   for (i = 0; i< bulletList.size(); i++) {
  252.     Bullet b = bulletList.get(i);
  253.     if(b.toDelete) {
  254.       continue;
  255.     }
  256.     b.x += b.dirX * b.speed;
  257.     b.y += b.dirY * b.speed;
  258.     if (b.x < 0    ||
  259.       b.x > width ||
  260.       b.y < 0     ||
  261.       b.y > height)
  262.     {
  263.       b.toDelete = true;
  264.     }
  265.     boolean player1Hit = checkCollision(b, p1);
  266.     boolean player2Hit = checkCollision(b, p2);
  267.     if (player1Hit) {
  268.       b.toDelete = true;
  269.       takeDamage(p1, b);
  270.     }
  271.     if (player2Hit) {
  272.       b.toDelete = true;
  273.       takeDamage(p2, b);
  274.     }
  275.   }
  276. }
  277.  
  278. boolean checkCollision(Bullet b, Tank t) {
  279.   if (b.toDelete || b.origin == t) {
  280.     return false;
  281.   }
  282.  
  283.   if (t.x < b.x + b.size &&
  284.     t.x + t.size > b.x &&
  285.     t.y < b.y + b.size &&
  286.     t.y + t.size > b.y)
  287.   {
  288.     return true;
  289.   }
  290.  
  291.   return false;
  292. }
  293.  
  294. void drawBullets() {
  295.   int i;
  296.   for (i = 0; i<bulletList.size(); i++) {
  297.     Bullet b = bulletList.get(i);
  298.     if (b.toDelete) {
  299.       continue;
  300.     }
  301.     fill(255, 255, 0);
  302.     stroke(255, 255, 0);
  303.     rect(b.x, b.y, bulletSize, bulletSize);
  304.   }
  305. }
  306.  
  307. void drawTank(Tank t) {
  308.   fill(255, 255, 255);
  309.   stroke(255, 255, 255);
  310.   rect(t.x, t.y, t.size, p1.size);
  311.  
  312.   if (t.shootDir == Direction.UP) {
  313.     rect(t.x + t.size/2 - barrelSize/2, t.y - barrelSize, barrelSize, barrelSize);
  314.   } else if (t.shootDir == Direction.RIGHT) {
  315.     rect(t.x + t.size, t.y + t.size/2 - barrelSize/2, barrelSize, barrelSize);
  316.   } else if (t.shootDir == Direction.DOWN) {
  317.     rect(t.x + t.size/2 - barrelSize/2, t.y + t.size, barrelSize, barrelSize);
  318.   } else {
  319.     rect(t.x - barrelSize, t.y + t.size/2 - barrelSize/2, barrelSize, barrelSize);
  320.   }
  321. }
  322.  
  323. void shootBullet(Tank t) {
  324.   if (!t.shoot) {
  325.     return;
  326.   }
  327.  
  328.   float x, y;
  329.   int dirX = 0, dirY = 0;
  330.   if (t.shootDir == Direction.UP) {
  331.     x = t.x + t.size/2 - barrelSize/2;
  332.     y = t.y - barrelSize - bulletSize;
  333.     dirY = -1;
  334.   } else if (t.shootDir == Direction.RIGHT) {
  335.     x = t.x + t.size + barrelSize;
  336.     y = t.y + t.size/2 - barrelSize/2;
  337.     dirX = 1;
  338.   } else if (t.shootDir == Direction.DOWN) {
  339.     x = t.x + t.size/2 - barrelSize/2;
  340.     y = t.y + t.size + barrelSize;
  341.     dirY = 1;
  342.   } else {
  343.     x = t.x - barrelSize - bulletSize;
  344.     y = t.y + t.size/2 - barrelSize/2;
  345.     dirX = -1;
  346.   }
  347.   bulletList.add(new Bullet(x, y, t, dirX, dirY));
  348.   t.shoot = false;
  349. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement