diff --git a/docs/坦克大战笔记.docx b/docs/坦克大战笔记.docx index 666b8f0..fb28f85 100644 Binary files a/docs/坦克大战笔记.docx and b/docs/坦克大战笔记.docx differ diff --git a/src/com/demo/tank/coruse2/Bullet.java b/src/com/demo/tank/coruse2/Bullet.java index d43266e..804d89c 100644 --- a/src/com/demo/tank/coruse2/Bullet.java +++ b/src/com/demo/tank/coruse2/Bullet.java @@ -8,14 +8,20 @@ public class Bullet { private static final int SPEED = 5; private static final int WIDTH = 20; private static final int HEIGHT = 20; + private boolean live = true; + private TankFrame tf; - public Bullet(int x, int y, Direction direction) { + public Bullet(int x, int y, Direction direction, TankFrame tf) { this.x = x; this.y = y; this.direction = direction; + this.tf = tf; } public void paint(Graphics g){ + if(!live){ + tf.bullets.remove(this); + } Color color = g.getColor(); g.setColor(Color.RED); g.fillOval(x, y, WIDTH, HEIGHT); @@ -36,6 +42,9 @@ public class Bullet { default: break; } + if(x < 0 || y<0 || x> TankFrame.GAME_WIDTH || x > TankFrame.GAME_HEIGHT){ + live = false; + } } @@ -62,4 +71,12 @@ public class Bullet { public void setDirection(Direction direction) { this.direction = direction; } + + public boolean isLive() { + return live; + } + + public void setLive(boolean live) { + this.live = live; + } } diff --git a/src/com/demo/tank/coruse2/Tank.java b/src/com/demo/tank/coruse2/Tank.java index 5615af9..6291594 100644 --- a/src/com/demo/tank/coruse2/Tank.java +++ b/src/com/demo/tank/coruse2/Tank.java @@ -74,7 +74,6 @@ public class Tank { } public void fire() { - Bullet bullet = new Bullet(x+15, y, this.dir); - tankFrame.bullet = bullet; + tankFrame.bullets.add(new Bullet(x, y, this.dir, tankFrame)); } } diff --git a/src/com/demo/tank/coruse2/TankFrame.java b/src/com/demo/tank/coruse2/TankFrame.java index be41f05..0caff8f 100644 --- a/src/com/demo/tank/coruse2/TankFrame.java +++ b/src/com/demo/tank/coruse2/TankFrame.java @@ -5,14 +5,18 @@ import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; public class TankFrame extends Frame { - private static final int GAME_WIDTH = 800; - private static final int GAME_HEIGHT = 600; + public static final int GAME_WIDTH = 800; + public static final int GAME_HEIGHT = 600; Image image = null; Tank tank = new Tank(500, 500, Direction.UP, this); - Bullet bullet = new Bullet(520, 440, Direction.UP); +// Bullet bullet = new Bullet(520, 440, Direction.UP); + List bullets = new ArrayList(); public TankFrame(){ setVisible(true); @@ -44,8 +48,26 @@ public class TankFrame extends Frame { @Override public void paint(Graphics g){ + //打印出子弹数量 + Color color = g.getColor(); + g.setColor(Color.WHITE); + g.drawString("当前子弹数量:" + bullets.size(), 60, 50); + g.setColor(color); + tank.paint(g); - bullet.paint(g); + for (int i = 0; i< bullets.size(); i++){ + bullets.get(i).paint(g); + } +// for(Iterator it = bullets.iterator(); it.hasNext();){ +// Bullet b = it.next(); +// if(!b.isLive()){ +// it.remove(); +// } +// } +// +// for (Bullet b : bullets){ +// b.paint(g); +// } } class MyKeyListener extends KeyAdapter{