实现了基本功能....地方坦克寻路做得比较弱智,每个坦克最多能同时发射5发炮弹....敌方有三种兵种,菜单栏没做响应,这个很简单,需要的可以自己加......上传全部代码,仅供学习参考......
- package tank.common;
-
- abstract public class Bullet implements Runnable, Common {
- private int x, y;
- private int speed;
- 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 die() {
- 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) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- switch (direction) {
- case UP:
- y -= speed;
- break;
- case DOWN:
- 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 DOWN = 1;
- public static final int RIGHT = 2;
- public static final int LEFT = 3;
- public static final int WIDTH = 600;
- public static final int HEIGHT = 400;
- public static final int WATER = 0;
- public static final int WALLS = 1;
- public static final int STEELS = 2;
- public static final int GRASS = 3;
-
- }
-
复制代码
- package tank.common;
-
- import java.awt.Color;
- import java.util.ArrayList;
-
- import tank.entity.NormalBullet;
-
- public abstract class Tank implements Common {
- protected int x;
-
- protected int y;
-
- private Color color;
-
- private int speed;
-
- private int direction;
-
- private ArrayList bullets;
-
- private ArrayList tanks;
-
- private Bullet bullet;
-
- private int maxBulletNum;
-
- private boolean alive;
-
- protected int lifes;
-
- ArrayList walls;
-
- private int tankImageID;
-
- {
- speed = 2;
- direction = UP;
- alive = true;
- }
-
- public Tank() {
- this.x = 0;
- this.y = 0;
- color = Color.black;
- }
-
- public Tank(int x, int y, Color color) {
- this.x = x;
- this.y = y;
- this.color = color;
- maxBulletNum = 5;
- bullets = new ArrayList();
- walls = new ArrayList();
- }
-
- protected boolean canMove(int x, int y) {
- if (x WIDTH - 20 || y HEIGHT - 20) {
- return false;
- }
-
- if (tanks == null) {
- return true;
- }
-
- if (tanks.size() == 1) {
- return true;
- }
-
- for (int i = 0; i Wall tempWall = walls.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 return tempWall.canBeWalk();
- }
- } else if (x if ((x + 20) >= tempWall.getX()
- && (y + 20) >= tempWall.getY()) {
- return tempWall.canBeWalk();
- }
- }
- }
- }
-
- for (int i = 0; i Tank tempTank = tanks.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()) {
- return false;
- }
- }
- }
- }
- return true;
- }
-
- public void damage(int power) {
- lifes -= power;
- if (lifes alive = false;
- }
- }
-
- public ArrayList getBullet() {
- return bullets;
- }
-
- public Color getColor() {
- return color;
- }
-
- public int getDirection() {
- return direction;
- }
-
- public int getImageID() {
- return tankImageID;
- }
-
- public int getSpeed() {
- return speed;
- }
-
- public ArrayList getWalls() {
- return this.walls;
- }
-
- public int getX() {
- return x;
- }
-
- public int getY() {
- return y;
- }
-
- public boolean isAlive() {
- return alive;
- }
-
- public void move(int direction) {
- setDirection(direction);
- int x = this.x;
- int y = this.y;
- switch (direction) {
- case UP:
- y -= speed;
- break;
- case DOWN:
- y += speed;
- break;
- case RIGHT:
- x += speed;
- break;
- case LEFT:
- x -= speed;
- break;
- }
- if (canMove(x, y)) {
- this.x = x;
- this.y = y;
- }
- }
-
- public void setAllTanks(ArrayList tanks) {
- this.tanks = tanks;
- }
-
- public void setDirection(int direction) {
- this.direction = direction;
- }
-
- final protected void setImageID(int ID) {
- this.tankImageID = ID;
- }
-
- public void setSpeed(int speed) {
- this.speed = speed;
- }
-
- public void setWalls(ArrayList wallList) {
- this.walls = wallList;
- }
-
- public void shot() {
- switch (direction) {
- case UP:
- bullet = new NormalBullet(x + 10, y, UP);
- break;
- case DOWN:
- bullet = new NormalBullet(x + 10, y + 20, DOWN);
- break;
- case RIGHT:
- bullet = new NormalBullet(x + 20, y + 10, RIGHT);
- break;
- case LEFT:
- 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 (bullets.size() >= maxBulletNum) {
-
- } else {
- new Thread(bullet).start();
- bullets.add(bullet);
- }
-
- }
- }
复制代码
- package tank.common;
-
- public abstract class Wall {
- private int x, y;
-
- private int wallImageID;
-
- private boolean canWalk;
-
- private boolean canFly;
-
- private boolean alive;
-
- private boolean canHit;
-
- public Wall(int x, int y, int ID, boolean walk, boolean fly, boolean Hit) {
- this.x = x;
- this.y = y;
- this.wallImageID = ID;
- this.canWalk = walk;
- this.canFly = fly;
- this.alive = true;
- this.canHit = Hit;
- }
-
- public boolean canBeFly() {
- return canFly;
- }
-
- public boolean canBeHit() {
- return this.canHit;
- }
-
- public boolean canBeWalk() {
- return canWalk;
- }
-
- public void die() {
- alive = false;
- }
-
- public int getImageID() {
- return wallImageID;
- }
-
- public int getX() {
- return x;
- }
-
- public int getY() {
- return y;
- }
-
- public boolean isAlive() {
- return alive;
- }
-
- }
-
复制代码
- package tank.entity;
-
- public class Bomb {
- private int x;
- private int y;
-
- private int life = 9;
-
- 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() {
- return alive;
- }
-
- }
-
复制代码
- package tank.entity;
-
- import tank.common.EnemyTank;
- import tank.common.Tank;
-
- public class EnemyTank1 extends EnemyTank {
-
- public EnemyTank1(int x, int y, Tank hero) {
- super(x, y, hero, 3, 1);
- setSpeed(2);
- setShotSpeed(0.8);
- }
- }
-
复制代码
- package tank.entity;
-
- import tank.common.EnemyTank;
- import tank.common.Tank;
-
- public class EnemyTank2 extends EnemyTank {
- public EnemyTank2(int x, int y, Tank hero) {
- super(x, y, hero, 5, 2);
- setSpeed(3);
- setShotSpeed(0.5);
- }
- }
-
复制代码
- package tank.entity;
-
- import tank.common.EnemyTank;
- import tank.common.Tank;
-
- public class EnemyTank3 extends EnemyTank {
- public EnemyTank3(int x, int y, Tank hero) {
- super(x, y, hero, 10, 3);
- setSpeed(5);
- setShotSpeed(0.2);
- }
- }
-
复制代码
- package tank.entity;
-
- import tank.common.Wall;
-
- public class Grass extends Wall {
- public Grass(int x, int y) {
- super(x, y, 3, true, true, false);
- }
- }
-
复制代码
- package tank.entity;
-
- import java.awt.Color;
-
- import tank.common.Tank;
-
- public class MyTank extends Tank {
- public MyTank(int x, int y) {
- super(x, y, Color.yellow);
- lifes = 5;
- super.setImageID(0);
- }
-
- public int getLife() {
- return lifes;
- }
-
- }
-
复制代码
- package tank.entity;
-
- import tank.common.Bullet;
-
- public class NormalBullet extends Bullet {
-
- public NormalBullet(int x, int y, int direction) {
- super(x, y, 2, direction);
- power = 1;
- }
-
- }
-
复制代码
- package tank.entity;
-
- import java.util.ArrayList;
-
- import tank.common.EnemyTank;
-
- public class Stage {
- private int totalEnemyNum;
- private int leaveEnemyNum;
- private int totalHeroLife;
- private int leaveHeroLife;
- private int level;
- private WallContainer wallContainer;
- private ArrayList enemeyTanks;
- private int activeEnemyTankNum;
- private EnemyTank activeEnemyTanks[];
- private MyTank hero;
-
- public Stage(int totalEnemyNum, int totalHeroLife, int level,
- int activeEnemyTankNum) {
- this.totalEnemyNum = totalEnemyNum;
- this.totalHeroLife = totalHeroLife;
- this.level = level;
- this.activeEnemyTankNum = activeEnemyTankNum;
- this.leaveEnemyNum = this.totalEnemyNum;
- this.leaveHeroLife = this.totalHeroLife;
- this.enemeyTanks = new ArrayList();
- this.activeEnemyTanks = new EnemyTank[this.activeEnemyTankNum];
- this.hero = new MyTank(290, 370);
- this.wallContainer = new WallContainer();
- }
-
- public void addEnemyTank(EnemyTank tank) {
- enemeyTanks.add(tank);
- }
-
- 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(0, 0, hero));
- } else {
- this.enemeyTanks.add(new EnemyTank3(0, 0, hero));
- }
- }
- }
-
- public int getActiveEnemyTankNum() {
- return this.activeEnemyTankNum;
- }
-
- public EnemyTank[] getEnemyTank() {
- return this.activeEnemyTanks;
- }
-
- public MyTank getHeroTank() {
- return this.hero;
- }
-
- public int getLeaveEnemyNum() {
- return this.leaveEnemyNum;
- }
-
- public int getLeaveHeroLife() {
- return this.leaveHeroLife;
- }
-
- public int getLevel() {
- return this.level;
- }
-
- public WallContainer getWallContainer() {
- return this.wallContainer;
- }
-
- public boolean isHeroDead() {
- if (this.leaveHeroLife return true;
- } else
- return false;
- }
-
- public EnemyTank popEnemyTank() {
- if (leaveEnemyNum > 0) {
- this.leaveEnemyNum--;
- EnemyTank temp = enemeyTanks.get(enemeyTanks.size() - 1);
- enemeyTanks.remove(temp);
- return temp;
- } else
- return null;
- }
-
- public MyTank popHero() {
- leaveHeroLife--;
- MyTank temp = new MyTank(290, 370);
- temp.setWalls(wallContainer.getWallList());
- return temp;
- }
-
- public void setHeroTank(MyTank tank) {
- this.hero = tank;
- }
- }
复制代码
- package tank.entity;
-
- import tank.common.Wall;
-
- public class Steels extends Wall {
- public Steels(int x, int y) {
- super(x, y, 2, false, false, false);
- }
- }
-
复制代码
- package tank.entity;
-
- import java.util.ArrayList;
-
- import tank.common.Common;
- import tank.common.Wall;
-
- public class WallContainer implements Common {
- private ArrayList data;
-
- public WallContainer() {
- data = new ArrayList();
- for (int i = 0; i this.addWall(10 + i * 20, 100, WATER);
- if (i == 11) {
- i += 4;
- }
- }
-
- for (int i = 0; i this.addWall(10 + i * 20, 160, WALLS);
- 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));
- break;
- case WALLS:
- data.add(new Walls(x, y));
- break;
- case STEELS:
- data.add(new Steels(x, y));
- break;
- case GRASS:
- data.add(new Grass(x, y));
- break;
- }
- }
-
- public ArrayList getWallList() {
- ArrayList temp = data;
- return temp;
- }
-
- public boolean isEmpty() {
- return data.isEmpty();
- }
-
- public void removeDead() {
- for (int i = 0; i Wall temp = data.get(i);
- if (!temp.isAlive())
- data.remove(temp);
- }
- }
- }
复制代码
- package tank.entity;
-
- import tank.common.Wall;
-
- public class Walls extends Wall {
- public Walls(int x, int y) {
- super(x, y, 1, false, false, true);
- }
- }
-
复制代码
- package tank.entity;
-
- import tank.common.Wall;
-
- public class Water extends Wall {
- public Water(int x, int y) {
- super(x, y, 0, false, true, false);
- }
- }
-
复制代码
- package tank.gui;
-
- import javax.swing.JFrame;
- import javax.swing.JMenu;
- import javax.swing.JMenuBar;
- import javax.swing.JMenuItem;
-
- public class TankFrame extends JFrame {
-
- /**
- *
- */
- private static final long serialVersionUID = 1L;
- 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.addSeparator();
- JMenuItem itemExit = new JMenuItem("退出");
- menu1.add(itemExit);
- // //////////////////////////////////
- JMenu menu2 = new JMenu("设置");
- JMenuItem itemSet = new JMenuItem("设置");
- menu2.add(itemSet);
- menuBar.add(menu2);
- // /////////////////////////////////
- JMenu menu3 = new JMenu("帮助");
- menuBar.add(menu3);
- JMenuItem itemInfo = new JMenuItem("关于");
- menu3.add(itemInfo);
- JMenuItem itemHelp = new JMenuItem("帮助");
- menu3.add(itemHelp);
- this.setJMenuBar(menuBar);
-
- this.setResizable(false);
- this.pack();
- this.setVisible(true);
- }
-
- }
-
复制代码
- package tank.start;
-
- import tank.gui.TankFrame;
-
- public class TankStart {
-
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- new TankFrame().setLocation(250, 150);
- }
-
- }
-
复制代码
|