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

📄 gamescene.java

📁 J2ME超级坦克大战源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:

import java.io.*;
import java.util.*;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;


public final class GameScene extends GameCanvas implements Runnable{
    
    
    public static int []enemyTanksCount=new int[4];
    
    /**
     * Total enemy tanks in one leve.
     */
    private static final int TOTAL_ENEMY_TANKS=20;
    
    /**
     * total enemy tanks remains.
     */
    public static int enemyTankRemains=TOTAL_ENEMY_TANKS;
    
    /**
     * whether can put a poweup, it's because a red tank is been shot dead or
     * in the given period.
     */
    public static boolean canPutPowerup=false;
    
    ////////////////////////////////////////////////////////////////////////////
    
    /**
     * The layer manager manage all actors in the game.
     */
    private LayerManager layerManager=new LayerManager();
    
    /**
     * Player's tank.
     */
    private PlayerTank playerTank;
    
    /**
     * Resource Manager.
     */
    private static ResourceManager resourceManager=ResourceManager.getInstance();
    
    /**
     * The battle field object.
     */
    private BattleField battleField=null;
    
    /**
     * This thread is core game logic.
     */
    private volatile Thread animationThread = null;
    
    /**
     * time taken and MILLIS_PER_TICK control the game speed
     */
    private long timeTaken = 0;
    
    /**
     *time taken and MILLIS_PER_TICK control the game speed
     */
    private static final int MILLIS_PER_TICK = 1;
    
    /**
     * the width of the game scene.
     */
    private static int sceneWidth;
    
    /**
     * the height of the game scene.
     */
    private static int sceneHeight;
    
    /**
     * the height of the score bar.
     */
    private static int barHeight=32;
    
    /**
     * the X origin of the battle field.
     */
    private int battleFieldX;
    
    /**
     * the Y origin of the battle field.
     */
    private int battleFieldY;
    
    /**
     * default lives of player
     */
    private final static int DEFAULT_PLAYER_LIVE=9;
    
    /**
     *  maximum game leves.
     */
    private static final int TOTAL_GAME_LEVELS=50;
    
    /**
     * total actors in the scene.
     */
    private int totalLayers=0;
    
    /**
     * is game over?
     */
    private boolean isGameover=false;
    
    /**
     * game over image
     */
    private static Image imgGameover=
            resourceManager.getImage(ResourceManager.GAME_OVER_SMALL);
    
    /**
     * pause image
     */
    private static Image imgPause=
            resourceManager.getImage(ResourceManager.PAUSE);
    
    /**
     * black number image from 0 to 9
     */
    private static Image imgNumberBlack=
            resourceManager.getImage(ResourceManager.NUMBER_BLACK);
    
    /**
     * enemy icon
     */
    private static Image imgEnemyIcon=
            resourceManager.getImage(ResourceManager.ENEMY_ICON);
    
    /**
     * first player icon
     */
    private static Image imgIP=
            resourceManager.getImage(ResourceManager.IP);
    
    /**
     * flag image
     */
    private static Image imgFlag=
            resourceManager.getImage(ResourceManager.FLAG);
    
    /**
     * offset X where start to draw the score bar
     */
    private int marginX=0;
    
    /**
     * offset Y where start to draw the score bar
     */
    private int marginY=0;
    
    /**
     * Random object to create random numbers.
     */
    private static Random rnd=new Random();
    
    /**
     * time control to create new enemy tank
     */
    private long enemySpawnStartTime=0;
    
    /**
     * minimun spawn timer
     */
    private final static long enemySpawnPeriod=2000;
    
    /**
     * timer control when to put an poweup in the battle field
     */
    private long putPowerupStartTime=0;
    
    /**
     * put poweup minimum period, 2 minutes
     */
    private final static long putPowerupPeriod=120000;
    
    /**
     * display game over or pause
     */
    private static Sprite gameStatus=null;
    
    /**
     * timer control when game over to display game over image,from bottom
     * to the middle of the screen.
     */
    private long gameOverStartTime=0;
    
