|
|
|
@ -1,6 +1,6 @@
|
|
|
|
|
package com.example.tankbattle;
|
|
|
|
|
|
|
|
|
|
import java.awt.Graphics;
|
|
|
|
|
import java.awt.*;
|
|
|
|
|
|
|
|
|
|
public class Bullet {
|
|
|
|
|
private static final int SPEED = 10;
|
|
|
|
@ -9,7 +9,7 @@ public class Bullet {
|
|
|
|
|
private int x, y;
|
|
|
|
|
private Dir dir;
|
|
|
|
|
|
|
|
|
|
private boolean live = true;
|
|
|
|
|
private boolean living = true;
|
|
|
|
|
|
|
|
|
|
private TankFrame tf;
|
|
|
|
|
|
|
|
|
@ -21,7 +21,7 @@ public class Bullet {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void paint(Graphics g) {
|
|
|
|
|
if (!live) {
|
|
|
|
|
if (!living) {
|
|
|
|
|
tf.bullets.remove(this);
|
|
|
|
|
}
|
|
|
|
|
switch (dir) {
|
|
|
|
@ -45,6 +45,7 @@ public class Bullet {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void move() {
|
|
|
|
|
if (!living) return;
|
|
|
|
|
switch (dir) {
|
|
|
|
|
case LEFT:
|
|
|
|
|
x -= SPEED;
|
|
|
|
@ -63,6 +64,19 @@ public class Bullet {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (x < 0 || y < 0 || x > TankFrame.GAME_WIDTH || y > TankFrame.GAME_HEIGHT) live = false;
|
|
|
|
|
if (x < 0 || y < 0 || x > TankFrame.GAME_WIDTH || y > TankFrame.GAME_HEIGHT) living = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void collideWith(Tank tank){
|
|
|
|
|
Rectangle rect1 = new Rectangle(this.x, this.y, WIDTH, HEIGHT);
|
|
|
|
|
Rectangle rect2 = new Rectangle(tank.getX(), tank.getY(), Tank.WIDTH, Tank.HEIGHT);
|
|
|
|
|
if (rect1.intersects(rect2)) {
|
|
|
|
|
tank.die();
|
|
|
|
|
this.die();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void die() {
|
|
|
|
|
this.living = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|