Java 坦克大戰單機版 源代碼

openkk 12年前發布 | 128K 次閱讀 Java Java開發

玩法:

方向鍵:控制走動

Ctrl:控制發彈

A:超級子彈

X:八方向發彈

=:添加敵方坦克

F2:重新開始

其他功能:

在頭上實時顯示血條

吃到血塊時能補血

自動積分

敵方坦克過少時自動添加

收獲:

系統復習了J2SE的基本上所有內容

對軟件工程的理解進一步加深

對面向對象的思想及其優點進一步加深

熟悉了eclipse的使用,包括打包發布以及Doc的生成Java 坦克大戰單機版 源代碼

Java 坦克大戰單機版 源代碼

 

源代碼:

//TankClient.java

package edu.wkx; import java.awt.Color; import java.awt.Frame; import java.awt.Graphics; import java.awt.Image; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.util.ArrayList; import java.util.List; import java.util.Random;

/**

  • 坦克客戶端主類
  • @author Java_Bean 王凱旋 /

@SuppressWarnings("serial") public class TankClient extends Frame {

static final int GAME_WIDTH = 800;
static final int GAME_HEIGHT = 600;
private int x = 50;
private int y = 50;
private int refreshTime = 16;
private int bloodTime = 500;
private int bloodStep = 0;
private int maxBloods = 10;
private int minTanks = 3;
private int maxTanks = 20;
private Image bufImg = null;
private Color bgColor = Color.GREEN;
private boolean hasGetdist = false;
private int disX = 0;
private int disY = 0;
private int enemyNum = 10;
private static Random rand = new Random();
private boolean start = false;
private int score = 0;
private int killPoint = 5;

private List<explode> explodes = new ArrayList<explode>();
private List<tank> tanks = new ArrayList<tank>();
private List<missile> missiles = new ArrayList<missile>();
private List<wall> walls = new ArrayList<wall>();
private List<blood> bloods = new ArrayList<blood>();

/**
 * 啟動窗口
 */

public void launchFrame() {
    this.setTitle("TankWar");
    this.setLocation(300, 300);
    this.setSize(GAME_WIDTH, GAME_HEIGHT);
    this.setResizable(false);
    this.addWindowListener(new WindowAdapter() {

        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }

    });
    this.addMouseListener(new MouseAdapter() {

        public void mouseReleased(MouseEvent e) {
            hasGetdist = false;
        }

    });
    this.addMouseMotionListener(new MouseAdapter() {

        public void mouseDragged(MouseEvent e) {

            if (!hasGetdist) {
                disX = e.getX();
                disY = e.getY();
                hasGetdist = true;
            }
            setLocation(e.getXOnScreen() - disX, e.getYOnScreen() - disY);
        }

    });
    this.setBackground(bgColor);
    this.addKeyListener(new KeyMonitor());
    this.setVisible(true);
    gameInit();
}

private void gameInit() {
    tanks.add(new Tank(x, y, true, this));
    addTank(10);
    walls.add(new Wall(300, 400, 300, 30));
    walls.add(new Wall(400, 300, 10, 200));
    score = 0;
    start = true;
}

// 重寫paint方法

private void addBlood() {
    bloodStep++;
    if (bloodStep >= bloodTime && bloods.size() < maxBloods) {
        bloodStep = 0;
        Blood blood = new Blood(x, y);
        blood.setX(rand.nextInt(GAME_WIDTH - blood.getSize()));
        blood.setY(rand.nextInt(GAME_HEIGHT - blood.getSize() - 25) + 25);
        for (int i = 0; i < tanks.size(); i++)
            if (blood.getRect().intersects(tanks.get(i).getRect()))
                return;
        for (int i = 0; i < walls.size(); i++)
            if (blood.getRect().intersects(walls.get(i).getRect()))
                return;
        for (int i = 0; i < bloods.size(); i++)
            if (blood.getRect().intersects(bloods.get(i).getRect()))
                return;
        bloods.add(blood);
    }
}

/**
 * 游戲對象繪制方法
 */

