📄 pottedfish.c
字号:
/*===========================================================================
FILE: cargame.c
===========================================================================*/
/*===============================================================================
INCLUDES AND VARIABLE DEFINITIONS(头文件和变量的定义)
=============================================================================== */
#include "AEEModGen.h" // Module interface definitions
#include "AEEAppGen.h" // Applet interface definitions
#include "AEEShell.h" // Shell interface definitions
//Sprite所需要的接口
#include "AEESprite.h"
#include "AEEHeap.h" //助手函数接口
//菜单部分所需要的接口
#include "AEEMenu.h" //使用IMENUCTL是必须包含的头文件
#include "AEEStdlib.h"
#include "AEEText.h"
#include "AEEImage.h"
//声音部分的接口
#include "AEESound.h"
#include "AEEMedia.h"
#include "mymenu.brh"//菜单的资源文件
#include "PottedFish.bid"
//定义菜单项
#define MY_MENU_ID_M1 10001 //帮助
#define MY_MENU_ID_M2 10002
#define MY_MENU_ID_M3 10003
#define MY_MENU_ID_M4 10004 //退出
#define MY_MENU_ID_M5 10005 //开始
#define MY_MENU_ID_M6 10006
#define MY_MENU_ID_M7 10007
#define MY_MENU_ID_M8 10008
#define MAX_HEIGHT 12800
#define WALL_START_POS_Y 150//墙
#define SCORE_START_X 9//得分
#define DIGIT_WEIGHT 7
//rgCmds中各种对象的索引
#define SPRITE_DIGIT 4
#define SPRITE_CAR 8
//各种对象的源图像贴索引
#define PIC_START_DIGIT 15
#define PIC_START_WALL 13
//车在不同角度下的速度表
static const uint16 speedTableX[] = {-4, -3, -3, -2, -1, 0, 1, 2, 3, 3, 4};
static const uint16 speedTableY[] = { 3, 3, 4, 4, 5, 6, 5, 4, 4, 3, 3};
typedef struct _PottedFish {
AEEApplet a ;
AEEDeviceInfo DeviceInfo;
ISprite *pISprite;
AEESpriteCmd rgCmds[10]; // wall sprites 4 + plane + 4 score sprites + terminator
AEETileMap rgMaps[2]; // one background layers + terminator
AEECallback cbTimer,cbtime; // 定时器的定义
AEEPoint frameStart; //屏幕的逻辑起始坐标
AEEPoint frameMax; //屏幕的逻辑最大长宽
byte direction; //角度
int layer; //游戏分数
int crashCounter; //crash之后的计数器
boolean keyRight; //右键是否按下
boolean keyLeft; //左键是否按下
boolean keyUp;
boolean keyDown;
boolean isCrash; //是否分解
uint16 BackGroundLayer[16];//Tile 索引数组
int go;//game over
IMenuCtl *pIMenuCtl;
uint16 DrawStringID;
ITextCtl *pITextCtl;
IMedia *pIMedia; //IMedia接口
boolean m_bIsContinue; //声音是否连续播放
} PottedFish;
/*-------------------------------------------------------------------
Function Prototypes
-------------------------------------------------------------------*/
static boolean PottedFish_HandleEvent(PottedFish* pMe, AEEEvent eCode,
uint16 wParam, uint32 dwParam);
boolean PottedFish_InitAppData(PottedFish* pMe);
void PottedFish_FreeAppData(PottedFish* pMe);
//游戏部分
static void PottedFish_Init(PottedFish *pMe);
static void PottedFish_Free(PottedFish *pMe);
static boolean PottedFish_HandleKeyEvent(PottedFish *pMe, AEEEvent eCode, uint16 wParam);
static void PottedFish_NextFrame(PottedFish *pMe);
static int PottedFish_LoadResources(PottedFish *pMe);
static void PottedFish_UnloadResources(PottedFish *pMe);
static void PottedFish_LoadSprites(PottedFish *pMe, const char * pszFile, uint8 unSpriteSize, boolean isTile);
static boolean PottedFish_IsCrash(PottedFish *pMe);//车分解函数
static int16 Random(int16 range);//随机生成
//菜单部分
boolean my_init_menu(PottedFish* pMe);
void DrawStr(PottedFish* pMe,uint16 DrawStringID);
boolean my_id_menu(PottedFish* pMe);
//音乐
void PlayBGMedia(PottedFish *pMe);
//game over
void GameOver(PottedFish* pMe);//游戏结束
/*===============================================================================
FUNCTION DEFINITIONS
=============================================================================== */
int AEEClsCreateInstance(AEECLSID ClsId, IShell *pIShell, IModule *po, void **ppObj)
{
*ppObj = NULL;
if( ClsId == AEECLSID_POTTEDFISH )
{
// Create the applet and make room for the applet structure
if( AEEApplet_New(sizeof(PottedFish),
ClsId,
pIShell,
po,
(IApplet**)ppObj,
(AEEHANDLER)PottedFish_HandleEvent,
(PFNFREEAPPDATA)PottedFish_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(PottedFish_InitAppData((PottedFish*)*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);
}
static boolean PottedFish_HandleEvent(PottedFish* pMe, AEEEvent eCode, uint16 wParam, uint32 dwParam)
{
IImage *pImage;
pMe->go=0;
switch (eCode)
{
case EVT_APP_START:
CALLBACK_Init(&pMe->cbtime,my_init_menu,pMe); //设置定时器 用来实现开机画面
ISHELL_SetTimerEx(pMe->a.m_pIShell,1500,&pMe->cbtime);
pImage=ISHELL_LoadImage(pMe->a.m_pIShell,"a.bmp");// 载入开机图片
IIMAGE_Draw(pImage,0,0); // 从0,0处开始绘制图片
IDISPLAY_Update(pMe->a.m_pIDisplay); // 更新屏幕
PottedFish_Init(pMe);
if (SUCCESS != PottedFish_LoadResources(pMe))
{
return FALSE;
}
return(TRUE);
case EVT_APP_STOP:
CALLBACK_Cancel(&pMe->cbtime);
CALLBACK_Cancel(&pMe->cbTimer);
IMENUCTL_Release(pMe->pIMenuCtl);
PottedFish_UnloadResources(pMe);
return(TRUE);
case EVT_APP_SUSPEND:
CALLBACK_Cancel(&pMe->cbTimer);
return(TRUE);
case EVT_APP_RESUME:
ISHELL_SetTimerEx(pMe->a.m_pIShell, 0, &pMe->cbTimer);
return(TRUE);
case EVT_APP_MESSAGE:
return(TRUE);
case EVT_COMMAND:
DBGPRINTF("the wParam is%d ",wParam);
switch(wParam)
{
case MY_MENU_ID_M1: //实现查看帮助文档
IMENUCTL_SetActive(pMe->pIMenuCtl,FALSE);//绘制菜单
pMe->DrawStringID = IDS_STRING_1006;
DrawStr(pMe,pMe->DrawStringID);
break;
//case MY_MENU_ID_M2://声音
// IMENUCTL_SetActive(pMe->pIMenuCtl,FALSE);
// pMe->DrawStringID = IDS_STRING_1007;
// DrawStr(pMe,pMe->DrawStringID);
// break;
case MY_MENU_ID_M4://退出
ISHELL_CloseApplet(pMe->a.m_pIShell,FALSE);
break;
case MY_MENU_ID_M5: // M5是开始游戏项的ID号 这个CASE是用来连接 开始游戏和软菜单
IMENUCTL_SetActive(pMe->pIMenuCtl,TRUE); //激活控件
my_id_menu(pMe);
break;
case IDS_STRING_1010:
{
AECHAR userName[20]; //规定输入数据的最大数值
ITEXTCTL_GetText(pMe->pITextCtl,userName,15);//设置在获取数据是的可输入最大字符数
}
break;
case IDS_STRING_1011:
// if(IMENUCTL_IsActive(pMe->pIMenuCtl)==FALSE)
// IMENUCTL_SetActive(pMe->pIMenuCtl,TRUE);
CALLBACK_Init(&pMe->cbtime,PottedFish_NextFrame,pMe);
ISHELL_SetTimerEx(pMe->a.m_pIShell, 100, &pMe->cbtime);
IMENUCTL_SetActive(pMe->pIMenuCtl,FALSE);
PlayBGMedia(pMe);
break;
}
break;
return(TRUE);
case EVT_KEY:
if(pMe->pIMenuCtl != NULL)//菜单选定
{
if(IMENUCTL_HandleEvent(pMe->pIMenuCtl,eCode,wParam,dwParam))
{
return TRUE;
}
}
if(pMe->pITextCtl )//文本框输入控制
{
ITEXTCTL_HandleEvent(pMe->pITextCtl,eCode,wParam,dwParam);
}
switch(wParam)
{
case AVK_1: // 实现音效和帮助按1键退出
if(IMENUCTL_IsActive(pMe->pIMenuCtl)==FALSE)
IMENUCTL_SetActive(pMe->pIMenuCtl,TRUE);
break;
case AVK_SELECT: //开机画面的定时器设置
CALLBACK_Cancel(&pMe->cbtime);
break;
}
return(TRUE);
default:
return PottedFish_HandleKeyEvent((PottedFish*)pMe, eCode, wParam);
}
return FALSE;
}
boolean PottedFish_InitAppData(PottedFish* pMe)
{
pMe->DeviceInfo.wStructSize = sizeof(pMe->DeviceInfo);
ISHELL_GetDeviceInfo(pMe->a.m_pIShell,&pMe->DeviceInfo);
return TRUE;
}
// this function is called when your application is exiting
void PottedFish_FreeAppData(PottedFish* pMe)
{}
//*************菜单函数******************//
boolean my_init_menu(PottedFish* pMe)
{
AEERect rect; //定义菜单显示的区域
CtlAddItem ai1,ai2,ai3,ai4,ai5; //定义 带图标菜单的结构
rect.x = 0;
rect.y = 0;
rect.dx = pMe->DeviceInfo.cxScreen; //全屏显示菜单
rect.dy = pMe->DeviceInfo.cyScreen;
DBGPRINTF("rect.dx is%d",rect.dx);
DBGPRINTF("rect.dy is%d",rect.dy);
ISHELL_CreateInstance(pMe->a.m_pIShell,AEECLSID_MENUCTL,&pMe->pIMenuCtl); //创建菜单实例
//定义一个带图标菜单
ai1.dwData =213;
ai1.pszResImage = MYMENU_RES_FILE; //包含有菜单图标的资源文件名
ai1.pszResText = MYMENU_RES_FILE; //包含有菜单文本字符串的资源文件名
ai1.wItemID = MY_MENU_ID_M5; //该菜单项的ID
ai1.wText = IDS_STRING_1001; //资源文件中菜单文本字符串的ID
ai1.wImage = IDI_OBJECT_5001; //资源文件中菜单图标的ID
ai1.pText = NULL;
ai1.pImage = NULL;
ai1.wFont = 0; //字体
//定义一个带图标菜单
ai2.dwData =213;
ai2.pszResImage = MYMENU_RES_FILE; //包含有菜单图标的资源文件名
ai2.pszResText = MYMENU_RES_FILE; //包含有菜单文本字符串的资源文件名
ai2.wItemID = MY_MENU_ID_M3; //该菜单项的ID
ai2.wText = IDS_STRING_1002; //资源文件中菜单文本字符串的ID
ai2.wImage = IDI_OBJECT_5002; //资源文件中菜单图标的ID
ai2.pText = NULL;
ai2.pImage = NULL;
ai2.wFont = 0; //字体
ai3.dwData =213;
ai3.pszResImage = MYMENU_RES_FILE; //包含有菜单图标的资源文件名
ai3.pszResText = MYMENU_RES_FILE; //包含有菜单文本字符串的资源文件名
ai3.wItemID = MY_MENU_ID_M1; //该菜单项的ID
ai3.wText = IDS_STRING_1003; //资源文件中菜单文本字符串的ID
ai3.wImage = IDI_OBJECT_5005; //资源文件中菜单图标的ID
ai3.pText = NULL;
ai3.pImage = NULL;
ai3.wFont = 0; //字体
ai4.dwData =213;
ai4.pszResImage = MYMENU_RES_FILE; //包含有菜单图标的资源文件名
ai4.pszResText = MYMENU_RES_FILE; //包含有菜单文本字符串的资源文件名
ai4.wItemID = MY_MENU_ID_M2; //该菜单项的ID
//ai4.wText = IDS_STRING_1004; //资源文件中菜单文本字符串的ID
ai4.wImage = IDI_OBJECT_5004; //资源文件中菜单图标的ID
ai4.pText = NULL;
ai4.pImage = NULL;
ai4.wFont = 0; //字体
//定义一个带图标菜单
ai5.dwData =213;
ai5.pszResImage = MYMENU_RES_FILE; //包含有菜单图标的资源文件名
ai5.pszResText = MYMENU_RES_FILE; //包含有菜单文本字符串的资源文件名
ai5.wItemID = MY_MENU_ID_M4; //该菜单项的ID
ai5.wText = IDS_STRING_1008; //资源文件中菜单文本字符串的ID
ai5.wImage = IDI_OBJECT_5003; //资源文件中菜单图标的ID
ai5.pText = NULL;
ai5.pImage = NULL;
ai5.wFont = 0; //字体
//设置菜单的标题
IMENUCTL_SetTitle(pMe->pIMenuCtl,MYMENU_RES_FILE,IDS_STRING_1005,NULL);
//使用IMENUCTL_AddItemEx()函数添加1个带图标的菜单
if (IMENUCTL_AddItemEx(pMe->pIMenuCtl,&ai1) == TRUE)
{
DBGPRINTF("icon menu item add success");
}else
{
DBGPRINTF("icon menu item add faild");
}
//使用IMENUCTL_AddItemEx()函数添加1个带图标的菜单
if (IMENUCTL_AddItemEx(pMe->pIMenuCtl,&ai2) == TRUE)
{
DBGPRINTF("icon menu item add success");
}else
{
DBGPRINTF("icon menu item add faild");
}
//使用IMENUCTL_AddItemEx()函数添加1个带图标的菜单
if (IMENUCTL_AddItemEx(pMe->pIMenuCtl,&ai3) == TRUE)
{
DBGPRINTF("icon menu item add success");
}else
{
DBGPRINTF("icon menu item add faild");
}
//使用IMENUCTL_AddItemEx()函数添加1个带图标的菜单
if (IMENUCTL_AddItemEx(pMe->pIMenuCtl,&ai4) == TRUE)
{
DBGPRINTF("icon menu item add success");
}else
{
DBGPRINTF("icon menu item add faild");
}
//使用IMENUCTL_AddItemEx()函数添加1个带图标的菜单
if (IMENUCTL_AddItemEx(pMe->pIMenuCtl,&ai5) == TRUE)
{
DBGPRINTF("icon menu item add success");
}else
{
DBGPRINTF("icon menu item add faild");
}
IMENUCTL_EnableCommand(pMe->pIMenuCtl,TRUE);
IMENUCTL_SetRect(pMe->pIMenuCtl,&rect);
IMENUCTL_SetActive(pMe->pIMenuCtl,TRUE);
return TRUE;
}
//*************软菜单的实现部分******************//
boolean my_id_menu(PottedFish* pMe)
{
AEERect rect;
ISHELL_CreateInstance(pMe->a.m_pIShell,AEECLSID_SOFTKEYCTL,(void**)&pMe->pIMenuCtl);
IMENUCTL_AddItem(pMe->pIMenuCtl,MYMENU_RES_FILE,IDS_STRING_1010,IDS_STRING_1011,NULL,1);//创建软键菜单增加软键条目
ISHELL_CreateInstance(pMe->a.m_pIShell,AEECLSID_TEXTCTL,(void**)&pMe->pITextCtl);// 创建ITextCtl接口
rect.x=0;
rect.y=0;
rect.dx=pMe->DeviceInfo.cxScreen;
rect.dy=60; //设置ITextCtl矩型
ITEXTCTL_SetProperties(pMe->pITextCtl,TP_FRAME);// 设置输入用户名时的边框
ITEXTCTL_SetTitle(pMe->pITextCtl,MYMENU_RES_FILE,IDS_STRING_1009,NULL);//设置标题为1009中的内容
ITEXTCTL_SetRect(pMe->pITextCtl,&rect); // 设置控件大小
ITEXTCTL_SetMaxSize(pMe->pITextCtl,15); //设置可输入的最大字符
ITEXTCTL_SetSoftKeyMenu(pMe->pITextCtl,pMe->pIMenuCtl);//增加软键菜单
ITEXTCTL_SetActive(pMe->pITextCtl,TRUE);//激活控件
return TRUE;
}
//*************绘图函数******************//
void DrawStr(PottedFish* pMe, uint16 DrawStringID)
{
AECHAR* pBuffer;
uint32 bufSize;
ISHELL_GetResSize(pMe->a.m_pIShell,MYMENU_RES_FILE,DrawStringID,RESTYPE_STRING,&bufSize);
pBuffer = (AECHAR*)MALLOC(bufSize);
IDISPLAY_ClearScreen(pMe->a.m_pIDisplay);
ISHELL_LoadResString(pMe->a.m_pIShell,
MYMENU_RES_FILE,
DrawStringID,
pBuffer,
bufSize);
IDISPLAY_DrawText(pMe->a.m_pIDisplay,
AEE_FONT_NORMAL,
pBuffer,
-1,50,50,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -