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

📄 gamemodel.java

📁 J2ME的游戏原代码!希望能帮助有需要帮助的师兄弟们!
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
package com.centerscore.game;

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

class GameModel implements Runnable {
    static boolean g_bPaused;
    BackGroundSprite m_pBackGroundSprite;
    byte g_Game_Mode;

    private GameMidlet m_pGameMidlet;
    private GameScreen m_pGameScreen;
    private Thread m_pDisplayThread;
    private Random m_pRandom;
    private boolean m_bRunning; // main loop run() is controlled by this
    private int m_waveBonus;

    // Board Model
    // Its a  array, ie. the board size. We can figure out how many cubes are on
    // each row because its the row index.
    // { row/cube#} {Cube color}
    private int[][] m_arr2BoardLayout = new int[Globals.QBERT_DIMENSIONS][Globals.QBERT_DIMENSIONS];

    // This is the available disc model.
    // It correlates with the rows and left/right edges of the board.
    // True = disc, False = !disc
    private boolean[][] m_arr2AvailableDiscs = new boolean[Globals.QBERT_DIMENSIONS][2];

    // Stage Details
    // This basically just holds the number/availability of characters, ie. wether
    // or not a char can be load'd or if they already exist. This might be changed
    // later to just each be a seperate variable.
    // Index: discs left, #red, #coily, #green, #slick, #sam, #ugg, #wrong-way, #purple ball
    private int[] m_arrBoardDetails = new int[9];

    // Variables for doing disc movement with qbert on them
    private boolean m_bDrop_Back_On_Board;
    private int g_Disc_Num_In_Use;

    private boolean g_bFinished_Lvl; // SD necessary?
    private boolean g_bGame_Playing = true;
    private boolean g_bCheck_Falling;
    private boolean g_bEnd_Stage;

    // Stage/Board info
    // cube colors (side)
    private int m_leftColor;
    private int m_rightColor;

    private int m_level;
    private int m_round;
    private int m_transitionTicks;

    private int m_startColor;
    private int m_endColor;
    private int m_interColor1;
    private byte m_cubeTurnBackType;

    private int m_numCubeColors;

    private int g_TimeStop;

    // Character's
    private CharacterSprite m_temp_enemy;
    private CharacterSprite m_pQBert;

    private int g_ToDisc_Cube;
    private int g_ToDisc_Row;
    private boolean m_bQbertOnDisc;

    private boolean m_bCoilyWaiting;

    private CharacterSprite[] g_Arr1Enemies = new CharacterSprite[10];

    // Enemy creation stuff
    private byte m_spawnTimerDuration;
    private byte m_monsterSpeedDelay;

    private int m_cur_Num_Enemies;
    private int m_numEnemies;
    private int m_tick_For_Enemy_Creation;
    private int m_maxEnemiesThisRound;

    // mode crap
    private CountdownTimerMS m_pModeTimer = new CountdownTimerMS();
    private CharacterSprite m_pQBert_Intro;

    private int m_qbertIntroStepDelay;
    private int m_qbertIntroStep;
    private int m_maxQbertIntroSteps;
    private int m_qbertLocX, m_qbertLocY;

    private int[][] m_arr2IntroColorGrid = new int[2][2];

    private byte[] m_arrQbert_IntroSteps;
    private byte m_qbertHopDelay;

    // intro keys
    private int m_demoKey;

    // Disc info
    static CharacterSprite[] g_Arr1Discs = new CharacterSprite[7];
    private int m_numDiscs;
    //private boolean m_bFlashOn;
    private int m_flashCounter;

    // key variables
    // ----------------------------------------
    private int m_keyCachedKeyValue;
    private boolean[] m_arrKeysDown = new boolean[4];


    GameModel(GameMidlet midlet, GameScreen gameScreen) {
        m_pRandom = new java.util.Random();
        setMode(Globals.MODE_INTRO);
        m_pGameMidlet = midlet;
        m_pGameScreen = gameScreen;

        QbertSound.initSound(this);

        m_pBackGroundSprite = new BackGroundSprite(m_arr2BoardLayout);
    }


    void start(boolean bResume, boolean bReset) {
        m_bRunning = true;

        // release all keys
        m_arrKeysDown[Globals.UPLEFT] = false;
        m_arrKeysDown[Globals.UPRIGHT] = false;
        m_arrKeysDown[Globals.DOWNLEFT] = false;
        m_arrKeysDown[Globals.DOWNRIGHT] = false;

        if (bResume) {
            synchronized (this) {
                g_bPaused = false;
                m_pModeTimer.restartTimer();
            }
        } else {
            if (bReset) {
                clearStage(true);
            }
            if (m_pDisplayThread == null) {
                m_pDisplayThread = new Thread(this);
                m_pDisplayThread.start();
            }
        }
    }

