實現了基本功能....地方坦克尋路做得比較弱智,每個坦克最多能同時發射5發砲彈....敵方有三種兵種,菜單欄沒做響應,這個很簡單,需要的可以自己加......上傳全部程式碼,僅供學習參考......
- package tank.common;
-
- abstract public class Bullet implements Runnable, Common {
- private int x, y;
- private int; > private int direction;
- private boolean alive;
- protected int power;
-
- public Bullet(int x, int y, int speed, int direction) {
- this.x = x;
- this.y = y;
- this.speed = speed;
- this.direction = direction;
- alive = true;
- }
-
- public void true;
- }
- alive = false;
- }
-
- public int getPower() {
- return power;
- }
-
- public int getX() {
- return x ;
- }
-
- public int getY() {
- return y;
- }
-
- public boolean isAlive() {
- return alive;
- }
-
- public void run() {
- while (true) {
- try {
- Thread.sleep(15);
- } catch (InterruptedException e) {
- // TO Auto-generated catch block
- e.printStackTrace();
- }
- switch (direction) {
- case UP:
- y -= speed;
- break;
- case DO🎜> case WN :
- y += speed;
- break;
- case RIGHT:
- x += speed;
- break;
- case LEFT:
- x -= speed;
- break;
- }
- if (x WIDTH || y > HEIGHT || y alive = false;
- return;
- }
- }
- }
-
- }
複製程式碼
-
- package tank.common;
-
- public interface Common {
- public static final int UP = 0;
- public static final int public static final int RIGHT = 2;
- public static final int LEFT = 3;
- public static final int WIDTH = 600;
- public static final int intIGHT = 400 int HEIGHT WATER = 0;
- public static final int WALLS = 1;
- public static final int STEELS = 2;
- public static final int GRASS = 3;
-
- public static final int GRASS = 3;
-
- }
複製程式碼複製程式碼複製程式碼複製程式碼複製程式碼複製程式碼
- 包tank.common;
-
- 導入java.awt.Color;
- 導入java.util.ArrayList;
-
- 導入tank. .NormalBullet;
-
- 公共抽象類別Tank 實作Common {
- protected int x;
-
- protected int y;
-
- private Color color;
- private int 方向;
- private ArrayList;子彈;
-
- private ArrayList; ;
-
- private int maxBulletNum;
-
- private boolean alive;
-
- protected int life;
-
- ArList
- {
- 速度= 2;
- 方向=上;
- 活= true;
- }
- public Tank() {
- this
- public Tank() {
- this
- public Tank() {
- this
- this .x = 0;
- this.y = 0;
- color = Color.black;
- }
- public Tank(int x, int y , 顏色顏色) {
- this .x = x;
- this.y = y;
- this.color = color;
- maxBulletNum = 5;
- 項目符號= new ArrayList( );
- wall = new ArrayList();
- }
-
- protected boolean canMove(int x, int y) {
- if (x )寬度- 20 || y 高度- 20) {
- 回傳false;
- }
-
- if (坦克== null) {
- 回傳true;
- }
-
- if (tanks.size () == 1) {
- return true;
- }
-
- for (int i = 0; i 牆壁tempWall = wall .get(i);
- if (tempWall.isAlive()) {
- if (x >= tempWall.getX() && y > ;= tempWall.getY()) {
- if (x return tempWall.canBeWalk();
- }
- } else if (x >= tempWall.getX() && y if (x && (y + 20) >= tempWall.getY()) {
- return tempWall. canBeWalk();
- }
- } else if (x ; = tempWall.getY()) {
- if ((x + 20) >= tempWall.getX ()
- && y 回傳tempWall.canBeWalk ();
- }
- } else if (x if ((x + 20) >= tempWall. getX()
- && (y + 20) >= tempWall.getY()) {
- return tempWall.canBeWalk ();
- }
- }
- }
- }
-
- for (int i = 0;我 Tank tempTank =坦克.get(i);
- if (tempTank == this)
- break;
- if (tempTank.isAlive()) {
- if ( x >= tempTank.getX() && y >= tempTank.getY()) {
- if (x return false;
- }
- } else if (x >= tempTank.getX() && y if (x && (y + 20) >= tempTank .getY()) {
- return false;
- }
- } else if (x = tempTank.getY()) {
- if ((x + 20) >= tempTank.getX()
- && y return false;
- }
- } else if (x if ((x + 20) >= tempTank. getX()
- && (y + 20) >= tempTank.getY()) {
- 回傳false;
- }
- }
- }
- }
- 回傳true;
- }
-
- public void Damage(int power) {
- lifes -= power;
- if (lifes live = false;
- }
- }
-
- public ArrayList; getBullet() {
- 回傳項目符號;
- }
-
- public Color getColor() {
- 回傳顏色;
- }
-
- public int getDirection() {
- 返回方向🎜> }
-
- public int getImageID() {
- return TankImageID;
- }
-
- public int getSpeed() {
- 回傳速度;
- }
- Wall>; getWalls() {
- return this.walls;
- }
-
- public int getX() {
- return x;
- }
-
- public int getY () {
- return y;
- }
-
- public boolean isAlive() {
- 回傳活;
- }
-
- public void move(int Direction) {
- setDirection (方向);
- int x = this.x;
- int y = this.y;
- switch (方向) {
- case UP:
- y -= speed;
- 中斷;
- 案例向下:
- y += 速度;
- 中斷;
- 案例右:
- x += 速度;
- 中斷;
- 案例向左:
- x - = 速度;
- 中斷;
- }
- if (canMove(x, y)) {
- this.x = x;
- this.y = y;
- }
- }
-
- public void setAllTanks(ArrayList坦克) {
- this.tanks =坦克;
- }
-
- public void setDirection(int Direction) {
- this.方向= 方向;
- }
-
- Final protected void setImageID(int ID) {
- this.tankImageID = ID;
- }
-
- public void setSpeed(int speed) {
-
- public void setSpeed(int speed) {
- this.speed ;
- }
-
- public void setWalls(ArrayList wallList) {
- this.walls = wallList;
- }
-
- public voidshot() {
- switch (方向) {
- case UP:
- bullet = new NormalBullet(x + 10, y, UP);
- break;
- case DOWN:
- bullet = new NormalBullet(x + 1000 ) ) , y + 20, 迭代);
- 中斷;
- 案例右:
- 項目符號 = new NormalBullet(x + 20, y + 10, 右);
- 中斷;
- 案例左:
- Bullet = new NormalBullet(x, y + 10, LEFT);
- break;
- }
- for (int i = 0; i Bullet temp =Bullets.get(i);
- if (!temp.isAlive()) {
- Bullets.remove(temp);
- }
- }
- if (temp);
- }
- }
- if (temp);
- }
- }
- if (temp);
- }
} if (temp); } } if (temp); } } if (temp);- }
- }
- if (temp);
- }
- }
- if(子彈. size() >= maxBulletNum) {
-
- } else {
- new Thread(bullet).start();
- bullets.add(bullet);
- }
- }
-
- }
- }
-
-
-
- 複製程式碼
-
-
-
-
-
- package Tank.common;
-
- 公共抽象類別 Wall {
- private int x, y;
-
- private int wallImageID;
- 火災布林值canWalk;
-
- 火災布林值canFly;
-
- 火災布林值alive;
-
- 火災布林值canHit;
-
-
- 火災布林值(Hit;
-
- this.x = x;
- this.y = y;
- this.wallImageID = ID;
- this .canWalk = walk ;
- this.canFly = 飛;
- this.alive = true;
- this.canHit = Hit;
- }
-
- public boolean canBeFly() {
-
- public boolean canBeFly() {
- 返回canFly;
- }
-
- public boolean canBeHit() {
- return this.canHit;
}
public boolean canBeWalk() { return can🎜> public boolean canBeWalk() { return can🎜>
public void die() { alive = false;- }
-
- public int getImageID() {
- return wallImageID
- }
- > public int getX() {
- return x;
- }
-
- public int getY() {
- return y;
- }
-
- public boolean isAlive() {
- 活回;
- }
-
- }
-
-
-
- 複製程式碼
-
-
-
-
-
- package Tank.entity;
-
- public class Bomb {
- private int x;
- private int y;
-
- private boolean alive;
-
- public Bomb(int x, int y) {
- this.x = x;
- this.y = y;
- alive = true;
- }
-
public void decrese() { if (life > 0) { life--; } else { alive = false ; } }
- public int getLife() {
- return life;
- }
-
- public int getX() {
- return x; > }
-
- public int getY() {
- return y;
- }
-
- public boolean isAlive() {
- 回傳活;
- }
複製程式碼
-
- 包tank.entity;
-
- 導入tank.common.EnemyTank;
- 導入tank.common.Tank;
-
- 公共擴充類EnemyTank {
-
- public EnemyTank1(int x, int y, 坦克英雄) {
- super(x, y, Hero, 3, 1);
- setSpeed(2);
- setShotSpeed( 0.8);
- }
}
複製程式碼
-
-
- 包tank.entity;
-
- 導入tank.common.EnemyTank;
- 導入tank.common.Tank;
-
- 公共擴充類EnemyTank {
- public EnemyTank2(int x, int y, 坦克英雄) {
- super(x, y, Hero, 5, 2);
- setSpeed(3);
- setShotSpeed(0.5);
- }
}
複製程式碼
包tank.entity;導入tank.common.EnemyTank;導入tank.common.Tank;公共擴充類EnemyTank { public EnemyTank3(int x, int y, 坦克英雄) { super(x, y, Hero, 10, 3); setSpeed(5); setShotSpeed(0.2); }}複製程式碼
- 包tank.entity;
-
- 導入tank.common.Wall;
-
- public class Grass extends Wall {
- public Grass(int x , int y) {
- super(x, y, 3, true, true, false);
- }
- }
-
複製程式碼
複製程式碼
-
-
- 複製程式碼
-
-
-
- 複製程式碼
-
-
-
- >
-
-
-
- 包tank.entity;
-
- 導入java.awt.Color;
-
- 導入tank.common.Tank;
導入tank.common.Tank;
publicpublic> class MyTank extends Tank { public MyTank(int x, int y) { super(x, y, Color.yellow);- lifes = 5;
- super.setImageID(0);
- }
-
- public int getLife() {
- return life;
- }
-
- }
-
-
-
- }
複製程式碼🎜>
包tank.entity;-
- 導入tank.common.Bullet;
-
- 公用類別NormalBulletpideBullet {
-
- 公用類別NormalBullet kallet {
- super(x, y, 2, 方向);
- power = 1;
- }
-
- }
-
-
-
- 複製程式碼
-
-
-
-
-
- 包tank.entity;
-
- 導入java.util.ArrayList;
-
- 導入tank.common.EnemyTank
- public
- 導入tank.common.EnemyTank
- public private int totalEnemyNum;
- private int leftEnemyNum;
- private int totalHeroLife;
- private int leftHeroLife; private ArrayList< ;敵方戰車> enemeyTanks;
- private int activeEnemyTankNum;
- private EnemyTank activeEnemyTanks[];
- private MyTank Hero; this.totalEnemyNum =totalEnemyNum;
- this.totalHeroLife =totalHeroLife;
- this.level = level;
- this.activeEnemypank. EnemyNum;
- this.leaveHeroLife = this.totalHeroLife;
- this.enemeyTanks = new ArrayList();
- this.activeEnemyTanks = new EnemyTank[this.activeEnemyTank> (290, 370);
- this.wallContainer = new WallContainer();
- }
-
- public void addEnemyTank(EnemyTank Tank) {
- enem *TeyT.addok) };
-
- public void autoCreateEnemyTank() {
- for (int i = 0; i double key = Math.random();
- if (key this.enemeyTanks.add(new EnemyTank1(0, 0, Hero));
- } else if (key >= 0.66) {
- this.enemeyTanks.add>(new EnemyTank2(new EnemyT)2(new) ) , 0, 英雄));
- } else {
- this.enemeyTanks.add(new EnemyTank3(0, 0, 英雄));
- }
- }
- }
- }
-
- public int getActiveEnemyTankNum() {
- return this.activeEnemyTankNum;
- }
-
- public EnemyTank[] getEnemyTank() { 🎜 > public MyTank getHeroTank() {
- return this.hero;
- }
-
- public int getLeaveEnemyNum() {
- return this.leaveEnemyNum }
- public int getLeaveHeroLife() {
- return this.leaveHeroLife;
- }
-
- public int getLevel() {
- return this.level;
- }
- } return this.wallContainer;
- }
-
- public boolean isHeroDead() {
- if (this.leaveHeroLife return true; } else
- return false;
- }
-
- public EnemyTank popEnemyTank() {
- if (leaveEnemyNum >; 0) {
- . enemeyTanks.get(enemeyTanks.size() - 1);
- enemeyTanks.remove(temp);
- return temp;
- } else
- return null public MyTank popHero() {
- leftHeroLife--;
- MyTank temp = new MyTank(290, 370);
- temp. setWalls(wallContainer.getWallList());
- temp. setWalls(wallContainer.getWallList());
- return temp; }
-
- public void setHeroTank(MyTank Tank) {
- this.hero = Tank;
- }
}
}
- 複製程式碼
- 🎜>
-
-
-
- 包tank.entity;
-
- 導入tank.common.Wall;
-
- public class Steels extends Wall {
public Steels(int x 。 >
- 包tank.entity;
-
- 導入java.util.ArrayList;
-
- 導入tank.common.Common;
- >tank.common .Wall;
-
- 公共類別WallContainer 實作Common {
- private ArrayList; data;
-
- public WallContainer() {
- data = new ArrayList();
- for (int i = 0; i this . addWall(10 + i * 20, 100, 水);
- if (i == 11) {
- i += 4;
- }
- }
-
- for (int i = 0; i this.addWall(10 + i * 20, 160, 牆);
- if (i == 12) {
- i += 4 ;
- }
- }
-
- for (int i = 0; i this.addWall(10 + i * 20, 220, STEELS);
- if (i == 11) {
- i += 4;
- }
- }
-
- for (int i = 0; i this.addWall(10 + i * 20, 280, GRASS);
- if (i == 12) {
- i += 4;
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
-
- public void addWall(int x, int y, int kind) {
- switch (kind) {
- case WATER:
- data.add(new Water(x, y));
- 打破;
- 案例牆:
- data.add(new Walls(x, y));
- 打破;
- 案例鋼鐵:
- data.add(new Steels(x, y) ) ));
- 中斷;
- case GRASS:
- data.add(new Grass(x, y));
- 中斷;
- }
- }
-
- public ArrayList getWallList() {
- ArrayList; temp = data;
- return temp;
- }
-
- public boolean isEmpty() {
- return data.isEmpty();
- }
public void🎜 ) { for (int i = 0; i 牆壁溫度= data.get(i); if (!temp.isAlive() ) data.remove(temp); } } }-
-
-
- 複製程式碼
-
-
-
-
-
- package Tank.entity;
導入 Tank.common.Wall; public class Walls extends Wall { public Walls(int x 。 >
- 包tank.entity;
-
- 導入tank.common.Wall;
-
- public class Water extends Wall {
- public Water(inte x 。 >
-
-
-
- 套件tank.gui;
導入javax.swing.JFrame; 導入javax.swing.JMenu;導入javax.swing.JMenu;Barswing.JMenu; import javax.swing.JMenuItem; public class TankFrame extends JFrame {
- /**
- *
- */
- private static final final long serialfored; private TankPanel gamePanel;
-
- public TankFrame() {
- super("坦克大戰————玄雨製作");
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);添加遊戲主面板
- gamePanel = new TankPanel();
- this.add(gamePanel);
- gamePanel.addMouseListener(gamePanel);
- this.addKeyListener(gamePanel);
-
- // 新增選單列
- JMenuBar menuBar = new JMenuBar();
- // ///////////////////////////// // // //
- JMenu menu1 = new JMenu("選單");
- menuBar.add(menu1);
- JMenuItem itemNewGame = new JMenuItem("新遊戲");
- menu1. add( itemNewGame) ;
- menu1.addSeparator();
- JMenuItem itemList = new JMenuItem("排行榜");
- menu1.add(itemList);
- menu1. JMenuItem itemExit = new JMenuItem("退出");
- menu1.add(itemExit);
- // /////////////////////// //// //// ///////
- JMenu menu2 = new JMenu("設定");
- JMenuItem itemSet = new JMenuItem("設定");
- menu2.add( itemSet);
- 選單列。 add(menu2);
- // /////////////////////////////////
- JMenu menu3 = new JMenu("幫助");
- menuBar.add(menu3);
- JMenuItem itemInfo = new JMenuItem("關於");
- menu3.add(itemInfo);
- JMenuItem itemHelp = new JMenux "幫助");
- menu3.add(itemHelp);
- this.setJMenuBar(menuBar);
-
- this.setResizable(false);
- this.pack();
- this.setVisible(true);
- }
-
- }
-
-
-
- 複製程式碼
-
- 包tank.start;
-
- 導入tank.gui.TankFrame;
-
- 公共類TankStart {
-
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO 自動生成的方法存根
- new TankFrame().setLocation(250, 150);
- }
-
- }
-
複製程式碼
|