📄 eatmouse.c
字号:
/*===========================================================================
FILE: eatmouse.c
===========================================================================*/
/*===============================================================================
INCLUDES AND VARIABLE DEFINITIONS
=============================================================================== */
#include "AEEModGen.h" // Module interface definitions
#include "AEEAppGen.h" // Applet interface definitions
#include "AEEShell.h" // Shell interface definitions
#include "eatmouse.bid"
#include "AEEStdLib.h"
#include "AEEFile.h" // AEE File Manager Services
/*-------------------------------------------------------------------
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
{
int mouseX;
int mouseY;
}EATMOUSE;
typedef enum
{
UP=0,
LEFT,
RIGHT,
DOWN
}eatmouse_DIRECTION;//控制蛇的方向
typedef struct _eatmouse {
AEEApplet a ; // First element of this structure must be AEEApplet
AEEDeviceInfo DeviceInfo; // always have access to the hardware device information
IDisplay *pIDisplay; // give a standard way to access the Display interface
IShell *pIShell; // give a standard way to access the Shell interface
// add your own variables here...
int AreaWidth; // 移动空间的宽
int AreaHeight; // 移动空间的高
int gameover;//游戏是否结束
int food_x;//the x cordinate for food
int food_y;//the y cordinate for food
eatmouse_DIRECTION current_snake_direction;
int snake_length; //蛇的长度
int score; //得分
EATMOUSE snake[200];//蛇长度数组,最长为200
} eatmouse;
/*-------------------------------------------------------------------
Function Prototypes
-------------------------------------------------------------------*/
static boolean eatmouse_HandleEvent(eatmouse* pMe, AEEEvent eCode,
uint16 wParam, uint32 dwParam);
boolean eatmouse_InitAppData(eatmouse* pMe);
void eatmouse_FreeAppData(eatmouse* pMe);
/*===============================================================================
FUNCTION DEFINITIONS
=============================================================================== */
#define LEFT_LIMIT 1
#define UP_LIMIT 1
#define EATMOUSE_HEAD_X LEFT_LIMIT+7
#define EATMOUSE_HEAD_Y UP_LIMIT+7
#define BLOCK_SIZE 6 //小于或等于 6
/********************************************************************
模块 : 贪吃蛇
功能描述: translate an integer into decimal sting
输入参数:
输出参数: 无
特别说明: 无
作者 : pro.mouse
创建时间: 2006/02/21
*********************************************************************/
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);
}
/********************************************************************
模块 : 贪吃蛇
功能描述: 画框子
输入参数:
输出参数: 框子坐标
特别说明: 无
作者 : pro.mouse
创建时间: 2006/02/21
*********************************************************************/
void write_video(eatmouse * 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);
}
/********************************************************************
模块 : 贪吃蛇
功能描述: 初始化框子
输入参数:
输出参数: 无
特别说明: 无
作者 : pro.mouse
创建时间: 2006/02/21
*********************************************************************/
void init_frame(eatmouse * 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),// 上色
MAKE_RGB(0,0,255),// 上色
IDF_RECT_FRAME );//| IDF_RECT_FILL);
}
/********************************************************************
模块 : 贪吃蛇
功能描述: 初始化蛇
输入参数:
输出参数: 无
特别说明: 无
作者 : pro.mouse
创建时间: 2006/02/21
*********************************************************************/
void init_snake(eatmouse * pMe)
{
int i;
pMe->snake_length=7;
pMe->current_snake_direction=RIGHT;
for(i=0;i<pMe->snake_length;i++)
{pMe->snake[i].mouseX=EATMOUSE_HEAD_X-i;
pMe->snake[i].mouseY=EATMOUSE_HEAD_Y;
}
for(i=pMe->snake_length-1;i>=0;i--)
{
write_video(pMe,pMe->snake[i].mouseX*BLOCK_SIZE,pMe->snake[i].mouseY*BLOCK_SIZE,MAKE_RGB(128,128,128),TRUE);
}
}
/********************************************************************
模块 : 贪吃蛇
功能描述: 蛇吃食物
输入参数:
输出参数: 无
特别说明: 无
作者 : pro.mouse
创建时间: 2006/02/21
*********************************************************************/
void generate_food(eatmouse *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].mouseX+pMe->snake[2].mouseY)%(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].mouseY+pMe->snake[3].mouseX)%(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].mouseX&&pMe->food_y==pMe->snake[i].mouseY) 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);
}
/********************************************************************
模块 : 贪吃蛇
功能描述: 蛇吃了自己或者超过长度
输入参数:
输出参数: 无
特别说明: 无
作者 : pro.mouse
创建时间: 2006/02/21
*********************************************************************/
boolean isself(eatmouse *pMe)
{
int i;
AECHAR GAMEOVER1[50]=
{
'I','s',' ','r','e','a','l','l','y',' ','a','n',' ','i','d','i','o','t',','
};
AECHAR GAMEOVER2[50]=
{
'M','o','r','e',' ','s','t','u','p','i','d',' ','t','h','a','n',' ','a',' ','P','i','g','!'
};
for(i=1;i<pMe->snake_length;i++)
if(pMe->snake[0].mouseX==pMe->snake[i].mouseX&&pMe->snake[0].mouseY==pMe->snake[i].mouseY)
{
IDISPLAY_DrawText(pMe->a.m_pIDisplay,
AEE_FONT_BOLD,
GAMEOVER1,
-1,
30,
35,
NULL,
IDF_ALIGN_CENTER);
IDISPLAY_DrawText(pMe->a.m_pIDisplay,
AEE_FONT_BOLD,
GAMEOVER2,
-1,
30,
55,
NULL,
IDF_ALIGN_CENTER);
break;
}
if(i>=pMe->snake_length)
return FALSE;
else
return TRUE;
}
/********************************************************************
模块 : 贪吃蛇
功能描述: 显示你的得分
输入参数:
输出参数: 无
特别说明: 无
作者 : pro.mouse
创建时间: 2006/02/21
*********************************************************************/
static void display_score(eatmouse *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);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -