⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.h

📁 这是我在ARM7Samsung3C44B0x芯片的实验板上开发的贪吃蛇游戏的源代码
💻 H
📖 第 1 页 / 共 2 页
字号:
#define CMD_NULL  0
#define CMD_BEGIN 1
#define CMD_PAUSE 2
#define CMD_HELP  3
#define CMD_STOP  4
#define SHIT_DEALY 50
#define FRUIT_RATE 0.8
#define SHIT_RATE  0.1
#define SHIT_LEN  10
#define DIR_UP      1
#define DIR_DOWN    2
#define DIR_LEFT    3
#define DIR_RIGHT   4

#define STATUS_RUN   1
#define STATUS_PAUSE 0
#define GRID_COUNT  20
#define SNAKE_LEN   5
#define DEFAULT_TICK 500

#define SELF    4
#define SHIT    3
#define ADOBE   2
#define FRUIT   1
#define NOTHING 0

u16 help_msg1[] = {' ',' ',' ',' ',' ',' ',' ',' ',0x8d2a ,0x5403 ,0x86c7,'\n',0};
u16 help_msg2[] = {' ',' ',' ',' ',0x86c7,0x53ef,0x4ee5,0x5403,0x679c,0x5b50,'(',0x6709,0x5fc3,0x5706,0x5708,')','\n',0};
u16 help_msg3[] = {0x957f,0x5927,',',' ',0x4f46,0x662f,0x86c7,0x5982,0x679c,0x5403,0x7684,0x592a,0x591a,0x5c31,'\n', 0};
u16 help_msg4[] = {0x4f1a,0x6392,0x6cc4,',',' ',0x6392,0x6cc4,0x7269,'(',0x5e26,0x53c9,0x65b9,0x6846,')',0x5728,'\n', 0};
u16 help_msg5[] = {0x5206,0x89e3,0x6389,0x4e4b,0x524d,0x4e0d,0x80fd,0x78b0,0x5230,',',' ',0x5426,0x5219,0x86c7,'\n', 0};
u16 help_msg6[] = {0x4f1a,0x56e0,0x4e3a,0x5403,0x5c4e,0x800c,0x6b7b,'.','\n', 0};
u16 help_msg7[] = {' ',' ',' ',' ',0x6309,0x952e,'2',' ','4',' ','6',' ','8',0x63a7,0x5236,0x65b9,0x5411,',',0x4e5f,'\n', 0};
u16 help_msg8[] = {0x53ef,0x4ee5,0x901a,0x8fc7,0x89e6,0x6478,0x5c4f,0x63a7,0x5236,0x65b9,0x5411,'(',0x70b9,'\n', 0};
u16 help_msg9[] = { 0x51fb,0x9760,0x8fd1,0x8fd0,0x52a8,0x573a,0x4e0a,0x4e0b,0x5de6,0x53f3,0x533a,0x57df,')','.',0};

u16 game_over[] = {'G','a','m','e',' ','O','v','e','r','!',0};
u16 game_over2[] = {'Y','o','u',' ','G','o','t',0};
u16 fenshu[] = {0,0,0,0,0,0,0,0,0};

struct Snake_Position
{
    int x;
    int y;
    int dir;
};

struct Fruit
{
    int x;
    int y;
};

struct Shit
{
    int x;
    int y;
    int ttl;
};




struct Fruit fruit;
struct Shit shit;
struct Shit shitt;

//蛇的活动区域
structRECT ground;

/* 0 表示没有按钮被按下,1表示开始,2表示暂停,3表示结束,4表示察看帮助*/
int cmd;
/* 1 表示向上,2 表示向下, 3 表示向左, 4 表示向右*/
int dir;

//记录蛇的方向
int curdir;

//记录命令
int curcmd;

//记录上一次命令
int lastcmd;

//记录蛇是否在跑,0 表示停,1表示跑
int isRun;

//指示蛇的头部
int head;
//表示蛇的长度
int len;
//表示蛇的数组
struct Snake_Position sp[500];

