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

📄 battlefield.java

📁 还记得儿时在游戏机上玩坦克大战吗?如今这种经典游戏不在游戏机上玩了
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            }
        }
         return bRet;
    }
    
    ////////////////////////////////////////////////////////////////////////////
    //--------------------------------- REVISIONS ------------------------------
    // Date       Name                 Tracking #         Description
    // ---------  -------------------  -------------      ----------------------
    // 16JAN2008  James Shen                 	          Initial Creation
    ////////////////////////////////////////////////////////////////////////////
    /**
     * Initialize the Enemy's start position.
     * @param tank the Enemy's tank.
     */
    public void initEnemyTankPos(EnemyTank tank){
        nextEnemyPos%=3;
        int x=enemyPos[nextEnemyPos][0];
        int y=enemyPos[nextEnemyPos][1];
        tank.setPosition(x,y);
        nextEnemyPos++;
    }
    
    ////////////////////////////////////////////////////////////////////////////
    //--------------------------------- REVISIONS ------------------------------
    // Date       Name                 Tracking #         Description
    // ---------  -------------------  -------------      ----------------------
    // 16JAN2008  James Shen                 	          Initial Creation
    ////////////////////////////////////////////////////////////////////////////
    /**
     * read battle field from HZK.
     * @param gameLevel the current game level.
     */
    public void readBattlefieldFromHZK(int gameLevel){
        InputStream is=this.getClass().getResourceAsStream("/hzk12");
        int []buffer=new int[24];
        int []hzData=new int[13*13+4];
        int index=0;
         try{
             is.skip(gameLevel*24);
             for(int i=0;i<24;i++) buffer[i]=is.read();
             int tempChar;
             int tempBit;;
             index=0;
             for(int i=0;i<12;i++){
                 for(int j=0;j<2;j++){
                     tempChar=buffer[i*2+j];
                     tempBit=0x80;
                     for(int k=0;k<8;k++){
                         if(j==1 && k>3) break;
                         if((tempBit & tempChar)!=0){
                             hzData[index++]='2';
                         }else{
                             hzData[index++]='0';
                         }
                         tempBit=tempBit>>1;
                     }
                 }
                 hzData[index++]='0';
                 hzData[index++]='\n';
             }
            byte[] byteArray=new byte[hzData.length];
            for(int i=0;i<byteArray.length;i++){
                byteArray[i]=(byte)hzData[i];
            }
            ByteArrayInputStream bais=new ByteArrayInputStream(byteArray);       
            
            initBattlefield(bais);
        }catch(Exception e){
            //inglore the exception.
        }
    }
    ////////////////////////////////////////////////////////////////////////////
    //--------------------------------- REVISIONS ------------------------------
    // Date       Name                 Tracking #         Description
    // ---------  -------------------  -------------      ----------------------
    // 16JAN2008  James Shen                 	          Initial Creation
    ////////////////////////////////////////////////////////////////////////////
    /**
     * Initialize the player's start position.
     * @param tank the player's tank.
     */
    public void initPlayerTankPos(PlayerTank tank){
        int x=(WIDTH_IN_TILES/4-2)*ResourceManager.TILE_WIDTH;
        int y=(HEIGHT_IN_TILES/2-1)*ResourceManager.TILE_WIDTH;
        //this place will be placed with player's tank.
        duplicateCell(x*2/ResourceManager.TILE_WIDTH,
                y*2/ResourceManager.TILE_WIDTH,0);
        tank.setPosition(x,y);
        
    }
    
    ////////////////////////////////////////////////////////////////////////////
    //--------------------------------- REVISIONS ------------------------------
    // Date       Name                 Tracking #         Description
    // ---------  -------------------  -------------      ----------------------
    // 16JAN2008  James Shen                 	          Initial Creation
    ////////////////////////////////////////////////////////////////////////////
    /**
     * Initialize the powerup's random start position. 
     * @param powerup the powerup object.
     */
    public void initPowerupPos(Powerup powerup){
        if(powerup.getType()==Powerup.HOME ||
                powerup.getType()==Powerup.HOME_DESTROYED){
            int x=(WIDTH_IN_TILES/4)*ResourceManager.TILE_WIDTH;
            int y=(HEIGHT_IN_TILES/2-1)*ResourceManager.TILE_WIDTH;
            powerup.setPosition(x,y);
        }else{
            int x0=(WIDTH_IN_TILES/4)*ResourceManager.TILE_WIDTH;
            int y0=(HEIGHT_IN_TILES/2-1)*ResourceManager.TILE_WIDTH;
            int x=(Math.abs(rnd.nextInt()) % (WIDTH_IN_TILES/2))
                                                *ResourceManager.TILE_WIDTH;
            int y=(Math.abs(rnd.nextInt()) % (WIDTH_IN_TILES/2))
                                                *ResourceManager.TILE_WIDTH;
            //aovid the home cell.
            while(x==x0 && y==y0){
                x=(Math.abs(rnd.nextInt()) % (WIDTH_IN_TILES/2))
                                                *ResourceManager.TILE_WIDTH;
                y=(Math.abs(rnd.nextInt()) % (WIDTH_IN_TILES/2))
                                                *ResourceManager.TILE_WIDTH;
            }
            powerup.setPosition(x,y);
        }
        
    }
    
    
    ////////////////////////////////////////////////////////////////////////////
    //--------------------------------- REVISIONS ------------------------------
    // Date       Name                 Tracking #         Description
    // ---------  -------------------  -------------      ----------------------
    // 16JAN2008  James Shen                 	          Initial Creation
    ////////////////////////////////////////////////////////////////////////////
    /**
     * Make home wall a concrete wall. 
     */
    public void makeHomeConcreteWall(){
        //Draw the player's home area.
        for(int i=0;i<6;i++){
            for(int j=0;j<4;j++){
               setCell(i+(WIDTH_IN_TILES/2-3), HEIGHT_IN_TILES-4+j, CONCRETE_WALL);
            }
        }
        //this place will be placed with player's flag.
        duplicateCell((WIDTH_IN_TILES-2)/2,HEIGHT_IN_TILES-2,0);
        concreteWallStartTime=System.currentTimeMillis();
    }
    
    ////////////////////////////////////////////////////////////////////////////
    //--------------------------------- REVISIONS ------------------------------
    // Date       Name                 Tracking #         Description
    // ---------  -------------------  -------------      ----------------------
    // 16JAN2008  James Shen                 	          Initial Creation
    ////////////////////////////////////////////////////////////////////////////
    /**
     * Read the battle from an input stream.
     * @param is input stream stores the battle field information.
     */
    public synchronized void initBattlefield(java.io.InputStream is) 
                                                        throws IOException {
        //The actually canvas can be larger than 13X13 . initialiaze the battle
        //field with some random value.
        Random rnd=new Random();
        for(int i=0;i<WIDTH_IN_TILES;i+=2){
            for(int j=0;j<HEIGHT_IN_TILES;j+=2){
                int value=Math.abs(rnd.nextInt()) % 24;
                if(value>17){
                    if(value==21 || value==22){
                        duplicateCell(i,j,-1 - ((i ^ j) & 1));
                    }else{
                        duplicateCell(i, j, value-17);
         
                    }
                }else{
                   duplicateCell(i,j,0);
                }
            }
        }
        try{
            if(is!=null){
                readBattlefield(is);
            }
        }catch(IOException e){}
        makeHomeBrickWall();
        
    }
       
    ////////////////////////////////////////////////////////////////////////////
    //--------------------------------- REVISIONS ------------------------------
    // Date       Name                 Tracking #         Description
    // ---------  -------------------  -------------      ----------------------
    // 16JAN2008  James Shen                 	          Initial Creation
    ////////////////////////////////////////////////////////////////////////////
    /**
     * @InheritDoc
     */
    public void tick() {
        int tickState = (tickCount++ >> 3); // slow down x8
        int tile = tickState % 2;
        setAnimatedTile(-1 - tile, waterFrames[tile][(tickState % 4) / 2]);
        if(concreteWallStartTime>0){
            long tickTime=System.currentTimeMillis();
            if(tickTime-concreteWallStartTime>concreteWallPeriod){
                makeHomeBrickWall();
                concreteWallStartTime=0;
            }
        }
    }
    
    ////////////////////////////////////////////////////////////////////////////
    //--------------------------------- REVISIONS ------------------------------
    // Date       Name                 Tracking #         Description
    // ---------  -------------------  -------------      ----------------------
    // 16JAN2008  James Shen                 	          Initial Creation
    ////////////////////////////////////////////////////////////////////////////
    /**
     * Make home wall a brick wall. 
     */
    private void makeHomeBrickWall(){
       //Draw the player's home area.
        for(int i=0;i<6;i++){
            for(int j=0;j<4;j++){
               setCell(i+(WIDTH_IN_TILES/2-3), HEIGHT_IN_TILES-4+j, BRICK_WALL);
            }
        }
        //this place will be placed with player's flag.
        duplicateCell((WIDTH_IN_TILES-2)/2,HEIGHT_IN_TILES-2,0);
        concreteWallStartTime=0;
    }
    
    ////////////////////////////////////////////////////////////////////////////
    //--------------------------------- REVISIONS ------------------------------
    // Date       Name                 Tracking #         Description
    // ---------  -------------------  -------------      ----------------------
    // 16JAN2008  James Shen                 	          Initial Creation
    ////////////////////////////////////////////////////////////////////////////
    /**
     * duplicate adjacent cell with given value. The reason for this ,the width
     * for each image tile is 6X6 ,when design the battle fields, for simplicity
     * we combine adjacent 2X2 cell to stand for a 12X12 area,i.e the 4 cells 
     * store the same value.
     * @param x the x index of the cell
     * @param y the y index of the cell
     * @param value the value for the cell
     */
    private void duplicateCell(int x,int y,int value){
        int maxCols=getColumns()-1;
        int maxRows=getRows()-1;
        if(x<0 || x>maxCols || y<0 || y>maxRows ) 
            return;
        setCell(x, y, value);
        setCell(x+1, y, value);
        setCell(x, y+1, value);
        setCell(x+1, y+1, value);
     }
    ////////////////////////////////////////////////////////////////////////////
    //--------------------------------- REVISIONS ------------------------------
    // Date       Name                 Tracking #         Description
    // ---------  -------------------  -------------      ----------------------
    // 16JAN2008  James Shen                 	          Initial Creation
    ////////////////////////////////////////////////////////////////////////////
    /**
     * Read the battle from an input stream.
     * @param is input stream stores the battle field information.
     */
    private void readBattlefield(java.io.InputStream is) throws IOException {
        int c = -1;
        int x0 = (WIDTH_IN_TILES-NUMBER_IN_TILES)/2;
        int y0 = (HEIGHT_IN_TILES-NUMBER_IN_TILES)/2;
        int x=0,y=0;
        while ((c = is.read()) != -1 && y < NUMBER_IN_TILES) {
            switch (c) {
                case ' '://empty
                case '0':
                    duplicateCell(x+x0, y+y0, 0);x+=2;
                    break;
                case '1'://snow field
                    duplicateCell(x+x0, y+y0,SNOW);x+=2;
                    break;
                case '2'://brick wall
                    duplicateCell(x+x0, y+y0, BRICK_WALL);x+=2;
                    break;
                case '3'://forest
                    duplicateCell(x+x0, y+y0, FOREST);x+=2;
                    break;
                case '4':
                case '5'://water
                    duplicateCell(x+x0, y+y0, -1 - ((x ^ y) & 1));x+=2;
                    break;
                case '6': //Concrete wall
                    duplicateCell(x+x0, y+y0, CONCRETE_WALL);x+=2;
                    break;
                case '\n'://new line
                    y+=2;
                    x = 0;
                    break;
                default:
            }
        }
    }
    
}

⌨️ 快捷键说明

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