    /**
     * after this period, display the score screen,default 4 seconds.
     */
    private final static long gameOverPeriod=4000;
    
    
    
    public GameScene() {
        super(false);
        setFullScreenMode(true);
        sceneWidth=getWidth();
        sceneHeight=getHeight();
        try{
            int xTiles=sceneWidth/ResourceManager.TILE_WIDTH;
            int yTiles=(sceneHeight-barHeight)/ResourceManager.TILE_WIDTH;
            if(xTiles % 2 ==0) xTiles--;
            xTiles=Math.min(xTiles,yTiles);
            yTiles=xTiles;
            battleFieldX=(sceneWidth-xTiles*ResourceManager.TILE_WIDTH)/2;
            battleFieldY=(sceneHeight-battleFieldX-xTiles*ResourceManager.TILE_WIDTH);
            battleField=new BattleField(xTiles,yTiles);
            battleField.initBattlefield(this.getClass().getResourceAsStream("/1.txt"));
            Powerup.setBattleField(battleField);
            Powerup.setLayerManager(layerManager);
            Tank.setBattleField(battleField);
            Tank.setLayerManager(layerManager);
            Bullet.setBattleField(battleField);
            Bullet.setLayerManager(layerManager);
            Explosion.setBattleField(battleField);
            Explosion.setLayerManager(layerManager);
            Score.setBattleField(battleField);
            Score.setLayerManager(layerManager);
            playerTank=Tank.getPlayerTank();
            layerManager.append(battleField);
            int offset=8;
            marginX=(sceneWidth-(imgEnemyIcon.getWidth()*10+offset*2
                    +imgIP.getWidth()+imgFlag.getWidth()+imgNumberBlack.getHeight()))/2;
            marginY=(sceneHeight-battleField.getHeight())/2;
            totalLayers=layerManager.getSize();
            gameStatus=new Sprite(imgGameover);
            gameStatus.setPosition((sceneWidth-gameStatus.getWidth())/2,
                    (sceneHeight-gameStatus.getHeight())/2);
            gameStatus.setVisible(false);
            newGame();
        }catch(IOException e){
            //Alert()
        }
        
    }
    
   
    public void newGame(){
        if(ResourceManager.isPlayingSound){
            ResourceManager.playSound(ResourceManager.OPEN_SOUND);
        }
        playerTank.initTank();
        playerTank.setAvaiableLives(DEFAULT_PLAYER_LIVE);
        EnemyTank.explodeAllEmenies();
        Bullet.stopAllBullets();
        Explosion.stopAllExplosions();
        Powerup.removeAllPowerups();
        Powerup.putNewPowerup(Powerup.HOME);
        enemyTankRemains=TOTAL_ENEMY_TANKS;
        timeTaken=0;
        canPutPowerup=false;
        enemySpawnStartTime=0;
        enemySpawnStartTime=0;
        putPowerupStartTime=0;
        gameOverStartTime=0;
        gameStatus.setVisible(false);
        //reset player's score to zero.
        if(isGameover){
            playerTank.setScore(0);
        }
        isGameover=false;
        rnd=new Random();
        for(int i=0;i<4;i++) enemyTanksCount[i]=0;
        String fileName="/level" + ResourceManager.gameLevel;
        try{
            //battleField.initBattlefield(this.getClass().getResourceAsStream(fileName));
            battleField.readBattlefieldFromHZK(ResourceManager.gameLevels[
                    ResourceManager.gameLevel]);
        }catch(Exception e){
            //inglore the exception.
        }
        stop();
        start();
    }
    
    
    private void drawNumber(Graphics g, int number,int x,int y){
        Image imageNumber=imgNumberBlack;
        String strNumber=String.valueOf(number);
        int numberWidth=imageNumber.getHeight();
        for(int i=0;i<strNumber.length();i++){
            char ch=strNumber.charAt(i);
            int index=(ch-'0') % 10;
            Image oneNumber=Image.createImage(imageNumber,index*numberWidth,0,
                    numberWidth,numberWidth,0);
            g.drawImage(oneNumber,x+i*numberWidth,y,0);
        }

⌨️ 快捷键说明

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