games_starwar.c

来自「几个嵌入式手机平台小游戏c源代码」· C语言 代码 · 共 2,173 行 · 第 1/5 页

C
2,173
字号
            ds_refresh();
#else
            Games_refresh();
#endif
        break;

        case OPUS_TIMER_GAME_TIMING:
            if (!gamePause && inGame)
                StarwarRun();
            break;

        case OPUS_FOCUS_KEYPAD :
            pKey = (KEYEVENT_STRUCT *)pMess;

            if(pKey->state == UHKEY_RELEASE) 
            {
               keyUp(pKey->code);
               break;
            }
            keyDown(pKey->code);
            break;
            
        case OPUS_TIMER_POPUP_MSG:
            if (gamesApp_data.popupwin != OP_NULL)
            {
                 close_old_popup();
            }
            Games_change_state((OP_UINT8) APP_GAMES_STATE_MENU);
            break;

        case OPUS_FOCUS_CUST_APP_PAUSE:
            gamePause=OP_TRUE;
            OPUS_Stop_Timer(OPUS_TIMER_GAME_TIMING);
            hmi_vibrator_off(); 
            if (gamesApp_data.popupwin!= OP_NULL)
            {
                close_old_popup();
                Games_change_state((OP_UINT8) APP_GAMES_STATE_MENU);
            }
            break;
            
        default :
          *handle = OP_FALSE;
  break;
  }
}


/*==================================================================================================
    FUNCTION: APP_Starwar_Init

    DESCRIPTION:
        Description of this specific function.

    ARGUMENTS PASSED:

    RETURN VALUE:

    IMPORTANT NOTES:
        
==================================================================================================*/
static void APP_Starwar_Init(void)
{
    OP_INT16 n;
    clear_full_screen(STARWAR_BG_COLOR);

    current_level = 0;
    atLevelEnd = OP_FALSE;
    total_enemies = 0;
    boss.lives= -1;
    missile_count = 3;
    
    srand( op_get_cur_tod()); /* set the seed for rand() */
    
    xSize = LCD_MAX_X- borderwidth*2; // the width of the real display region
    ySize = LCD_MAX_Y- borderwidth*2;

    shipX = (xSize - shipWidth) / 2;  // the original position of the ship
    shipY = ySize - scoreheight - borderwidth - shipHeight;

    for (n=0; n<maxBullet; n++) 
    {
        playerFires[n].active = OP_FALSE;
        playerFires[n].frame=-1;
        enemyFires[n].active = OP_FALSE;
        enemyFires[n].frame=-1;
    }

    for (n=0; n<maxMissile; n++) 
    {
        missiles[n].enemy_id= -1;
        missiles[n].frame=-1;
    }
    
    for(n = 0; n < maxEnemies; n++)
    {
        DelEnemy(n);
    }

    enemyCount = 0;
    enemyMode=-1;

    gamePause=OP_FALSE;
    canFire=OP_TRUE;
    isGameOver=OP_FALSE;
    
    keepFiring=OP_FALSE;

    // Set Up Ship variables
    scur = smax;
    distance=0;
    score=0;
    shipVibrationCount = 0;

#ifdef PACIFIC_VERSION
    ds_draw_icon_rm(shipX,shipY, ship);
#else
    Games_direct_draw_icon_rm(shipX,shipY, ship);
#endif
    for (n = 0; n < numStars; n++) 
    {
        ds_put_pixel(starsX[n], starsY[n], starsC[n]);
    }
    
    ShowScore();
    ShowIntroScreen();
#ifdef PACIFIC_VERSION
    ds_refresh();
#else
    Games_refresh();
#endif
}


