// 专门判断子弹是否击中坦克的函数 public boolean hitTank(Bullet b, Tank et) { boolean b1 = false; // 判断该坦克的方向 switch (et.direct) { // 向上和向下范围一样 case 0: case 2: if (b.x >= et.x && b.x <= et.x + 20 && b.y >= et.y && b.y <= et.y + 30) { // 击中1.子弹死亡 2.目标死亡 b.alive = false; et.alive = false; b1 = true; // 创建一颗炸弹,放入Vector中 Bomb bomb = new Bomb(et.x, et.y); bombs.add(bomb); } break; case 1: case 3: if (b.x >= et.x && b.x <= et.x + 30 && b.y >= et.y && b.y <= et.y + 20) { // 击中 b.alive = false; et.alive = false; b1 = true; // 创建一颗炸弹,放入Vector中 Bomb bomb = new Bomb(et.x, et.y); bombs.add(bomb); } break; // default:System.out.println("xxxxxxxxx"); } return b1; }
public ArrayList<Integer> getTankDamage_notFire() { ArrayList<Integer> damages = new ArrayList<Integer>(); for (Tank t : tanks) { damages.add(t.current_damge()); } return damages; }
public ArrayList<Integer> getEachTankDamage() { ArrayList<Integer> damages = new ArrayList<Integer>(); for (Tank t : tanks) { damages.add(t.fire(getFortress())); } notifyListeners(); return damages; }
/** Test method for {@link icd3.Tank#Tank()}. */ @Test public void testTank() { Tank defaultTank = new Tank(); assertTrue(defaultTank.getBottom() == 0.0); assertTrue(defaultTank.getTop() == 0.0); assertTrue(defaultTank.baseArea() == 0.0); }
/** Test method for {@link icd3.Tank}. */ @Test public void testTankBehavior() { Tank tank = new Tank(); tank.setCoordinates(m_bottomLeft, m_topRight); assertTrue(tank.getBottom() == -4.5); assertTrue(tank.getTop() == 3.5); assertTrue(tank.baseArea() == 1.0); }
public void processMove(Tank tank) throws InterruptedException { this.tank = tank; int i = 0; Direction tankDirection = tank.getDirection(); int tankXCoordinate = tank.getX(); int tankYCoordinate = tank.getY(); tank.turn(tankDirection); while (i < 64) { if (tankDirection == Direction.UP && tankYCoordinate > 0) { tank.updateY(-1); } if (tankDirection == Direction.DOWN && tankYCoordinate < 512) { tank.updateY(1); } if (tankDirection == Direction.LEFT && tankXCoordinate > 0) { tank.updateX(-1); } if (tankDirection == Direction.RIGHT && tankXCoordinate < 512) { tank.updateX(1); } i += 1; repaint(); Thread.sleep(tank.getSpeed()); } }
public boolean hitTank(Tank t) { if (this.isLive() && t.isLive() && (this.isGood() != t.isGood()) && this.getRect().intersects(t.getRect())) { // Missile this.setLive(false); tc.missileList.remove(this); // Tank if (t.isGood()) { t.setLife(t.getLife() - Tank.LIFE_MAX / 5); if (t.getLife() == 0) { t.setLive(false); } } else { t.setLive(false); } // Explode Explode e = new Explode(t.x, t.y, this.tc); tc.explodeList.add(e); return true; } return false; }
public boolean hitTanks(List<Tank> ts) { Tank t = null; for (int i = 0; i < ts.size(); i++) { t = ts.get(i); if (t.isLive() && t != null) { if (hitTank(t)) { return true; } } } return false; }
private void illegaleMove(Direction direction) { System.out.println( "[illegal move] " + "derection:" + direction + "||" + "tankX:" + tank.getX() + "||" + "tankY:" + tank.getY()); return; }
public int fireDown() throws Exception { String str = getQuadrantXY(tank.getX(), tank.getY()); int tankV = Integer.valueOf(str.substring(0, 1)); int tankH = Integer.valueOf(str.substring(2, str.length())); tank.turn(Direction.DOWN); for (int idx = tankV; idx < battleField.getBattleField().length; idx++) { for (int j = tankH; j < tankH + 1; j++) { if (battleField.scanQuadrant(idx, j) == "B") { tank.fire(); } } } return tankV; }
public static void main(String[] args) { Tank tank = new Tank(Color.BLACK, 10, 100); T34 t34 = new T34(Color.BLACK, 5, 50); Bt7 bt7 = new Bt7(Color.GREEN, 4, 65); Tiger tiger = new Tiger(Color.BROWN, 3, 45); System.out.println(tank.toString() + " " + tank.move()); System.out.println(t34.toString() + " " + t34.move()); System.out.println(bt7.toString() + " " + bt7.move()); System.out.println(tiger.toString() + " " + tiger.move()); }
void union(int i, int j, int id, int H) { for (int k = 0; k < 4; k++) { int ni = i + move[k][0]; int nj = j + move[k][1]; if (0 <= ni && ni < h && 0 <= nj && nj < w && !u[ni][nj] && map[ni][nj] == H) { Tank t = ref.get(id); t.add(ni, nj); ref.put(id, t); u[ni][nj] = true; tanks[ni][nj] = id; union(ni, nj, id, H); } } }
public int fireLeft() throws Exception { String str = getQuadrantXY(tank.getX(), tank.getY()); int tankV = Integer.valueOf(str.substring(0, 1)); int tankH = Integer.valueOf(str.substring(2, str.length())); tank.turn(Direction.LEFT); for (int idx = tankV; idx < tankV + 1; idx++) { for (int j = tankH; j >= 0; j--) { if (battleField.scanQuadrant(idx, j) == "B") { tank.fire(); } } } return tankH; }
private void moveRight(Direction direction) { if (direction == Direction.RIGHT) { tank.updateX(step); System.out.println( "[move right] " + "derection:" + direction + "||" + "tankX:" + tank.getX() + "||" + "tankY:" + tank.getY()); } }
private void moveLeft(Direction direction) { if (direction == Direction.LEFT) { tank.updateX(-step); System.out.println( "[move left] " + "derection:" + direction + "||" + "tankX:" + tank.getX() + "||" + "tankY:" + tank.getY()); } }
public void run() { while (AliveWallPaperTank.TankChangeDirectionFlag) { try { ArrayList<Tank> alTank = new ArrayList<Tank>(AliveWallPaperTank.alTank); // 获取已存在坦克存放列表 for (Tank tank : alTank) // 循环每一个坦克 { tank.changeDirection(); } Thread.sleep(1000); // 每隔一秒钟检测一次 } catch (Exception e) { e.printStackTrace(); } } }
private void moveDown(Direction direction) { if (direction == Direction.DOWN) { tank.updateY(step); System.out.println( "[move down] " + "derection:" + direction + "||" + "tankX:" + tank.getX() + "||" + "tankY:" + tank.getY()); } }
private void moveUp(Direction direction) { if (direction == Direction.UP) { tank.updateY(-step); System.out.println( "[move up] " + "derection:" + direction + "||" + "tankX:" + tank.getX() + "||" + "tankY:" + tank.getY()); } }
// MAIN FUNCTION public static void main(String[] args) throws InterruptedException { // notre fonction principale while (true) { initGame(); // on initialise la fenêtre du jeu generateGround(); // on génère le terrain dans des tableaux Menu.navigation(); // on affiche le menu de navigation while (!endOfGame) { // TANT QUE LA PARTIE N'EST PAS FINIE // StdDraw.clear(Constants.BACKGROUND_COLOR); StdDraw.picture(Constants.X_MAX / 2, Constants.Y_MAX / 2, "./src/ciel.png"); for (int i = 0; i < Menu.numberOfPlayers; i++) { runPlayer(i); // actions de chaque joueur (déplacement, Obus, mine) } endOfGame = Tank.endGame(); // on vérifie si il reste des joueurs showGround(); // (on fait varier le x de chaque rectangle) controlTanks(); // gérer les joueurs (collisions, vie, scores) controlObus(); // gérer les Obus (déplacement, collisions) controlMines(); // gérer les Mines (déplacement, collisions) controlMissiles(); // gérer les Missiles (déplacement, collisions) StdDraw.show(10); // durée d'affichage de la vue (cadence) Thread.sleep(10); // pause pendant l'affichage } endGame(); // quand un joueur est mort (affichage gagnant) clearGame(); // effacer toutes les données du jeu pour recommencer une partie } }
public void processFire(Bullet bullet) throws InterruptedException { this.bullet = bullet; Direction tankDirection = tank.getDirection(); while (bullet.getY() > minusLimit && bullet.getY() < plusLimit && bullet.getX() > minusLimit && bullet.getX() < plusLimit) { if (tankDirection == Direction.UP) { bullet.updateY(-1); } else if (tankDirection == Direction.DOWN) { bullet.updateY(1); } else if (tankDirection == Direction.LEFT) { bullet.updateX(-1); } else if (tankDirection == Direction.RIGHT) { bullet.updateX(1); } if (processInterception()) { bullet.destroy(); } repaint(); Thread.sleep(bullet.getSpeed()); } }
@Override public void actionPerformed(ActionEvent e) { /// TODO Auto-generated method stub if (e.getSource() == timer) { if (influTank.army == 1) { for (Tank otherTank : map.layer_3_CT) { if (otherTank.tankState == 5) otherTank.tankState = otherTank.tempState; } } else { for (Tank otherTank : map.layer_3_UT) { if (otherTank.tankState == 5) otherTank.tankState = otherTank.tempState; } } timer.stop(); timer = null; } else { state++; } }
// retour menu accueil protected static void clearGame() { Menu.navigation = true; Menu.home = true; Menu.numberOfPlayers = 0; myObus.clear(); myTank.clear(); myRect.clear(); myMines.clear(); myMissiles.clear(); endOfGame = Tank.endGame(); }
public int isWon() { /*1 == lose, -1 == win*/ int game_continue = 0; if (fortress.getHp() <= 0) { return 1; } else { for (Tank t : tanks) { if (t.getNumber_undamaged_cell() != 0) { return game_continue; } } return -1; // win // for(int i =0; i<number_tanks;i++){ // if (tanks[i].getNumber_undamaged_cell()!=0){ // isAllTankDown = false; // } // if (isAllTankDown){ // return -1; // } // } } }
public void use(Tank tank) { amou--; if (tank.tools[tank.currentTool].amou == 0) { boolean count = false; for (int i = 0; i < tank.tools.length; i++) { if (tank.tools[i].amou != 0) { count = true; break; } } if (count) { do { tank.currentTool++; if (tank.currentTool == 7) tank.currentTool = 0; } while (tank.tools[tank.currentTool].amou == 0); } else { tank.currentTool = -1; } } LaserBomb laserBomb = new LaserBomb(tank.coordX, tank.coordY, tank.map, tank.direction, tank); tank.map.layer_5.add(laserBomb); }
public void layout(IFigure container) { Rectangle area = container.getClientArea(); area.height -= 1; Dimension scaleSize = new Dimension(0, 0); Dimension markerSize = new Dimension(0, 0); if (scale != null) { if (scale.isVisible()) { scaleSize = scale.getPreferredSize(-1, area.height); scale.setBounds(new Rectangle(area.x, area.y, scaleSize.width, scaleSize.height)); } else { scaleSize = scale.getPreferredSize(-1, area.height + 2 * scale.getMargin()); scale.setBounds( new Rectangle(area.x, area.y - scale.getMargin(), scaleSize.width, scaleSize.height)); scaleSize.height = 0; scaleSize.width = 0; } } if (marker != null && marker.isVisible()) { markerSize = marker.getPreferredSize(); marker.setBounds( new Rectangle( area.x + area.width - markerSize.width, marker.getScale().getBounds().y, markerSize.width, markerSize.height)); } if (tank != null) { tank.setBounds( new Rectangle( area.x + scaleSize.width, scale.getValuePosition(scale.getRange().getUpper(), false), area.width - scaleSize.width - markerSize.width, scale.getTickLength() + tank.getLineWidth())); } }
public int fireCannons(Cell cell, Tank[] tanks) { if (!cell.getHasBeenAttacked()) { // he cell has not been attacked, check if it is tank cell.setHasBeenAttacked(true); if (cell.getIsTank()) { // it is part of tank cell.setTankHit(true); for (Tank t : tanks) { if (t.getCells().contains(cell)) { t.setNumber_undamaged_cell(t.getNumber_undamaged_cell() - 1); } } return 1; // hit } else { return -1; // miss } } else { // the cell has been attacked, next check if it is tank and if (cell.isTankHit()) { return 0; // hit, but do nothing } else { return -1; } } }
/** Write your code here. */ void runTheGame() throws Exception { tank.turn(Direction.RIGHT); // tank.moveRandom(); // tank.clear(); tank.moveToQuadrant(4, 4); // tank.destroy(); // repaint(); tank.moveToQuadrant(3, 7); tank.moveToQuadrant(7, 2); // tank.move(); // tank.move(); // tank.move(); // // tank.turn(2); // tank.fire(); // tank.fire(); // tank.fire(); // // tank.move(); // tank.fire(); // tank.fire(); // tank.fire(); }
void run() { Scanner sc = new Scanner(System.in); for (; ; ) { w = sc.nextInt(); h = sc.nextInt(); if ((w | h) == 0) break; fj = sc.nextInt(); fi = sc.nextInt(); q = sc.nextInt(); map = new int[h][w]; for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) map[i][j] = sc.nextInt(); ref = new HashMap<Integer, Tank>(); tanks = new int[h][w]; u = new boolean[h][w]; int id = 1; for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) { if (map[i][j] == 0 || u[i][j]) continue; Tank t = new Tank(id); t.add(i, j); tanks[i][j] = id; ref.put(id, t); u[i][j] = true; union(i, j, id, map[i][j]); id++; } int L = sc.nextInt(); while (L-- != 0) { int time = sc.nextInt(); int pj = sc.nextInt(), pi = sc.nextInt(); for (int i : ref.keySet()) ref.get(i).height = 0; flow(time); System.out.println((int) ref.get(tanks[pi][pj]).height); } } }
public void addAntagonist(AnchorPane root) { antagonistArrayList = new ArrayList<Tank>(); Random rand = new Random(); int noEnemy = rand.nextInt(8 - 2) + 2; for (int i = 0; i < noEnemy; i++) { antagonistArrayList.add(tank = new Tank()); tank.positionX = 100; tank.positionY = 100; } System.out.println("antagonist: " + antagonistArrayList.size()); // implementera någon form av iterator Iterator<Tank> iterator = antagonistArrayList.iterator(); while (iterator.hasNext()) { Tank antagonist = iterator.next(); // System.out.println("Building position, X:" + antagonist.getTranslateX() + " Y:" + // antagonist.getTranslateY()); } }
/** Test method for {@link icd3.Tank#equals(Object)}. */ @Test public void testTankEquality() { Tank defaultTank = new Tank(); double[] unitBottom = {0, 0, 0}; double[] unitTop = {1, 1, 1}; Tank unitTank = new Tank(unitBottom, unitTop); double[] otherUnitBottom = new double[3]; double[] otherUnitTop = new double[3]; for (int i = 0; i < 3; ++i) { otherUnitBottom[i] = 0; otherUnitTop[i] = 1; } Tank otherUnitTank = new Tank(otherUnitBottom, otherUnitTop); assertTrue(defaultTank.equals(defaultTank)); assertTrue(unitTank.equals(unitTank)); assertTrue(unitTank.equals(otherUnitTank)); assertTrue(otherUnitTank.equals(unitTank)); assertFalse(defaultTank.equals(null)); assertFalse(unitTank.equals(null)); }