📄 snake.c
字号:
/*===========================================================================
FILE: snake.c
===========================================================================*/
/*===============================================================================
INCLUDES AND VARIABLE DEFINITIONS
=============================================================================== */
#include "AEEModGen.h" // Module interface definitions
#include "AEEAppGen.h" // Applet interface definitions
#include "AEEShell.h" // Shell interface definitions
#include "AEEMenu.h"
#include "snake.bid"
#include "AEEStdlib.h"
#include "AEEGraphics.h"
#include "AEEBitmap.h"
#include "AEEImage.h"
#include "aeedisp.h"
#include "AEEMedia.h"
#include "AEEFile.h" //文件系统然后定义程序需要使用的常量和变量:
#define SAVE_FILE "/save.dat" /* 存储文件名字 */
#define SNAKE_MAX_LENGTH 1024 //蛇最大长度(节数)
#define LIVE 1 //蛇活着,移动中
#define DEAD 0 //蛇死亡,游戏失败
#define BLACK MAKE_RGB(0, 0, 0) //黑色
#define WHITE MAKE_RGB(255, 255, 255) //白色
#define RED MAKE_RGB(255, 0, 0) //红色
#define GREEN MAKE_RGB(0, 255, 0) //绿色
#define BLUE MAKE_RGB(0, 0, 255) //蓝色
enum move_direction{ //蛇移动方向
RIGHT = 1, //1 向右
LEFT, //2 向左
UP,//3 向上
DOWN, //4 向下
};
enum app_mode{ //程序中各个状态
MODE_MENU,
MODE_GAME,
MODE_HIGH_SCORE,
MODE_HELP,
};
enum menu_item{ //主菜单各个选项
ITEM_MENU_START,
ITEM_MENU_HIGH_SCORE,
ITEM_MENU_HELP,
ITEM_MENU_EXIT,
};
/*-------------------------------------------------------------------
Applet structure. All variables in here are reference via "pMe->"
-------------------------------------------------------------------*/
// create an applet structure that's passed around. All variables in
// here will be able to be referenced as static.
typedef struct _snake {
AEEApplet a ; // First element of this structure must be AEEApplet
AEEDeviceInfo DeviceInfo; // always have access to the hardware device information
// add your own variables here...
int Mode; //程序中状态
IMenuCtl *pIMenuCtl; //菜单接口
AEERect ScreenRect; //屏幕大小矩形
//游戏画面和逻辑相关
int x[SNAKE_MAX_LENGTH];
//int x; 蛇每节X坐标
int y[SNAKE_MAX_LENGTH];
//int y; 蛇每节Y坐标
int length;
//蛇当前长度(节数)
int direction;
// 蛇的移动方向
int life;
// 蛇的生命: LIVE 1 活者,DEAD 0 死亡
int foodx;
//食物X坐标
int foody;
//食物Y坐标
boolean yes; //1,需要生成食物 ; 0, 不需要
boolean bDirectionChg;
unsigned int scores; //得分
//sound
IMedia *pimedia; //新增的音频接口
unsigned int HighScore[5]; /* 最高分纪录 */
} snake;
/*-------------------------------------------------------------------
Function Prototypes
-------------------------------------------------------------------*/
static boolean snake_HandleEvent(snake* pMe, AEEEvent eCode, uint16 wParam, uint32 dwParam);
static boolean snake_InitAppData(snake* pMe);
static void snake_FreeAppData(snake* pMe);
static boolean init_Menuctl(snake* pMe);
static void main_Display(snake* pMe);
static void release_Menuctl(snake* pMe);
static void draw_Lost(snake* pMe);
static void load_Draw_BMP(snake* pMe);
static void draw_Ellipse(snake* pMe);
static void load_Draw_PNG(snake* pMe);
static void stopSound(snake* pMe);
static void playTone(snake* pMe);
static void playSound(snake* pMe);
static void show_HighScore(snake* pMe);
static void saveScore(snake* pMe);
static void loadScore(snake* pMe);
static void main_Loop(snake* pMe);
static void new_Food(snake* pMe);
static void init_GameData(snake* pMe);
/*===============================================================================
FUNCTION DEFINITIONS
=============================================================================== */
/*===========================================================================
FUNCTION: AEEClsCreateInstance
DESCRIPTION
This function is invoked while the app is being loaded. All Modules must provide this
function. Ensure to retain the same name and parameters for this function.
In here, the module must verify the ClassID and then invoke the AEEApplet_New() function
that has been provided in AEEAppGen.c.
After invoking AEEApplet_New(), this function can do app specific initialization. In this
example, a generic structure is provided so that app developers need not change app specific
initialization section every time except for a call to IDisplay_InitAppData().
This is done as follows: InitAppData() is called to initialize AppletData
instance. It is app developers responsibility to fill-in app data initialization
code of InitAppData(). App developer is also responsible to release memory
allocated for data contained in AppletData -- this can be done in
IDisplay_FreeAppData().
PROTOTYPE:
int AEEClsCreateInstance(AEECLSID ClsId,IShell * pIShell,IModule * po,void ** ppObj)
PARAMETERS:
clsID: [in]: Specifies the ClassID of the applet which is being loaded
pIShell: [in]: Contains pointer to the IShell object.
pIModule: pin]: Contains pointer to the IModule object to the current module to which
this app belongs
ppObj: [out]: On return, *ppObj must point to a valid IApplet structure. Allocation
of memory for this structure and initializing the base data members is done by AEEApplet_New().
DEPENDENCIES
none
RETURN VALUE
AEE_SUCCESS: If the app needs to be loaded and if AEEApplet_New() invocation was
successful
EFAILED: If the app does not need to be loaded or if errors occurred in
AEEApplet_New(). If this function returns FALSE, the app will not be loaded.
SIDE EFFECTS
none
===========================================================================*/
int AEEClsCreateInstance(AEECLSID ClsId, IShell *pIShell, IModule *po, void **ppObj)
{
*ppObj = NULL;
if( ClsId == AEECLSID_SNAKE )
{
// Create the applet and make room for the applet structure
if( AEEApplet_New(sizeof(snake),
ClsId,
pIShell,
po,
(IApplet**)ppObj,
(AEEHANDLER)snake_HandleEvent,
(PFNFREEAPPDATA)snake_FreeAppData) ) // the FreeAppData function is called after sending EVT_APP_STOP to the HandleEvent function
{
//Initialize applet data, this is called before sending EVT_APP_START
// to the HandleEvent function
if(snake_InitAppData((snake*)*ppObj))
{
//Data initialized successfully
return(AEE_SUCCESS);
}
else
{
//Release the applet. This will free the memory allocated for the applet when
// AEEApplet_New was called.
IAPPLET_Release((IApplet*)*ppObj);
return EFAILED;
}
} // end AEEApplet_New
}
return(EFAILED);
}
/*===========================================================================
FUNCTION SampleAppWizard_HandleEvent
DESCRIPTION
This is the EventHandler for this app. All events to this app are handled in this
function. All APPs must supply an Event Handler.
PROTOTYPE:
boolean SampleAppWizard_HandleEvent(IApplet * pi, AEEEvent eCode, uint16 wParam, uint32 dwParam)
PARAMETERS:
pi: Pointer to the AEEApplet structure. This structure contains information specific
to this applet. It was initialized during the AEEClsCreateInstance() function.
ecode: Specifies the Event sent to this applet
wParam, dwParam: Event specific data.
DEPENDENCIES
none
RETURN VALUE
TRUE: If the app has processed the event
FALSE: If the app did not process the event
SIDE EFFECTS
none
===========================================================================*/
static boolean snake_HandleEvent(snake* pMe, AEEEvent eCode, uint16 wParam, uint32 dwParam)
{
switch (eCode)
{
// App is told it is starting up
case EVT_APP_START:
// Add your code here...
//将该矩形设置成设备屏幕大小
pMe->ScreenRect.x = 0;
pMe->ScreenRect.y = 0;
pMe->ScreenRect.dx = pMe->DeviceInfo.cxScreen;
pMe->ScreenRect.dy = pMe->DeviceInfo.cyScreen;
if ( !init_Menuctl(pMe) )
return FALSE; //初始化菜单控件
playSound(pMe);
// ISHELL_SetTimer(pMe->a.m_pIShell,5000,(PFNNOTIFY)(main_Loop),pMe);
//main_Loop(pMe); //启动程序主循环
return(TRUE);
// App is told it is exiting
case EVT_APP_STOP:
// Add your code here...
return(TRUE);
// App is being suspended
case EVT_APP_SUSPEND:
// Add your code here...
return(TRUE);
// App is being resumed
case EVT_APP_RESUME:
// Add your code here...
return(TRUE);
// An SMS message has arrived for this app. Message is in the dwParam above as (char *)
// sender simply uses this format "//BREW:ClassId:Message", example //BREW:0x00000001:Hello World
case EVT_APP_MESSAGE:
// Add your code here...
return(TRUE);
// A key was pressed. Look at the wParam above to see which key was pressed. The key
// codes are in AEEVCodes.h. Example "AVK_1" means that the "1" key was pressed.
case EVT_KEY:
// Add your code here...
if(pMe->pIMenuCtl){ //菜单控件激活情况下,按键事件由菜单控件自动处理
return IMENUCTL_HandleEvent(pMe->pIMenuCtl, eCode, wParam, dwParam);
}
switch(wParam) {
case AVK_SELECT:
if ((pMe->Mode == MODE_HIGH_SCORE) || (pMe->life == 1 && pMe->Mode == MODE_GAME)){ /*排行榜和游戏失败界面按确定键可以回到菜单画面*/
init_Menuctl(pMe);
pMe->Mode = MODE_MENU;
}
break;
case AVK_LEFT:
if(pMe->direction != RIGHT && pMe->bDirectionChg)
pMe->direction = LEFT;
pMe->bDirectionChg = FALSE;
break;
case AVK_RIGHT:
if(pMe->direction != LEFT && pMe->bDirectionChg)
pMe->direction = RIGHT;
pMe->bDirectionChg = FALSE;
break;
case AVK_UP:
if(pMe->direction != DOWN && pMe->bDirectionChg)
pMe->direction = UP;
pMe->bDirectionChg = FALSE;
break;
case AVK_DOWN:
if(pMe->direction != UP && pMe->bDirectionChg)
pMe->direction = DOWN;
pMe->bDirectionChg = FALSE;
break;
default:
pMe->bDirectionChg = FALSE;
pMe->direction = RIGHT;
break;
}
return(TRUE);
case EVT_COMMAND:
switch (wParam){ //各菜单选择后处理,目前除了退出,其他只打印出调试信息
case ITEM_MENU_START://开始游戏
DBGPRINTF("---------ITEM_MENU_START----------");
release_Menuctl(pMe);
pMe->Mode = MODE_GAME;
init_GameData(pMe);
ISHELL_SetTimer(pMe->a.m_pIShell,200,(PFNNOTIFY)(main_Loop),pMe);
break;
case ITEM_MENU_HELP://帮助
DBGPRINTF("--------ITEM_MENU_HELP-----------");
pMe->Mode = MODE_HELP;
break;
case ITEM_MENU_EXIT: //退出程序
DBGPRINTF("--------ITEM_MENU_EXIT-----------");
ISHELL_CloseApplet(pMe->a.m_pIShell, FALSE);
break;
case ITEM_MENU_HIGH_SCORE: //最高分数
DBGPRINTF("---------ITEM_MENU_HIGH_SCORE----------");
pMe->Mode = MODE_HIGH_SCORE; //将程序状态设为显示最高分数
release_Menuctl(pMe);
loadScore(pMe); /* 读取排行榜 */
show_HighScore(pMe); //显示
// main_Display(pMe);
break;
}
return TRUE;
// If nothing fits up to this point then we'll just break out
default:
break;
}
return FALSE;
}
// this function is called when your application is starting up
static boolean snake_InitAppData(snake* pMe)
{
// Get the device information for this handset.
// Reference all the data by looking at the pMe->DeviceInfo structure
// Check the API reference guide for all the handy device info you can get
pMe->DeviceInfo.wStructSize = sizeof(pMe->DeviceInfo);
ISHELL_GetDeviceInfo(pMe->a.m_pIShell,&pMe->DeviceInfo);
// Insert your code here for initializing or allocating resources...
// if there have been no failures up to this point then return success
return TRUE;
}
// this function is called when your application is exiting
static void snake_FreeAppData(snake* pMe)
{
// insert your code here for freeing any resources you have allocated...
// example to use for releasing each interface:
// if ( pMe->pIMenuCtl != NULL ) // check for NULL first
// {
// IMENUCTL_Release(pMe->pIMenuCtl) // release the interface
// pMe->pIMenuCtl = NULL; // set to NULL so no problems trying to free later
// }
//
stopSound(pMe);
ISHELL_CancelTimer(pMe->a.m_pIShell,(PFNNOTIFY)main_Loop,pMe);
release_Menuctl(pMe);
}
static boolean init_Menuctl(snake* pMe)
{
if(SUCCESS == ISHELL_CreateInstance(pMe->a.m_pIShell, AEECLSID_MENUCTL, (void **)&(pMe->pIMenuCtl)) )
{
DBGPRINTF("---create menuctl instance ok---!");
IMENUCTL_SetRect(pMe->pIMenuCtl, &(pMe->ScreenRect)); // 设置菜单覆盖区域为整个设备屏幕
IMENUCTL_SetTitle(pMe->pIMenuCtl, NULL, NULL, L"Main Menu"); // 设置标题
IMENUCTL_SetProperties (pMe->pIMenuCtl, MP_UNDERLINE_TITLE); //给标题加下划线
IMENUCTL_AddItem(pMe->pIMenuCtl, NULL, NULL, ITEM_MENU_START, L"Start", NULL); // 添加菜单项
IMENUCTL_AddItem(pMe->pIMenuCtl, NULL, NULL, ITEM_MENU_HIGH_SCORE, L"High Score", NULL);
IMENUCTL_AddItem(pMe->pIMenuCtl, NULL, NULL, ITEM_MENU_HELP, L"Help", NULL);
IMENUCTL_AddItem(pMe->pIMenuCtl, NULL, NULL, ITEM_MENU_EXIT, L"Exit", NULL);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -