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

📄 gamescene.java

📁 还记得儿时在游戏机上玩坦克大战吗?如今这种经典游戏不在游戏机上玩了
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            type=Powerup.TANK;
        }else if(powupSelection>80){
            type=Powerup.BOMB;
        }else if(powupSelection>70){
            type=Powerup.SHOVEL;
        }else if(powupSelection>60){
            type=Powerup.CLOCK;
        }else if(powupSelection>50){
            type=Powerup.SHIELD;
        }else{
            type=Powerup.STAR;
        }
        Powerup.putNewPowerup(type);
        putPowerupStartTime=System.currentTimeMillis();
        canPutPowerup=false;
    }
    
    ////////////////////////////////////////////////////////////////////////////
    //--------------------------------- REVISIONS ------------------------------
    // Date       Name                 Tracking #         Description
    // ---------  -------------------  -------------      ----------------------
    // 20JAN2008  James Shen                 	          Initial Creation
    ////////////////////////////////////////////////////////////////////////////
    /**
     * Game over,either all player tanks or the home has been destroyed.
     */
    private void gameOver(){
        isGameover=true;
        gameStatus.setImage(imgGameover,imgGameover.getWidth(),
                imgGameover.getHeight());
        gameStatus.setVisible(true);
        gameStatus.setPosition(gameStatus.getX(),sceneHeight);
        playerTank.stop();
        
        gameOverStartTime=System.currentTimeMillis();
    }
    
    ////////////////////////////////////////////////////////////////////////////
    //--------------------------------- REVISIONS ------------------------------
    // Date       Name                 Tracking #         Description
    // ---------  -------------------  -------------      ----------------------
    // 20JAN2008  James Shen                 	          Initial Creation
    ////////////////////////////////////////////////////////////////////////////
    /**
     * Game logic is here.
     */
    private void applyGameLogic(){
        //normal game sequence.
        long tickTime=System.currentTimeMillis();;
        if(!isGameover){
            //Check if player obtain some powerup.
            Powerup.checkPlayerTank(playerTank);
            //Spawn enemy tank if needed
            boolean canSpawnEnemyTank=false;
            if(enemyTankRemains-EnemyTank.getVisibleEnemyTanks()>0){
                if(EnemyTank.getVisibleEnemyTanks()<10){
                    if(enemySpawnStartTime>0){
                        if(tickTime-enemySpawnStartTime>enemySpawnPeriod){
                            canSpawnEnemyTank=true;
                        }
                    }else{
                        canSpawnEnemyTank=true;
                    }
                }
            }else{
                if(EnemyTank.getVisibleEnemyTanks()==0){
                    ResourceManager.gameLevel++;
                    showScoreScreen();
                }
            }
            if(canSpawnEnemyTank){
                EnemyTank enemyTank=null;
                int tankSelection=Math.abs(rnd.nextInt()) % 100;
                if(tankSelection>90-ResourceManager.gameLevel){
                    enemyTank=EnemyTank.newEnemyTank(EnemyTank.TYPE_HEAVY);
                }else if(tankSelection>75-ResourceManager.gameLevel){
                    enemyTank=EnemyTank.newEnemyTank(EnemyTank.TYPE_SMART);
                }else if(tankSelection>55-ResourceManager.gameLevel){
                    enemyTank=EnemyTank.newEnemyTank(EnemyTank.TYPE_FAST);
                }else {
                    enemyTank=EnemyTank.newEnemyTank(EnemyTank.TYPE_SIMPLE);
                }
                if(enemyTank!=null){
                    
                    tankSelection=Math.abs(rnd.nextInt()) % 100;
                    if(tankSelection+ResourceManager.gameLevel>90){
                        
                        enemyTank.setHasPrize(true);
                    }
                    enemySpawnStartTime=tickTime;
                }
                
            }
            //Check if player has been killed
            if(!playerTank.isVisible()){
                if(playerTank.getAvaiableLives()>0){
                    playerTank.initTank();
                    playerTank.setVisible(true);
                }else{
                    gameOver();
                }
            }
            //Check if home is been destoryed, game over
            if(Powerup.isHomeDestroyed()){
                gameOver();
            }
            //put an poweup in the battle field
            if((tickTime-putPowerupStartTime>putPowerupPeriod) || canPutPowerup){
                putAnPowerup();
            }
        }else{
            //game is over, display game over animation.
            if(((tickTime-gameOverStartTime)<gameOverPeriod) &&
                    gameOverStartTime>0){
                int finalY=(sceneHeight-gameStatus.getHeight())/2;
                if(gameStatus.getY()>finalY){
                    gameStatus.setPosition(gameStatus.getX(),
                            gameStatus.getY()-1);
                }
            }else{
                showScoreScreen();
                gameOverStartTime=0;
            }
            
        }
    }
    
    ////////////////////////////////////////////////////////////////////////////
    //--------------------------------- REVISIONS ------------------------------
    // Date       Name                 Tracking #         Description
    // ---------  -------------------  -------------      ----------------------
    // 20JAN2008  James Shen                 	          Initial Creation
    ////////////////////////////////////////////////////////////////////////////
    /**
     * draw the score bar
     * @param g the graphics object.
     */
    private void drawScoreBar(Graphics g){
        
        int offset=8;
        int x,y;
        
        for(int i=0;i<enemyTankRemains-EnemyTank.getVisibleEnemyTanks();i++){
            int changeRow=i>9 ? 1:0;
            x=marginX+(i %10) *imgEnemyIcon.getWidth();
            y=marginY+changeRow*imgEnemyIcon.getWidth();
            g.drawImage(imgEnemyIcon,x,y,0);
        }
        //draw IP
        x=marginX+imgEnemyIcon.getWidth()*10+offset;
        y=marginY;
        int lives=playerTank.getAvaiableLives();
        drawNumber(g,lives,x+imgIP.getWidth()-imgNumberBlack.getHeight(),
                y+imgIP.getHeight()-imgNumberBlack.getHeight());
        g.drawImage(imgIP,x,y,0);
        x+=imgIP.getWidth()+offset;
        y=marginY;
        g.drawImage(imgFlag,x,y,0);
        drawNumber(g,ResourceManager.gameLevel,x+imgFlag.getWidth(),
                y+imgIP.getHeight()-imgNumberBlack.getHeight());
    }
    
    ////////////////////////////////////////////////////////////////////////////
    //--------------------------------- REVISIONS ------------------------------
    // Date       Name                 Tracking #         Description
    // ---------  -------------------  -------------      ----------------------
    // 20JAN2008  James Shen                 	          Initial Creation
    ////////////////////////////////////////////////////////////////////////////
    /**
     * clear the back ground.
     * @param g the graphics object.
     */
    private void clearBackground(Graphics g){
        g.setColor(0x808080);
        g.fillRect(0,0,sceneWidth,sceneHeight);
        g.setColor(0x000000);
        g.fillRect(battleFieldX,battleFieldY,
                battleField.getWidth(),battleField.getWidth());
    }
    
    ////////////////////////////////////////////////////////////////////////////
    //--------------------------------- REVISIONS ------------------------------
    // Date       Name                 Tracking #         Description
    // ---------  -------------------  -------------      ----------------------
    // 20JAN2008  James Shen                 	          Initial Creation
    ////////////////////////////////////////////////////////////////////////////
    /**
     * paint.
     * @param g the graphics object.
     */
    public void paint(Graphics g){
        //Clear the background.
        clearBackground(g);
        layerManager.paint(g,battleFieldX,battleFieldY);
        drawScoreBar(g);
        if(gameStatus.isVisible()){
            gameStatus.paint(g);
        }
        
    }
    
    ////////////////////////////////////////////////////////////////////////////
    //--------------------------------- REVISIONS ------------------------------
    // Date       Name                 Tracking #         Description
    // ---------  -------------------  -------------      ----------------------
    // 20JAN2008  James Shen                 	          Initial Creation
    ////////////////////////////////////////////////////////////////////////////
    /**
     * start the game..
     */
    private synchronized void start() {
        animationThread = new Thread(this);
        animationThread.start();
    }
    
    ////////////////////////////////////////////////////////////////////////////
    //--------------------------------- REVISIONS ------------------------------
    // Date       Name                 Tracking #         Description
    // ---------  -------------------  -------------      ----------------------
    // 20JAN2008  James Shen                 	          Initial Creation
    ////////////////////////////////////////////////////////////////////////////
    /**
     * stop the game.
     */
    private synchronized void stop() {
        animationThread = null;
    }
    
    ////////////////////////////////////////////////////////////////////////////
    //--------------------------------- REVISIONS ------------------------------
    // Date       Name                 Tracking #         Description
    // ---------  -------------------  -------------      ----------------------
    // 20JAN2008  James Shen                 	          Initial Creation
    ////////////////////////////////////////////////////////////////////////////
    /**
     * run.
     */
    public void run(){
        Thread currentThread = Thread.currentThread();
        
        try {
            while (currentThread == animationThread) {
                long startTime = System.currentTimeMillis();
                // Don't advance game or draw if canvas is covered by a system
                // screen.
                if (isShown()) {
                    tick();
                    repaint();
                }
                timeTaken = System.currentTimeMillis() - startTime;
                if (timeTaken < MILLIS_PER_TICK) {
                    synchronized (this) {
                        if(MILLIS_PER_TICK > timeTaken){
                            wait(MILLIS_PER_TICK - timeTaken);
                            timeTaken = System.currentTimeMillis() - startTime;
                        }
                    }
                } else {
                    Thread.yield();
                }
            }
        } catch (InterruptedException e) {
        }
        
    }
    
    ////////////////////////////////////////////////////////////////////////////
    //--------------------------------- REVISIONS ------------------------------
    // Date       Name                 Tracking #         Description
    // ---------  -------------------  -------------      ----------------------
    // 20JAN2008  James Shen                 	          Initial Creation
    ////////////////////////////////////////////////////////////////////////////
    /**
     * show the score screen.
     */
    private void showScoreScreen(){
        resourceManager.scoreScreen.show(isGameover);
        ResourceManager.setCurrentScreen(resourceManager.scoreScreen);
    }
    
    ////////////////////////////////////////////////////////////////////////////
    //--------------------------------- REVISIONS ------------------------------
    // Date       Name                 Tracking #         Description
    // ---------  -------------------  -------------      ----------------------
    // 20JAN2008  James Shen                 	          Initial Creation
    ////////////////////////////////////////////////////////////////////////////
    /**
     * key press.
     */
    protected void keyPressed(int keyCode) {
        int gameAction=0;
        if(isGameover){
            
            return;
        }
        switch(keyCode){
            case Canvas.KEY_NUM2:
                gameAction=Canvas.UP;
                break;
            case Canvas.KEY_NUM8:
                gameAction=Canvas.DOWN;
                break;
            case Canvas.KEY_NUM5:
                gameAction=Canvas.FIRE;
                break;
            case Canvas.KEY_NUM4:
                gameAction=Canvas.LEFT;
                break;
            case Canvas.KEY_NUM6:
                gameAction=Canvas.RIGHT;
                break;
                
            default:
                gameAction = getGameAction(keyCode);
        }
        if(!isGameover){
            playerTank.keyPressed(gameAction);
        }
        
    }
    
    
    ////////////////////////////////////////////////////////////////////////////
    //--------------------------------- REVISIONS ------------------------------
    // Date       Name                 Tracking #         Description
    // ---------  -------------------  -------------      ----------------------
    // 20JAN2008  James Shen                 	          Initial Creation
    ////////////////////////////////////////////////////////////////////////////
    /**
     * key release.
     */
    protected void keyReleased(int keyCode) {
        int gameAction=0;
        switch(keyCode){
            case Canvas.KEY_NUM2:
                gameAction=Canvas.UP;
                break;
            case Canvas.KEY_NUM8:
                gameAction=Canvas.DOWN;
                break;
            case Canvas.KEY_NUM5:
                gameAction=Canvas.FIRE;
                break;
            case Canvas.KEY_NUM4:
                gameAction=Canvas.LEFT;
                break;
            case Canvas.KEY_NUM6:
                gameAction=Canvas.RIGHT;
                break;
                
            default:
                gameAction = getGameAction(keyCode);
        }
        if(!isGameover){
            playerTank.keyReleased(gameAction);
        }
    }
    
    
    ////////////////////////////////////////////////////////////////////////////
    //--------------------------------- REVISIONS ------------------------------
    // Date       Name                 Tracking #         Description
    // ---------  -------------------  -------------      ----------------------
    // 20JAN2008  James Shen                 	          Initial Creation
    ////////////////////////////////////////////////////////////////////////////
    /**
     * tick. one game step.
     */
    private void tick(){
        
        for(int i=0;i<totalLayers;i++){
            Layer layer=layerManager.getLayerAt(i);
            if(layer.isVisible()){
                Actor actor=(Actor)layer;
                
                actor.tick();
            }
        }
        applyGameLogic();
    }
    
    
}

⌨️ 快捷键说明

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