/*==================================================================================================
    FUNCTION: StarwarRun

    DESCRIPTION:
        Description of this specific function.

    ARGUMENTS PASSED:

    RETURN VALUE:

    IMPORTANT NOTES:
        
==================================================================================================*/
static void StarwarRun(void)
{
    if (isGameOver)
    {
        GameOver();
        return;
    }
    
    switch(current_level)
    {
        case 0:
            cantEnemies=4;
            break;
        case 1:
            cantEnemies=5;
            break;
        case 2:
            cantEnemies=6;
            break;
        case 3:
            cantEnemies=7;
            break;
        case 4:
            cantEnemies=8;
            break;
    }

#ifdef PACIFIC_VERSION
    clear_full_screen(STARWAR_BG_COLOR);
#else
#ifdef WIN32
    clear_full_screen(STARWAR_BG_COLOR);
#else
    clear_all_objects();
#endif
#endif
   
    moveBullet();
    moveEnemies();
    NewEnemyFire();
    moveStars();
    MoveShip();

    Collisions();

    /* when atLevelEnd is true, there should be no new enemies, until the ship move to the top of the screen */
    if (atLevelEnd == OP_FALSE)  
    {
        NewEnemies();
        if(keepFiring && inGame && canFire)  
        {            
            NewPlayerFire();  
        }
        testCrash();
    }

    distance++;
    if (distance % 3 == 0)
        canFire=OP_TRUE;
   if (distance % 200 == 0) 
    {
        if (scur<smax)
        {
            scur++;
        }
        if (cantEnemies<maxEnemies)
        {
            cantEnemies++;
        }
        missile_count++;
    }

    repaint();
    ShowScore();
#ifdef PACIFIC_VERSION
    ds_refresh();
#else
    Games_refresh();
#endif

    shipVibrationCount--;
    if (shipVibrationCount< 0)
    {
        hmi_vibrator_off(); 
    }

    OPUS_Start_Timer(OPUS_TIMER_GAME_TIMING, 80, 0,  PERIODIC);
    
} 

static DS_COLOR NewColor(void)
{
     OP_UINT16 rgb[3];
     OP_UINT8 t;
     OP_UINT8 i;
     for (i=0; i<3; i++) 
        rgb[i] = 0;
     t = (OP_UINT8) (rand()%3);
     rgb[t] = (OP_UINT16) (rand() % 256);
     return _RGB(rgb[0], rgb[1], rgb[2]);
}

static void keyDown(OP_INT16 key)
{
    switch(key)
    {
        case KEY_LEFT:
        case KEY_5: 
            if (inGame && atLevelEnd == OP_FALSE)
            {            
                dx=-1;
            }
            break;

        case KEY_RIGHT:
        case KEY_6:
            if (inGame && atLevelEnd == OP_FALSE)
            {            
                dx=1;
            }
            break;

        case KEY_UP:
        case KEY_3: 
            if (inGame && atLevelEnd == OP_FALSE)
            {            
                dy=-1;
            }
            break;

        case KEY_DOWN:
        case KEY_9:
            if (inGame && atLevelEnd == OP_FALSE)
            {            
                dy=1;
            }
            break;

        case KEY_OK:
        case KEY_4: 
            if (atLevelEnd == OP_FALSE)
            {
                keepFiring=OP_TRUE; /* if keep pressing OK, keep firing */
                if(inGame && canFire)  
                {            
                    NewPlayerFire();  
                }
            }
            break;

        case KEY_1: 
            if (inGame && atLevelEnd == OP_FALSE)
            {            
                if (missile_count>0)
                {
                    NewMissile();
                }
            }
            break;

        case KEY_SOFT_LEFT:
            if(gamesApp_data.popupwin == OP_NULL)  /* if there is a popup, it means that a game is over */
            {
                if (inGame)
                {            
                    inGame=OP_FALSE;
                    hmi_vibrator_off(); 
                }
                else
                {
                    inGame=OP_TRUE;
#ifndef WIN32
                    if (isShowingIntro)
                    {
                        isShowingIntro = OP_FALSE;
#ifdef PACIFIC_VERSION
                        ds_fill_rect(0, 0, LCD_MAX_X_COOR, LCD_MAX_Y_COOR, STARWAR_BG_COLOR);
#else
                        Games_direct_fill_rect(0, 0, LCD_MAX_X_COOR, LCD_MAX_Y_COOR, STARWAR_BG_COLOR);
#endif
                    }
#endif 
                    //OPUS_Start_Timer(OPUS_TIMER_GAME_TIMING, 50, 0,  PERIODIC);
                    StarwarRun();
                }
            }
            break;
            
        case KEY_SOFT_RIGHT:
        case KEY_CLEAR:
            if(inGame)
            {
                GameOver();
            }
            else
            {
                inGame=OP_FALSE;
                close_old_popup();
                OPUS_Stop_Timer(OPUS_TIMER_GAME_TIMING);
                Games_change_state((OP_UINT8) APP_GAMES_STATE_MENU);
            }
        break;

        case KEY_END:
            inGame=OP_FALSE;
            OPUS_Stop_Timer(OPUS_TIMER_GAME_TIMING);
            break;
           
    }
}