    private void clearStage(boolean bNewGame) {
        m_bDrop_Back_On_Board = false;
        m_demoKey = -1;
        g_Disc_Num_In_Use = 0;
        m_bCoilyWaiting = false;
        // SD setMode(Globals.MODE_INTRO);
        m_transitionTicks = 15;
        g_bGame_Playing = false;
        g_bEnd_Stage = false;
        m_tick_For_Enemy_Creation = 0;
        g_ToDisc_Cube = 1;
        g_ToDisc_Row = 1;
        g_bCheck_Falling = true;
        g_bFinished_Lvl = false;

        int x = Globals.QBERT_START_X;
        int y = Globals.QBERT_START_Y;
        int dir = (int) Globals.DOWNRIGHT;
        int row = 1;
        int cube = 1;
        int score = 0;
        int lives = 2;

        if (m_pQBert != null && !bNewGame) {
            score = m_pQBert.charScore();
            m_pBackGroundSprite.updateScore(score);
            lives = m_pQBert.charLives();
        }

        m_pQBert = new CharacterSprite(x, y, dir, Globals.QBERT, row, cube, score, lives);
        m_pQBert.setImageCycle(3);

        m_qbertHopDelay = 0;

        if (bNewGame) {
            m_pQBert.charLives(2);
            m_pBackGroundSprite.updateLivesLeft(2);
            m_pQBert.charScore(0);
            m_pBackGroundSprite.updateScore(score);
            m_round = 0;
            m_level = 0;
            g_bPaused = false;
            m_pBackGroundSprite.updateLvl(m_level);
            m_pBackGroundSprite.updateRound(m_round);
            m_maxEnemiesThisRound = Globals.BASE_ENEMIES_PER_STAGE; // base NUMBER
        }

        // reinstantiate level info
        setRoundVariables();

        for (int i = 0; i < Globals.QBERT_DIMENSIONS; i++) {
            for (int j = 0; j < Globals.QBERT_DIMENSIONS; j++) {
                m_arr2BoardLayout[i][j] = m_startColor;
            }
        }

        makeDiscs();
        clearEnemies();

        m_pBackGroundSprite.forceRedraw();

        if (m_round == 0) {
            setMode(Globals.MODE_INTRO);
        } else {
            setMode(Globals.MODE_NORMAL);
        }

        // clear keys
        m_keyCachedKeyValue = 0;
    }


    void handleKeyPress(int key) {
        //m_pQBert.charOnBoard()) {
        if (key == Globals.KEY_1_UL) {
            m_arrKeysDown[Globals.UPLEFT] = true;
            m_keyCachedKeyValue = key;
        } else if (key == Globals.KEY_1_UR) {
            m_arrKeysDown[Globals.UPRIGHT] = true;
            m_keyCachedKeyValue = key;
		/* Start	Modified		14-05-2003		for key 4,6	*/
        } else if (key == Globals.KEY_1_LL || key == Globals.KEY_1_DDL) {
            m_arrKeysDown[Globals.DOWNLEFT] = true;
            m_keyCachedKeyValue = key;
        } else if (key == Globals.KEY_1_LR || key == Globals.KEY_1_DDR) {
            m_arrKeysDown[Globals.DOWNRIGHT] = true;
            m_keyCachedKeyValue = key;
		/* End		Modified	14-05-2003	*/
        } else if (m_pGameScreen.getGameAction(key) == Canvas.FIRE) {
            m_pGameMidlet.commandAction(m_pGameMidlet.m_cmdPause, m_pGameScreen);
        }
    }

    void handleKeyRelease(int key) {

        if (key == Globals.KEY_1_UL) {
                m_arrKeysDown[Globals.UPLEFT] = false;
        } else if (key == Globals.KEY_1_UR) {
                m_arrKeysDown[Globals.UPRIGHT] = false;
		/* Start	Modified		14-05-2003	for key 4,6	*/
        } else if (key == Globals.KEY_1_LL || key == Globals.KEY_1_DDL) {
                m_arrKeysDown[Globals.DOWNLEFT] = false;
        } else if (key == Globals.KEY_1_LR || key == Globals.KEY_1_DDR) {
                m_arrKeysDown[Globals.DOWNRIGHT] = false;
		/* End	Modified		14-05-2003	*/
           } else if (key == Globals.KEY_CHEAT_KEY) {
            if (m_pGameMidlet.g_bCheatOn) {
                setLevelComplete();
            }
        }
    }


