diff --git a/src/com/msb/Bullet.java b/src/com/msb/Bullet.java new file mode 100644 index 0000000..9e98e77 --- /dev/null +++ b/src/com/msb/Bullet.java @@ -0,0 +1,44 @@ +package com.msb; + +import java.awt.*; + +/** + * @Author bingor + * @Date 2022-09-30 14:33 + * @Description: 子弹类 + * @Version: 1.0 + */ +public class Bullet extends Frame { + + private int x, y; + private DirEnum dir; + public static final int WIDTH = 8; + public static final int HEIGHT = 8; + public static final int SPEED = 5; + + public Bullet(int x, int y, DirEnum dir) { + this.x = x; + this.y = y; + this.dir = dir; + } + + public void paint(Graphics g) { + Color color = g.getColor(); + g.setColor(Color.RED); + g.fillOval(x, y, WIDTH, HEIGHT); + g.setColor(color); //还原画笔原来的颜色 + moving(); + } + + public void moving() { + + switch (dir) { + case UP: y -= SPEED; break; + case DOWN: y += SPEED; break; + case LEFT: x -= SPEED; break; + case RIGHT: x += SPEED; break; + default: break; + } + + } +} diff --git a/src/com/msb/TankFrame.java b/src/com/msb/TankFrame.java index 271aeae..3260ece 100644 --- a/src/com/msb/TankFrame.java +++ b/src/com/msb/TankFrame.java @@ -15,6 +15,7 @@ import java.awt.event.WindowEvent; public class TankFrame extends Frame { Tank tank = new Tank(200, 200, DirEnum.RIGHT); + Bullet bullet = new Bullet(200, 200, DirEnum.DOWN); public TankFrame() { @@ -39,6 +40,7 @@ public class TankFrame extends Frame { public void paint(Graphics g) { super.paint(g); tank.paint(g); + bullet.paint(g); } //这里使用内部内的原因是,只有TankFrame用到