diff --git a/docs/坦克大战笔记.docx b/docs/坦克大战笔记.docx index 47c8a0d..6db9858 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 new file mode 100644 index 0000000..b5581d2 --- /dev/null +++ b/src/com/demo/tank/coruse2/Bullet.java @@ -0,0 +1,65 @@ +package com.demo.tank.coruse2; + +import java.awt.*; + +public class Bullet { + private int x, y; + private Direction direction; + private static final int SPEED = 5; + private static final int WIDTH = 20; + private static final int HEIGHT = 20; + + public Bullet(int x, int y, Direction direction) { + this.x = x; + this.y = y; + this.direction = direction; + } + + public void paint(Graphics g){ + Color color = g.getColor(); + g.setColor(Color.BLACK); + g.fillOval(x, y, WIDTH, HEIGHT); + g.setColor(color); + move(); + } + + private void move() { + switch (direction){ + case UP: y -= SPEED; + break; + case DOWN: y += SPEED; + break; + case LEFT: x -= SPEED; + break; + case RIGHT: x += SPEED; + break; + default: + break; + } + } + + + public int getX() { + return x; + } + + public void setX(int x) { + this.x = x; + } + + public int getY() { + return y; + } + + public void setY(int y) { + this.y = y; + } + + public Direction getDirection() { + return direction; + } + + public void setDirection(Direction direction) { + this.direction = direction; + } +} diff --git a/src/com/demo/tank/coruse2/TankFrame.java b/src/com/demo/tank/coruse2/TankFrame.java index 27d3d01..119d6d5 100644 --- a/src/com/demo/tank/coruse2/TankFrame.java +++ b/src/com/demo/tank/coruse2/TankFrame.java @@ -8,6 +8,7 @@ import java.awt.event.WindowEvent; public class TankFrame extends Frame { Tank tank = new Tank(500, 500, Direction.UP); + Bullet bullet = new Bullet(520, 440, Direction.UP); public TankFrame(){ setVisible(true); @@ -26,6 +27,7 @@ public class TankFrame extends Frame { @Override public void paint(Graphics g){ tank.paint(g); + bullet.paint(g); } class MyKeyListener extends KeyAdapter{