public void paint(Graphics g) {
    if (start) {
        addBlood();
        g.drawString("MissilesCount : " + missiles.size(), 10, 40);
        g.drawString("ExplodeCount : " + explodes.size(), 10, 50);
        g.drawString("TanksCount : " + (tanks.size()), 10, 60);
        g.drawString("WallsCount : " + walls.size(), 10, 70);
        g.drawString("BloodsCount : " + bloods.size(), 10, 80);
        g.drawString("Score : " + score, 10, 100);
        for (int i = 0; i < explodes.size(); i++)
            explodes.get(i).draw(g);
        for (int i = 0; i < missiles.size(); i++) {
            Missile missile = missiles.get(i);
            for (int j = 0; j < walls.size(); j++) {
                Wall w = walls.get(j);
                if (missile.hitWall(w))
                    missiles.remove(missile);
            }
            for (int k = 0; k < tanks.size(); k++) {
                Tank tank = tanks.get(k);
                if (missiles.contains(missile) && missile.hitTank(tank)) {
                    missiles.remove(missile);
                    int life = tank.getLife() - missile.getAttackHurt();
                    if (life <= 0) {
                        score += killPoint;
                        tanks.remove(tank);
                        explodes.add(new Explode(tank.getX()
                                + tank.getSize() / 2, tank.getY()
                                + tank.getSize() / 2, this));
                        if (tank.isGood())
                            gameOver();
                    } else {
                        tank.setLife(life);
                    }
                }
            }
        }
        for (int i = 0; i < tanks.size(); i++) {
            Tank t = tanks.get(i);
            t.draw(g);
            if (t.isGood())
                g.drawString("TankLife : " + t.getLife(), 10, 90);
            for (int j = 0; j < bloods.size(); j++) {
                Blood blood = bloods.get(j);
                if (blood.getRect().intersects(t.getRect())) {
                    bloods.remove(blood);
                    t.setLife(t.getLife() + blood.getBlood());
                }
            }
        }
        for (int i = 0; i < missiles.size(); i++)
            missiles.get(i).draw(g);
        for (int i = 0; i < walls.size(); i++)
            walls.get(i).draw(g);
        for (int i = 0; i < bloods.size(); i++)
            bloods.get(i).draw(g);
        if(tanks.size()<MINTANKS) while(true&&tanknum="" tanknum){="" addtank(int="" gameinit();="" keyevent.vk_f2)="" e.getkeycode()="=" &&="" (!start="" addtank(1);="" keyevent.vk_equals)="" (e.getkeycode()="=" tanks.get(i).keypressed(e.getkeycode());="" keypressed(keyevent="" tanks.get(i).keyreleased(e.getkeycode());="" i++)="" tanks.size();="" <="" i="0;" (int="" for="" (start)="" keyreleased(keyevent="" keyadapter="" extends="" keymonitor="" 添加鍵盤監聽器類="" repaint();="" e.printstacktrace();="" e)="" (interruptedexception="" catch="" thread.sleep(refreshtime);="" try="" (true)="" while="" run()="" runnable="" implements="" paintthread="" 運行線程="" null);="" g.drawimage(bufimg,="" 將虛擬屏幕貼到屏幕="" paint(gbufimg);="" 調用paint方法在虛擬屏幕上繪制圖形="" gbufimg.setcolor(c);="" 恢復虛擬屏幕的畫筆顏色="" game_width,="" 0,="" gbufimg.fillrect(0,="" gbufimg.setcolor(bgcolor);="" 重繪背景="" c="gBufImg.getColor();" color="" 保存虛擬屏幕的畫筆顏色="" gbufimg="bufImg.getGraphics();" graphics="" 得到虛擬屏幕的圖像類="" game_height);="" bufimg="this.createImage(GAME_WIDTH," 創建一個虛擬屏幕(圖片)="" null)="" (bufimg="=" if="" g)="" update(graphics="" public="" 添加雙緩沖消除屏幕閃爍="" *="" **="" start="false;" walls.clear();="" bloods.clear();="" missiles.clear();="" tanks.clear();="" gameover()="" void="" private="" 80);="" restart?,="" to="" f2="" g.drawstring(?press="" 60);="" score,="" +="" ?="" :="" g.drawstring(?score="" 40);="" 10,="" over?,="" g.drawstring(?game="" {="" else="" }="" addtank(maxtanks-tanks.size());="">0){
        boolean addAble=true;
        Tank t = new Tank(x, y, false, TankClient.this);
        t.setX(rand.nextInt(GAME_WIDTH - t.getSize()));
        t.setY(rand.nextInt(GAME_HEIGHT - t.getSize() - 25) + 25);
        for (int i = 0; i < tanks.size(); i++)
            if (t.getRect().intersects(tanks.get(i).getRect())){
                addAble=false;
                break;
            }
        for (int i = 0; i < walls.size(); i++)
            if (t.getRect().intersects(walls.get(i).getRect())){
                addAble=false;
                break;
            }
        if(addAble){
            tanks.add(t);
            tankNum--;  
        }
    }
}

/**
 * 主方法
 * @param args 命令行參數
 */

public static void main(String[] args) {
    TankClient tc = new TankClient();
    tc.launchFrame();
    new Thread(tc.new PaintThread()).start();
}

public int getX() {
    return x;
}

public void setX(int x) {
    this.x = x;
}

public int getY() {
    return y;
}

public void setY(int y) {
    this.y = y;
}

public int getRefreshTime() {
    return refreshTime;
}

public void setRefreshTime(int refreshTime) {
    this.refreshTime = refreshTime;
}

public Image getBufImg() {
    return bufImg;
}

public void setBufImg(Image bufImg) {
    this.bufImg = bufImg;
}

public Color getBgColor() {
    return bgColor;
}

public void setBgColor(Color bgColor) {
    this.bgColor = bgColor;
}

public boolean isHasGetdist() {
    return hasGetdist;
}

public void setHasGetdist(boolean hasGetdist) {
    this.hasGetdist = hasGetdist;
}

public int getDisX() {
    return disX;
}

public void setDisX(int disX) {
    this.disX = disX;
}

public int getDisY() {
    return disY;
}

public void setDisY(int disY) {
    this.disY = disY;
}

public List<explode> getExplodes() {
    return explodes;
}

public void setExplodes(List<explode> explodes) {
    this.explodes = explodes;
}

public List<tank> getTanks() {
    return tanks;
}

public void setTanks(List<tank> tanks) {
    this.tanks = tanks;
}

public List<missile> getMissiles() {
    return missiles;
}

public void setMissiles(List<missile> missiles) {
    this.missiles = missiles;
}

public int getEnemyNum() {
    return enemyNum;
}

public void setEnemyNum(int enemyNum) {
    this.enemyNum = enemyNum;
}
public List<wall> getWalls() {
    return walls;
}

public void setWalls(List<wall> walls) {
    this.walls = walls;
}


}</wall></wall></missile></missile></tank></tank></explode></explode></MINTANKS)></blood></blood></wall></wall></missile></missile></tank></tank></explode></explode></pre>

//Tank.java

package edu.wkx; import java.awt.Color; import java.awt.Graphics; import java.awt.Rectangle; import java.awt.event.KeyEvent; import java.util.Random;

/**

  • 坦克類
  • @author Java_Bean 王凱旋 /

public class Tank { private int x; private int y; private int size = 50; private int xSpeed = 10; private int ySpeed = 10; private boolean bU = false; private boolean bD = false; private boolean bL = false; private boolean bR = false; private Direction dir = Direction.STOP; private Direction fireDir = Direction.R; private boolean good = true; private static Random rand = new Random(); private int moveStep = 0; private int moveSpeed = 5; private int fireStep = 0; private int fireSpeed = 100; private int turnStep = 0; private int turnSpeed = 100; private int oriX = 0; private int oriY = 0; private int superFireSize = 100; private int totalLife = 100; private int life = 0; private int lifeBarLength = 7; private int lifeBarWidth = 70;

private Color goodColor = Color.RED;
private Color enemyColor = Color.DARK_GRAY;
private Color fireColor = Color.BLUE;
private Color lifeBarColor = Color.BLUE;
private TankClient tc = null;

enum Direction {
    U, D, L, R, LU, RU, LD, RD, STOP
};

/**
 * 構造方法
 * @param x 坦克出現位置的x坐標
 * @param y 坦克出現位置的y坐標
 * @param good 坦克的類別
 */

public Tank(int x, int y, boolean good) {
    this.x = x;
    this.y = y;
    this.good = good;
    this.life = this.totalLife;
}

/**
 * 構造方法
 * @param x 坦克出現位置的x坐標
 * @param y 坦克出現位置的y坐標
 * @param good 坦克的類別
 * @param tc 坦克所屬主類的引用
 */

public Tank(int x, int y, boolean good, TankClient tc) {
    this(x, y, good);
    this.tc = tc;
    moveStep = rand.nextInt(moveSpeed);
    fireStep = rand.nextInt(fireSpeed);
    turnStep = rand.nextInt(turnSpeed);
}

/**
 * 在屏幕上畫出它自己
 * @param g 圖形類
 */

public void draw(Graphics g) {
    Color orgColor = g.getColor();
    if (good)
        g.setColor(goodColor);
    else {
        g.setColor(enemyColor);
        turnStep++;
        if (turnStep == turnSpeed) {
            Direction[] dirs = Direction.values();
            dir = dirs[rand.nextInt(dirs.length)];
            if (dir != Direction.STOP)
                fireDir = dir;
            turnStep = 0;
        }
        moveStep++;
        if (moveStep == moveSpeed) {
            move();
            moveStep = 0;
        }
        fireStep++;
        if (fireStep == fireSpeed) {
            fire();
            fireStep = 0;
        }
    }
    g.fillOval(x, y, size, size);

    g.setColor(fireColor);
    switch (fireDir) {
    case U:
        g.drawLine(x + size / 2, y + size / 2, x + size / 2, y);
        break;
    case D:
        g.drawLine(x + size / 2, y + size / 2, x + size / 2, y + size);
        break;
    case L:
        g.drawLine(x + size / 2, y + size / 2, x, y + size / 2);
        break;
    case R:
        g.drawLine(x + size / 2, y + size / 2, x + size, y + size / 2);
        break;
    case LU:
        g.drawLine(x + size / 2, y + size / 2, x, y);
        break;
    case RU:
        g.drawLine(x + size / 2, y + size / 2, x + size, y);
        break;
    case LD:
        g.drawLine(x + size / 2, y + size / 2, x, y + size);
        break;
    case RD:
        g.drawLine(x + size / 2, y + size / 2, x + size, y + size);
        break;
    }

    // lifeBarlifeBarLength
    g.setColor(lifeBarColor);
    g.drawRect(x + size / 2 - lifeBarWidth / 2, y - 5 - lifeBarLength,
            lifeBarWidth, lifeBarLength);
    g.setColor(Color.RED);
    g.fillRect(
            x + size / 2 - lifeBarWidth / 2 + 2,
            y - 5 - lifeBarLength + 2,
            (int) ((double) life / (double) totalLife * (double) (lifeBarWidth - 4)+1),
            lifeBarLength - 3);

    g.setColor(orgColor);
}

private void move() {
    switch (dir) {
    case U:
        y -= ySpeed;
        break;
    case D:
        y += ySpeed;
        break;
    case L:
        x -= xSpeed;
        break;
    case R:
        x += xSpeed;
        break;
    case LU:
        x -= xSpeed;
        y -= ySpeed;
        break;
    case RU:
        x += xSpeed;
        y -= ySpeed;
        break;
    case LD:
        x -= xSpeed;
        y += ySpeed;
        break;
    case RD:
        x += xSpeed;
        y += ySpeed;
        break;
    case STOP:
        break;
    }
    boolean condition = x < 0 || x + size > TankClient.GAME_WIDTH
            || y - 20 < 0 || y + size > TankClient.GAME_HEIGHT;
    for (int i = 0; i < tc.getWalls().size(); i++) {
        Wall wall = tc.getWalls().get(i);
        if (this.getRect().intersects(wall.getRect()))
            condition = true;
    }
    for (int i = 0; i < tc.getTanks().size(); i++) {
        Tank t = tc.getTanks().get(i);
        if (!this.equals(t) && this.getRect().intersects(t.getRect()))
            condition = true;
    }
    if (condition) {
        x = oriX;
        y = oriY;
        return;
    }
    oriX = x;
    oriY = y;
}

/**
 * 鍵盤按下的處理
 * @param key 鍵位值
 */

public void keyPressed(int key) {
    if (good) {
        switch (key) {
        case KeyEvent.VK_UP:
            bU = true;
            makeDir();
            move();
            break;
        case KeyEvent.VK_DOWN:
            bD = true;
            makeDir();
            move();
            break;
        case KeyEvent.VK_LEFT:
            bL = true;
            makeDir();
            move();
            break;
        case KeyEvent.VK_RIGHT:
            bR = true;
            makeDir();
            move();
            break;
        case KeyEvent.VK_CONTROL:
            fire();
            break;
        case KeyEvent.VK_A:
            bigFire();
            break;
        case KeyEvent.VK_X:
            xFire();
            break;
        }
    }
}

private void fire() {
    tc.getMissiles().add(
            new Missile(x + size / 2, y + size / 2, fireDir, this, tc));
}

private void bigFire() {
    tc.getMissiles().add(
            new Missile(x + size / 2, y + size / 2, fireDir, this, tc,
                    superFireSize));
}

private void xFire() {
    Direction[] dirs = Direction.values();
    for (int i = 0; i < dirs.length - 1; i++)
        tc.getMissiles().add(
                new Missile(x + size / 2, y + size / 2, dirs[i], this, tc));
}

/**
 * 按鍵釋放的處理
 * @param key 鍵位值
 */

public void keyReleased(int key) {
    if (good) {
        switch (key) {
        case KeyEvent.VK_UP:
            bU = false;
            makeDir();
            break;
        case KeyEvent.VK_DOWN:
            bD = false;
            makeDir();
            break;
        case KeyEvent.VK_LEFT:
            bL = false;
            makeDir();
            break;
        case KeyEvent.VK_RIGHT:
            bR = false;
            makeDir();
            break;
        }
    }
}

private void makeDir() {
    if (bU && !bD && !bL && !bR)
        fireDir = dir = Direction.U;
    if (!bU && bD && !bL && !bR)
        fireDir = dir = Direction.D;
    if (!bU && !bD && bL && !bR)
        fireDir = dir = Direction.L;
    if (!bU && !bD && !bL && bR)
        fireDir = dir = Direction.R;
    if (bU && !bD && bL && !bR)
        fireDir = dir = Direction.LU;
    if (bU && !bD && !bL && bR)
        fireDir = dir = Direction.RU;
    if (!bU && bD && bL && !bR)
        fireDir = dir = Direction.LD;
    if (!bU && bD && !bL && bR)
        fireDir = dir = Direction.RD;
}

public Rectangle getRect() {
    return new Rectangle(x, y, size, size);
}

public int getX() {
    return x;
}

public void setX(int x) {
    this.x = x;
}

public int getY() {
    return y;
}

public void setY(int y) {
    this.y = y;
}

public int getSize() {
    return size;
}

public void setSize(int size) {
    this.size = size;
}

public int getxSpeed() {
    return xSpeed;
}

public void setxSpeed(int xSpeed) {
    this.xSpeed = xSpeed;
}

public int getySpeed() {
    return ySpeed;
}

public void setySpeed(int ySpeed) {
    this.ySpeed = ySpeed;
}

public boolean isbU() {
    return bU;
}

public void setbU(boolean bU) {
    this.bU = bU;
}

public boolean isbD() {
    return bD;
}

public void setbD(boolean bD) {
    this.bD = bD;
}

public boolean isbL() {
    return bL;
}

public void setbL(boolean bL) {
    this.bL = bL;
}

public boolean isbR() {
    return bR;
}

public void setbR(boolean bR) {
    this.bR = bR;
}

public Direction getDir() {
    return dir;
}

public void setDir(Direction dir) {
    this.dir = dir;
}

public Direction getFireDir() {
    return fireDir;
}

public void setFireDir(Direction fireDir) {
    this.fireDir = fireDir;
}

public boolean isGood() {
    return good;
}

public void setGood(boolean good) {
    this.good = good;
}

public Color getGoodColor() {
    return goodColor;
}

public void setGoodColor(Color goodColor) {
    this.goodColor = goodColor;
}

public Color getEnemyColor() {
    return enemyColor;
}

public void setEnemyColor(Color enemyColor) {
    this.enemyColor = enemyColor;
}

public Color getFireColor() {
    return fireColor;
}

public void setFireColor(Color fireColor) {
    this.fireColor = fireColor;
}

public TankClient getTc() {
    return tc;
}

public void setTc(TankClient tc) {
    this.tc = tc;
}

public static Random getRand() {
    return rand;
}

public static void setRand(Random rand) {
    Tank.rand = rand;
}

public int getMoveStep() {
    return moveStep;
}

public void setMoveStep(int moveStep) {
    this.moveStep = moveStep;
}

public int getMoveSpeed() {
    return moveSpeed;
}

public void setMoveSpeed(int moveSpeed) {
    this.moveSpeed = moveSpeed;
}

public int getFireStep() {
    return fireStep;
}

public void setFireStep(int fireStep) {
    this.fireStep = fireStep;
}

public int getFireSpeed() {
    return fireSpeed;
}

public void setFireSpeed(int fireSpeed) {
    this.fireSpeed = fireSpeed;
}

public int getTurnStep() {
    return turnStep;
}

public void setTurnStep(int turnStep) {
    this.turnStep = turnStep;
}

public int getTurnSpeed() {
    return turnSpeed;
}

public void setTurnSpeed(int turnSpeed) {
    this.turnSpeed = turnSpeed;
}

public int getOriX() {
    return oriX;
}

public void setOriX(int oriX) {
    this.oriX = oriX;
}

public int getOriY() {
    return oriY;
}

public void setOriY(int oriY) {
    this.oriY = oriY;
}

public int getSuperFireSize() {
    return superFireSize;
}

public void setSuperFireSize(int superFireSize) {
    this.superFireSize = superFireSize;
}

public int getTotalLife() {
    return totalLife;
}

public void setTotalLife(int totalLife) {
    this.totalLife = totalLife;
}

public int getLife() {
    return life;
}

public void setLife(int life) {
    if(life>this.totalLife)
        life=this.totalLife;
    if(life<0)
        life=0;
    this.life = life;
}

}</pre>

//Wall.java

package edu.wkx; import java.awt.Color; import java.awt.Graphics; import java.awt.Rectangle;

/**

  • 墻類
  • @author Java_Bean 王凱旋 /

public class Wall { private int x = 0; private int y = 0; private int width = 0; private int height = 0; private Color color = Color.BLACK;

/**
 * 構造方法
 * @param x 墻出現的x坐標
 * @param y 墻出現的y坐標
 * @param width 墻的寬度
 * @param height 墻的高度
 */

public Wall(int x, int y, int width, int height) {
    this.x = x;
    this.y = y;
    this.width = width;
    this.height = height;
}

/**
 * 構造方法
 * @param x 墻出現的x坐標
 * @param y 墻出現的y坐標
 * @param width 墻的寬度
 * @param height 墻的高度
 * @param color 墻的顏色
 */

public Wall(int x, int y, int width, int height, Color color) {
    this.x = x;
    this.y = y;
    this.width = width;
    this.height = height;
    this.color = color;
}

/**
 * 在屏幕上畫出它自己
 * @param g 圖形類
 */

public void draw(Graphics g) {
    Color orgColor = g.getColor();
    g.setColor(color);
    g.fillRect(x, y, width, height);
    g.setColor(orgColor);
}

/**
 * 得到代表所占范圍的矩形
 * @return 代表他所占范圍的矩形
 */

public Rectangle getRect() {
    return new Rectangle(x, y, width, height);
}

}</pre>

