修改碰撞检测,使用工厂方法创建坦克和子弹

dp
kn5886348135 3 years ago
parent 84e1120105
commit f33e0b52d6

@ -11,7 +11,7 @@ public class Bullet extends BaseBullet {
public static int WIDTH = ResourceMgr.bulletD.getWidth(); public static int WIDTH = ResourceMgr.bulletD.getWidth();
public static int HEIGHT = ResourceMgr.bulletD.getHeight(); public static int HEIGHT = ResourceMgr.bulletD.getHeight();
private Rectangle rect = new Rectangle(); Rectangle rect = new Rectangle();
private int x, y; private int x, y;
private Dir dir; private Dir dir;

@ -7,7 +7,7 @@ public class DefaultFireStrategy implements FireStrategy {
int bX = t.x + Tank.WIDTH/2 - Bullet.WIDTH/2; int bX = t.x + Tank.WIDTH/2 - Bullet.WIDTH/2;
int bY = t.y + Tank.HEIGHT/2 - Bullet.HEIGHT/2; int bY = t.y + Tank.HEIGHT/2 - Bullet.HEIGHT/2;
t.tf.bullets.add(new Bullet(bX, bY, t.dir, t.group, t.tf)); new Bullet(bX, bY, t.dir, t.group, t.tf);
if(t.group == Group.GOOD) new Thread(()->new Audio("audio/tank_fire.wav").play()).start(); if(t.group == Group.GOOD) new Thread(()->new Audio("audio/tank_fire.wav").play()).start();
} }

