星星博客's Archiver

cnangel 发表于 2004-3-5 13:01

[转帖]教你做个“打耗子”的BREW游戏

[color=#DC143C]作者:板凳南瓜[/color]
因为非常的简易,希望各位大虾不要笑话
只是为新来的做个参考~~希望大家能共同进步~~
当时图片临时找了个,在上面画了个X,表示耗子被打中。


1。头文件和资源文件,包含你要用的函数和CLASSID,还有资源的定义
#include "AEEModGen.h" // Module interface definitions
#include "AEEAppGen.h" // Applet interface definitions
#include "AEEShell.h" // Shell interface definitions
#include "AEEStdLib.h"
#include "AEE.h"
#include "HitMe.bid" //CLASS ID
#include "HitMe_res.h" //资源文件


2。定义一些,自己要用的数值
#define SIZE_X 3
#define SIZE_Y 3
#define PIC_SIZE 32 //头像设置
#define HM_TIMER 800 //打耗子时间
#define OK_TIMER 2000 //成功时间


3。主要的数据结构

typedef struct _CHitMe
{
AEEApplet a; // Mandatory first AEEApplet data member
int m_cxWidth; // Stores the device screen width
int m_cyHeight; // Stores the device screen height

int m_iCurIndex; //0~~9 //耗子在那里跑??
int m_iHit; //你敲在那里了?
int m_iCount; //敲的次数
boolean m_bHited; //打中没有?

IImage * m_pIImageGood;// 耗子被打中与没打中的图片
IImage * m_pIImageBad;//
}CHitMe;


4。函数的定义

static boolean HitMe_HandleEvent(CHitMe * pMe, AEEEvent eCode,
uint16 wParam, uint32 dwParam);
static boolean HitMe_InitAppData(IApplet* pMe);
static void HitMe_FreeAppData(IApplet* pMe);

static void HitMe_GetIndex(CHitMe* pMe);
static void HitMe_Show(CHitMe* pMe);
static void HitMe_DrawTitle(CHitMe* pMe);
static void HitMe_DrawFrame(CHitMe* pMe);
static void HitMe_DrawImage(CHitMe* pMe,int xc, int yc,int16 nResID);

static void HitMe_OnTimer(CHitMe * pme);


5。入口函数!关键地方

int AEEClsCreateInstance(AEECLSID ClsId,IShell * pIShell,IModule * po,void ** ppObj)
{
*ppObj = NULL;

if(ClsId == AEECLSID_HITME){
if(AEEApplet_New(sizeof(CHitMe), ClsId, pIShell,po,(IApplet**)ppObj,
(AEEHANDLER)HitMe_HandleEvent,(PFNFREEAPPDATA)HitMe_FreeAppData)//释放数据函数指针
== TRUE)
{
if(HitMe_InitAppData((IApplet*)*ppObj) == TRUE)//初始化数据

return (AEE_SUCCESS);
}
}
return (EFAILED);
}



6。具体函数的内容
///###########
//数据初始话
static boolean HitMe_InitAppData(IApplet* pi)
{
AEEDeviceInfo di;
CHitMe * pMe = (CHitMe*)pi;
pMe->m_pIImageGood = NULL;
pMe->m_pIImageBad = NULL;
//这里有两个图片的资源使用,32X32的
if ((pMe->m_pIImageGood = ISHELL_LoadResImage(pMe->a.m_pIShell, HITME_RES_FILE,IDB_GOOD)) == NULL)
return FALSE;
if ((pMe->m_pIImageBad = ISHELL_LoadResImage(pMe->a.m_pIShell, HITME_RES_FILE,IDB_BAD)) == NULL)
return FALSE;

ISHELL_GetDeviceInfo(pMe->a.m_pIShell,&di);
pMe->m_cxWidth = di.cxScreen; //屏幕的尺寸
pMe->m_cyHeight = di.cyScreen;

HitMe_GetIndex(pMe);

pMe->m_iCount = 0;//开始为0

return TRUE;
}


//###

//释放数据
static void HitMe_FreeAppData(IApplet* pi)
{
CHitMe * pMe = (CHitMe*)pi;
if (pMe->m_pIImageGood != NULL)
{
IIMAGE_Release (pMe->m_pIImageGood);
pMe->m_pIImageGood = NULL;
}
if (pMe->m_pIImageBad != NULL)
{
IIMAGE_Release (pMe->m_pIImageBad);
pMe->m_pIImageBad = NULL;
}
//释放图片哦
}

//###
//事件处理入口
static boolean HitMe_HandleEvent(CHitMe * pMe, AEEEvent eCode, uint16 wParam, uint32 dwParam)
{
int iHit = -1;
switch (eCode)
{
case EVT_APP_START:
case EVT_APP_RESUME:
HitMe_Show(pMe);//第一显示

ISHELL_SetTimer(pMe->a.m_pIShell, HM_TIMER, (PFNNOTIFY)HitMe_OnTimer, pMe);//启动定时器

return(TRUE);

case EVT_KEY: // Process key-down event.
switch (wParam)//开打耗子
{
case AVK_1:pMe->m_iHit=1;break;
case AVK_2:pMe->m_iHit=2;break;
case AVK_3:pMe->m_iHit=3;break;
case AVK_4:pMe->m_iHit=4;break;
case AVK_5:pMe->m_iHit=5;break;
case AVK_6:pMe->m_iHit=6;break;
case AVK_7:pMe->m_iHit=7;break;
case AVK_8:pMe->m_iHit=8;break;
case AVK_9:pMe->m_iHit=9;break;
default:
return(FALSE);
}
//判断打到没有?
if( pMe->m_iHit == pMe->m_iCurIndex && ( pMe->m_bHited!=TRUE))
{
pMe->m_iCount++; //打到了,加分
pMe->m_bHited = TRUE; //显示打到耗子
ISHELL_SetTimer(pMe->a.m_pIShell, OK_TIMER, (PFNNOTIFY)HitMe_OnTimer, pMe);//多等一会儿 }
HitMe_Show(pMe);//刷新

return TRUE;

case EVT_APP_STOP:
return TRUE;


default:
break;
}
return FALSE;
}


//###
//显示刷新
static void HitMe_Show(CHitMe* pMe)
{
IDISPLAY_ClearScreen (pMe->a.m_pIDisplay); //清屏

HitMe_DrawTitle(pMe);//标题,分值

HitMe_DrawFrame(pMe);//耗子


IDISPLAY_Update(pMe->a.m_pIDisplay); //刷新
}


//###
//画图片
static void HitMe_DrawImage(CHitMe* pMe,int xc, int yc,int16 nResID)
{
IImage* pImage = NULL;
if ((pImage = ISHELL_LoadResImage(pMe->a.m_pIShell, HITME_RES_FILE,nResID)) != NULL)
{
IIMAGE_Draw (pImage,xc, yc);
IIMAGE_Release (pImage);
pImage = NULL;
}

}


//###
//显示分数和状态
static void HitMe_DrawTitle(CHitMe* pMe)
{
AEERect rc;
AECHAR szBuf[80] = {0};
AECHAR szFormat[50];


SETAEERECT (&rc, 0, 14, pMe->m_cxWidth, 2);
IDISPLAY_DrawRect (pMe->a.m_pIDisplay, &rc, 0, 1,
IDF_RECT_FILL);

HitMe_DrawImage(pMe,0,0,IDB_ICON);

STR_TO_WSTR("HitMe!->%d[%d]",szFormat,sizeof(szFormat));

WSPRINTF(szBuf,80,szFormat,pMe->m_iCurIndex,pMe->m_iCount);


IDISPLAY_DrawText(pMe->a.m_pIDisplay, AEE_FONT_BOLD, szBuf,
-1, 17, 0, 0, 0);
}


//###
//画耗子
static void HitMe_DrawFrame(CHitMe* pMe)
{
AEERect rc;
int iBoxX,iBoxY ;
int x,y;
int i,j,iCur;
AECHAR szBuf[80] = {0};
AECHAR szFormat[50];

SETAEERECT (&rc, 0, 15, pMe->m_cxWidth, pMe->m_cyHeight);
IDISPLAY_DrawRect (pMe->a.m_pIDisplay, &rc, 0, 1,
IDF_RECT_FRAME);

iBoxX = (rc.dx - rc.x )/SIZE_X ;
iBoxY = (rc.dy - rc.y )/SIZE_Y ;
//耗子在那里?
for(i = 0 ; i < 3 ; i++ )
for(j = 0 ; j <3 ; j++ )
{
x = rc.x + i* iBoxX ;
y = rc.y + j* iBoxY ;
// SETAEERECT (&rc, x+1, y+1, x+2, y+2);
// IDISPLAY_DrawRect (pMe->a.m_pIDisplay, &rc, 0, 1,IDF_RECT_FRAME);

iCur = (j)*3+i+1;
if( iCur == pMe->m_iCurIndex )
{
if(pMe->m_bHited==TRUE)
{
HitMe_DrawImage(pMe,x+ (iBoxX-PIC_SIZE)/2, y+ (iBoxY-PIC_SIZE)/2,IDB_BAD);
pMe->m_bHited = FALSE;
}
else
{
HitMe_DrawImage(pMe,x+ (iBoxX-PIC_SIZE)/2, y+ (iBoxY-PIC_SIZE)/2,IDB_GOOD);
}
}
else if( iCur == pMe->m_iHit )
{
HitMe_DrawImage(pMe,x+ (iBoxX-PIC_SIZE)/2, y+ (iBoxY-PIC_SIZE)/2,IDB_EMPTY);
}
else
{
STR_TO_WSTR("%d",szFormat,sizeof(szFormat));
WSPRINTF(szBuf,80,szFormat,iCur);
IDISPLAY_DrawText(pMe->a.m_pIDisplay, AEE_FONT_NORMAL, szBuf,-1,
x+ (iBoxX)/2-2, y+ (iBoxY)/2-1, 0, 0);

}
}
}

//####
//定时器,随机的跑耗子
static void HitMe_OnTimer(CHitMe * pMe)
{
if( pMe->m_bHited !=TRUE)
{
ISHELL_SetTimer(pMe->a.m_pIShell, HM_TIMER, (PFNNOTIFY)HitMe_OnTimer, pMe);
HitMe_GetIndex(pMe);
}
HitMe_Show(pMe);
}

//###
//随机函数
static void HitMe_GetIndex(CHitMe* pMe)
{
int iIndex ;
GET_RAND((byte*)&iIndex,sizeof(int));
if( iIndex < 0 )
iIndex = -iIndex;
pMe->m_iCurIndex = (iIndex)%9+1;
pMe->m_iHit = -1;
}


7。总结,简单,虽然没有用SPRITE,因为当时还是入门阶段
简单的实现了一个小游戏,如果包装和精心调整,更换一下图片
就和后来 在联通上看到的 打鼹鼠 差不多了
当时我是相当的菜鸟,还在MOVE2008问如何生成MIF文件的CLASSID
菜到了家,上面有什么错误和误导的地方还请大家包涵。
当时我连真机都没摸过,所以也没上过真机,
只供新手参考一下~
还请多指教~~~

我本善良 发表于 2005-9-27 14:11

[转帖]教你做个“打耗子”的BREW游戏

鄙视

页: [1]

Powered by Discuz! Archiver 7.0.0  © 2001-2009 Comsenz Inc.