//Explode.java

package edu.wkx; import java.awt.Color; import java.awt.Graphics;

/**

  • 爆炸類
  • @author Java_Bean 王凱旋 /

public class Explode { private int x = 0; private int y = 0; private Color color = Color.WHITE; private int totalStep = 70; private int speed = 8; private int step = 0; private TankClient tc = null;

/**
 * 構造函數
 * @param x 爆炸地點的x坐標
 * @param y 爆炸地點的y坐標
 * @param tc 所屬主類的引用
 */

public Explode(int x, int y, TankClient tc) {
    this.x = x;
    this.y = y;
    this.tc = tc;
}

/**
 * 在屏幕上能畫出他自己
 * @param g 圖形類
 */

public void draw(Graphics g) {
    Color orgColor = g.getColor();
    g.setColor(color);
    g.fillOval(x - step / 2, y - step / 2, step, step);
    step+=speed;
    if (step >= totalStep) {
        step = 0;
        tc.getExplodes().remove(this);
    }
    g.setColor(orgColor);
}

public int getX() {
    return x;
}

public void setX(int x) {
    this.x = x;
}

public int getY() {
    return y;
}

public void setY(int y) {
    this.y = y;
}

public Color getColor() {
    return color;
}

public void setColor(Color color) {
    this.color = color;
}

