course3 绘制子弹

master
Jian Hu 3 years ago
parent b14b6fd4f7
commit a213dc71d5

Binary file not shown.

@ -6,8 +6,8 @@ public class Bullet {
private int x, y; private int x, y;
private Direction direction; private Direction direction;
private static final int SPEED = 5; private static final int SPEED = 5;
private static final int WIDTH = 20; public static final int WIDTH = ResourceManager.bulletD.getWidth();
private static final int HEIGHT = 20; public static final int HEIGHT = ResourceManager.bulletD.getHeight();
private boolean live = true; private boolean live = true;
private TankFrameV3 tf; private TankFrameV3 tf;
@ -22,10 +22,20 @@ public class Bullet {
if(!live){ if(!live){
tf.bullets.remove(this); tf.bullets.remove(this);
} }
Color color = g.getColor(); switch (direction){
g.setColor(Color.RED); case UP:
g.fillOval(x, y, WIDTH, HEIGHT); g.drawImage(ResourceManager.bulletU, x, y, null);
g.setColor(color); break;
case DOWN:
g.drawImage(ResourceManager.bulletD, x, y, null);
break;
case LEFT:
g.drawImage(ResourceManager.bulletL, x, y, null);
break;
case RIGHT:
g.drawImage(ResourceManager.bulletR, x, y, null);
break;
}
move(); move();
} }

@ -6,6 +6,7 @@ import java.io.IOException;
public class ResourceManager { public class ResourceManager {
public static BufferedImage tankL, tankR, tankU, tankD; public static BufferedImage tankL, tankR, tankU, tankD;
public static BufferedImage bulletL, bulletR, bulletU, bulletD;
static { static {
try { try {
@ -13,6 +14,11 @@ public class ResourceManager {
tankR = ImageIO.read(ResourceManager.class.getClassLoader().getResourceAsStream("com/demo/tank/images/tankR.gif")); tankR = ImageIO.read(ResourceManager.class.getClassLoader().getResourceAsStream("com/demo/tank/images/tankR.gif"));
tankU = ImageIO.read(ResourceManager.class.getClassLoader().getResourceAsStream("com/demo/tank/images/tankU.gif")); tankU = ImageIO.read(ResourceManager.class.getClassLoader().getResourceAsStream("com/demo/tank/images/tankU.gif"));
tankD = ImageIO.read(ResourceManager.class.getClassLoader().getResourceAsStream("com/demo/tank/images/tankD.gif")); tankD = ImageIO.read(ResourceManager.class.getClassLoader().getResourceAsStream("com/demo/tank/images/tankD.gif"));
bulletL = ImageIO.read(ResourceManager.class.getClassLoader().getResourceAsStream("com/demo/tank/images/bulletL.gif"));
bulletR = ImageIO.read(ResourceManager.class.getClassLoader().getResourceAsStream("com/demo/tank/images/bulletR.gif"));
bulletU = ImageIO.read(ResourceManager.class.getClassLoader().getResourceAsStream("com/demo/tank/images/bulletU.gif"));
bulletD = ImageIO.read(ResourceManager.class.getClassLoader().getResourceAsStream("com/demo/tank/images/bulletD.gif"));
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }

@ -8,6 +8,8 @@ public class Tank {
private static final int SPEED = 10; private static final int SPEED = 10;
private boolean moving; private boolean moving;
private TankFrameV3 tankFrame; private TankFrameV3 tankFrame;
public static final int WIDTH = ResourceManager.tankD.getWidth();
public static final int HEIGHT = ResourceManager.tankD.getHeight();
public Tank(int x, int y, Direction dir, TankFrameV3 tankFrame) { public Tank(int x, int y, Direction dir, TankFrameV3 tankFrame) {
@ -86,6 +88,8 @@ public class Tank {
} }
public void fire() { public void fire() {
tankFrame.bullets.add(new Bullet(x, y, this.dir, tankFrame)); int bx = x + Tank.WIDTH/2 - Bullet.WIDTH/2;
int by = y + Tank.HEIGHT/2 - Bullet.HEIGHT/2;
tankFrame.bullets.add(new Bullet(bx, by, this.dir, tankFrame));
} }
} }

Loading…
Cancel
Save