@ -2,7 +2,7 @@ package com.example.tankbattle;
import com.example.tankbattle.abstractfactory.BaseExplode; import com.example.tankbattle.abstractfactory.BaseExplode;
import java.awt.*; import java.awt.Graphics;
public class Explode extends BaseExplode { public class Explode extends BaseExplode {
public static int WIDTH = ResourceMgr.explodes[0].getWidth(); public static int WIDTH = ResourceMgr.explodes[0].getWidth();
@ -18,6 +18,8 @@ public class Explode extends BaseExplode {
this.x = x; this.x = x;
this.y = y; this.y = y;
this.tf = tf; this.tf = tf;
new Thread(()->new Audio("audio/explode.wav").play()).start();
} }
@Override @Override

@ -9,7 +9,7 @@ public class FourDirFireStrategy implements FireStrategy {
Dir[] dirs = Dir.values(); Dir[] dirs = Dir.values();
for(Dir dir : dirs) { for(Dir dir : dirs) {
new Bullet(bX, bY, dir, t.group, t.tf); t.tf.gf.createBullet(bX, bY, dir, t.group, t.tf);
} }
if(t.group == Group.GOOD) new Thread(()->new Audio("audio/tank_fire.wav").play()).start(); if(t.group == Group.GOOD) new Thread(()->new Audio("audio/tank_fire.wav").play()).start();

@ -8,9 +8,11 @@ public class Main {
// 初始化敌方坦克 // 初始化敌方坦克
for (int i = 0; i < initTankCount; i++) { for (int i = 0; i < initTankCount; i++) {
tf.tanks.add(new Tank(50 + i * 80, 200, Dir.DOWN, Group.BAD, tf)); tf.tanks.add(tf.gf.createTank(50 + i * 80, 200, Dir.DOWN, Group.BAD, tf));
} }
new Thread(() -> new Audio("audio/war1.wav").loop()).start();
while (true) { while (true) {
Thread.sleep(50); Thread.sleep(50);
tf.repaint(); tf.repaint();

@ -2,7 +2,7 @@ package com.example.tankbattle;
import com.example.tankbattle.abstractfactory.BaseTank; import com.example.tankbattle.abstractfactory.BaseTank;
import java.awt.*; import java.awt.Graphics;
import java.util.Random; import java.util.Random;
public class Tank extends BaseTank { public class Tank extends BaseTank {
@ -11,22 +11,18 @@ public class Tank extends BaseTank {
public static int WIDTH = ResourceMgr.goodTankU.getWidth(); public static int WIDTH = ResourceMgr.goodTankU.getWidth();
public static int HEIGHT = ResourceMgr.goodTankU.getHeight(); public static int HEIGHT = ResourceMgr.goodTankU.getHeight();
Rectangle rect = new Rectangle();
private Random random = new Random(); private Random random = new Random();
public int x,y; int x,y;
public Dir dir = Dir.DOWN; Dir dir = Dir.DOWN;
private boolean moving = true; private boolean moving = true;
public TankFrame tf = null; TankFrame tf = null;
private boolean living = true; private boolean living = true;
public Group group = Group.BAD;
FireStrategy fs; FireStrategy fs;
public Tank(int x, int y, Dir dir, Group group, TankFrame tf) { public Tank(int x, int y, Dir dir, Group group, TankFrame tf) {
@ -57,7 +53,16 @@ public class Tank extends BaseTank {
} }
public void fire(){ public void fire(){
fs.fire(this); // fs.fire(this);
int bX = this.x + Tank.WIDTH/2 - Bullet.WIDTH/2;
int bY = this.y + Tank.HEIGHT/2 - Bullet.HEIGHT/2;
Dir[] dirs = Dir.values();
for(Dir dir : dirs) {
tf.gf.createBullet(bX, bY, dir, group, tf);
}
if(group == Group.GOOD) new Thread(()->new Audio("audio/tank_fire.wav").play()).start();
} }
public Dir getDir() { public Dir getDir() {
@ -103,31 +108,24 @@ public class Tank extends BaseTank {
break; break;
} }
rect.x = this.x; if (this.group == Group.BAD && random.nextInt(100) > 95)
rect.y = this.y; this.fire();
if (this.group == Group.BAD && random.nextInt(100) > 95) this.fire();
if (this.group == Group.BAD && random.nextInt(100) > 95) { if (this.group == Group.BAD && random.nextInt(100) > 95)
randomDir(); randomDir();
}
boundsCheck(); boundsCheck();
//update rect
rect.x = this.x;
rect.y = this.y;
} }
private void boundsCheck() { private void boundsCheck() {
if (this.x < 0) { if (this.x < 2) x = 2;
this.x = 2; if (this.y < 28) y = 28;
}
if (this.y < 28) {
this.y = 28;
}
if (this.x > TankFrame.GAME_WIDTH - Tank.WIDTH) { if (this.x > TankFrame.GAME_WIDTH - Tank.WIDTH-2) this.x = TankFrame.GAME_WIDTH - Tank.WIDTH - 2;
this.x = TankFrame.GAME_WIDTH - Tank.WIDTH; if (this.y > TankFrame.GAME_HEIGHT - Tank.HEIGHT-2) this.y = TankFrame.GAME_HEIGHT - Tank.HEIGHT-2;
}
if (this.y > TankFrame.GAME_HEIGHT - Tank.HEIGHT) {
this.y = TankFrame.GAME_HEIGHT - Tank.HEIGHT;
}
} }
private void randomDir() { private void randomDir() {

@ -8,7 +8,6 @@ import com.example.tankbattle.FireStrategy;
import com.example.tankbattle.Group; import com.example.tankbattle.Group;
import com.example.tankbattle.PropertyMgr; import com.example.tankbattle.PropertyMgr;
import com.example.tankbattle.ResourceMgr; import com.example.tankbattle.ResourceMgr;
import com.example.tankbattle.Tank;
import com.example.tankbattle.TankFrame; import com.example.tankbattle.TankFrame;
import java.awt.Color; import java.awt.Color;
@ -138,6 +137,9 @@ public class RectTank extends BaseTank{
randomDir(); randomDir();
boundsCheck(); boundsCheck();
// update rect
rect.x = this.x;
rect.y = this.y;
} }
private void boundsCheck() { private void boundsCheck() {
@ -146,10 +148,10 @@ public class RectTank extends BaseTank{
if (this.y < 28) if (this.y < 28)
y = 28; y = 28;
if (this.x > TankFrame.GAME_WIDTH - Tank.WIDTH) if (this.x > TankFrame.GAME_WIDTH - RectTank.WIDTH-2)
x = TankFrame.GAME_WIDTH - Tank.WIDTH; x = TankFrame.GAME_WIDTH - RectTank.WIDTH;
if (this.y > TankFrame.GAME_HEIGHT - Tank.HEIGHT) if (this.y > TankFrame.GAME_HEIGHT - RectTank.HEIGHT-2)
y = TankFrame.GAME_HEIGHT - Tank.HEIGHT; y = TankFrame.GAME_HEIGHT - RectTank.HEIGHT;
} }
private void randomDir() { private void randomDir() {

Loading…
Cancel
Save