public int getTotalStep() {
    return totalStep;
}

public void setTotalStep(int totalStep) {
    this.totalStep = totalStep;
}

public int getStep() {
    return step;
}

public void setStep(int step) {
    this.step = step;
}

public TankClient getTc() {
    return tc;
}

public void setTc(TankClient tc) {
    this.tc = tc;
}

public int getSpeed() {
    return speed;
}

public void setSpeed(int speed) {
    this.speed = speed;
}


}</pre>

//Missile.java

package edu.wkx; import java.awt.Color; import java.awt.Graphics; import java.awt.Rectangle;

/**

  • 子彈類
  • @author Java_Bean 王凱旋 /

public class Missile {

private int x = 0;
private int y = 0;
private int size = 7;
private Color color = Color.BLACK;
private int xSpeed = 15;
private int ySpeed = 15;
private Tank.Direction dir = Tank.Direction.STOP;
private TankClient tc = null;
private Tank t = null;
private int attackHurt = 10;
private int bigAttackHurt = 100;

/**
 * 構造函數
 * @param x 子彈所在位置的x坐標
 * @param y 子彈所在位置的y坐標
 * @param dir 子彈飛出的方向
 * @param t 發出子彈的坦克的引用
 * @param tc 子彈所屬主類的引用
 */

public Missile(int x, int y, Tank.Direction dir, Tank t, TankClient tc) {
    this.x = x - size / 2;
    this.y = y - size / 2;
    this.dir = dir;
    this.tc = tc;
    this.t = t;
}