static void keyUp(OP_INT16 key)    // if release the pressed key, stop moving
{
    if (atLevelEnd == OP_FALSE)
    {
        if (key == KEY_LEFT || key == KEY_RIGHT || key == KEY_5 || key ==  KEY_6)
             dx=0;
        if (key == KEY_UP || key == KEY_DOWN || key == KEY_3 || key == KEY_9)
             dy=0;
        if (key == KEY_OK || key == KEY_4)
            keepFiring=OP_FALSE;
    }
}

static void repaint(void)
{
    OP_INT8 i;
    // Show stars
    for (i = 0; i < numStars; i++) 
    {
#ifdef PACIFIC_VERSION
        ds_put_pixel(starsX[i], starsY[i], starsC[i]);

#else
#ifdef WIN32
        ds_put_pixel(starsX[i], starsY[i], starsC[i]);
#else
        Games_direct_fill_rect(starsX[i], starsY[i], starsX[i], starsY[i], starsC[i]);
#endif
#endif
    }

    ShowEnemies();
    ShowBullet();
#ifdef PACIFIC_VERSION
    ds_draw_icon_rm(shipX, shipY, ship);
#else
    Games_direct_draw_icon_rm(shipX, shipY, ship);
#endif
    if (boss.lives > 0)
    {
#ifdef PACIFIC_VERSION
        ds_draw_icon_rm(boss.x, boss.y, bossID[boss.id]);
#else
        Games_direct_draw_icon_rm(boss.x, boss.y, bossID[boss.id]);
#endif
    }
    
    /* draw the boom frame when the ship is hit */
    /*if (shipBoomFrame>0)
    {
        Games_direct_draw_icon_rm(shipX+(shipWidth-bomWidth)/2, shipY+(shipHeight-bomHeight)/2,  boom[shipBoomFrame-1]);
        shipBoomFrame--;
        if (shipBoomFrame < bframes - 1)
        {
            hmi_vibrator_off(); 
        }
    }*/
    
    /* draw the engine fire of the ship */
#ifdef PACIFIC_VERSION
    ds_draw_icon_rm((OP_INT16)(shipX+( (shipWidth-fireWidth) / 2 )), (OP_INT16)(shipY+shipHeight), engineFire[distance % 2]);
#else
    Games_direct_draw_icon_rm((OP_INT16)(shipX+( (shipWidth-fireWidth) / 2 )), (OP_INT16)(shipY+shipHeight), engineFire[distance % 2]);
#endif

}


static void ShowIntroScreen(void)
{
    OP_INT16 x;
    OP_INT16 y;
    OP_UINT8 ustr[MAX_STRING_LENGTH];

    isShowingIntro = OP_TRUE;

    op_memset((void*)ustr, 0, MAX_STRING_LENGTH); 

⌨️ 快捷键说明

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