games_starwar.c

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

C
2,173
字号
#ifdef __cplusplus
extern "C" {
#endif

/*==================================================================================================

    MODULE NAME : module-filename.c

    GENERAL DESCRIPTION
        General description of this grouping of functions.

    TECHFAITH Telecom Confidential Proprietary
    (c) Copyright 2002 by TECHFAITH Telecom Corp. All Rights Reserved.
====================================================================================================

    Revision History
   
    Modification                  Tracking
    Date         Author           Number      Description of changes
    ----------   --------------   ---------   -----------------------------------------------------------------------------------
    MM/DD/YYYY   name of author   crxxxxx     Brief description of changes made

    01/14/2003     Steven Lai        PR- PP00004  wrong action when received OPUS_FOCUS_CUST_PAINT event 
    02/12/2003     Steven Lai        PP00064       Fix the bug that it will display a empty screen if we press soft left 
                                                   twice successively at the beginning
    02/12/2003     Steven Lai        PP00065       Add a prompt PMPT_TEXT_STARWAR_INTRO
    03/03/2002   Steven Lai      PP00132      Change some popup timer from absolute number to default value 
    03/01/2003   Steven Lai      PC00009      Restructure Games app
    03/13/2003   Steven Lai      PP00159      Removed some useless code
    03/26/2003   lindawang        C000018     Set new pacific image for the first version.

    04/25/2003   Steven Lai      P000166      The timer event may be dismissed by NVM operations, so if it 
                                              is of one-shot type, the game will stop.
    03-05-29   Chenyu          P000466      Display  the picture of  game  cargoder animation   & fix sublcd   display
                                          is of one-shot type, the game will stop.
    07/02/2003   Steven Lai       C000662     Add a sound-switch for Games App
                                              replace all the App_change_state() with Games_change_state()
                                              fixed some bugs

    Self-documenting Code
    Describe/explain low-level design of this module and/or group of funtions and/or specific
    funtion that are hard to understand by reading code and thus requires detail description.
    Free format !
        
====================================================================================================
    INCLUDE FILES
==================================================================================================*/
#include "APP_include.h"

#include "MENU_main.h"
#include "GAMES_main.h"

#ifdef GAMES_STARWAR

#include <stdlib.h>

#include "uhapi.h"
#include "sp_sysutils.h"
#include "FORMAT_utility.h"

#include "GAMES_highscore.h"
#include "product.h"
/*==================================================================================================
    LOCAL FUNCTION PROTOTYPES
==================================================================================================*/
void APP_Starwar_entry(void);
void App_Starwar_exit(void);
void App_Starwar_handler ( OPUS_EVENT_ENUM_TYPE   event, 
                                 void                   *pMess, 
                                 OP_BOOLEAN             *handle );
static void APP_Starwar_Init(void);
static void on_KeyPress(void  *pMess);
static void StarwarRun(void );
static DS_COLOR NewColor(void); 
static void keyDown(OP_INT16 key);
static void keyUp(OP_INT16 key);
static void repaint(void);
static void ShowIntroScreen(void);
static void ShowScore(void);
static void MoveShip(void);
static void NewPlayerFire(void);
static void NewMissile(void);
static void setMissileTarget(OP_UINT8 missile_id);
static void NewEnemyFire(void);
static void moveBullet(void);
static void ShowBullet(void);
static void ShowEnemies(void);
static void NewEnemies(void);
static void newBoss(void);
static void moveEnemies(void);
static void moveStars (void);
static void Collisions(void);
static void testCrash(void);
static void HitShip(void);
static void DelEnemy(OP_INT16 n);
static void DelPlayerFire(OP_UINT8 n);
static void DelEnemyFire(OP_UINT8 n);
static void levelPassed(void);
static void GameOver(void);
static void drawDigit(OP_INT16 wInteger,OP_INT16 x, OP_INT16 y);
static void Games_refresh(void);
static void Games_direct_draw_icon_rm 
(
    OP_INT16           x,
    OP_INT16           y,
    RM_RESOURCE_ID_T    icon_res_id
);
static void Games_direct_fill_rect 
(
    OP_INT16    left,
    OP_INT16    top,
    OP_INT16    right,
    OP_INT16    bottom,
    DS_COLOR    color
);
#ifndef WIN32
static void clear_all_objects(void);
#endif

/*==================================================================================================
    LOCAL CONSTANTS
==================================================================================================*/
#define         STARWAR_BG_COLOR             COLOR_BLACK   

#define  engineFireFrame    2  // engineFire is a 2 frame animation
#define  borderwidth    0
#define  shipWidth    24
#define  shipHeight    24
#define  fireWidth    8
#define  fireHeight    8  
#define  movex    10   // the move step of ship
#define  movey    5   
#define  scoreheight    20 
#define  bulWidth    8  // playerfire is a image of 8*8
#define  bulHeight    16
#define  missileWidth    8  
#define  missileHeight    8
#define  bomWidth    16
#define  bomHeight    24
#define  bframes    4   /* there are 4 frames for boom animation */
#define  numStars    10

#define  maxBullet  24
#define  maxMissile  3
#define  maxEnemies  30

#define  MAX_STRING_LENGTH    100
/*==================================================================================================
    LOCAL TYPEDEFS - Structure, Union, Enumerations...etc
==================================================================================================*/

/*
 * description of this structure, if needed. - Remember self documenting code
 */

 /*
  * There is a item named "frame" in both of the following 2 structures. Their purpose is to show the boom when collisions.
  * If a bullet hit an object (whether an enemy, a ship, or another bullet), set the frame value to "bframes". Each time when 
  * we refresh the screen, use boom[frame] to draw the boom image, and minus 1 from frame, until frame < 0.
  * If an enemy crash into the ship, set its frame to "bframes".
  */
typedef struct 
{        
    OP_INT8 id;    /* if id < 0, this object has been destroied */
    OP_INT16 x;    
    OP_INT16 y;
    OP_INT16 xSpeed;
    OP_INT16 ySpeed;
    OP_INT8 frame;  /* if the enemy crashed into the ship, show the boom frame */
    OP_INT8 fire; /* when fire = 0, can shoot a enemyfire */
    OP_INT8 lives;
} 
Enemy;

typedef struct {        
    OP_BOOLEAN active;
    OP_INT16 x;    
    OP_INT16 y;
    OP_INT16 speed;
    OP_INT8 frame;  /* if the bullet hit the enemy or the ship, show the boom frame */
} Bullet;

typedef struct {        
    OP_INT8 enemy_id;
    OP_INT16 x;    
    OP_INT16 y;
    OP_INT8 frame;  /* if the bullet hit the enemy or the ship, show the boom frame */
} Missile;

/*==================================================================================================
    LOCAL MACROS
==================================================================================================*/
/*
 * description of this macro, if needed. - Remember self documenting code
 */

/*==================================================================================================
    LOCAL VARIABLES
==================================================================================================*/

static OP_UINT8 current_level;
static OP_BOOLEAN atLevelEnd;
static OP_INT16    shipX, shipY,dx=0, dy=0;
static OP_BOOLEAN canFire; /* when canFire is true , the player can shoot*/
static OP_BOOLEAN keepFiring; /* whether the player is firing */
static OP_INT8 shipVibrationCount;  /* used to indicate whether stop the vibrator */
static RM_RESOURCE_ID_T const ship = ICON_STARWAR_SHIP;
static RM_RESOURCE_ID_T  const engineFire[engineFireFrame]=
{
    ICON_STARWAR_FIRE0,
    ICON_STARWAR_FIRE1
};

// Bullet variables
static RM_RESOURCE_ID_T  const playerfire = ICON_STARWAR_PLAYERFIRE;
static RM_RESOURCE_ID_T  const missile = ICON_STARWAR_MISSILE;
static RM_RESOURCE_ID_T  const enemyfire = ICON_STARWAR_ENEMYFIRE;
static Bullet playerFires[maxBullet];
static Bullet enemyFires[maxBullet];
static OP_UINT8 missile_count;
static Missile missiles[maxMissile];

static Enemy enemies[maxEnemies]; 
static Enemy boss;
static OP_UINT8  cantEnemies;
static OP_UINT8  enemyCount;
static OP_INT8 enemyMode;
static OP_UINT16 total_enemies;

RM_RESOURCE_ID_T  const enemyID[5]=
{
    ICON_STARWAR_ENEMY0,
    ICON_STARWAR_ENEMY1,
    ICON_STARWAR_ENEMY2,
    ICON_STARWAR_ENEMY3,
    ICON_STARWAR_ENEMY4,
};

static OP_INT16 const enemiesWidth[5]=
{
    16,  
    16,  
    16, 
    16, 
    8   
};

static OP_INT16 const enemiesHeight[5]=
{
    12,  
    16,  
    16,  
    16,  
    8   
};

RM_RESOURCE_ID_T  const     bossID[5]=
{
    ICON_STARWAR_BOSS0,
    ICON_STARWAR_BOSS1,
    ICON_STARWAR_BOSS2,
    ICON_STARWAR_BOSS3,
    ICON_STARWAR_BOSS4,
};

static OP_INT16 const bossWidth[5]=
{
    56,  
    72,  
    64, 
    72,
    72
};

static OP_INT16 const bossHeight[5]=
{
    20,  
    24,  
    40,  
    32,
    38
};


/*
 *
 * These are for the star field.
 * To hurry up the initial speed, us consts here.
 * All the digits for starsX, starsY, and starsC are produced by the rand() function.
 *
 */
static OP_INT16 starsX[numStars]=
{
    70, 112, 17, 71, 4, 7, 11, 62, 116, 29
};
static OP_INT16 starsY[numStars]=
{
    78, 36, 81, 77, 76, 57, 85, 71, 9, 30
};

static DS_COLOR starsC[numStars]=
{
    416, 1408, 57344, 10240, 25, 448, 36864, 13, 53248, 17
};


static OP_INT16 const starSpeed = 8;
static OP_INT16 xSize, ySize;

// Variables for big boom
static RM_RESOURCE_ID_T  const boom[bframes]=
{
    ICON_STARWAR_BOOM0,
    ICON_STARWAR_BOOM1,
    ICON_STARWAR_BOOM2,
    ICON_STARWAR_BOOM3,
};

static OP_INT16    distance=0;
static OP_INT8 const smax = 9;
static OP_INT8    scur;
static OP_UINT16   score;


static OP_BOOLEAN gamePause;    /* if there is a MT call come in, set gamePause to OP_TRUE */
static OP_BOOLEAN inGame;    /* if user press soft left key, set inGame to OP_FALSE */
static OP_BOOLEAN isGameOver;    /* if the lives of user's ship is 0, or have passed the last level, set isGameOver to OP_TRUE */
static OP_BOOLEAN isShowingIntro; /* whether the introduction information is displayed now. This variable is used to judge whether we should refresh the screen */

/*==================================================================================================
    GLOBAL VARIABLES
==================================================================================================*/
extern APP_GAMES_STRUCTURE_NAME_T   gamesApp_data;
extern APP_SUBSTATE_STRUCTURE_NAME_T games_substate;



/*==================================================================================================
    LOCAL FUNCTIONS
==================================================================================================*/

/*==================================================================================================
    FUNCTION: APP_Starwar_entry

    DESCRIPTION:
        Description of this specific function.

    ARGUMENTS PASSED:

    RETURN VALUE:

    IMPORTANT NOTES:
        
==================================================================================================*/
void APP_Starwar_entry(void)
{
    ds_set_screen_mode(SCREEN_FULL_IMAGE);

    gamesApp_data.popupwin=OP_NULL;

    APP_Starwar_Init();
}

/*==================================================================================================
    FUNCTION: App_Starwar_exit

    DESCRIPTION:
        Description of this specific function.

    ARGUMENTS PASSED:

    RETURN VALUE:

    IMPORTANT NOTES:
        
==================================================================================================*/
void App_Starwar_exit(void)
{
    close_old_popup();
    hmi_vibrator_off(); 
    ds_set_softkeys_rm(PMPT_SKEY_EMPTY, PMPT_SKEY_EMPTY, PMPT_SKEY_EMPTY);
}


/*==================================================================================================
    FUNCTION: app_Starwar_handler

    DESCRIPTION:
        Description of this specific function.

    ARGUMENTS PASSED:

    RETURN VALUE:

    IMPORTANT NOTES:
        
==================================================================================================*/
void    
App_Starwar_handler
( 
    OPUS_EVENT_ENUM_TYPE   event, 
  void                   *pMess, 
  OP_BOOLEAN             *handle 
)
{
    KEYEVENT_STRUCT *pKey;

    *handle = OP_TRUE;

    switch (event)
    {
        case OPUS_FOCUS_CUST_PAINT :
            if ( (pMess != OP_NULL) && (*((OP_BOOLEAN *)pMess) == OP_TRUE))
            {
                if (ds_get_screen_mode() != SCREEN_FULL_IMAGE)
                    ds_set_screen_mode(SCREEN_FULL_IMAGE);
                clear_full_screen(STARWAR_BG_COLOR);
                gamePause=OP_FALSE;
                if (inGame)
                {
                    if (atLevelEnd == OP_FALSE)
                    {
                        dx=0;
                        dy=0;
                        keepFiring=OP_FALSE;
                    }
                    StarwarRun();
                }
                else    /* If before the call came this game had been paused by user, just display all the objects here */
                {
                    repaint();
                    ShowScore();
                }
            }
#ifdef PACIFIC_VERSION

⌨️ 快捷键说明

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