/**
 * 構造函數
 * @param x 子彈所在位置的x坐標
 * @param y 子彈所在位置的y坐標
 * @param dir 子彈飛出的方向
 * @param t 發出子彈的坦克的引用
 * @param tc 子彈所屬主類的引用
 * @param size 子彈的大小
 */

public Missile(int x, int y, Tank.Direction dir, Tank t, TankClient tc,
        int size) {
    this.size = size;
    this.x = x - size / 2;
    this.y = y - size / 2;
    this.dir = dir;
    this.tc = tc;
    this.t = t;
    this.attackHurt = bigAttackHurt;
}

/**
 * 在屏幕上畫出他自己
 * @param g 圖形類
 */

public void draw(Graphics g) {
    Color orgColor = g.getColor();
    g.setColor(color);
    g.fillOval(x, y, size, size);
    g.setColor(orgColor);
    move();
}

private void move() {
    switch (dir) {
    case U:
        y -= ySpeed;
        break;
    case D:
        y += ySpeed;
        break;
    case L:
        x -= xSpeed;
        break;
    case R:
        x += xSpeed;
        break;
    case LU:
        x -= xSpeed;
        y -= ySpeed;
        break;
    case RU:
        x += xSpeed;
        y -= ySpeed;
        break;
    case LD:
        x -= xSpeed;
        y += ySpeed;
        break;
    case RD:
        x += xSpeed;
        y += ySpeed;
        break;
    }
    if (x + size / 2 < 0 || y + size / 2 < 0
            || x + size / 2 > TankClient.GAME_WIDTH
            || y + size / 2 > TankClient.GAME_HEIGHT)
        tc.getMissiles().remove(this);
}

