1、软件具体设计说明书 一、 TankWar类: TankWar继承Frame类。 1、 paint( )方法 paint( ) 方法关键实现对坦克类和子弹类, 障碍物类和爆炸类等画到窗口上。坦克类又分为MyTank和EnwmyTank 两类。对这两类用不一样颜色画到窗口上。画子弹时经过传输坦克颜色画出对应子弹颜色。 子弹画坦克死亡则不再对其进行重画。 2、 update( )方法。 update( ) 方法关键是为了处理, 重画频率太快, paint( ) 方法还没有完成, 重画坦克时出现闪烁现象。 经过双缓冲技术将全部东西画在虚拟图片上,一次性显示出来处理闪烁现象。
2、3、 内部类KeyMon键盘监听 KeyMon继承KeyAdapter类。经过KeyMon类监听, 用户能够经过键盘控制自己坦克。 4、 内部类MyTankWarThread MyTankWarThread实现Runnable接口, 经过MyTankWarThread实现对坦克重画, 实现坦克移动。 5、 showFrame( )方法 showFrame( )方法关键是设置一个窗口及其多种参数。 二、 Tank类 Tank类是实现Runnable接口一个抽象类 1、 Tank类关键属性有: int x; //坦克位置 int y; // int old
3、x; //坦克上一步坐标 int oldy; // Dir dir; // Tank方向组员变量 int speed = 5; boolean isLive = true; 2、 Tank类提供多种结构方法 Tank(int x, int y) Tank(int x, int y, Dir dir) Tank(int x, int y, Dir dir, TankWar tw) 依据不一样需要使用不一样结构方法。 3、 drawTank ()方法 drawTank()方法依据坦克类型不一样能够画出不一样类型坦克和不一样方向坦克。敌人
4、坦克用BLUE画出来, 自己坦克用RED颜色画出来, 以区分敌我。 4、 fire()方法 fire()方法功效是实现打出一颗子弹, fire()方法调用了画坦克颜色变量 c , 画出与坦克一样颜色子弹, 以区分子弹是那一方发出来。 5、 isHitWall()方法 该方法是用来判定Tank是否与障碍物发生碰撞 public boolean isHitWall() { Wall w = null; for (int i = 0; i < tw.walls.size(); i++) { w = tw.walls.get(i); if (w.isHits(
5、this.getRectangle())) { return true; } } return false; } 6、 getRectangle()方法 该方法关键是用来检测是否发生了碰撞。 public Rectangle getRectangle() { return new Rectangle(x, y, TANK_WIDTH, TANK_HEIGHT); } 7、 add(int count, TankWar tw)方法 该方法是用来添加敌人坦克, 当敌人坦克数量降低时, 而此时敌人count又未小于0时添加对应数量敌人坦克来增加游
6、戏可玩性。 三、 MyTank类 MyTank类继承Tank类 1、 locDir(KeyEvent e)方法 经过键盘监听, 对按键进行监听来实现用户能够经过W D S A四个方向键对坦克控制 2、 keyReleased(KeyEvent e)方法 该方法关键是为了预防连续发子弹问题, 只有放开SPACE按键才能够发弹, 这么就增加了游戏可玩性。 3、 stay()方法 该方法关键实现暂停功效, 这个是本游戏代码中一个亮点, 经过线程来实现 public void stay() { int tc = Thread.activeCount(); Thr
7、ead[] threads = new Thread[tc]; Thread.enumerate(threads); if (tw.flag) { for (int i = 0; i < threads.length; i++) { System.out.println(tc + ":suspend " + threads[i].getName()); if (threads[i].getName().matches("^Thread-.*")) { threads[i].suspend(); tw.flag = false;
8、 } } } else if (! tw.flag) { for (int i = 0; i < threads.length; i++) { System.out.println(tc + ":resume " + threads[i].getName()); if (threads[i].getName().matches("^Thread-.*")) { threads[i].resume(); tw.flag = true; } } } } 4、 move()方法 该方
9、法是实现坦克移动, 假如碰到障碍物或出界, 则返回上一次位置, 经过(oldX , oldY)坐标统计坦克每一次移动位置。 public void move() { if (dir == Dir.U) { oldy = y; y = y - speed; if (isHitWall()) { y = oldy; } } else if (dir == Dir.R) { oldx = x; x = x + speed; if (isHitWall()) { x = oldx; } } else
10、 if (dir == Dir.D) { oldy = y; y = y + speed; if (isHitWall()) { y = oldy; } } else if (dir == Dir.L) { oldx = x; x = x - speed; if (isHitWall()) { x = oldx; } } //判定坦克出界 if (x > TankWar.GAME_WIDTH - TANK_WIDTH - 5) { x = TankWar.GAME_WIDTH -
11、 TANK_WIDTH - 5; } if (x < 5) { x = 5; } if (y > TankWar.GAME_HEIGHT - TANK_HEIGHT - 5) { y = TankWar.GAME_HEIGHT - TANK_HEIGHT - 5; } if (y < TANK_HEIGHT) { y = TANK_HEIGHT; } } 5、 drawTank()方法 这个方法是实现坦克自动移动, 玩家能够经过方向键改版方向, 移动过程中能够发射子弹。 public void drawTank(Gra
12、phics g) { super.drawTank(g); if (isStay) { move(); } } 四、 EneyTank类 EnemyTank类继承Tank类 1、 moveTrace()方法 该方法实现是敌人坦克伪智能功效, 这个是本坦克大战一个亮点。敌人坦克出来后会依据目前位置和hero位置比较, 进而对hero进行追击。 public void moveTrace(){ if(step>0){ move(); step--; return; } int dx = this.x- tw.h
13、ero.x ; int dy = this.y- tw.hero.y ; if (tw.hero.isLive) { int next = (int) (Math.random() * 2); if (next == 1) { if (dx > 0) { this.dir = Dir.L; } else if (dx < 0) { this.dir = Dir.R; } } else { if (dy > 0) { this.dir = Dir.U; } el
14、se if (dy < 0) { this.dir = Dir.D; } } } else { move(); } step = (int) (Math.random()*12+3); } 2、 move()方法 敌人坦克移动时调用方法, 方向是经过数来确定。 3、 hitTank(Tank tank)方法 敌人碰撞检测方法, 假如和hero发生碰撞则hero发生爆炸。 4、 run()方法 敌人坦克开启线程控制。 public void run() { while(isLive) { try {
15、 Thread.sleep(50); } catch (InterruptedException e) { e.printStackTrace(); } moveTrace(); } } 五、 Shells子弹类 子弹类继承了Runnable类, 基础属性有子弹坐标(x , y), 方向Dir. 1、 drawShells(Graphics g)方法 这是一个画子弹方法, 子弹颜色经过调用坦克颜色this.c来实现画出与坦克一样颜色。 2、 move() 子弹移动方法, 子弹方法经过坦克目前方一直确定。 3、 run() 子弹开启线程。 4、 public Rectangle getRectangle() 子弹检测碰撞方法。 六、 Dir类 这是一个枚举类。 public enum Dir {L,R,U,D} 七、 Bomb爆炸类 爆炸类只有一个drawBomd(Graphics g)方法, 每当坦克消亡后则调用drawBomd(Graphics g)方法。 八、 WallNode障碍物类 关键是一个public void draw(Graphics g) 方法将障碍物经过paint画出来。






