diff --git a/src/main/java/com/study/tank/Bullet.java b/src/main/java/com/study/tank/Bullet.java index b3d8d67..199080c 100644 --- a/src/main/java/com/study/tank/Bullet.java +++ b/src/main/java/com/study/tank/Bullet.java @@ -10,15 +10,16 @@ import java.awt.*; * @Auther: xiaoshengjie * @Date: 2022/10/22/上午11:16 */ -public class Bullet extends BaseBullet { +public class Bullet extends GameObject { public static final int bWidth = ImageManger.bulletL.getWidth(); public static final int bHeight = ImageManger.bulletL.getHeight(); - private int x, y; + public int x, y; private Dir dir = Dir.DOWN; private final int speed = 10; private boolean living = true; - TankFrame tf = null; - private Group group = Group.BAD; + //TankFrame tf = null; + public Group group = Group.BAD; + public GameModel gm; public Group getGroup() { return group; @@ -28,13 +29,13 @@ public class Bullet extends BaseBullet { this.group = group; } - public Bullet(int x, int y, Dir dir, Group group, TankFrame tankFrame) { + public Bullet(int x, int y, Dir dir, Group group, GameModel gm) { this.x = x; this.y = y; this.dir = dir; - this.tf = tankFrame; + this.gm = gm; this.group = group; - tankFrame.bullets.add(this); + gm.add(this); new Thread(() -> { new Audio("audio/tank_fire.wav"); }).start(); @@ -42,7 +43,7 @@ public class Bullet extends BaseBullet { public void paint(Graphics g) { - if (!living) tf.bullets.remove(this); + if (!living) gm.remove(this); switch (dir) { case DOWN: g.drawImage(ImageManger.bulletD, x, y, null); @@ -86,7 +87,7 @@ public class Bullet extends BaseBullet { /** * 碰撞 */ - public void collideWithTank(BaseTank tank) { + public void collideWithTank(Tank tank) { if (this.group == tank.getGroup()) return; Rectangle rect1 = new Rectangle(this.x, this.y, bWidth, bHeight); Rectangle rect2 = new Rectangle(tank.getX(), tank.getY(), Tank.tankWidth, Tank.tankHeight); @@ -94,7 +95,7 @@ public class Bullet extends BaseBullet { if (rect1.intersects(rect2)) { tank.die(); this.die(); - tf.explodes.add(tf.gf.createExplode(this.x + bWidth / 2 - Explode.bWidth / 2, this.y + bWidth - Explode.bHeight / 2, tf)); + gm.add(new Explode(this.x + bWidth / 2 - Explode.bWidth / 2, this.y + bWidth - Explode.bHeight / 2, gm)); } } diff --git a/src/main/java/com/study/tank/Explode.java b/src/main/java/com/study/tank/Explode.java index 8b0f9fd..963b5f1 100644 --- a/src/main/java/com/study/tank/Explode.java +++ b/src/main/java/com/study/tank/Explode.java @@ -9,39 +9,31 @@ import java.awt.*; * @Auther: xiaoshengjie * @Date: 2022/10/22/上午11:16 */ -public class Explode extends BaseExplode { - public static final int bWidth = ImageManger.explodes[0].getWidth(); - public static final int bHeight = ImageManger.explodes[0].getHeight(); - private int x, y; - TankFrame tf = null; - private int step = 0; - - - public Explode(int x, int y, TankFrame tankFrame) { - this.x = x; - this.y = y; - this.tf = tankFrame; - - new Thread(new Runnable() { - public void run() { - new Audio("audio/explode.wav").play(); - } - }).start(); - } - -// public void paint(Graphics g) { -// g.drawImage(ImageManger.explodes[step++], x, y, null); -// if (step >= ImageManger.explodes.length) { -// tf.explodes.remove(this); -// } -// } - - - @Override - public void paint(Graphics g) { - g.drawImage(ImageManger.explodes[step++], x, y, null); - if (step >= ImageManger.explodes.length) { - tf.explodes.remove(this); - } - } +public class Explode extends GameObject { + public static final int bWidth = ImageManger.explodes[0].getWidth(); + public static final int bHeight = ImageManger.explodes[0].getHeight(); + private int x, y; + public GameModel gm; + private int step = 0; + + public Explode(int x, int y, GameModel gameModel) { + this.x = x; + this.y = y; + this.gm = gameModel; + + new Thread(new Runnable() { + public void run() { + new Audio("audio/explode.wav").play(); + } + }).start(); + } + + + @Override + public void paint(Graphics g) { + g.drawImage(ImageManger.explodes[step++], x, y, null); + if (step >= ImageManger.explodes.length) { + gm.remove(this); + } + } } diff --git a/src/main/java/com/study/tank/GameModel.java b/src/main/java/com/study/tank/GameModel.java new file mode 100644 index 0000000..7fe2286 --- /dev/null +++ b/src/main/java/com/study/tank/GameModel.java @@ -0,0 +1,57 @@ +package com.study.tank; + +import com.study.tank.cor.BulletAndTankCollider; +import com.study.tank.cor.Collider; +import com.study.tank.cor.ColliderChain; +import com.study.tank.cor.TankTankCollider; +import com.study.tank.factory.BaseTank; + +import java.awt.*; +import java.util.ArrayList; +import java.util.List; + +/** + * @author xsj + * @date 2022/10/27 14:30 + */ +public class GameModel { + public Tank myTank = new Tank(150, 150, Dir.DOWN, Group.GOOD, this); + List gameModelList = new ArrayList<>(); + //Collider collider1 = new BulletAndTankCollider();//子弹和坦克的碰撞 + //Collider collider2 = new TankTankCollider();//坦克和坦克的碰撞 + ColliderChain chain = new ColliderChain(); + + public GameModel() { + //初始化敌人坦克 + int initCountTank = Integer.parseInt((String) PropertyMgr.get("initTankCount")); + for (int i = 0; i < initCountTank; i++) { + gameModelList.add(new Tank(80 + i * 100, 50, Dir.DOWN, Group.BAD, this)); + } + } + + public void add(GameObject object) { + this.gameModelList.add(object); + } + + public void remove(GameObject object) { + this.gameModelList.remove(object); + } + + public void paint(Graphics g) { + myTank.paint(g); + for (int i = 0; i < gameModelList.size(); i++) { + gameModelList.get(i).paint(g); + } + + for (int i = 0; i < gameModelList.size(); i++) { + for (int j = 0; j < gameModelList.size(); j++) { + GameObject o1 = gameModelList.get(i); + GameObject o2 = gameModelList.get(j); + //collider1.collider(o1, o2, this);// + //collider2.collider(o1, o2, this); + chain.collider(o1, o2, this); + } + } + } + +} diff --git a/src/main/java/com/study/tank/GameObject.java b/src/main/java/com/study/tank/GameObject.java new file mode 100644 index 0000000..589a404 --- /dev/null +++ b/src/main/java/com/study/tank/GameObject.java @@ -0,0 +1,13 @@ +package com.study.tank; + +import java.awt.*; + +/** + * @author xsj + * @date 2022/10/27 14:20 + */ +public abstract class GameObject { + public int x, y; + + public abstract void paint(Graphics g); +} diff --git a/src/main/java/com/study/tank/Tank.java b/src/main/java/com/study/tank/Tank.java index d6e1bb5..2a49f7e 100644 --- a/src/main/java/com/study/tank/Tank.java +++ b/src/main/java/com/study/tank/Tank.java @@ -2,8 +2,8 @@ package com.study.tank; import com.study.tank.factory.BaseTank; import com.study.tank.strategy.FireStrategy; -import com.study.tank.strategy.Imp.DefaultFireStrategy; -import com.study.tank.strategy.Imp.GoodTankFireStrategy; +//import com.study.tank.strategy.Imp.DefaultFireStrategy; +//import com.study.tank.strategy.Imp.GoodTankFireStrategy; import java.awt.*; import java.util.Random; @@ -13,7 +13,7 @@ import java.util.Random; * @Auther: xiaoshengjie * @Date: 2022/10/22/上午10:08 */ -public class Tank extends BaseTank { +public class Tank extends GameObject { public static final int tankWidth = ImageManger.goodTankU.getWidth(); public static final int tankHeight = ImageManger.goodTankU.getHeight(); private boolean moving = true; @@ -23,54 +23,26 @@ public class Tank extends BaseTank { private final int speed = 4; public TankFrame tankFrame = null; private Random random = new Random(); - //Group group = Group.BAD; - -// FireStrategy fs; - -// public Group getGroup() { -// return group; -// } -// -// public void setGroup(Group group) { -// this.group = group; -// } - - public Tank(int x, int y, Dir dir, Group group, TankFrame tankFrame) { -// this.x = x; -// this.y = y; - super(x, y, group); + public Group group = Group.BAD; + public GameModel gm; + public Rectangle rectangle = new Rectangle(); + int oldX, oldY; + + public Tank(int x, int y, Dir dir, Group group, GameModel gm) { + this.x = x; + this.y = y; this.dir = dir; - //this.group = group; - this.tankFrame = tankFrame; -// try { -// if (this.group == Group.GOOD) { -// String goodName = (String) PropertyMgr.get("goodFs"); -// //fs = (GoodTankFireStrategy) Class.forName(goodName).newInstance(); -// fs = (FireStrategy) Class.forName(goodName).getDeclaredConstructor().newInstance(); -// } else { -// fs = new DefaultFireStrategy(); -// } -// } catch (Exception e) { -// -// } - } - -// public Tank(int x, int y, Dir dir, Group group, boolean moving, TankFrame tankFrame) { -// this.x = x; -// this.y = y; -// this.dir = dir; -// this.group = group; -// this.tankFrame = tankFrame; -// this.moving = false; -// } - -// public int getX() { -// return x; -// } -// -// public int getY() { -// return y; -// } + this.group = group; + this.gm = gm; + } + + public int getX() { + return x; + } + + public int getY() { + return y; + } public Dir getDir() { return dir; @@ -80,13 +52,13 @@ public class Tank extends BaseTank { return speed; } -// public void setX(int x) { -// this.x = x; -// } -// -// public void setY(int y) { -// this.y = y; -// } + public void setX(int x) { + this.x = x; + } + + public void setY(int y) { + this.y = y; + } public boolean isMoving() { return moving; @@ -100,36 +72,19 @@ public class Tank extends BaseTank { this.dir = dir; } -// public void paint(Graphics g) { -// System.out.println("tank paint"); -// if (!living) { -// tankFrame.tanks.remove(this); -// moving = false; -// return; -// } -// switch (this.dir) { -// case DOWN: -// g.drawImage(this.group == Group.GOOD ? ImageManger.goodTankD : ImageManger.badTankD, this.x, this.y, null); -// break; -// case UP: -// g.drawImage(this.group == Group.GOOD ? ImageManger.goodTankU : ImageManger.badTankU, this.x, this.y, null); -// break; -// case RIGHT: -// g.drawImage(this.group == Group.GOOD ? ImageManger.goodTankR : ImageManger.badTankR, this.x, this.y, null); -// break; -// case LEFT: -// g.drawImage(this.group == Group.GOOD ? ImageManger.goodTankL : ImageManger.badTankL, this.x, this.y, null); -// break; -// } -// move(); -// } + public Group getGroup() { + return group; + } + public void setGroup(Group group) { + this.group = group; + } @Override public void paint(Graphics g) { System.out.println("tank paint"); if (!living) { - tankFrame.tanks.remove(this); + gm.remove(this); moving = false; return; } @@ -152,8 +107,13 @@ public class Tank extends BaseTank { public void move() { if (!living) return; - if (!this.moving) return; - switch (dir) { + if (!this.moving) { + goBack(); + return; + } + oldX = x; + oldY = y; + switch (this.dir) { case LEFT: x -= speed; break; @@ -203,26 +163,34 @@ public class Tank extends BaseTank { int by = this.getY() + Tank.tankHeight / 2 - ImageManger.bulletD.getHeight() / 2; switch (this.dir) { case DOWN: - new Bullet(bx, by + 6, this.dir, this.getGroup(), this.tankFrame); - new Bullet(bx, by - 6, this.dir, this.getGroup(), this.tankFrame); + new Bullet(bx, by + 6, this.dir, this.getGroup(), this.gm); + new Bullet(bx, by - 6, this.dir, this.getGroup(), this.gm); break; case UP: - new Bullet(bx + 1, by + 6, this.dir, this.getGroup(), this.tankFrame); - new Bullet(bx + 1, by - 6, this.dir, this.getGroup(), this.tankFrame); + new Bullet(bx + 1, by + 6, this.dir, this.getGroup(), this.gm); + new Bullet(bx + 1, by - 6, this.dir, this.getGroup(), this.gm); break; case LEFT: - new Bullet(bx + 6, by, this.dir, this.getGroup(), this.tankFrame); - new Bullet(bx - 6, by, this.dir, this.getGroup(), this.tankFrame); + new Bullet(bx + 6, by, this.dir, this.getGroup(), this.gm); + new Bullet(bx - 6, by, this.dir, this.getGroup(), this.gm); break; case RIGHT: - new Bullet(bx + 6, by + 1, this.dir, this.getGroup(), this.tankFrame); - new Bullet(bx - 6, by + 1, this.dir, this.getGroup(), this.tankFrame); + new Bullet(bx + 6, by + 1, this.dir, this.getGroup(), this.gm); + new Bullet(bx - 6, by + 1, this.dir, this.getGroup(), this.gm); break; } } - @Override + public void stop() { + this.moving = false; + } + + public void goBack() { + x = oldX; + y = oldY; + } + public void die() { this.living = false; } diff --git a/src/main/java/com/study/tank/TankFrame.java b/src/main/java/com/study/tank/TankFrame.java index 79e2317..5818ea5 100644 --- a/src/main/java/com/study/tank/TankFrame.java +++ b/src/main/java/com/study/tank/TankFrame.java @@ -17,11 +17,8 @@ import java.util.ArrayList; public class TankFrame extends Frame { public static final int GAME_WIDTH = Integer.parseInt((String) PropertyMgr.get("gameWidth")); public static final int GAME_HEIGHT = Integer.parseInt((String) PropertyMgr.get("gameHeight")); - public ArrayList tanks = new ArrayList(); - public Tank myTank = new Tank(150, 150, Dir.DOWN, Group.GOOD, this); - public ArrayList bullets = new ArrayList(); - public ArrayList explodes = new ArrayList(); - public GameFactory gf = new DefaultFactory(); + GameModel gameModel = new GameModel(); +// public GameFactory gf = new DefaultFactory(); public TankFrame() { //设置窗口大小 @@ -58,26 +55,7 @@ public class TankFrame extends Frame { @Override public void paint(Graphics g) { - Color c = g.getColor(); - g.setColor(Color.BLUE); - g.drawString("子弹的数量:" + bullets.size(), 5, 40); - g.drawString("敌人的数量:" + tanks.size(), 5, 60); - g.setColor(c); - myTank.paint(g); - for (int i = 0; i < tanks.size(); i++) { - tanks.get(i).paint(g); - } - for (int i = 0; i < bullets.size(); i++) { - bullets.get(i).paint(g); - } - for (int i = 0; i < bullets.size(); i++) { - for (int j = 0; j < tanks.size(); j++) { - bullets.get(i).collideWithTank(tanks.get(j)); - } - } - for (int i = 0; i < explodes.size(); i++) { - explodes.get(i).paint(g); - } + gameModel.paint(g); } /** @@ -128,7 +106,7 @@ public class TankFrame extends Frame { bD = false; break; case KeyEvent.VK_SPACE: - myTank.fire(); + gameModel.myTank.fire(); break; } setDirMethod(); @@ -136,14 +114,14 @@ public class TankFrame extends Frame { public void setDirMethod() { if (!bD && !bU && !bR && !bL) { - myTank.setMoving(false); + gameModel.myTank.setMoving(false); } else { - myTank.setMoving(true); + gameModel.myTank.setMoving(true); } - if (bL) myTank.setDir(Dir.LEFT); - if (bR) myTank.setDir(Dir.RIGHT); - if (bU) myTank.setDir(Dir.UP); - if (bD) myTank.setDir(Dir.DOWN); + if (bL) gameModel.myTank.setDir(Dir.LEFT); + if (bR) gameModel.myTank.setDir(Dir.RIGHT); + if (bU) gameModel.myTank.setDir(Dir.UP); + if (bD) gameModel.myTank.setDir(Dir.DOWN); } } diff --git a/src/main/java/com/study/tank/TestMain.java b/src/main/java/com/study/tank/TestMain.java index 8ecdaa3..e5d7492 100644 --- a/src/main/java/com/study/tank/TestMain.java +++ b/src/main/java/com/study/tank/TestMain.java @@ -10,11 +10,6 @@ public class TestMain { public static void main(String[] args) throws InterruptedException { TankFrame tf = new TankFrame(); - //初始化敌人坦克 - int initCountTank = Integer.parseInt((String) PropertyMgr.get("initTankCount")); - for (int i = 0; i < initCountTank; i++) { - tf.tanks.add(tf.gf.createTank(80 + i * 100, 50, Dir.DOWN, Group.BAD, tf)); - } new Thread(() -> { new Audio("audio/war1.wav").loop(); diff --git a/src/main/java/com/study/tank/cor/BulletAndTankCollider.java b/src/main/java/com/study/tank/cor/BulletAndTankCollider.java new file mode 100644 index 0000000..dc1b0fb --- /dev/null +++ b/src/main/java/com/study/tank/cor/BulletAndTankCollider.java @@ -0,0 +1,40 @@ +package com.study.tank.cor; + +import com.study.tank.*; + +import java.awt.*; + +/** + * @author xsj + * @date 2022/10/27 17:13 + */ +public class BulletAndTankCollider implements Collider { + + @Override + public boolean collider(GameObject o1, GameObject o2, GameModel gameModel) { + if (o1 instanceof Bullet && o2 instanceof Tank) { + Bullet bullet = (Bullet) o1; + Tank tank = (Tank) o2; + if (collideWithTank(bullet, tank, gameModel)) return true; + } else if (o1 instanceof Tank && o2 instanceof Bullet) { + return collider(o2, o1, gameModel); + } + return false; + } + + + public boolean collideWithTank(Bullet bullet, Tank tank, GameModel gameModel) { + if (bullet.group == tank.getGroup()) return false; + Rectangle rect1 = new Rectangle(bullet.x, bullet.y, bullet.bWidth, bullet.bHeight); + Rectangle rect2 = new Rectangle(tank.getX(), tank.getY(), Tank.tankWidth, Tank.tankHeight); + //碰撞矩形 + if (rect1.intersects(rect2)) { + tank.die(); + bullet.die(); + gameModel.add(new Explode(bullet.x + bullet.bWidth / 2 - Explode.bWidth / 2, bullet.y + bullet.bWidth - Explode.bHeight / 2, gameModel)); + return true; + } + return false; + } + +} diff --git a/src/main/java/com/study/tank/cor/Collider.java b/src/main/java/com/study/tank/cor/Collider.java new file mode 100644 index 0000000..e752aa6 --- /dev/null +++ b/src/main/java/com/study/tank/cor/Collider.java @@ -0,0 +1,12 @@ +package com.study.tank.cor; + +import com.study.tank.GameModel; +import com.study.tank.GameObject; + +/** + * @author xsj + * @date 2022/10/27 15:39 + */ +public interface Collider { + boolean collider(GameObject o1, GameObject o2, GameModel gameModel); +} diff --git a/src/main/java/com/study/tank/cor/ColliderChain.java b/src/main/java/com/study/tank/cor/ColliderChain.java new file mode 100644 index 0000000..c9422a4 --- /dev/null +++ b/src/main/java/com/study/tank/cor/ColliderChain.java @@ -0,0 +1,29 @@ +package com.study.tank.cor; + +import com.study.tank.GameModel; +import com.study.tank.GameObject; + +import java.util.ArrayList; +import java.util.List; + +/** + * @author xsj + * @date 2022/10/28 9:32 + */ +public class ColliderChain implements Collider { + List colliders = new ArrayList<>(); + + public ColliderChain() { + colliders.add(new BulletAndTankCollider()); + colliders.add(new TankTankCollider()); + } + + + @Override + public boolean collider(GameObject o1, GameObject o2, GameModel gameModel) { + for (Collider c : colliders) { + if (c.collider(o1, o2, gameModel)) return true; + } + return false; + } +} diff --git a/src/main/java/com/study/tank/cor/TankTankCollider.java b/src/main/java/com/study/tank/cor/TankTankCollider.java new file mode 100644 index 0000000..c397f3a --- /dev/null +++ b/src/main/java/com/study/tank/cor/TankTankCollider.java @@ -0,0 +1,37 @@ +package com.study.tank.cor; + +import com.study.tank.*; + +import java.awt.*; + +/** + * @author xsj + * @date 2022/10/27 17:35 + */ +public class TankTankCollider implements Collider { + + @Override + public boolean collider(GameObject o1, GameObject o2, GameModel gameModel) { + if (o1 instanceof Tank && o2 instanceof Tank) { + Tank tank1 = (Tank) o1; + Tank tank2 = (Tank) o2; + if (tank1.group == tank2.group) { + if (tank1.rectangle.intersects(tank2.rectangle)) { + tank1.stop(); + tank2.stop(); + return true; + } + } else { + //碰撞矩形 + if (tank1.rectangle.intersects(tank2.rectangle)) { + tank1.die(); + tank2.die(); + gameModel.add(new Explode(tank1.getX() + tank1.tankWidth/2, tank1.y + tank1.tankHeight/2, gameModel)); + gameModel.add(new Explode(tank2.getX() + tank2.tankWidth/2, tank2.y + tank2.tankHeight/2, gameModel)); + return true; + } + } + } + return false; + } +} diff --git a/src/main/java/com/study/tank/factory/DefaultFactory.java b/src/main/java/com/study/tank/factory/DefaultFactory.java index 322a22f..0cf896c 100644 --- a/src/main/java/com/study/tank/factory/DefaultFactory.java +++ b/src/main/java/com/study/tank/factory/DefaultFactory.java @@ -1,25 +1,25 @@ -package com.study.tank.factory; - -import com.study.tank.*; - -/** - * @author xsj - * @date 2022/10/26 13:13 - */ -public class DefaultFactory extends GameFactory { - - @Override - public BaseTank createTank(int x, int y, Dir dir, Group group, TankFrame tankFrame) { - return new Tank(x, y, dir, group, tankFrame); - } - - @Override - public BaseBullet createBullet(int x, int y, Dir dir, Group group, TankFrame tankFrame) { - return new Bullet(x,y,dir,group,tankFrame); - } - - @Override - public BaseExplode createExplode(int x, int y, TankFrame tankFrame) { - return new Explode(x, y, tankFrame); - } -} +//package com.study.tank.factory; +// +//import com.study.tank.*; +// +///** +// * @author xsj +// * @date 2022/10/26 13:13 +// */ +//public class DefaultFactory extends GameFactory { +// +// @Override +// public BaseTank createTank(int x, int y, Dir dir, Group group, TankFrame tankFrame) { +// return new Tank(x, y, dir, group, tankFrame); +// } +// +// @Override +// public BaseBullet createBullet(int x, int y, Dir dir, Group group, TankFrame tankFrame) { +// return new Bullet(x,y,dir,group,tankFrame); +// } +// +// @Override +// public BaseExplode createExplode(int x, int y, TankFrame tankFrame) { +// return new Explode(x, y, tankFrame); +// } +//} diff --git a/src/main/java/com/study/tank/factory/RectExplode.java b/src/main/java/com/study/tank/factory/RectExplode.java index d054829..43fbc32 100644 --- a/src/main/java/com/study/tank/factory/RectExplode.java +++ b/src/main/java/com/study/tank/factory/RectExplode.java @@ -1,46 +1,46 @@ -package com.study.tank.factory; - -import com.study.tank.Audio; -import com.study.tank.ImageManger; -import com.study.tank.TankFrame; - -import java.awt.*; - -/** - * @author xsj - * @date 2022/10/26 13:37 - */ -public class RectExplode extends BaseExplode { - public static final int bWidth = ImageManger.explodes[0].getWidth(); - public static final int bHeight = ImageManger.explodes[0].getHeight(); - private int x, y; - TankFrame tf = null; - private int step = 0; - - - public RectExplode(int x, int y, TankFrame tankFrame) { - this.x = x; - this.y = y; - this.tf = tankFrame; - - new Thread(new Runnable() { - public void run() { - new Audio("audio/explode.wav").play(); - } - }).start(); - } - - @Override - public void paint(Graphics g) { -// g.drawImage(ImageManger.explodes[step++], x, y, null); - Color c = g.getColor(); - g.setColor(Color.red); - g.fillRect(x, y, 10 * step, 10 * step); -// if (step >= ImageManger.explodes.length) { -// tf.explodes.remove(this); -// } - step++; - if (step > 5) tf.explodes.remove(this); - g.setColor(c); - } -} +//package com.study.tank.factory; +// +//import com.study.tank.Audio; +//import com.study.tank.ImageManger; +//import com.study.tank.TankFrame; +// +//import java.awt.*; +// +///** +// * @author xsj +// * @date 2022/10/26 13:37 +// */ +//public class RectExplode extends BaseExplode { +// public static final int bWidth = ImageManger.explodes[0].getWidth(); +// public static final int bHeight = ImageManger.explodes[0].getHeight(); +// private int x, y; +// TankFrame tf = null; +// private int step = 0; +// +// +// public RectExplode(int x, int y, TankFrame tankFrame) { +// this.x = x; +// this.y = y; +// this.tf = tankFrame; +// +// new Thread(new Runnable() { +// public void run() { +// new Audio("audio/explode.wav").play(); +// } +// }).start(); +// } +// +// @Override +// public void paint(Graphics g) { +//// g.drawImage(ImageManger.explodes[step++], x, y, null); +// Color c = g.getColor(); +// g.setColor(Color.red); +// g.fillRect(x, y, 10 * step, 10 * step); +//// if (step >= ImageManger.explodes.length) { +//// tf.explodes.remove(this); +//// } +// step++; +// if (step > 5) tf.explodes.remove(this); +// g.setColor(c); +// } +//} diff --git a/src/main/java/com/study/tank/factory/RectFactory.java b/src/main/java/com/study/tank/factory/RectFactory.java index e951037..671c55c 100644 --- a/src/main/java/com/study/tank/factory/RectFactory.java +++ b/src/main/java/com/study/tank/factory/RectFactory.java @@ -1,25 +1,25 @@ -package com.study.tank.factory; - -import com.study.tank.*; - -/** - * @author xsj - * @date 2022/10/26 13:36 - */ -public class RectFactory extends GameFactory { - - @Override - public BaseTank createTank(int x, int y, Dir dir, Group group, TankFrame tankFrame) { - return new WarTank(x, y, dir, group, tankFrame); - } - - @Override - public BaseBullet createBullet(int x, int y, Dir dir, Group group, TankFrame tankFrame) { - return new RoundBullet(x, y, dir, group, tankFrame); - } - - @Override - public BaseExplode createExplode(int x, int y, TankFrame tankFrame) { - return new RectExplode(x, y, tankFrame); - } -} +//package com.study.tank.factory; +// +//import com.study.tank.*; +// +///** +// * @author xsj +// * @date 2022/10/26 13:36 +// */ +//public class RectFactory extends GameFactory { +// +// @Override +// public BaseTank createTank(int x, int y, Dir dir, Group group, TankFrame tankFrame) { +// return new WarTank(x, y, dir, group, tankFrame); +// } +// +// @Override +// public BaseBullet createBullet(int x, int y, Dir dir, Group group, TankFrame tankFrame) { +// return new RoundBullet(x, y, dir, group, tankFrame); +// } +// +// @Override +// public BaseExplode createExplode(int x, int y, TankFrame tankFrame) { +// return new RectExplode(x, y, tankFrame); +// } +//} diff --git a/src/main/java/com/study/tank/factory/RoundBullet.java b/src/main/java/com/study/tank/factory/RoundBullet.java index 7f137bd..8b61f04 100644 --- a/src/main/java/com/study/tank/factory/RoundBullet.java +++ b/src/main/java/com/study/tank/factory/RoundBullet.java @@ -1,100 +1,112 @@ -package com.study.tank.factory; - -import com.study.tank.*; - -import java.awt.*; - -/** - * @Description: - * @Auther: xiaoshengjie - * @Date: 2022/10/22/上午11:16 - */ -public class RoundBullet extends BaseBullet { - public static final int bWidth = ImageManger.bulletL.getWidth(); - public static final int bHeight = ImageManger.bulletL.getHeight(); - private int x, y; - private Dir dir = Dir.DOWN; - private final int speed = 10; - private boolean living = true; - TankFrame tf = null; - private Group group = Group.BAD; - - public Group getGroup() { - return group; - } - - public void setGroup(Group group) { - this.group = group; - } - - public RoundBullet(int x, int y, Dir dir, Group group, TankFrame tankFrame) { - this.x = x; - this.y = y; - this.dir = dir; - this.tf = tankFrame; - this.group = group; - tankFrame.bullets.add(this); - new Thread(() -> { - new Audio("audio/tank_fire.wav"); - }).start(); - } - - @Override - public void paint(Graphics g) { - if (!living) tf.bullets.remove(this); - Color c = g.getColor(); - g.setColor(Color.RED); - g.fillOval(x, y, 20, 20); - g.setColor(c); - move(); - } - +//package com.study.tank.factory; +// +//import com.study.tank.*; +// +//import java.awt.*; +// +///** +// * @Description: +// * @Auther: xiaoshengjie +// * @Date: 2022/10/22/上午11:16 +// */ +//public class RoundBullet extends BaseBullet { +// public static final int bWidth = ImageManger.bulletL.getWidth(); +// public static final int bHeight = ImageManger.bulletL.getHeight(); +// private int x, y; +// private Dir dir = Dir.DOWN; +// private final int speed = 10; +// private boolean living = true; +// TankFrame tf = null; +// private Group group = Group.BAD; +// +// public Group getGroup() { +// return group; +// } +// +// public void setGroup(Group group) { +// this.group = group; +// } +// +// public RoundBullet(int x, int y, Dir dir, Group group, TankFrame tankFrame) { +// this.x = x; +// this.y = y; +// this.dir = dir; +// this.tf = tankFrame; +// this.group = group; +// tankFrame.bullets.add(this); +// new Thread(() -> { +// new Audio("audio/tank_fire.wav"); +// }).start(); +// } +// +// @Override // public void paint(Graphics g) { // if (!living) tf.bullets.remove(this); +// Color c = g.getColor(); +// g.setColor(Color.RED); +// g.fillOval(x, y, 20, 20); +// g.setColor(c); +// move(); +// } +// +//// public void paint(Graphics g) { +//// if (!living) tf.bullets.remove(this); +//// switch (dir) { +//// case DOWN: +//// g.drawImage(ImageManger.bulletD, x, y, null); +//// break; +//// case UP: +//// g.drawImage(ImageManger.bulletU, x, y, null); +//// break; +//// case RIGHT: +//// g.drawImage(ImageManger.bulletR, x, y, null); +//// break; +//// case LEFT: +//// g.drawImage(ImageManger.bulletL, x, y, null); +//// break; +//// } +//// move(); +//// } +// +// /** +// * 移动 +// */ +// public void move() { // switch (dir) { -// case DOWN: -// g.drawImage(ImageManger.bulletD, x, y, null); -// break; -// case UP: -// g.drawImage(ImageManger.bulletU, x, y, null); +// case LEFT: +// x -= speed; // break; // case RIGHT: -// g.drawImage(ImageManger.bulletR, x, y, null); +// x += speed; // break; -// case LEFT: -// g.drawImage(ImageManger.bulletL, x, y, null); +// case UP: +// y -= speed; +// break; +// case DOWN: +// y += speed; // break; // } -// move(); +// if (x < 0 || y < 0 || x > TankFrame.GAME_WIDTH || y > TankFrame.GAME_HEIGHT) { +// living = false; +// } // } - - /** - * 移动 - */ - public void move() { - switch (dir) { - case LEFT: - x -= speed; - break; - case RIGHT: - x += speed; - break; - case UP: - y -= speed; - break; - case DOWN: - y += speed; - break; - } - if (x < 0 || y < 0 || x > TankFrame.GAME_WIDTH || y > TankFrame.GAME_HEIGHT) { - living = false; - } - } - - /** - * 碰撞 - */ -// public void collideWithTank(Tank tank) { +// +// /** +// * 碰撞 +// */ +//// public void collideWithTank(Tank tank) { +//// if (this.group == tank.getGroup()) return; +//// Rectangle rect1 = new Rectangle(this.x, this.y, bWidth, bHeight); +//// Rectangle rect2 = new Rectangle(tank.getX(), tank.getY(), Tank.tankWidth, Tank.tankHeight); +//// //碰撞矩形 +//// if (rect1.intersects(rect2)) { +//// tank.die(); +//// this.die(); +//// tf.explodes.add(tf.gf.createExplode(this.x + bWidth / 2 - Explode.bWidth / 2, this.y + bWidth - Explode.bHeight / 2, tf)); +//// } +//// } +// @Override +// public void collideWithTank(BaseTank tank) { // if (this.group == tank.getGroup()) return; // Rectangle rect1 = new Rectangle(this.x, this.y, bWidth, bHeight); // Rectangle rect2 = new Rectangle(tank.getX(), tank.getY(), Tank.tankWidth, Tank.tankHeight); @@ -105,21 +117,9 @@ public class RoundBullet extends BaseBullet { // tf.explodes.add(tf.gf.createExplode(this.x + bWidth / 2 - Explode.bWidth / 2, this.y + bWidth - Explode.bHeight / 2, tf)); // } // } - @Override - public void collideWithTank(BaseTank tank) { - if (this.group == tank.getGroup()) return; - Rectangle rect1 = new Rectangle(this.x, this.y, bWidth, bHeight); - Rectangle rect2 = new Rectangle(tank.getX(), tank.getY(), Tank.tankWidth, Tank.tankHeight); - //碰撞矩形 - if (rect1.intersects(rect2)) { - tank.die(); - this.die(); - tf.explodes.add(tf.gf.createExplode(this.x + bWidth / 2 - Explode.bWidth / 2, this.y + bWidth - Explode.bHeight / 2, tf)); - } - } - - public void die() { - this.living = false; - } - -} +// +// public void die() { +// this.living = false; +// } +// +//} diff --git a/src/main/java/com/study/tank/factory/WarTank.java b/src/main/java/com/study/tank/factory/WarTank.java index dc1f658..df1233b 100644 --- a/src/main/java/com/study/tank/factory/WarTank.java +++ b/src/main/java/com/study/tank/factory/WarTank.java @@ -1,170 +1,170 @@ -package com.study.tank.factory; - -import com.study.tank.*; -import com.study.tank.strategy.FireStrategy; -import com.study.tank.strategy.Imp.DefaultFireStrategy; - -import java.awt.*; -import java.util.Random; - -/** - * @Description: - * @Auther: xiaoshengjie - * @Date: 2022/10/22/上午10:08 - */ -public class WarTank extends BaseTank { - public static final int tankWidth = ImageManger.goodTankU.getWidth(); - public static final int tankHeight = ImageManger.goodTankU.getHeight(); - private boolean moving = true; - private int x, y; - private boolean living = true; - public Dir dir = Dir.DOWN; - private final int speed = 4; - public TankFrame tankFrame = null; - private Random random = new Random(); - Group group = Group.BAD; - - FireStrategy fs; - - public Group getGroup() { - return group; - } - - public void setGroup(Group group) { - this.group = group; - } - - public WarTank(int x, int y, Dir dir, Group group, TankFrame tankFrame) { -// this.x = x; -// this.y = y; - super(x, y, group); - this.dir = dir; +//package com.study.tank.factory; +// +//import com.study.tank.*; +//import com.study.tank.strategy.FireStrategy; +//import com.study.tank.strategy.Imp.DefaultFireStrategy; +// +//import java.awt.*; +//import java.util.Random; +// +///** +// * @Description: +// * @Auther: xiaoshengjie +// * @Date: 2022/10/22/上午10:08 +// */ +//public class WarTank extends BaseTank { +// public static final int tankWidth = ImageManger.goodTankU.getWidth(); +// public static final int tankHeight = ImageManger.goodTankU.getHeight(); +// private boolean moving = true; +// private int x, y; +// private boolean living = true; +// public Dir dir = Dir.DOWN; +// private final int speed = 4; +// public TankFrame tankFrame = null; +// private Random random = new Random(); +// Group group = Group.BAD; +// +// FireStrategy fs; +// +// public Group getGroup() { +// return group; +// } +// +// public void setGroup(Group group) { // this.group = group; - this.tankFrame = tankFrame; - try { - if (this.group == Group.GOOD) { - String goodName = (String) PropertyMgr.get("goodFs"); - //fs = (GoodTankFireStrategy) Class.forName(goodName).newInstance(); - fs = (FireStrategy) Class.forName(goodName).getDeclaredConstructor().newInstance(); - } else { - fs = new DefaultFireStrategy(); - } - } catch (Exception e) { - - } - } - -// public Tank(int x, int y, Dir dir, Group group, boolean moving, TankFrame tankFrame) { +// } +// +// public WarTank(int x, int y, Dir dir, Group group, TankFrame tankFrame) { +//// this.x = x; +//// this.y = y; +// super(x, y, group); +// this.dir = dir; +//// this.group = group; +// this.tankFrame = tankFrame; +// try { +// if (this.group == Group.GOOD) { +// String goodName = (String) PropertyMgr.get("goodFs"); +// //fs = (GoodTankFireStrategy) Class.forName(goodName).newInstance(); +// fs = (FireStrategy) Class.forName(goodName).getDeclaredConstructor().newInstance(); +// } else { +// fs = new DefaultFireStrategy(); +// } +// } catch (Exception e) { +// +// } +// } +// +//// public Tank(int x, int y, Dir dir, Group group, boolean moving, TankFrame tankFrame) { +//// this.x = x; +//// this.y = y; +//// this.dir = dir; +//// this.group = group; +//// this.tankFrame = tankFrame; +//// this.moving = false; +//// } +// +// public int getX() { +// return x; +// } +// +// public int getY() { +// return y; +// } +// +// public Dir getDir() { +// return dir; +// } +// +// public int getSpeed() { +// return speed; +// } +// +// public void setX(int x) { // this.x = x; +// } +// +// public void setY(int y) { // this.y = y; +// } +// +// public boolean isMoving() { +// return moving; +// } +// +// public void setMoving(boolean moving) { +// this.moving = moving; +// } +// +// public void setDir(Dir dir) { // this.dir = dir; -// this.group = group; -// this.tankFrame = tankFrame; -// this.moving = false; -// } - - public int getX() { - return x; - } - - public int getY() { - return y; - } - - public Dir getDir() { - return dir; - } - - public int getSpeed() { - return speed; - } - - public void setX(int x) { - this.x = x; - } - - public void setY(int y) { - this.y = y; - } - - public boolean isMoving() { - return moving; - } - - public void setMoving(boolean moving) { - this.moving = moving; - } - - public void setDir(Dir dir) { - this.dir = dir; - } - - @Override - public void paint(Graphics g) { - System.out.println("war paint"); - if (!living) { - tankFrame.tanks.remove(this); - moving = false; - return; - } - Color c = g.getColor(); - g.setColor(group == Group.GOOD ? Color.BLUE : Color.YELLOW); - g.fillRect(x, y, 40, 40); - g.setColor(c); - move(); - } - - public void move() { - if (!living) return; - if (!this.moving) return; - switch (dir) { - case LEFT: - x -= speed; - break; - case RIGHT: - x += speed; - break; - case UP: - y -= speed; - break; - case DOWN: - y += speed; - break; - } - if (group == Group.BAD && random.nextInt(100) > 95) - this.fire(); - - if (this.group == Group.BAD && random.nextInt(100) > 95) - this.randomDir(); - - boundsCheck(); - } - - private void boundsCheck() { - if (this.x < 0) x = TankFrame.GAME_WIDTH - WarTank.tankWidth; - if (this.y < 28) y = TankFrame.GAME_HEIGHT - WarTank.tankHeight; - if (this.x > TankFrame.GAME_WIDTH - tankWidth) x = 0; - if (this.y > TankFrame.GAME_HEIGHT - tankHeight) y = 28; - } - - //0.4几率的随机方向 - private void randomDir() { - if (random.nextInt(100) > 60) - this.dir = Dir.values()[random.nextInt(4)]; - } - - /** - * 开火 - */ - public void fire() { - //fs.fire(this); - int bx = this.getX() + this.tankWidth / 2 - ImageManger.bulletD.getWidth() / 2 + 2; - int by = this.getY() + this.tankHeight / 2 - ImageManger.bulletD.getHeight() / 2 + 2; - new Bullet(bx, by, this.dir, this.getGroup(), this.tankFrame); - } - - @Override - public void die() { - this.living = false; - } -} +// } +// +// @Override +// public void paint(Graphics g) { +// System.out.println("war paint"); +// if (!living) { +// tankFrame.tanks.remove(this); +// moving = false; +// return; +// } +// Color c = g.getColor(); +// g.setColor(group == Group.GOOD ? Color.BLUE : Color.YELLOW); +// g.fillRect(x, y, 40, 40); +// g.setColor(c); +// move(); +// } +// +// public void move() { +// if (!living) return; +// if (!this.moving) return; +// switch (dir) { +// case LEFT: +// x -= speed; +// break; +// case RIGHT: +// x += speed; +// break; +// case UP: +// y -= speed; +// break; +// case DOWN: +// y += speed; +// break; +// } +// if (group == Group.BAD && random.nextInt(100) > 95) +// this.fire(); +// +// if (this.group == Group.BAD && random.nextInt(100) > 95) +// this.randomDir(); +// +// boundsCheck(); +// } +// +// private void boundsCheck() { +// if (this.x < 0) x = TankFrame.GAME_WIDTH - WarTank.tankWidth; +// if (this.y < 28) y = TankFrame.GAME_HEIGHT - WarTank.tankHeight; +// if (this.x > TankFrame.GAME_WIDTH - tankWidth) x = 0; +// if (this.y > TankFrame.GAME_HEIGHT - tankHeight) y = 28; +// } +// +// //0.4几率的随机方向 +// private void randomDir() { +// if (random.nextInt(100) > 60) +// this.dir = Dir.values()[random.nextInt(4)]; +// } +// +// /** +// * 开火 +// */ +// public void fire() { +// //fs.fire(this); +// int bx = this.getX() + this.tankWidth / 2 - ImageManger.bulletD.getWidth() / 2 + 2; +// int by = this.getY() + this.tankHeight / 2 - ImageManger.bulletD.getHeight() / 2 + 2; +// new Bullet(bx, by, this.dir, this.getGroup(), this.tankFrame); +// } +// +// @Override +// public void die() { +// this.living = false; +// } +//} diff --git a/src/main/java/com/study/tank/strategy/Imp/DefaultFireStrategy.java b/src/main/java/com/study/tank/strategy/Imp/DefaultFireStrategy.java index b575f10..7e1f828 100644 --- a/src/main/java/com/study/tank/strategy/Imp/DefaultFireStrategy.java +++ b/src/main/java/com/study/tank/strategy/Imp/DefaultFireStrategy.java @@ -1,20 +1,20 @@ -package com.study.tank.strategy.Imp; - -import com.study.tank.Bullet; -import com.study.tank.ImageManger; -import com.study.tank.Tank; -import com.study.tank.strategy.FireStrategy; - -/** - * @author xsj - * @date 2022/10/25 10:30 - */ -public class DefaultFireStrategy implements FireStrategy { - - @Override - public void fire(Tank tank) { - int bx = tank.getX() + tank.tankWidth / 2 - ImageManger.bulletD.getWidth() / 2 + 2; - int by = tank.getY() + tank.tankHeight / 2 - ImageManger.bulletD.getHeight() / 2 + 2; - new Bullet(bx, by, tank.dir, tank.getGroup(), tank.tankFrame); - } -} +//package com.study.tank.strategy.Imp; +// +//import com.study.tank.Bullet; +//import com.study.tank.ImageManger; +//import com.study.tank.Tank; +//import com.study.tank.strategy.FireStrategy; +// +///** +// * @author xsj +// * @date 2022/10/25 10:30 +// */ +//public class DefaultFireStrategy implements FireStrategy { +// +// @Override +// public void fire(Tank tank) { +// int bx = tank.getX() + tank.tankWidth / 2 - ImageManger.bulletD.getWidth() / 2 + 2; +// int by = tank.getY() + tank.tankHeight / 2 - ImageManger.bulletD.getHeight() / 2 + 2; +// new Bullet(bx, by, tank.dir, tank.getGroup(), tank.tankFrame); +// } +//} diff --git a/src/main/java/com/study/tank/strategy/Imp/GoodTankFireStrategy.java b/src/main/java/com/study/tank/strategy/Imp/GoodTankFireStrategy.java index 2759d13..dc939cf 100644 --- a/src/main/java/com/study/tank/strategy/Imp/GoodTankFireStrategy.java +++ b/src/main/java/com/study/tank/strategy/Imp/GoodTankFireStrategy.java @@ -1,39 +1,39 @@ -package com.study.tank.strategy.Imp; - -import com.study.tank.Bullet; -import com.study.tank.ImageManger; -import com.study.tank.Tank; -import com.study.tank.strategy.FireStrategy; - -/** - * @author xsj - * @date 2022/10/25 13:28 - */ -public class GoodTankFireStrategy implements FireStrategy { - - @Override - public void fire(Tank tank) { - int bx = tank.getX() + Tank.tankWidth / 2 - ImageManger.bulletD.getWidth() / 2; - int by = tank.getY() + Tank.tankHeight / 2 - ImageManger.bulletD.getHeight() / 2; - switch (tank.dir) { - case DOWN: - new Bullet(bx, by + 6, tank.dir, tank.getGroup(), tank.tankFrame); - new Bullet(bx, by - 6, tank.dir, tank.getGroup(), tank.tankFrame); - break; - case UP: - new Bullet(bx + 1, by + 6, tank.dir, tank.getGroup(), tank.tankFrame); - new Bullet(bx + 1, by - 6, tank.dir, tank.getGroup(), tank.tankFrame); - break; - case LEFT: - new Bullet(bx + 6, by, tank.dir, tank.getGroup(), tank.tankFrame); - new Bullet(bx - 6, by, tank.dir, tank.getGroup(), tank.tankFrame); - break; - case RIGHT: - new Bullet(bx + 6, by + 1, tank.dir, tank.getGroup(), tank.tankFrame); - new Bullet(bx - 6, by + 1, tank.dir, tank.getGroup(), tank.tankFrame); - break; - - } - - } -} +//package com.study.tank.strategy.Imp; +// +//import com.study.tank.Bullet; +//import com.study.tank.ImageManger; +//import com.study.tank.Tank; +//import com.study.tank.strategy.FireStrategy; +// +///** +// * @author xsj +// * @date 2022/10/25 13:28 +// */ +//public class GoodTankFireStrategy implements FireStrategy { +// +// @Override +// public void fire(Tank tank) { +// int bx = tank.getX() + Tank.tankWidth / 2 - ImageManger.bulletD.getWidth() / 2; +// int by = tank.getY() + Tank.tankHeight / 2 - ImageManger.bulletD.getHeight() / 2; +// switch (tank.dir) { +// case DOWN: +// new Bullet(bx, by + 6, tank.dir, tank.getGroup(), tank.tankFrame); +// new Bullet(bx, by - 6, tank.dir, tank.getGroup(), tank.tankFrame); +// break; +// case UP: +// new Bullet(bx + 1, by + 6, tank.dir, tank.getGroup(), tank.tankFrame); +// new Bullet(bx + 1, by - 6, tank.dir, tank.getGroup(), tank.tankFrame); +// break; +// case LEFT: +// new Bullet(bx + 6, by, tank.dir, tank.getGroup(), tank.tankFrame); +// new Bullet(bx - 6, by, tank.dir, tank.getGroup(), tank.tankFrame); +// break; +// case RIGHT: +// new Bullet(bx + 6, by + 1, tank.dir, tank.getGroup(), tank.tankFrame); +// new Bullet(bx - 6, by + 1, tank.dir, tank.getGroup(), tank.tankFrame); +// break; +// +// } +// +// } +//}