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

📄 gameengine.java

📁 J2ME的游戏原代码!希望能帮助有需要帮助的师兄弟们!
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
                                                || m_objErix.m_nErixYPos == m_objErix.m_nErixBoundaryEndPoint){
                                            m_objErix.m_bErixMoving = false;
                                            m_objErix.m_bIsErixOnBoundary = true;
                                        }
                                    } else if(m_objErix.m_nErixDirection == Erix.LEFT_DIRECTION
                                            || m_objErix.m_nErixDirection == Erix.RIGHT_DIRECTION) {
                                        //If his direction was Left/Right then
                                        //check if his x position has reached
                                        //either boundary start or end points.
                                        if(m_objErix.m_nErixXPos == m_objErix.m_nErixBoundaryStartPoint
                                                || m_objErix.m_nErixXPos == m_objErix.m_nErixBoundaryEndPoint){
                                             m_objErix.m_bErixMoving = false;
                                             m_objErix.m_bIsErixOnBoundary = true;
                                        }
                                    }
                                }
                                // End of else of
                                // whether Erix was in the unsecured refgion.

                            } else {
                                //Skipping Erix movement by means of this
                                //counter.
                                m_objErix.m_nDelayCounter++;
                            }
                        } catch(Exception e) {
                            System.out.println("Exception in move or "+
                                                "isPathBeingCrossed()");
                        }
                    } // end of if Erix is moving.



                    //Sort the entities on the screen in order to render them
                    //properly
                    sort();

                    //Allow canvas to listen to key inputs.
                    m_objErix.m_bKeyPressed = false;

                } else if (m_nEngineMode == GameEngine.LEVEL_COMPLETE_MODE) {
                    //Level Complete mode. Increment the level.
                    //Also check if all 24 levels are over.

                        if (m_bytLevelNumber < 24) {
                            // Increment the level number.
                            m_bytLevelNumber++;

                            // Prepare to start the new level.
                            initialiseLevel(m_bytLevelNumber);
                            // The "Level Start" sound has been eliminated as of 20/06/2003
                            //queueSound(GameEffects.LEVEL_START_SOUND);
                        } else {
                            // All 24 levels are over.
                            // The player has won the game.
                            m_nEngineMode = GameEngine.PRE_GAME_WIN_MODE;
                            queueSound(GameEffects.GAME_WIN_SOUND);
                        }

                } else if (m_nEngineMode == GameEngine.LEVEL_INITIALIZE_MODE){
                    m_nDelayCounter++;
                    if (m_nDelayCounter > LEVEL_INITIALIZE_COUNTER) {
                        m_nDelayCounter = 0;
                        initialiseLevel(m_bytLevelNumber);
                        // The "Level Start" sound has been eliminated as of 20/06/2003
                        //queueSound(GameEffects.LEVEL_START_SOUND);
                    }
                } else if (m_nEngineMode == GameEngine.PAUSED_MODE) {
                    //Do Nothing in paused mode.
                } else if (m_nEngineMode == GameEngine.PRE_LEVEL_COMPLETE_MODE) {
                    m_nDelayCounter++;
                    if(m_nDelayCounter > PRE_LEVEL_COMPLETE_COUNTER) {
                        m_nDelayCounter = 0;
                        m_nEngineMode = GameEngine.LEVEL_COMPLETE_MODE;
                    }
                } else if (m_nEngineMode == GameEngine.PRE_GAME_WIN_MODE) {
                    // Delay while the sound effect plays.
                    m_nDelayCounter++;
                    if(m_nDelayCounter > PRE_GAME_WIN_COUNTER){
                        m_nDelayCounter = 0;
                        m_nEngineMode = GameEngine.GAME_WIN_MODE;
                    }
                } else if(m_nEngineMode == GameEngine.LIFE_LOSE_MODE) {
                    // We need to play the "life lose sound" after the screen
                    // has been updated to depict the loss of life.
                    // Therefore, we invoke the above sound only after
                    // atleast one game loop iteration has been skipped.
					if (m_bytLifeCount <= 0) {
						queueSound(GameEffects.GAME_LOSE_SOUND);
						m_nEngineMode = GAME_OVER_MODE;
						// Set a flag to get game display updated.
						m_bUpdateGameScreen = true;
					} else {

						if (m_nDelayCounter == 1) {
							queueSound(GameEffects.LIFE_LOSE_SOUND);
						}
						m_nDelayCounter++;
						if (m_nDelayCounter > LIFE_LOSE_MODE_COUNTER) {
							m_nDelayCounter = 0;
							m_nEngineMode = GameEngine.LEVEL_INITIALIZE_MODE;
						}
					}
/*

                    m_nDelayCounter++;
                    if (m_nDelayCounter > LIFE_LOSE_MODE_COUNTER) {
                        m_nDelayCounter = 0;

                        if (m_bytLifeCount <= 0) {
                            queueSound(GameEffects.GAME_LOSE_SOUND);
                            m_nEngineMode = GAME_OVER_MODE;
                            // Set a flag to get game display updated.
                            m_bUpdateGameScreen = true;
                        } else {
                            m_nEngineMode = GameEngine.LEVEL_INITIALIZE_MODE;
                        }
                    }
*/
                } else if (m_nEngineMode == GameEngine.GAME_OVER_MODE
                        || m_nEngineMode == GameEngine.GAME_WIN_MODE) {
                    m_nDelayCounter++;
                    if (m_nDelayCounter > GAME_WIN_OVER_MODE_COUNTER) {
                        m_nDelayCounter = 0;
                        m_bGameOver = true;
                    }
                } else if (m_nEngineMode == GameEngine.LEVEL_START_MODE) {
                    m_nDelayCounter++;
                    if (m_nDelayCounter > LEVEL_START_MODE_COUNTER) {
                        m_nDelayCounter = 0;
                        m_nEngineMode = GameEngine.NORMAL_MODE;
                        // set this flag to force repainting
                        //m_bUpdateGameScreen = true;
                    }
                }

                //change starts to call the repaint before thread sleep - keshav 4 Jun 2003
                  /*lFinishTime = System.currentTimeMillis();
                lTimeElapsed = lFinishTime - lStartTime;

                if(lTimeElapsed < GAME_LOOP_TIME){
                    //Sleep for the time remaining after finishing the run
                    //method.
                    try {
                        Thread.sleep(GAME_LOOP_TIME - lTimeElapsed);
                    } catch(InterruptedException e) {
                    }
                    // If a game is in normal progress
                    // (i.e., no initializations being performed),
                    // update the game display and
                    // play a sound (if any requested).
                    if(m_nEngineMode != GameEngine.LEVEL_INITIALIZE_MODE
                            && m_nEngineMode != GameEngine.LEVEL_COMPLETE_MODE){
                        m_objCanvas.repaint();
                        //System.out.println("************repaint called ** m_nDelayCounter is ");
                        // Play a sound if any has been requested.
                        if (m_nHighestPrioritySoundRequested != GameEffects.NO_SOUND) {
                            m_objEffects.playSound(m_nHighestPrioritySoundRequested);
                            // Close the request, because it has been handled.
                            m_nHighestPrioritySoundRequested = GameEffects.NO_SOUND;
                        }
                    }
                }else {
                    System.out.println("lTimeElapsed >= GAME_LOOP_TIME...hence not painted");
                }*/

                // If a game is in normal progress
                // (i.e., no initializations being performed),
                // update the game display and
                // play a sound (if any requested).
                if (m_nEngineMode != GameEngine.LEVEL_INITIALIZE_MODE
                                                && m_nEngineMode != GameEngine.LEVEL_COMPLETE_MODE){
                    m_objCanvas.repaint();
                    m_objCanvas.serviceRepaints();

                    // Play a sound if any has been requested.
                    if (m_nHighestPrioritySoundRequested != GameEffects.NO_SOUND) {
                        m_objEffects.playSound(m_nHighestPrioritySoundRequested);
                        // Close the request, because it has been handled.
                        m_nHighestPrioritySoundRequested = GameEffects.NO_SOUND;
                    }
                }

                lFinishTime = System.currentTimeMillis();
                lTimeElapsed = lFinishTime - lStartTime;

                try{
                    if (lTimeElapsed < GAME_LOOP_TIME) {
                    //Sleep for the time remaining after finishing the run()
                        synchronized(this)
                        {
                            wait(GAME_LOOP_TIME - lTimeElapsed);
                        }
                    } else {
                        Thread.currentThread().yield();
                    }
                } catch(InterruptedException e) {
                }
                //change ends to call the repaint before thread sleep - keshav 4 Jun 2003

            } // End of the while loop that is the main game loop.
        } catch(Exception e) {
            System.out.println("Engine stopped - " + e);
            e.printStackTrace();
        }

        if (m_nEngineMode == GAME_OVER_MODE || m_nEngineMode == GAME_WIN_MODE) {
            m_objMidlet.gameOver();
        }
        m_nEngineMode = -1;
    } // end of method "run"


    /**
     * Sorts the entities (Erix, Enemies and Pillars) into the order in which they
     * should be painted.
     * To achieve the "3D" look, it is important that when two entities are
     * passing close to each other, they should overlap according to their
     * "distance" from the end of the play area from which the user is viewing
     * the game. For this purpose, the sort() method is invoked at the end of
     * every iteration of the game loop. Erix and the Enemies are sorted in the
     * descending order of their distance from the bottom of the playfield. In
     * case this quantity is the same for two entities, they are sorted in
     * descending order of their distances from the left edge of the playfield.
     */
    private void sort() {
        try{
            byte nTempEntity;
            int nYValue1;
            int nYValue2;
            for(int i = 0; i < m_nNumberOfEnemies;i++){
                if(m_arrGameEntities[i] == -1){
                    nYValue1 = m_objErix.m_nErixYPos;
                }else{
                    nYValue1 = m_objEnemy[m_arrGameEntities[i]].m_nEnemyYPos;
                }

                if(m_arrGameEntities[i+1] == -1){
                    nYValue2 = m_objErix.m_nErixYPos;
                }else{
                    nYValue2 = m_objEnemy[m_arrGameEntities[i+1]].m_nEnemyYPos;
                }

                if(nYValue1 > nYValue2){
                    nTempEntity = m_arrGameEntities[i];
                    m_arrGameEntities[i] = m_arrGameEntities[i+1];
                    m_arrGameEntities[i+1] = nTempEntity;
                }
            }
        } catch(Exception e) {
            System.out.println("Exception in sorting entities - "+e);
        }
    }


    /**
     * Checks for the collision of enemies with the boundaries of the
     * "unsecured" area, and also with Erix.
     * The return value of this method is used by the GameEngine in the
     * following way:
     * <ul>
     *     <li> If the return value indicates that the concerned Enemy collided
     *          with the boundary, the GameEngine changes the direction of the
     *          Enemy using the setDirection() method of the Enemy. The
     *          direction of the Enemy after the collision varies from its
     *          direction before the collision by 90 degrees.
     *     <li> If the return value indicates that the Enemy collided with
     *          Erix or Erix's path, the GameEngine requests the 'Lose Life'
     *          sound to be played, and uses the processLoseLife() method to
     *          do the required processing for the player losing a life.
     * </ul>
     * @param nEnemyIndex The index of the enemy in the enemy object array
     * with which the collision is to be detetcted.
     * @return Indicates whether Enemy collided with Erix or bounced of with the
     * boundary.
     */
    private int checkEnemyCollision(int nEnemyIndex) {
        int nReturnValue = 0;

            // Check the enemy's collision with Erix itself:
            // Check each enemy's current position with Erix to
            // determine if they coincide. If so, this is a collision.
            if (m_objEnemy[nEnemyIndex].m_nEnemyXPos == m_objErix.m_nErixXPos
                    && m_objEnemy[nEnemyIndex].m_nEnemyYPos == m_objErix.m_nErixYPos) {
                return ENEMY_ERIX_COLLISION;
            }

            // Check the enemy's collision with Erix's path:
            int nRowNum = 0;
            int nColNum = 0;
            int nBitPosition = 0;
            byte bytPathBits = 0;

            try {
                nRowNum = m_objEnemy[nEnemyIndex].m_nEnemyYPos;
                nColNum = m_objEnemy[nEnemyIndex].m_nEnemyXPos >> 3;
                nBitPosition = m_objEnemy[nEnemyIndex].m_nEnemyXPos % 8;
                bytPathBits = m_objErix.m_arrPathBitmap[nRowNum][nColNum];
                switch(nBitPosition){
                    case 0:
                        bytPathBits &= Erix.ZERO_BIT_SET;
                        if(bytPathBits == Erix.ZERO_BIT_SET){
                            nReturnValue = ENEMY_ERIX_COLLISION;
                        }
                        break;
                    case 1:
                        bytPathBits &= Erix.FIRST_BIT_SET;
                        if(bytPathBits == Erix.FIRST_BIT_SET){
                            nReturnValue = ENEMY_ERIX_COLLISION;
                        }
                        break;
                    case 2:
                        bytPathBits &= Erix.SECOND_BIT_SET;
                        if(bytPathBits == Erix.SECOND_BIT_SET){
                            nReturnValue = ENEMY_ERIX_COLLISION;
                        }
                        break;
                    case 3:
                        bytPathBits &= Erix.THIRD_BIT_SET;
                        if(bytPathBits == Erix.THIRD_BIT_SET){
                            nReturnValue = ENEMY_ERIX_COLLISION;
                        }
                        break;
                    case 4:
                        bytPathBits &= Erix.FOURTH_BIT_SET;
                        if(bytPathBits == Erix.FOURTH_BIT_SET){
                            nReturnValue = ENEMY_ERIX_COLLISION;
                        }
                        break;

⌨️ 快捷键说明

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