diff --git a/docs/坦克大战笔记.docx b/docs/坦克大战笔记.docx index b3f2d42..0e717e5 100644 Binary files a/docs/坦克大战笔记.docx and b/docs/坦克大战笔记.docx differ diff --git a/src/com/demo/tank/course4/Bullet.java b/src/com/demo/tank/course4/Bullet.java index 4beea81..392ced0 100644 --- a/src/com/demo/tank/course4/Bullet.java +++ b/src/com/demo/tank/course4/Bullet.java @@ -5,7 +5,7 @@ import java.awt.*; public class Bullet { private int x, y; private Direction direction; - private static final int SPEED = 5; + private static final int SPEED = 10; public static final int WIDTH = ResourceManager.bulletD.getWidth(); public static final int HEIGHT = ResourceManager.bulletD.getHeight(); private boolean live = true; @@ -68,6 +68,10 @@ public class Bullet { if(bulletRect.intersects(tankRect)){ tank.die(); this.die(); + //爆炸 + int ex = tank.getX() + Tank.WIDTH/2 - Explode.WIDTH/2; + int ey = tank.getY() + Tank.HEIGHT/2 - Explode.HEIGHT/2; + tf.explodes.add(new Explode(ex, ey , tf)); } } diff --git a/src/com/demo/tank/course4/Explode.java b/src/com/demo/tank/course4/Explode.java index 7fbf453..39d57b1 100644 --- a/src/com/demo/tank/course4/Explode.java +++ b/src/com/demo/tank/course4/Explode.java @@ -8,7 +8,6 @@ public class Explode { private int x, y; public static final int WIDTH = ResourceManager.explodes[0].getWidth(); public static final int HEIGHT = ResourceManager.explodes[0].getHeight(); - private boolean living = true; private TankFrameV4 tf; private int step = 0; @@ -23,7 +22,8 @@ public class Explode { public void paint(Graphics g){ g.drawImage(ResourceManager.explodes[step++], x, y, null); if(step >= ResourceManager.explodes.length){ - step = 0; + //播放完爆炸效果图片, remove + tf.explodes.remove(this); } } diff --git a/src/com/demo/tank/course4/Tank.java b/src/com/demo/tank/course4/Tank.java index 4fcf7dc..3029409 100644 --- a/src/com/demo/tank/course4/Tank.java +++ b/src/com/demo/tank/course4/Tank.java @@ -7,7 +7,7 @@ public class Tank { private int x,y; private Direction dir; private static final int SPEED = 8; - private boolean moving = false; + private boolean moving = true; private boolean living = true; private Group group = Group.BAD; private TankFrameV4 tankFrame; @@ -58,7 +58,18 @@ public class Tank { default: break; } -// if(random.nextInt(10) > 8) this.fire(); + if(this.group == Group.BAD) { + if(random.nextInt(10) == 5){ + this.fire(); + } + if(random.nextInt(100) > 95){ + this.randomDirection(); + } + } + } + + private void randomDirection() { + this.dir = Direction.values()[random.nextInt(4)]; } public void fire() { diff --git a/src/com/demo/tank/course4/TankFrameV4.java b/src/com/demo/tank/course4/TankFrameV4.java index dc4e6be..e6f2109 100644 --- a/src/com/demo/tank/course4/TankFrameV4.java +++ b/src/com/demo/tank/course4/TankFrameV4.java @@ -9,19 +9,20 @@ import java.util.ArrayList; import java.util.List; public class TankFrameV4 extends Frame { - public static final int GAME_WIDTH = 800; - public static final int GAME_HEIGHT = 600; + public static final int GAME_WIDTH = 1080; + public static final int GAME_HEIGHT = 800; Image image = null; - Tank tank = new Tank(500, 500, Direction.UP, Group.GOOD,this); + Tank tank = new Tank(380, 660, Direction.UP, Group.GOOD,this); // Bullet bullet = new Bullet(520, 440, Direction.UP); List bullets = new ArrayList(); List enemyTanks = new ArrayList<>(); - Explode explode = new Explode(200, 300, this); + + List explodes = new ArrayList<>(); public TankFrameV4(){ setVisible(true); - setBounds(500, 200 , GAME_WIDTH, GAME_HEIGHT); + setBounds(400, 100 , GAME_WIDTH, GAME_HEIGHT); setResizable(false); setTitle("tank war"); this.addKeyListener(new MyKeyListener()); @@ -54,12 +55,11 @@ public class TankFrameV4 extends Frame { g.setColor(Color.WHITE); g.drawString("当前子弹数量:" + bullets.size(), 60, 50); g.drawString("当前敌人数量:" + enemyTanks.size(), 60, 80); + g.drawString("当前爆炸数量:" + explodes.size(), 60, 100); g.setColor(color); tank.paint(g); - explode.paint(g); - for(int i = 0; i< enemyTanks.size(); i++){ enemyTanks.get(i).paint(g); } @@ -68,11 +68,16 @@ public class TankFrameV4 extends Frame { bullets.get(i).paint(g); } + //碰撞检测 for(int i = 0; i< bullets.size(); i++){ for(int j=0; j< enemyTanks.size(); j++){ bullets.get(i).collideWith(enemyTanks.get(j)); } } + + for (int i = 0; i< explodes.size(); i++){ + explodes.get(i).paint(g); + } // for(Iterator it = bullets.iterator(); it.hasNext();){ // Bullet b = it.next(); // if(!b.isLive()){ @@ -127,7 +132,7 @@ public class TankFrameV4 extends Frame { case KeyEvent.VK_S: bD = false; break; - case KeyEvent.VK_J: + case KeyEvent.VK_SPACE: tank.fire(); break; default: