⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 playertank.java

📁 J2ME超级坦克大战源码
💻 JAVA
字号:

import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;


public final class PlayerTank extends Tank{
    
    /**
     * Is key pressed?
     */
    private boolean keyPressed=false;
    
    /**
     * player's current grade,if equal 0,mean player dies.
     */
    private int grade = MIN_GRADE;
    
    /**
     * Player's minimum grade
     */
    private static final int MIN_GRADE=1;
    
    /**
     * tank can shoot 2 bulltes.   **
     */
    private static final int GRADE_TWO_BULLETS=2;
    
    /**
     * tank can shoot 3 bulltes.   ***
     */
    private static final int GRADE_THREE_BULLETS=3;
    
    /**
     * tank can break concrete walls. ****
     */
    private static final int GRADE_BREAK_CONCRETE_WALL=4;
    
    /**
     * tank can break water and snow. *****
     */
    private static final int GRADE_BREAK_WATER=5;
    
    /**
     * tank add one layer of shell. ******
     */
    private static final int GRADE_SHELL_1=6;
    
    /**
     * tank move fast, go one star. *
     */
    private static final int GRADE_SPEED=7;
    
    /**
     * tank add two layers of shell. *******
     */
    private static final int GRADE_SHELL_2=8;
    
    /**
     * Player's maximum grade
     */
    private static final int MAX_GRADE=8;
    
    /**
     * the player's tank is just created.
     */
    private static final int NEW_BORN=9;
    
    /**
     * the Invulnerable sheild for the player tank.
     */
    private Powerup sheild;
    
    /**
     * player tank is invulnerable at the start of the level,
     * and can become invulnerable if collects {@see Powerup.SHIELD}.
     */
    private int invulnerabilityTicks;
    
    /**
     * the time begin invulnerable
     */
    private long invulnerableTime;
    
    /**
     * invulnerable period. for start ,the time is 7.5 seconds.
     */
    private static final int invulnerablePeriod=30000;
    
    /**
     * store current direction as shooting direction
     */
    private int currentDirection=BattleField.NONE;
    
    /**
     * how many bullets can player shoot at same time.
     */
    private int avaiableBullets=1;
    
    /**
     * same direction,switch 2 image to make tank move animation.
     */
    private boolean switchImage=false;
    
    /**
     * how many lifes player has.
     */
    private int avaiableLife=3;
    
    /**
     * the score the player gets.
     */
    private int score=0;
    
    
   
    protected PlayerTank() {
        super(ResourceManager.getInstance().getImage(ResourceManager.PLAYER),
                ResourceManager.TILE_WIDTH,ResourceManager.TILE_WIDTH);
        sheild=Powerup.getInvulnerable();
    }
    
    
    public void addScore(int value) {
        score+=value;
    }
    
    
    public void setScore(int value) {
        score=value;
    }
    
    public int getScore() {
        return score;
    }
    
    
    public void initTank(){
        if(battleField!=null){
            battleField.initPlayerTankPos(this);
            setVisible(true);
            direction=BattleField.NONE;
            grade=NEW_BORN;
            newBornTimer=0;
            avaiableBullets=1;
            speed=DEFAULT_SPEED;
            shoot=false;
        }
    }
    
    
    public void keyPressed(int gameAction){
        keyPressed=true;
        if (gameAction == GameCanvas.UP) {
            direction = BattleField.NORTH;
        } else if (gameAction == GameCanvas.RIGHT) {
            direction = BattleField.EAST;
        } else if (gameAction == GameCanvas.LEFT) {
            direction = BattleField.WEST;
        } else if (gameAction == GameCanvas.DOWN) {
            direction = BattleField.SOUTH;
        } else if (gameAction == GameCanvas.FIRE) {
            shoot = true;
        }
        if(direction!=BattleField.NONE){
            currentDirection=direction;
        }
    }
    
   
    public void keyReleased(int gameAction){
        keyPressed=false;
        switch (gameAction) {
            case GameCanvas.UP:
                if(direction == BattleField.NORTH){
                    direction = BattleField.NONE;
                    break;
                }
            case GameCanvas.DOWN:
                if(direction == BattleField.SOUTH){
                    direction = BattleField.NONE;
                    break;
                }
            case GameCanvas.LEFT:
                if(direction == BattleField.WEST){
                    direction = BattleField.NONE;
                    break;
                }
            case GameCanvas.RIGHT:
                if(direction == BattleField.EAST){
                    direction = BattleField.NONE;
                    break;
                }
            case GameCanvas.FIRE:
                shoot = false;
        }
    }
    
    
    public void upgrade(Powerup powerup){
        switch(powerup.getType()){
            case Powerup.CLOCK:
                EnemyTank.immobilizedStartTime=System.currentTimeMillis();
                break;
            case Powerup.STAR:
                grade++;
                if(grade>MAX_GRADE ) grade=MAX_GRADE;
                switch(grade){
                    case GRADE_SPEED:
                    {
                        speed*=2;
                        int x=getX();
                        int y=getY();
                        x=(x / speed) *speed;
                        y=(y / speed) *speed;
                        setPosition(x,y);
                    }
                    break;
                    case GRADE_TWO_BULLETS:
                        avaiableBullets=2;
                        break;
                    case GRADE_THREE_BULLETS:
                        avaiableBullets=3;
                        break;
                }
        
                break;
            case Powerup.SHIELD:
                invulnerableTime=System.currentTimeMillis();
                invulnerabilityTicks=invulnerablePeriod;
                sheild.setPosition(getX(),getY());
                sheild.setVisible(true);
                break;
            case Powerup.BOMB:
                EnemyTank.explodeAllEmenies();
                break;
            case Powerup.TANK:
                avaiableLife++;
                break;
            case Powerup.SHOVEL:
                battleField.makeHomeConcreteWall();
                break;
                
        }
    }
    
    
    public void setAvaiableLives(int live){
        avaiableLife=live;
    }
    
   
    public int getAvaiableLives(){
        return avaiableLife;
    }
    
   
    public void think(){
        if(grade==NEW_BORN){
            newBornTimer++;
            if(newBornTimer>4){
                grade= MIN_GRADE;
                direction=BattleField.NONE;
                currentDirection=BattleField.NORTH;
                newBornTimer=0;
                setFrame(0);
                invulnerableTime=System.currentTimeMillis();
                invulnerabilityTicks=invulnerablePeriod/4;
                sheild.setPosition(getX(),getY());
                sheild.setVisible(true);
                
            }else{
                try{
                    setFrame(newBornTimer*9-1);
                }catch(Exception e){
                    //System.out.println("Playertank");
                }
            }
            
        } else{
            long tickTime = System.currentTimeMillis();
            if(tickTime-invulnerableTime>invulnerabilityTicks){
                sheild.setVisible(false);
            }else{
                sheild.setPosition(getX(),getY());
                sheild.setVisible(true);
            }
            changeDirection(direction);
            if(currentDirection!=BattleField.NONE){
                switchImage=!switchImage;
                int offset=switchImage? 0:1;
                try{
                    setFrame(currentDirection*9+((int)(grade-1)/2)*2 +offset);
                }catch(Exception e){
                    //System.out.println("Playertank1");
                }
            }
        }
        
    }
    
    
   
    public boolean isInvulnerable(){
        return sheild.isVisible();
    }
    
    
    public Bullet shoot(){
        Bullet bullet=null;
        int bulletCount=Bullet.getPlayerBulletCount();
        if(shoot && bulletCount<avaiableBullets){
            int step=ResourceManager.TILE_WIDTH;
            bullet=Bullet.getFreeBullet();
            if(bullet!=null){
                if(ResourceManager.isPlayingSound){
                    ResourceManager.playSound(ResourceManager.SHOOT_SOUND);
                }
                int x = getRefPixelX();
                int y = getRefPixelY();
                
                switch (currentDirection) {
                    case BattleField.NORTH:
                        y -= step / 2;
                        break;
                    case BattleField.EAST:
                        x += step / 2;
                        break;
                    case BattleField.SOUTH:
                        y += step / 2;
                        break;
                    case BattleField.WEST:
                        x -= step / 2;
                        break;
                }
                bullet.setSpeed(ResourceManager.TILE_WIDTH/2);
                bullet.setDirection(currentDirection);
                if(grade>=GRADE_BREAK_WATER){
                    bullet.setStrength(Bullet.GRADE_BREAK_WATER);
                }else if(grade>=GRADE_BREAK_CONCRETE_WALL){
                    bullet.setStrength(Bullet.GRADE_BREAK_CONCRETE_WALL);
                }else{
                    bullet.setStrength(Bullet.GRADE_DEFAULT);
                }
                
                bullet.setRefPixelPosition(x-1, y-1);
                bullet.setFriendly(true);
                bullet.setVisible(true);
            }
        }
        return bullet;
    }

    
    public void stop(){
        direction=BattleField.NONE;
        shoot=false;
    }
   
    protected void explode() {
        if(!isInvulnerable()){
            if(grade>=GRADE_BREAK_WATER){
                grade--;
            }else{
                avaiableLife--;
                super.explode();
            }
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -