坦克和子弹分组

master
kn5886348135 3 years ago
parent 2f49ccba39
commit 6a9ab6b79c

@ -13,11 +13,14 @@ public class Bullet {
private TankFrame tf; private TankFrame tf;
public Bullet(int x, int y, Dir dir, TankFrame tf) { private Group group = Group.BAD;
public Bullet(int x, int y, Dir dir, Group group, TankFrame tf) {
this.x = x; this.x = x;
this.y = y; this.y = y;
this.dir = dir; this.dir = dir;
this.tf = tf; this.tf = tf;
this.group = group;
} }
public void paint(Graphics g) { public void paint(Graphics g) {
@ -68,6 +71,7 @@ public class Bullet {
} }
public void collideWith(Tank tank){ public void collideWith(Tank tank){
if (this.group == tank.getGroup()) return;
Rectangle rect1 = new Rectangle(this.x, this.y, WIDTH, HEIGHT); Rectangle rect1 = new Rectangle(this.x, this.y, WIDTH, HEIGHT);
Rectangle rect2 = new Rectangle(tank.getX(), tank.getY(), Tank.WIDTH, Tank.HEIGHT); Rectangle rect2 = new Rectangle(tank.getX(), tank.getY(), Tank.WIDTH, Tank.HEIGHT);
if (rect1.intersects(rect2)) { if (rect1.intersects(rect2)) {

@ -0,0 +1,5 @@
package com.example.tankbattle;
public enum Group {
GOOD, BAD
}

@ -6,7 +6,7 @@ public class Main {
// 初始化敌方坦克 // 初始化敌方坦克
for (int i = 0; i < 5; i++) { for (int i = 0; i < 5; i++) {
tf.tanks.add(new Tank(50 + i * 80, 200, Dir.DOWN, tf)); tf.tanks.add(new Tank(50 + i * 80, 200, Dir.DOWN, Group.BAD, tf));
} }
while (true) { while (true) {

@ -1,10 +1,28 @@
package com.example.tankbattle; package com.example.tankbattle;
import java.awt.Graphics; import java.awt.Graphics;
import java.util.Random;
public class Tank { public class Tank {
private static final int SPEED = 5;
public static int WIDTH = ResourceMgr.tankD.getWidth();
public static int HEIGHT = ResourceMgr.tankD.getHeight();
private Random random = new Random();
private int x,y; private int x,y;
private Dir dir = Dir.DOWN;
private boolean moving = false;
private TankFrame tf = null;
private boolean living = true;
private Group group = Group.BAD;
public int getX() { public int getX() {
return x; return x;
} }
@ -21,17 +39,13 @@ public class Tank {
this.y = y; this.y = y;
} }
private Dir dir = Dir.DOWN; public Group getGroup() {
private static final int SPEED = 5; return group;
}
public static int WIDTH = ResourceMgr.tankD.getWidth();
public static int HEIGHT = ResourceMgr.tankD.getHeight();
private boolean moving = false;
private TankFrame tf = null;
private boolean living = true; public void setGroup(Group group) {
this.group = group;
}
public boolean isMoving() { public boolean isMoving() {
return moving; return moving;
@ -50,13 +64,13 @@ public class Tank {
} }
public Tank(int x, int y, Dir dir, Group group, TankFrame tf) {
public Tank(int x, int y, Dir dir, TankFrame tf) {
super(); super();
this.x = x; this.x = x;
this.y = y; this.y = y;
this.dir = dir; this.dir = dir;
this.tf = tf; this.tf = tf;
this.group = group;
} }
public void paint(Graphics g) { public void paint(Graphics g) {
@ -98,12 +112,14 @@ public class Tank {
default: default:
break; break;
} }
if (random.nextInt(10) > 5) this.fire();
} }
public void fire(){ public void fire(){
int bX = this.x + Tank.WIDTH / 2 - Bullet.WIDTH / 2; int bX = this.x + Tank.WIDTH / 2 - Bullet.WIDTH / 2;
int bY = this.y + Tank.HEIGHT / 2 - Bullet.HEIGHT / 2; int bY = this.y + Tank.HEIGHT / 2 - Bullet.HEIGHT / 2;
tf.bullets.add(new Bullet(bX, bY, this.dir, this.tf)); tf.bullets.add(new Bullet(bX, bY, this.dir, this.group, this.tf));
} }
public void die() { public void die() {

@ -13,7 +13,7 @@ import java.util.List;
public class TankFrame extends Frame { public class TankFrame extends Frame {
Tank myTank = new Tank(200, 400, Dir.DOWN, this); Tank myTank = new Tank(200, 400, Dir.DOWN, Group.GOOD, this);
List<Tank> tanks = new ArrayList<>(); List<Tank> tanks = new ArrayList<>();
List<Bullet> bullets = new ArrayList<>(); List<Bullet> bullets = new ArrayList<>();

Loading…
Cancel
Save