📄 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 "snake.bid"
#include "AEEStdLib.h"
//add service
#include "AEEFile.h" // AEE File Manager Services
/* definition */
/*-------------------------------------------------------------------
Function Prototypes
-------------------------------------------------------------------*/
static boolean snake_HandleEvent(IApplet * pi, AEEEvent eCode,
uint16 wParam, uint32 dwParam);
static boolean Snake_InitAppData(IApplet * pi);
/*===============================================================================
FUNCTION DEFINITIONS
=============================================================================== */
/*----------------------snake game---------------------------*/
typedef struct {
int x;
int y;
}SNAKE;
typedef enum{
UP=0,
LEFT,
RIGHT,
DOWN
}SNAKE_DIRECTION;
typedef struct _CSnakeApp
{
AEEApplet a; // Mandatory data member
int AreaWidth; // Stores the area width
int AreaHeight; // Stores the area height
int gameover;//indicate whether game over
int food_x;//the x cordinate for food
int food_y;//the y cordinate for food
SNAKE_DIRECTION current_snake_direction;
int snake_length;
int score;
SNAKE snake[200];
}CSnakeApp;
#define LEFT_LIMIT 1
#define UP_LIMIT 1
#define SNAKE_HEAD_X LEFT_LIMIT+7
#define SNAKE_HEAD_Y UP_LIMIT+7
#define BLOCK_SIZE 6 //less than or equal to 6
/*translate an integer into decimal sting*/
unsigned int up_Itoa(AECHAR * buffer, int eger)
{
int len, ueger;
unsigned char minusP;
minusP = (eger < 0);
if(minusP)
{
if(buffer)
{
*(buffer++) = '-';
}
ueger = (int)(0 - eger);
}
else
{
ueger = (unsigned int)eger;
}
{
unsigned int tmp;
tmp = ueger;
len = 1;
while ( (tmp /= 10) ) len += 1;
}
{
unsigned int pt;
pt = len;
if (buffer) buffer[pt] = 0;
while(pt--)
{
if (buffer)
{
buffer[pt] = (char)('0' + (ueger % 10));
}
ueger /= 10;
}
}
return (len + 1 + minusP);
}
//====================================================================
// Function: write_video
// Description: draw a frame
// input value position(x,y)
//====================================================================
void write_video
(
CSnakeApp * pMe,
int x,
int y,
RGBVAL color,
unsigned char full
)
{
AEERect rect;
rect.x=x;
rect.y=y;
rect.dx=BLOCK_SIZE;
rect.dy=BLOCK_SIZE;
if(full)
IDISPLAY_DrawRect(pMe->a.m_pIDisplay,
&rect,
RGB_BLACK,// RGBVAL
color,// RGBVAL
IDF_RECT_FRAME | IDF_RECT_FILL);
else
IDISPLAY_DrawRect(pMe->a.m_pIDisplay,
&rect,
RGB_WHITE,// RGBVAL
RGB_WHITE,// RGBVAL
IDF_RECT_FRAME | IDF_RECT_FILL);
}
/**/
void init_frame(CSnakeApp * pMe)
{
AEERect rect;
rect.x=LEFT_LIMIT*BLOCK_SIZE-1;
rect.y=UP_LIMIT*BLOCK_SIZE-1;
rect.dx=(pMe->AreaWidth-LEFT_LIMIT+1)*BLOCK_SIZE+2;
rect.dy=(pMe->AreaHeight-UP_LIMIT+1)*BLOCK_SIZE+2;
IDISPLAY_DrawRect(pMe->a.m_pIDisplay,
&rect,
MAKE_RGB(0,0,255),// RGBVAL
MAKE_RGB(0,0,255),// RGBVAL
IDF_RECT_FRAME );//| IDF_RECT_FILL);
}
void init_snake(CSnakeApp * pMe)
{int i;
pMe->snake_length=7;
pMe->current_snake_direction=RIGHT;
for(i=0;i<pMe->snake_length;i++)
{pMe->snake[i].x=SNAKE_HEAD_X-i;
pMe->snake[i].y=SNAKE_HEAD_Y;
}
for(i=pMe->snake_length-1;i>=0;i--)
write_video(pMe,pMe->snake[i].x*BLOCK_SIZE,pMe->snake[i].y*BLOCK_SIZE,MAKE_RGB(128,128,128),TRUE);
}
void generate_food(CSnakeApp *pMe)
{
int i;
int found=FALSE;
while(!found)
{
pMe->food_x=(int)GET_UPTIMEMS();;
pMe->food_x=(pMe->food_x%(pMe->AreaWidth-LEFT_LIMIT-2)+pMe->snake[0].x+pMe->snake[2].y)%(pMe->AreaWidth-LEFT_LIMIT-2);
pMe->food_y=(int)GET_UPTIMEMS();;
pMe->food_y=(pMe->food_y%(pMe->AreaHeight-UP_LIMIT-2)+pMe->snake[1].y+pMe->snake[3].x)%(pMe->AreaHeight-UP_LIMIT-2);
pMe->food_x=pMe->food_x+LEFT_LIMIT+1;
pMe->food_y=pMe->food_y+UP_LIMIT+1;
for(i=0;i<pMe->snake_length;i++)
{
if(pMe->food_x==pMe->snake[i].x&&pMe->food_y==pMe->snake[i].y) break;
}
if(i>=pMe->snake_length) found=TRUE;
}
write_video(pMe,pMe->food_x*BLOCK_SIZE,pMe->food_y*BLOCK_SIZE,MAKE_RGB(255,0,0),TRUE);
}
boolean isself(CSnakeApp *pMe)
{int i;
for(i=1;i<pMe->snake_length;i++)
if(pMe->snake[0].x==pMe->snake[i].x&&pMe->snake[0].y==pMe->snake[i].y)
break;
if(i>=pMe->snake_length)
return FALSE;
else
return TRUE;
}
static void display_score(CSnakeApp *pMe)
{
AECHAR game_score[15]={'s','c','o','r','e',':',0,0,0,0,0,0,0,0,0};
up_Itoa(game_score+6,(int)pMe->score);
IDISPLAY_DrawText(pMe->a.m_pIDisplay, // Display instance
AEE_FONT_BOLD, // Use BOLD font
game_score, // Text - Normally comes from resource
-1, // -1 = Use full string length
0, // x-cordinate
(pMe->AreaHeight+UP_LIMIT)*BLOCK_SIZE+1, // y-cordinate
NULL, // No clipping
IDF_ALIGN_CENTER);// | IDF_ALIGN_MIDDLE);
}
static void update_map(CSnakeApp *pMe)
{
int j;
for(j=0;j<pMe->snake_length;j++)
write_video(pMe,pMe->snake[j].x*BLOCK_SIZE,pMe->snake[j].y*BLOCK_SIZE,MAKE_RGB(128,128,128),FALSE);
for(j=pMe->snake_length-1;j>0;j--)
{
pMe->snake[j].x=pMe->snake[j-1].x;
pMe->snake[j].y=pMe->snake[j-1].y;
}
switch(pMe->current_snake_direction)
{
case UP:
pMe->snake[0].y--;
if(pMe->snake[0].y<UP_LIMIT)
pMe->snake[0].y=pMe->AreaHeight;
break;
case DOWN:
pMe->snake[0].y++;
if(pMe->snake[0].y>pMe->AreaHeight)
pMe->snake[0].y=UP_LIMIT;
break;
case LEFT:
pMe->snake[0].x--;
if(pMe->snake[0].x<LEFT_LIMIT)
pMe->snake[0].x=pMe->AreaWidth;
break;
case RIGHT:
pMe->snake[0].x++;
if(pMe->snake[0].x>pMe->AreaWidth)
pMe->snake[0].x=LEFT_LIMIT;
break;
}
for(j=pMe->snake_length-1;j>=0;j--)
write_video(pMe,pMe->snake[j].x*BLOCK_SIZE,pMe->snake[j].y*BLOCK_SIZE,MAKE_RGB(128,128,128),TRUE);
if(pMe->snake[0].x==pMe->food_x&&pMe->snake[0].y==pMe->food_y)
{generate_food(pMe);
pMe->snake_length++;
pMe->score+=8;
display_score(pMe);
}
if(isself(pMe))
pMe->gameover=TRUE;
if(!pMe->gameover)
ISHELL_SetTimer(pMe->a.m_pIShell, 100, (PFNNOTIFY) update_map, (AEEApplet *) pMe);
else{
}
IDISPLAY_Update (pMe->a.m_pIDisplay);
}
/*===========================================================================
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){
if(AEEApplet_New(sizeof(CSnakeApp), ClsId, pIShell,po,(IApplet**)ppObj,
(AEEHANDLER)snake_HandleEvent,NULL)
== TRUE)
{
// Add your code here .....
if(Snake_InitAppData((IApplet * ) * ppObj) == TRUE)
return (AEE_SUCCESS);
}
}
return (EFAILED);
}
/*===========================================================================
FUNCTION snake_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 snake_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(IApplet * pi, AEEEvent eCode, uint16 wParam, uint32 dwParam)
{
int timer=100;
CSnakeApp *pMe=(CSnakeApp *)pi;
switch (eCode)
{
case EVT_APP_START:
init_frame(pMe);
timer=100;
init_snake(pMe);
generate_food(pMe);
IDISPLAY_Update (pMe->a.m_pIDisplay);
ISHELL_SetTimer(pMe->a.m_pIShell, timer, (PFNNOTIFY) update_map, (AEEApplet *) pMe);
return(TRUE);
case EVT_APP_STOP:
// Add your code here .....
return TRUE;
//case
case EVT_KEY:
switch (wParam)
{
case AVK_CLR:
ISHELL_CancelTimer(pMe->a.m_pIShell, (PFNNOTIFY) update_map, (AEEApplet *) pMe);
return(FALSE);
case AVK_UP:
if(pMe->current_snake_direction!=DOWN)
{
pMe->current_snake_direction=UP;
}
break;
case AVK_DOWN:
if(pMe->current_snake_direction!=UP)
{
pMe->current_snake_direction=DOWN;
}
break;
case AVK_LEFT:
if(pMe->current_snake_direction!=RIGHT)
{
pMe->current_snake_direction=LEFT;
}
break;
case AVK_RIGHT:
if(pMe->current_snake_direction!=LEFT)
{
pMe->current_snake_direction=RIGHT;
}
break;
}
IDISPLAY_Update (pMe->a.m_pIDisplay);
return(TRUE);
default:
break;
}
return FALSE;
}
/*initialize applet-specific data.*/
static boolean Snake_InitAppData(IApplet * pi)
{
AEEDeviceInfo di;
CSnakeApp *pMe=(CSnakeApp *)pi;
ISHELL_GetDeviceInfo(pMe->a.m_pIShell,&di);
pMe->AreaWidth=(di.cxScreen-LEFT_LIMIT)/BLOCK_SIZE-1;
pMe->AreaHeight=(di.cyScreen-UP_LIMIT)/BLOCK_SIZE-2;
pMe->gameover=FALSE;
return TRUE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -