坦克打出一串子弹并销毁超出边界子弹

master
terry 3 years ago
parent d31f0d4b85
commit 45a1939f92

Binary file not shown.

@ -8,14 +8,20 @@ public class Bullet {
private static final int SPEED = 5; private static final int SPEED = 5;
private static final int WIDTH = 20; private static final int WIDTH = 20;
private static final int HEIGHT = 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.x = x;
this.y = y; this.y = y;
this.direction = direction; this.direction = direction;
this.tf = tf;
} }
public void paint(Graphics g){ public void paint(Graphics g){
if(!live){
tf.bullets.remove(this);
}
Color color = g.getColor(); Color color = g.getColor();
g.setColor(Color.RED); g.setColor(Color.RED);
g.fillOval(x, y, WIDTH, HEIGHT); g.fillOval(x, y, WIDTH, HEIGHT);
@ -36,6 +42,9 @@ public class Bullet {
default: default:
break; 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) { public void setDirection(Direction direction) {
this.direction = direction; this.direction = direction;
} }
public boolean isLive() {
return live;
}
public void setLive(boolean live) {
this.live = live;
}
} }

@ -74,7 +74,6 @@ public class Tank {
} }
public void fire() { public void fire() {
Bullet bullet = new Bullet(x+15, y, this.dir); tankFrame.bullets.add(new Bullet(x, y, this.dir, tankFrame));
tankFrame.bullet = bullet;
} }
} }

@ -5,14 +5,18 @@ import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent; import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter; import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent; import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class TankFrame extends Frame { public class TankFrame extends Frame {
private static final int GAME_WIDTH = 800; public static final int GAME_WIDTH = 800;
private static final int GAME_HEIGHT = 600; public static final int GAME_HEIGHT = 600;
Image image = null; Image image = null;
Tank tank = new Tank(500, 500, Direction.UP, this); 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<Bullet> bullets = new ArrayList();
public TankFrame(){ public TankFrame(){
setVisible(true); setVisible(true);
@ -44,8 +48,26 @@ public class TankFrame extends Frame {
@Override @Override
public void paint(Graphics g){ 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); tank.paint(g);
bullet.paint(g); for (int i = 0; i< bullets.size(); i++){
bullets.get(i).paint(g);
}
// for(Iterator<Bullet> 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{ class MyKeyListener extends KeyAdapter{

Loading…
Cancel
Save