    private void setRoundVariables() {
        int lvl = Math.min(m_level, 5);
        int[] arrLevelData = Globals.LEVEL_DATA[lvl][m_round];
        // colors ----------------------------------------
        // start color
        m_startColor = arrLevelData[Globals.LVL_INDEX_START_CLR];
        m_numCubeColors = arrLevelData.length - Globals.LVL_INDEX_START_CLR ;
        // end color is at end of array
        m_endColor = arrLevelData[Globals.LVL_INDEX_START_CLR + m_numCubeColors - 1];
        m_leftColor = arrLevelData[Globals.LVL_INDEX_LEFT_CLR];
        m_rightColor = arrLevelData[Globals.LVL_INDEX_RIGHT_CLR];

        if (m_numCubeColors > 2)
            m_interColor1 =  arrLevelData[Globals.LVL_INDEX_START_CLR + 1];
        m_pBackGroundSprite.setColors(m_startColor, m_endColor,
                                      m_leftColor, m_rightColor);

        // cubes
        m_cubeTurnBackType = (byte)arrLevelData[Globals.LVL_INDEX_CUBE_TURN_BACK_TYPE];

        // enemies
        m_spawnTimerDuration = (byte) arrLevelData[Globals.LVL_INDEX_SPAWN_TIMER];
        m_monsterSpeedDelay = (byte) arrLevelData[Globals.LVL_INDEX_MONSTER_SPEED];

        // enemy count
        m_maxEnemiesThisRound = Math.max(Globals.MAX_ENEMIES_ON_A_STAGE, Globals.BASE_ENEMIES_PER_STAGE + m_level);
    }


    private void makeDiscs() {
        int lvl = Math.min(5, m_level);

        int count = 0;

        CharacterSprite disc;
        int x = 0;
        int y = 0;
        int dir = 0;
        int cube = 0;
        int row = 0;

        for (int i = 0; i < Globals.MAX_DISCS; i++) {
                g_Arr1Discs[i] = null;
        }

        for (int i = 0; i < Globals.QBERT_DIMENSIONS; i++) {
            for (int j = 0; j < 2; j++) {
                if (Globals.DISC_DATA[lvl][m_round][i][j]) {
                    // We need a disc
                    m_arr2AvailableDiscs[i][j] = true;
                    row = i;
                    cube = j;
                    x = (Globals.GAME_WIDTH / 2);

                    y = calcDiscY(row);
                    //25 + (((row - 1) * 16) / 4 * 3);
                    if (j == 0) {
                        x += - (8 * row) - 13;
                    } else {
                        x += + (8 * row) + 3;
                        cube = row + 1;
                    }

                    disc = new CharacterSprite(x, y, 0, Globals.DISC, row, cube, 0, 0);
                    disc.setImageCycle(4);
                    disc.charIndex(count);
                    g_Arr1Discs[count] = disc;
                    count++;
                } else {
                    m_arr2AvailableDiscs[i][j] = false;
                }
            }
        }
        m_numDiscs = count;
    }

    private int calcDiscY(int row) {
        return 25 + (((row - 1) * 16) / 4 * 3) + 4;
    }

    void pause() {
        synchronized (this) {
        m_pModeTimer.pauseTimer();
            g_bPaused = true;
        }
    }


    void exit() {
        QbertSound.stopSound();
        m_bRunning = false;
    }


    public void run() {
		//VALLI MR2
		long l1;
        try {
            while (m_bRunning) {
				//VALLI MR2
				l1 = System.currentTimeMillis();
                if (g_bPaused) {
                    // SD updateGameScreen();
                    //try {
                    //    Thread.sleep(100);
                    //} catch(Exception e) {}
                    continue;
                }
                switch (g_Game_Mode) {
                case Globals.MODE_BLANK_BOARD:
                    if (m_pModeTimer.isOver()) {
                        setMode(Globals.MODE_NORMAL);
                    }
                break;
                case Globals.MODE_NORMAL :
                    newGameAction();

                    // do actions for qbert
                    if (m_bQbertOnDisc) {
                        moveQbertOnDisc();
                        calcFutureMoves(g_Arr1Discs, 7);
                        m_tick_For_Enemy_Creation = 0;
                    } else if (m_qbertHopDelay > 0) {

⌨️ 快捷键说明

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