/**
 * 判斷是否撞到了坦克
 * @param t 所要判斷的坦克
 * @return 是否撞到了坦克
 */

public boolean hitTank(Tank t) {
    if (this.getRect().intersects(t.getRect())) {
        if ((this.t.isGood() && !t.isGood())
                || (!this.t.isGood() && t.isGood()))
            return true;
    }
    return false;
}

/**
 * 判斷是否撞到了墻
 * @param w 所要判斷的墻
 * @return 是否撞到了墻
 */

public boolean hitWall(Wall w) {
    return this.getRect().intersects(w.getRect());
}

/**
 * 得到表示子彈范圍的矩形
 * @return 表示子彈范圍的矩形
 */

public Rectangle getRect() {
    return new Rectangle(x, y, size, size);
}

public int getX() {
    return x;
}

public void setX(int x) {
    this.x = x;
}

public int getY() {
    return y;
}

public void setY(int y) {
    this.y = y;
}

public int getSize() {
    return size;
}

public void setSize(int size) {
    this.size = size;
}

public Color getColor() {
    return color;
}

public void setColor(Color color) {
    this.color = color;
}

public int getxSpeed() {
    return xSpeed;
}

public void setxSpeed(int xSpeed) {
    this.xSpeed = xSpeed;
}

public int getySpeed() {
    return ySpeed;
}