int setdir(int x, int y, structRECT rec);//计算方向
void clear();
void Snake_ini();
void Game_ini();
void drawOneBody(struct Snake_Position *bb, int color);
void setSnakeRect(structRECT *bd, struct Snake_Position *bb);
void printRectArt(structRECT *bd);
void cmd_begin();
void cmd_help();
void cmd_pause();
void cmd_stop();
int checkAhead();
void grownUp();
void goAhead();
struct Snake_Position getNextAheadPosition(struct Snake_Position sp, int dir);
int getNext(int index);
void genFruitAndShit();
int isInGround(int x, int y);
int isInEmptyGround(int x, int y);
void drawFruit(struct Fruit);
void drawShit(struct Shit);
structRECT getRecByCoor(int x, int y);
void checkShit();

//处理帮助命令
void cmd_help()
{
	PDC pdc = CreateDC();
	int bottom=230-10;
	int left=65;
	int right=310;
	int top=10+10;


    TextOut(pdc,left + 10,top + 20+ 19*0,help_msg1,TRUE,FONTSIZE_MIDDLE);
    TextOut(pdc,left + 10,top + 20+ 19*1,help_msg2,TRUE,FONTSIZE_MIDDLE);
    TextOut(pdc,left + 10,top + 20+ 19*2,help_msg3,TRUE,FONTSIZE_MIDDLE);
    TextOut(pdc,left + 10,top + 20+ 19*3,help_msg4,TRUE,FONTSIZE_MIDDLE);
    TextOut(pdc,left + 10,top + 20+ 19*4,help_msg5,TRUE,FONTSIZE_MIDDLE);
    TextOut(pdc,left + 10,top + 20+ 19*5,help_msg6,TRUE,FONTSIZE_MIDDLE);
    TextOut(pdc,left + 10,top + 20+ 19*6,help_msg7,TRUE,FONTSIZE_MIDDLE);
    TextOut(pdc,left + 10,top + 20+ 19*7,help_msg8,TRUE,FONTSIZE_MIDDLE);
    TextOut(pdc,left + 10,top + 20+ 19*8,help_msg9,TRUE,FONTSIZE_MIDDLE);

    DestoryDC(pdc);
}

void cmd_begin()
{
    Snake_ini();
}

void cmd_pause()
{
}

void cmd_stop()
{
	PDC pdc = CreateDC();
	int bottom=230-10;
	int left=65;
	int right=310;
	int top=10+10;

    int bai;
    int shi;
    int ge;


    clear();

    TextOut(pdc,left + 80,top + 50,game_over,TRUE,FONTSIZE_MIDDLE);
    TextOut(pdc,left + 80,top + 80,game_over2,TRUE,FONTSIZE_MIDDLE);
    len-= SNAKE_LEN;
    len*=len;
    ge  = len%10;
    len/=10;
    shi = len%10;
    len/=10;
    bai = len%10;
    fenshu[0] = bai + '0';
    fenshu[1] = shi + '0';
    fenshu[2] = ge + '0';
    TextOut(pdc,left + 140,top + 80,fenshu,TRUE,FONTSIZE_MIDDLE);

    lastcmd = curcmd;
	DestoryDC(pdc);
}

//蛇前进一格
void goAhead()
{
    int tail = getNext(head);
    struct Snake_Position newhead = getNextAheadPosition(sp[head],dir);

    //第一步,擦掉蛇尾巴
    shitt.x = sp[tail].x;
    shitt.y = sp[tail].y;
    drawOneBody(&sp[tail],COLOR_WHITE);

    //第二步,求新的蛇头
    head = tail;
    sp[head] = newhead;

    //第三步,画新的蛇头
    drawOneBody(&sp[head],COLOR_BLACK);
}

void grownUp()
{
    int index = len - 1;
    for(;index > head;index --)
    {
        sp[index + 1] = sp[index];    
    }

    len++;
}


//检查前方是什么
int checkAhead()
{
    struct Snake_Position p = getNextAheadPosition(sp[head],dir);

    if(!isInGround(p.x,p.y))
        return ADOBE;

    if(p.x == fruit.x  && p.y == fruit.y)
        return FRUIT;

    if(p.x == shit.x  && p.y == shit.y)
        return SHIT;

    if(isOnSnakeOrFuitOrShit(p.x,p.y))
        return SELF;

    return NOTHING;
}

