您好,登錄后才能下訂單哦!
這篇文章主要介紹“怎么用JAVA實現經典游戲坦克大戰”的相關知識,小編通過實際案例向大家展示操作過程,操作方法簡單快捷,實用性強,希望這篇“怎么用JAVA實現經典游戲坦克大戰”文章能幫助大家解決問題。
1、要有難度關卡:第一關,第二關,第三關,第四關,第五關;第一關地圖最簡單,第五關地圖最難;
2、坦克要有血條,打多次才會死
3、地圖要我有我方坦克四輛(相當于4條命)和多輛敵方坦克
4、我方坦克的炮彈數量是固定的,設置為500
5、地圖右邊顯示基本信息
6、地圖上要在磚塊,鐵塊,河流
游戲開始頁面
窗口布局
public class GameFrame extends JFrame { /** * serialVersionUID */ private static final long serialVersionUID = -1176914786963603304L; public GameFrame() { super(); this.setSize(800, 700); this.setTitle("坦克大戰"); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); this.setResizable(false); this.setIconImage(TankGameImages.myTankImg[0]); // 顯示器屏幕大小 Dimension screenSizeInfo = Toolkit.getDefaultToolkit().getScreenSize(); int leftTopX = ((int) screenSizeInfo.getWidth() - this.getWidth()) / 2; int leftTopY = ((int) screenSizeInfo.getHeight() - this.getHeight()) / 2; // 設置顯示的位置在屏幕中間 this.setLocation(leftTopX, leftTopY); } }
地圖渲染核心算法
@Service public class PaintService { @Autowired private GameContext context; private Brick rightBrick = new Brick(700, 50); private Iron rightIron = new Iron(700, 50); private Water rightWater = new Water(700, 50); /** * 畫出東西(包括坦克、障礙物。。) * * @param g Graphics * @param stuff 東西對象 * @param panel 被畫的那個面板 */ public void drawStuff(Graphics g, Stuff stuff, JPanel panel) { switch (stuff.getType()) { //枚舉的switch,有意思,不需要+StuffTypeEnum.TANK case TANK: Tank tank = (Tank) stuff; switch (stuff.getDirect()) { // 判斷所朝的方向 case NORTH: this.drawNorth(g, tank, panel); break; case SOUTH: this.drawSouth(g, tank, panel); break; case WEST: this.drawWest(g, tank, panel); break; case EAST: this.drawEast(g, tank, panel); break; } break; case BRICK: // g.setColor(new Color(216, 90, 49)); // g.fill3DRect(stuff.getX() - 20, stuff.getY() - 20, 40, 40, false); g.drawImage(TankGameImages.stuffImg[StuffTypeEnum.BRICK.getKey()], stuff.getX() - 10, stuff.getY() - 10, 20, 20, panel); break; case IRON: // g.setColor(new Color(225, 225, 225)); // g.fill3DRect(stuff.getX() - 20, // stuff.getY() - 20, 40, 40, false); g.drawImage(TankGameImages.stuffImg[StuffTypeEnum.IRON.getKey()], stuff.getX() - 10, stuff.getY() - 10, 20, 20, panel); break; case WATER: // g.setColor(new Color(65, 64, 253)); // g.fillRect(stuff.getX() - 20, // stuff.getY() - 20, 40, 40); g.drawImage(TankGameImages.stuffImg[StuffTypeEnum.WATER.getKey()], stuff.getX() - 10, stuff.getY() - 10, 20, 20, panel); break; } } /** * 畫出爆炸 * * @param g Graphics * @param bombs 炸彈對象容器 * @param panel 被畫的那個面板 */ public void drawBomb(Graphics g, Vector<Bomb> bombs, JPanel panel) { for (int i = 0; i < bombs.size(); i++) { int l = bombs.get(i).getL(); Bomb b = bombs.get(i); // 從炸彈容器中取出一顆炸彈 if (b.getLifeTime() > 24) { // 生命值21-25 g.drawImage(TankGameImages.bomb[0], b.getX() - l / 2, b.getY() - l / 2, l, l, panel); } else if (b.getLifeTime() > 18) { // 生命值16-20 g.drawImage(TankGameImages.bomb[1], b.getX() - l / 2, b.getY() - l / 2, l, l, panel); } else if (b.getLifeTime() > 12) { // 生命值11-15 g.drawImage(TankGameImages.bomb[2], b.getX() - l / 2, b.getY() - l / 2, l, l, panel); } else if (b.getLifeTime() > 6) { // 生命值6-10 g.drawImage(TankGameImages.bomb[3], b.getX() - l / 2, b.getY() - l / 2, l, l, panel); } else { // 生命值低于6 g.drawImage(TankGameImages.bomb[4], b.getX() - l / 2, b.getY() - l / 2, l, l, panel); } b.lifeDown(); // 生命隨時間衰減 if (b.getLifeTime() == 0) { // 該炸彈死亡 b.setLive(false); } } } /** * 畫出敵人坦克和子彈 * * @param g Graphics * @param enemies 敵人坦克容量 * @param panel 被畫的面板 */ public void drawEnemyTank(Graphics g, Vector<EnemyTank> enemies, JPanel panel) { for (int i = 0; i < enemies.size(); i++) { this.drawStuff(g, enemies.get(i), panel); // 畫出敵人的坦克 for (int j = 0; j < enemies.get(i).getBullets().size(); j++) { if (enemies.get(i).getBullets().get(j) != null) { Bullet eb = enemies.get(i).getBullets().get(j); g.drawImage(TankGameImages.bullet, eb.getX() - 2, eb.getY() - 2, 4, 4, panel); } } } } /** * 畫出我的坦克和子彈 * * @param g Graphics * @param myTanks 我的坦克容量 * @param panel 被畫的那個面板 */ public void drawMyTank(Graphics g, Vector<MyTank> myTanks, JPanel panel) { for (int m = 0; m < myTanks.size(); m++) { MyTank myTank = myTanks.get(m); // 取出我的坦克 this.drawStuff(g, myTank, panel); // 畫出我的坦克 for (int i = 0; i < myTank.getBullets().size(); i++) { if (myTank.getBullets().get(i) != null) { Bullet b = myTank.getBullets().get(i); g.drawImage(TankGameImages.bullet, b.getX() - 2, b.getY() - 2, 4, 4, panel); } } } } /** * 畫出地圖 * * @param g Graphics * @param map 地圖對象 * @param panel 被畫的那個面板 */ public void drawMap(Graphics g, Map map, JPanel panel) { Vector<Brick> bricks = map.getBricks(); Vector<Iron> irons = map.getIrons(); Vector<Water> waters = map.getWaters(); for (int i = 0; i < bricks.size(); i++) { this.drawStuff(g, bricks.get(i), panel); } for (int i = 0; i < irons.size(); i++) { this.drawStuff(g, irons.get(i), panel); } for (int i = 0; i < waters.size(); i++) { this.drawStuff(g, waters.get(i), panel); } } /** * 畫出一個面朝北的坦克 * * @param g Graphics * @param tank 東西對象 * @param panel 被畫的那個面板 */ public void drawNorth(Graphics g, Tank tank, JPanel panel) { // int x = tank.getX(); // int y = tank.getY(); //0.設置畫筆顏色 // g.setColor(Color.white); // //1.畫出左邊的矩形 // g.fill3DRect(x - 20, y - 20, 10, // 40, false); // g.fill3DRect(x + 10, y - 20, 10, 40, false);//2.畫出右邊矩形 // //3.更換畫筆顏色 // g.setColor(tank.getColor()); //4.畫出輪子條紋 // for (int // i = 0; // i < 20 - 1; // i++) { // g.drawLine(x - 20, y - 20 + (i + 1) * 2, x - 10 - 1, y - 20 + (i + 1) * 2); // g.drawLine(x + 10, y - 20 + (i + 1) * 2, x + 20 - 1, y - 20 + (i + 1) * 2); // } //5.畫出中間1矩形 // g.fill3DRect(x - 15, y - 14, 30, 28, false); //6.更換畫筆顏色 // g.setColor(Color.white); //7.畫出中間2矩形 // g.draw3DRect(x - 10, y - 9, 20, // 18, false); //8.畫出中間3矩形 // g.draw3DRect(x - 3, y - 5, 6, 10, false); //9.畫直線 // g.drawLine(x - 15, y - 14, x - 10, y - 9); // g.drawLine(x + 15, y - 14, x + 10, y - 9); // g.drawLine(x - 15, y + 14, x - 10, y + 9); // g.drawLine(x + 15, y + 14, x + 10, y + 9); // g.setColor(tank.getColor()); //10.畫矩形 // g.fill3DRect(x - 3, y - 12, 6, 3, // false); // g.fill3DRect(x - 2, y - 20, 4, 2, false); // g.fill3DRect(x - 1, y - 20, // 2, 11, false); Image image; if (tank.getTankType() == TankTypeEnum.MY) { g.setColor(Color.green); image = TankGameImages.myTankImg[DirectionEnum.NORTH.getKey()];// 初始化圖片 } else { image = TankGameImages.enemyTankImg[DirectionEnum.NORTH.getKey()]; g.setColor(Color.gray); } g.drawImage(image, tank.getX() - 20, tank.getY() - 20, 40, 40, panel); g.fillRect(tank.getX() - 20, tank.getY() - 30, tank.getBlood() * 4, 5); } /** * 畫出一個方向朝南的坦克 * * @param g Graphics * @param tank 東西對象 * @param panel 被畫的那個面板 */ public void drawSouth(Graphics g, Tank tank, JPanel panel) { // int x = tank.getX(); // int y = tank.getY(); // g.setColor(Color.white); // g.fill3DRect(x - 20, y - 20, 10, 40, false); // g.fill3DRect(x + 10, y - 20, 10, // 40, false); // g.setColor(tank.getColor()); // for (int i = 0; i < 20 - 1; i++) { // g.drawLine(x - 20, y - 20 + (i + 1) * 2, x - 10 - 1, y - 20 + (i + 1) * 2); // g.drawLine(x + 10, // y - 20 + (i + 1) * 2, x + 20 - 1, y - 20 + (i + 1) * 2); // } // g.fill3DRect(x - 15, y - 14, 30, // 28, false); // g.setColor(Color.white); // g.draw3DRect(x - 10, y - 9, 20, // 18, false); // g.draw3DRect(x - 3, y - 5, 6, 10, false); // g.drawLine(x - 15, // y - 14, x - 10, y - 9); // g.drawLine(x + 15, y - 14, x + 10, y - 9); // g.drawLine(x - 15, // y + 14, x - 10, y + 9); // g.drawLine(x + 15, y + 14, x + 10, y + 9); // g.setColor(tank.getColor()); // g.fill3DRect(x - 3, y + 9, 6, 3, false); // g.fill3DRect(x - 1, y + 9, 2, 11, false); // g.fill3DRect(x - 2, y + 18, 4, 2, // false); Image image; if (tank.getTankType() == TankTypeEnum.MY) { g.setColor(Color.green); image = TankGameImages.myTankImg[DirectionEnum.SOUTH.getKey()];// 初始化圖片 } else { image = TankGameImages.enemyTankImg[DirectionEnum.SOUTH.getKey()]; g.setColor(Color.gray); } g.drawImage(image, tank.getX() - 20, tank.getY() - 20, 40, 40, panel); g.fillRect(tank.getX() - 20, tank.getY() - 30, tank.getBlood() * 4, 5); } /** * 畫出一個方向朝西的坦克 * * @param g Graphics * @param tank 東西對象 * @param panel 被畫的那個面板 */ public void drawWest(Graphics g, Tank tank, JPanel panel) { // int x = tank.getX(); // int y = tank.getY(); // g.setColor(Color.white); // g.fill3DRect(x - 20, y - 20, 40, 10, false); // g.fill3DRect(x - 20, y + 10, 40, // 10, false); // g.setColor(tank.getColor()); // for (int i = 0; i < 20 - 1; i++) { // g.drawLine(x - 20 + (i + 1) * 2, y - 20, x - 20 + (i + 1) * 2, y - 10 - 1); // g.drawLine(x - 20 + (i + 1) * 2, y - 20 + 30, x - 20 + (i + 1) * 2, y - 10 - 1 + 30); // } // g.fill3DRect(x - 14, y - 15, 28, 30, false); // g.setColor(Color.white); // g.draw3DRect(x - 9, y - 10, 18, 20, false); // g.draw3DRect(x - 5, y - 3, 10, // 6, false); // g.drawLine(x - 15, y - 14, x - 10, y - 9); // g.drawLine(x + 15, y - 14, // x + 10, y - 9); // g.drawLine(x - 15, y + 14, x - 10, y + 9); // g.drawLine(x + 15, y + 14, // x + 10, y + 9); // g.setColor(tank.getColor()); // g.fill3DRect(x - 12, y - 3, 3, // 6, false); // g.fill3DRect(x - 20, y - 1, 11, 2, false); // g.fill3DRect(x - 20, // y - 2, 2, 4, false); Image image; if (tank.getTankType() == TankTypeEnum.MY) { image = TankGameImages.myTankImg[DirectionEnum.WEST.getKey()];// 初始化圖片 g.setColor(Color.green); } else { image = TankGameImages.enemyTankImg[DirectionEnum.WEST.getKey()]; g.setColor(Color.gray); } g.drawImage(image, tank.getX() - 20, tank.getY() - 20, 40, 40, panel); g.fillRect(tank.getX() - 20, tank.getY() - 30, tank.getBlood() * 4, 5); } /** * 畫出一個方向朝東的坦克 * * @param g Graphics * @param tank 東西對象 * @param panel 被畫的那個面板 */ public void drawEast(Graphics g, Tank tank, JPanel panel) { // int x = tank.getX(); // int y = tank.getY(); // g.setColor(Color.white); // g.fill3DRect(x - 20, y - 20, 40, 10, false); // g.fill3DRect(x - 20, y + 10, 40, // 10, false); // g.setColor(tank.getColor()); // for (int i = 0; i < 20 - 1; i++) { // g.drawLine(x - 20 + (i + 1) * 2, y - 20, x - 20 + (i + 1) * 2, y - 10 - 1); // g.drawLine(x - 20 + (i + 1) * 2, y - 20 + 30, x - 20 + (i + 1) * 2, y - 10 - 1 + 30); // } // g.fill3DRect(x - 14, y - 15, 28, 30, false); // g.setColor(Color.white); // g.draw3DRect(x - 9, y - 10, 18, 20, false); // g.draw3DRect(x - 5, y - 3, 10, // 6, false); // g.drawLine(x - 15, y - 14, x - 10, y - 9); // g.drawLine(x + 15, y - 14, // x + 10, y - 9); // g.drawLine(x - 15, y + 14, x - 10, y + 9); // g.drawLine(x + 15, y + 14, // x + 10, y + 9); // g.setColor(tank.getColor()); // g.fill3DRect(x + 9, y - 3, 3, 6, // false); // g.fill3DRect(x + 9, y - 1, 11, 2, false); // g.fill3DRect(x + 18, y - 2, // 2, 4, false); Image image; if (tank.getTankType() == TankTypeEnum.MY) { image = TankGameImages.myTankImg[DirectionEnum.EAST.getKey()];// 初始化圖片 g.setColor(Color.green); } else { image = TankGameImages.enemyTankImg[DirectionEnum.EAST.getKey()]; g.setColor(Color.gray); } g.drawImage(image, tank.getX() - 20, tank.getY() - 20, 40, 40, panel); g.fillRect(tank.getX() - 20, tank.getY() - 30, tank.getBlood() * 4, 5); } /** * 畫出游戲右邊的那個面板 * * @param g Graphics * @param tgp 游戲主要面板對象 */ public void drawRight(Graphics g, GamePanel tgp, RealTimeGameData data) { if (data.getMapMakingFlag().equals(Boolean.TRUE)) { g.drawString("當前選中畫筆(可按C鍵切換)", 620, 20); if (data.getCurrentStuff() == StuffTypeEnum.IRON) { drawStuff(g, rightIron, tgp); } else if (data.getCurrentStuff() == StuffTypeEnum.BRICK) { drawStuff(g, rightBrick, tgp); } else if (data.getCurrentStuff() == StuffTypeEnum.WATER) { drawStuff(g, rightWater, tgp); } else { g.drawString("橡皮擦", 680, 50); } } else { for (int i = 0; i < data.getEnemyTankNum(); i++) { if (i >= 4) { g.drawImage(TankGameImages.enemyTankImg[DirectionEnum.NORTH.getKey()], 402 + 50 * i, 100, 40, 40, tgp); } else { g.drawImage(TankGameImages.enemyTankImg[DirectionEnum.NORTH.getKey()], 602 + 50 * i, 20, 40, 40, tgp); } } for (int j = 0; j < data.getMyTankNum(); j++) { g.drawImage(TankGameImages.myTankImg[DirectionEnum.NORTH.getKey()], 602 + 50 * j, 400, 40, 40, tgp); } g.drawString("我的坦克子彈數目:" + data.getMyBulletNum(), 620, 500); } } public void rePaintPanel(GamePanel panel, Graphics g) { RealTimeGameData data = context.getGameData(); if (data.isStart()) { g.setColor(Color.black); g.fillRect(0, 0, GameConstants.GAME_PANEL_WIDTH, GameConstants.GAME_PANEL_HEIGHT); g.fillRect(280, 600, 40, 40); this.drawMap(g, data.getMap(), panel); this.drawMyTank(g, data.getMyTanks(), panel); // 畫出我的坦克(包括子彈) this.drawEnemyTank(g, data.getEnemies(), panel); // 畫出敵人坦克(包括子彈) this.drawBomb(g, data.getBombs(), panel); // 畫出爆炸 this.drawRight(g, panel, data); if (data.getMyTankNum() == 0) { // 如果我的坦克數量為0 g.drawImage(TankGameImages.gameOver, 250, data.getDy(), 100, 100, panel); } if (data.getEnemyTankNum() == 0) { // 如果敵人坦克的數量為0 g.drawImage(TankGameImages.gameWin, 250, data.getDy(), 100, 100, panel); } if (data.getDy() == 250) { g.fillRect(0, 0, 800, 600); g.setColor(Color.BLUE); if (data.getMyTankNum() == 0) { g.drawString("失敗了!!!", 300, 220); } else { g.drawString("挑戰成功,請稍等...", 300, 220); } g.drawString( ("敵人坦克死亡數量:" + (8 - data.getEnemyTankNum())), 300, 260); g.drawString("我的坦克死亡總數量:" + data.getBeKilled(), 300, 280); g.drawString( "我的炮彈消耗總數量:" + (GameConstants.MY_TANK_INIT_BULLET_NUM - data .getMyBulletNum()), 300, 300); g.drawString("敵人坦克剩余數量:" + data.getEnemyTankNum(), 300, 320); g.drawString("我的坦克剩余總數量:" + data.getMyTankNum(), 300, 340); g.drawString("我的炮彈剩余總數量:" + data.getMyBulletNum(), 300, 360); } } else { g.drawImage(TankGameImages.startImage, 0, 0, 800, 700, panel); //g.drawImage(TankGameImages.font, 0, data.getKy(), panel); if (data.isIconSmile()) { //g.drawImage(TankGameImages.yctSmile1, data.getKx(), 45, // panel); data.setIconSmile(false); } else { //g.drawImage(TankGameImages.yctSmile2, data.getKx(), 45, // panel); data.setIconSmile(true); } } } }
坦克核心算法
@Service public class TankEventService { /** * 判斷坦克是否與另一個事物重疊 * * @param stuff 東西對象 * @param length 兩者之間的最短距離 * @return 是否重疊 */ public boolean isTankOverlap(Tank tank, Stuff stuff, int length) { boolean b = false; int x = stuff.getX(); int y = stuff.getY(); if (tank.getDirect() == DirectionEnum.NORTH) { tank.setY(tank.getY() - tank.getSpeed()); if (Math.abs(tank.getY() - y) < length && Math.abs(tank.getX() - x) < length) { b = true; tank.setY(tank.getY() + tank.getSpeed()); } else { tank.setY(tank.getY() + tank.getSpeed()); } } if (tank.getDirect() == DirectionEnum.SOUTH) { tank.setY(tank.getY() + tank.getSpeed()); if (Math.abs(tank.getY() - y) < length && Math.abs(tank.getX() - x) < length) { b = true; } tank.setY(tank.getY() - tank.getSpeed()); } if (tank.getDirect() == DirectionEnum.EAST) { tank.setX(tank.getX() + tank.getSpeed()); if (Math.abs(tank.getY() - y) < length && Math.abs(tank.getX() - x) < length) { b = true; } tank.setX(tank.getX() - tank.getSpeed()); } if (tank.getDirect() == DirectionEnum.WEST) { tank.setX(tank.getX() - tank.getSpeed()); if (Math.abs(tank.getY() - y) < length && Math.abs(tank.getX() - x) < length) { b = true; } tank.setX(tank.getX() + tank.getSpeed()); } return b; } /** * 判斷是否重疊 * * @param enemies 敵人坦克容量 * @return 是否重疊 */ public boolean isMyTankOverlap(MyTank tank, Vector<EnemyTank> enemies) { for (int i = 0; i < enemies.size(); i++) { if (isTankOverlap(tank, enemies.get(i), 40)) return true; } return false; } /** * 判斷自己跟別的坦克是否重疊 * * @param enemies 敵人坦克容量 * @param myTanks 我的坦克容量 * @return 是否重疊 */ public boolean isEnemyTankOverlap(EnemyTank enemy, Vector<EnemyTank> enemies, Vector<MyTank> myTanks) { for (int i = 0; i < enemies.size(); i++) { if (enemy != enemies.get(i)) { if (isTankOverlap(enemy, enemies.get(i), 40)) { enemy.setOverlapNo(true); return true; } } } for (int j = 0; j < myTanks.size(); j++) { if (isTankOverlap(enemy, myTanks.get(j), 40)) { enemy.setOverlapYes(true); return true; } } enemy.setOverlapNo(false); enemy.setOverlapYes(false); return false; } /** * 每隔36毫秒 一直向西走 */ public void enemyGoWest(EnemyTank enemy) { for (; ; ) { GameTimeUnit.sleepMillis(36); if (!enemy.isOverlapNo()&& !enemy.isOverlapYes()) { enemy.goWest(); } if (enemy.getMyTankLocation() != DirectionEnum.WEST) { enemy.setDirect(enemy.getMyTankDirect()); break; } } } /** * 每隔36毫秒 一直向東走 */ public void enemyGoEast(EnemyTank enemy) { for (; ; ) { GameTimeUnit.sleepMillis(36); if (!enemy.isOverlapNo() && !enemy.isOverlapYes()) { enemy.goEast(); } if (enemy.getMyTankLocation() != DirectionEnum.EAST) { enemy.setDirect(enemy.getMyTankDirect()); break; } } } /** * 每隔36毫秒 一直向北走 */ public void enemyGoNorth(EnemyTank enemy) { for (; ; ) { GameTimeUnit.sleepMillis(36); if (!enemy.isOverlapNo() && !enemy.isOverlapYes()) { enemy.goNorth(); } if (enemy.getMyTankLocation() != DirectionEnum.NORTH) { enemy.setDirect(enemy.getMyTankDirect()); break; } } } /** * 每隔36毫秒 一直向南走 */ public void enemyGoSouth(EnemyTank enemy) { for (; ; ) { GameTimeUnit.sleepMillis(36); if (!enemy.isOverlapNo() && !enemy.isOverlapYes()) { enemy.goSouth(); } if (enemy.getMyTankLocation() != DirectionEnum.SOUTH) { enemy.setDirect(enemy.getMyTankDirect()); break; } } } /** * 從指定的三個方向中隨機選擇一個 * * @param direct1 方向1 * @param direct2 方向2 * @param direct3 方向3 */ public DirectionEnum enemyGetRandomDirect(DirectionEnum direct1, DirectionEnum direct2, DirectionEnum direct3) { int random = (int) (Math.random() * 3); DirectionEnum returnDirect = DirectionEnum.INVALID; switch (random) { case 0: returnDirect = direct1; break; case 1: returnDirect = direct2; break; case 2: returnDirect = direct3; break; } return returnDirect; } /** * 讓敵人坦克能夠發現我的坦克并開炮 * * @param myTank 我的坦克 * @param map 地圖對象 */ public void enemyFindAndKill(EnemyTank enemy, MyTank myTank, Map map) { int myX = myTank.getX(); int myY = myTank.getY(); int enX = enemy.getX(); int enY = enemy.getY(); if (Math.abs(myX - enX) < 20 && myY <= 580) { if (enY < myY) { int s = 0; for (int t = 0; t < map.getIrons().size(); t++) { Iron iron = map.getIrons().get(t); if (Math.abs(enX - iron.getX()) <= 10 && iron.getY() > enY && iron.getY() < myY) { s = 1; break; } } if (s == 0) { enemy.setShot(true); enemy.setMyTankLocation(DirectionEnum.SOUTH); } } else { int s = 0; for (int t = 0; t < map.getIrons().size(); t++) { Iron iron = map.getIrons().get(t); if (Math.abs(enX - iron.getX()) <= 10 && iron.getY() < enY && iron.getY() > myY) { s = 1; break; } } if (s == 0) { enemy.setShot(true); enemy.setMyTankLocation(DirectionEnum.NORTH); } } } else if (Math.abs(myY - enY) < 20 && myY <= 580) { if (enX > myX) { int s = 0; for (int t = 0; t < map.getIrons().size(); t++) { Iron iron = map.getIrons().get(t); if (Math.abs(enY - iron.getY()) <= 10 && iron.getX() < enX && iron.getX() > myX) { s = 1; break; } } if (s == 0) { enemy.setShot(true); enemy.setMyTankLocation(DirectionEnum.WEST); } } else { int s = 0; for (int t = 0; t < map.getIrons().size(); t++) { Iron iron = map.getIrons().get(t); if (Math.abs(enY - iron.getY()) <= 10 && iron.getX() > enX && iron.getX() < myX) { s = 1; break; } } if (s == 0) { enemy.setShot(true); enemy.setMyTankLocation(DirectionEnum.EAST); } } } else { enemy.setShot(false); enemy.setMyTankLocation(DirectionEnum.INVALID); } } }
關于“怎么用JAVA實現經典游戲坦克大戰”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識,可以關注億速云行業資訊頻道,小編每天都會為大家更新不同的知識點。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。