public void setySpeed(int ySpeed) {
    this.ySpeed = ySpeed;
}

public Tank.Direction getDir() {
    return dir;
}

public void setDir(Tank.Direction dir) {
    this.dir = dir;
}

public TankClient getTc() {
    return tc;
}

public void setTc(TankClient tc) {
    this.tc = tc;
}

public Tank getT() {
    return t;
}

public void setT(Tank t) {
    this.t = t;
}

public int getAttackHurt() {
    return attackHurt;
}

public void setAttackHurt(int attackHurt) {
    this.attackHurt = attackHurt;
}

}</pre>

//Blood.java

package edu.wkx; import java.awt.Color; import java.awt.Graphics; import java.awt.Rectangle;

/**

  • 補血血塊類
  • @author Java_Bean 王凱旋 /

public class Blood { private int x = 0; private int y = 0; private int blood = 15; private int size = 10;

private Color color=Color.RED;

/**
 * 構造方法
 * @param x 血塊出現的x坐標
 * @param y 血塊出現的y坐標
 */

public Blood(int x, int y) {
    this.x = x;
    this.y = y;
}

/**
 * 構造方法
 * @param x 血塊出現的x坐標
 * @param y 血塊出現的y坐標
 * @param blood 一個血塊回復的生命值
 */

public Blood(int x, int y, int blood) {
    this(x, y);
    this.blood = blood;
}

/**
 * 判斷是否和坦克相撞
 * @param t 與之相撞的坦克
 * @return  是否相撞
 */

public boolean hitTank(Tank t) {
    return this.getRect().intersects(t.getRect());
}

/**
 * 在屏幕上畫出他自己
 * @param g 圖形類
 */

void draw(Graphics g) {
    Color orgColor = g.getColor();
    g.setColor(color);
    g.fillOval(x, y, size, size);
    g.setColor(orgColor);
}

/**
 * 得到血塊所代表的矩形
 * @return 血塊代表的矩形
 */

public Rectangle getRect() {
    return new Rectangle(x, y, size, size);
}

public int getX() {
    return x;
}

public void setX(int x) {
    this.x = x;
}

public int getY() {
    return y;
}

public void setY(int y) {
    this.y = y;
}

public int getBlood() {
    return blood;
}

public void setBlood(int blood) {
    this.blood = blood;
}

public int getSize() {
    return size;
}

public void setSize(int size) {
    this.size = size;
}

}</pre>
轉自:http://blog.csdn.net/jack_wong2010/article/details/7664370

 本文由用戶 openkk 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!