void Game_ini()
{
    cmd = curcmd = CMD_STOP;
    lastcmd = CMD_NULL;
    fruit.x = fruit.y = 0;
    shit.x = fruit.y = 0;
}

//初始化蛇的身体
void Snake_ini()
{
    int i;
    clear();

    len = SNAKE_LEN;
    head = len - 1;

    for(i = 0; i < len; i++)
    {
        sp[i].x = GRID_COUNT/2 - len/2 + i;
        sp[i].y = GRID_COUNT/2;
        sp[i].dir = DIR_RIGHT;
        drawOneBody(&sp[i],COLOR_BLACK);
    }
    dir = DIR_RIGHT;
}

//按照某颜色画蛇的身体
void drawOneBody(struct Snake_Position *bb, int color)
{
    structRECT bd;
    PDC pdc = CreateDC();
    
    //求出蛇身的小矩形
    setSnakeRect(&bd,bb);

    FillRect(pdc,bd.left,bd.top ,bd.right, bd.bottom,GRAPH_MODE_NORMAL,color);

    DestoryDC(pdc);
}

//获得蛇身矩形
void setSnakeRect(structRECT *bd, struct Snake_Position *bb)
{
    int dx = (ground.right - ground.left)/GRID_COUNT;
    int dy = (ground.bottom - ground.top)/GRID_COUNT;

    bd->bottom = dy * bb->y + ground.top;
    bd->right  = dx * bb->x + ground.left;

    bd->left = bd->right - dx;
    bd->top = bd->bottom - dy;
}

void genFruitAndShit()
{
    if(fruit.x == 0 && fruit.y == 0 && rand() < (int)(RAND_MAX*FRUIT_RATE))
    {//尝试生成果子
        int x = rand()%GRID_COUNT;
        int y = rand()%GRID_COUNT;
        if(isInEmptyGround(x,y))
        {
            fruit.x = x;
            fruit.y = y;
            drawFruit(fruit);
        }
    }

    if(len > SHIT_LEN && shit.x == 0 && shit.y == 0 && rand() < (int)(RAND_MAX*SHIT_RATE))
    {//尝试让蛇排泄来减肥
        shit = shitt;
        shit.ttl = SHIT_DEALY ;
        drawShit(shit);
    }

}

//清屏
void clear()
{
	PDC pdc = CreateDC();
    FillRect(pdc,ground.left,ground.top ,	ground.right, ground.bottom,GRAPH_MODE_NORMAL,COLOR_WHITE);
	DestoryDC(pdc);
}


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
structRECT Line_Button_RECT,Circle_Button_RECT,Curve_Button_RECT,Clear_Button_RECT;
structRECT *pLine_Button_RECT,*pCircle_Button_RECT,*pCurve_Button_RECT,*pClear_Button_RECT,*pDraw_Wnd_RECT;


U16  Tch_Scr_State_Click[]={0x89e6,0x6478,0x72b6,0x6001,0x003a,0x0020,0x5355,0x51fb,0};//触摸状态:单击
U16  Tch_Scr_State_DBClick[]={0x89e6,0x6478,0x72b6,0x6001,0x003a,0x0020,0x53cc,0x51fb,0};//触摸屏双击
U16  Tch_Scr_State_Down[]={0x89e6,0x6478,0x72b6,0x6001,0x003a,0x0020,0x6309,0x4e0b,0};//触摸屏按下
U16  Tch_Scr_State_Up[]={0x89e6,0x6478,0x72b6,0x6001,0x003a,0x0020,0x62ac,0x8d77,0};//触摸屏抬起
U16  Tch_Scr_State_Move[]={0x89e6,0x6478,0x72b6,0x6001,0x003a,0x0020,0x79fb,0x52a8,0};//触摸屏移动

POSMSG pMsg;
structPOINT Touch_Position, *pTouch_Position ;
ButtonCtrl Line_Button,Circle_Button,Curve_Button,Clear_Button;
PButtonCtrl pLine_Button,pCircle_Button,pCurve_Button,pClearButton;
Wnd Draw_Wnd;
PWnd pDraw_Wnd;


char Begin_Buttorn_Caption[]="Stop";
char Pause_Button_Caption[]="Help";
char Stop_Button_Caption[]="Pause";
char Help_Button_Caption[]="Begin";

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -