坦克打出子弹

master
terry 3 years ago
parent 69fcc9bc73
commit d31f0d4b85

Binary file not shown.

@ -17,7 +17,7 @@ public class Bullet {
public void paint(Graphics g){ public void paint(Graphics g){
Color color = g.getColor(); Color color = g.getColor();
g.setColor(Color.BLACK); g.setColor(Color.RED);
g.fillOval(x, y, WIDTH, HEIGHT); g.fillOval(x, y, WIDTH, HEIGHT);
g.setColor(color); g.setColor(color);
move(); move();

@ -6,12 +6,14 @@ public class Tank {
private Direction dir; private Direction dir;
private static final int SPEED = 10; private static final int SPEED = 10;
private boolean moving; private boolean moving;
private TankFrame tankFrame;
public Tank(int x, int y, Direction dir) { public Tank(int x, int y, Direction dir, TankFrame tankFrame) {
this.x = x; this.x = x;
this.y = y; this.y = y;
this.dir = dir; this.dir = dir;
this.tankFrame = tankFrame;
} }
public void paint(Graphics g) { public void paint(Graphics g) {
@ -70,4 +72,9 @@ public class Tank {
public void setMoving(boolean moving) { public void setMoving(boolean moving) {
this.moving = moving; this.moving = moving;
} }
public void fire() {
Bullet bullet = new Bullet(x+15, y, this.dir);
tankFrame.bullet = bullet;
}
} }

@ -11,7 +11,7 @@ public class TankFrame extends Frame {
private static final int GAME_HEIGHT = 600; private static final int GAME_HEIGHT = 600;
Image image = null; Image image = null;
Tank tank = new Tank(500, 500, Direction.UP); 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);
public TankFrame(){ public TankFrame(){
@ -35,7 +35,7 @@ public class TankFrame extends Frame {
} }
Graphics imgGraphic = image.getGraphics(); Graphics imgGraphic = image.getGraphics();
Color color = g.getColor(); Color color = g.getColor();
imgGraphic.setColor(Color.WHITE); imgGraphic.setColor(Color.BLACK);
imgGraphic.fillRect(0,0, GAME_WIDTH, GAME_HEIGHT); imgGraphic.fillRect(0,0, GAME_WIDTH, GAME_HEIGHT);
imgGraphic.setColor(color); imgGraphic.setColor(color);
paint(imgGraphic); paint(imgGraphic);
@ -90,6 +90,9 @@ public class TankFrame extends Frame {
case KeyEvent.VK_S: case KeyEvent.VK_S:
bD = false; bD = false;
break; break;
case KeyEvent.VK_SPACE:
tank.fire();
break;
default: default:
break; break;
} }

Loading…
Cancel
Save