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

📄 main_task.c

📁 程序实现了贪吃蛇游戏
💻 C
字号:
void Main_Task(void *Id)             //Main_Test_Task

{

 

    POSMSG  pMsg;   

    Wnd snakeWindow;     //显示蛇的窗口变量

    PWnd psnakeWindow;   //指向该窗口指针

 

    char bmpname[12]={'1','2','7','_','3','2',' ',' ','B','M','P',0};

    char bmpnamelogo[12]={'1','4','5','_','5','9',' ',' ','B','M','P',0};

    int loop;

 

    char snakeDraw_Wnd_Caption_8[]="Snake Draw";

    U16 snakeDraw_Wnd_Caption_16[20];

 

    //初始化游戏状态

    initfinished = FALSE;

    gameover = FALSE;

    gamepass = FALSE;

    pause = FALSE;

    catch = FALSE;

   

    //定义显示蛇的窗口位置

    psnakeWindow=&snakeWindow;

    psnakeRect = &snakeRect;

    snakeRect.bottom = 220;

    snakeRect.left = 20;

    snakeRect.right = 220;

    snakeRect.top = 20;

 

    prightRect = &rightRect;

    rightRect.bottom = 220;

    rightRect.left = 230;

    rightRect.right = 318;

    rightRect.top = 20;

 

    pbarRect1 = &barRect1;

    barRect1.bottom = 100;

    barRect1.left = 30;

    barRect1.right = 100;

    barRect1.top = 80;

 

    pbarRect2 = &barRect2;

    barRect2.bottom = 160;

    barRect2.left = 90;

    barRect2.right = 210;

    barRect2.top = 140;

 

    //以5个象素为一个单位时,栅栏的位置

    pbarRect1_5 = &barRect1_5;

    barRect1_5.bottom = 16;

    barRect1_5.left = 3;

    barRect1_5.right = 16;

    barRect1_5.top = 13;

 

    pbarRect2_5 = &barRect2_5;

    barRect2_5.bottom = 28;

    barRect2_5.left = 15;

    barRect2_5.right = 38;

    barRect2_5.top = 25;

 

    HeadDirect = 1;   //1右,2左,3上,4下

    HeadPos[0] = 4;

    HeadPos[1] = 1;

    EndDirect  = 1;

    EndPos[0] = 1;

    EndPos[1] = 1;

   

 

    pdc = CreateDC();

    ClearScreen();//清屏

    ShowBmp(pdc,bmpnamelogo, 1, 1);

    OSTimeDly(2000);

    ClearScreen();

 

 

      //创建显示蛇窗口

    strChar2Unicode(snakeDraw_Wnd_Caption_16, snakeDraw_Wnd_Caption_8);

    psnakeWindow =CreateWindow(120, psnakeRect, FONTSIZE_SMALL,WND_STYLE_MODE, snakeDraw_Wnd_Caption_16, NULL);

    DrawWindow(psnakeWindow);

   

    ClearScreen();

   

    ChangePointCount=0;

    ShowBmp(pdc,bmpname, 230, 20);

    Draw3DRect2(pdc, prightRect, RGB(160, 160, 160), RGB(50, 50, 50));

   

    initfinished = TRUE;

   

    while(1)

    {

        pMsg = WaitMessage(0);

        switch (pMsg->Message)

        {

            case OSM_KEY:

                onKey(pMsg->WParam, pMsg->LParam);

        }

        DeleteMessage(pMsg);

        OSTimeDly(200);

    }

   

    DestoryDC(pdc);

}

 

 

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void onKey(int nkey, int fnkey)

{

    Uart_Printf("Hellow world!%d \n",nkey); 

    switch(nkey)

    {  

        case 1://New Game-key 1

            newGame();

            return;

        case 3://Pause-key 3

            pausegame();

            return;

        case 4://moveLeft-key 4

            //moveLeft();

            AddChangePoint(2);

            return;

        case 6://moveRight-key 6

            AddChangePoint(1);

            return;

        case 2://moveUp-key 2

            AddChangePoint(3);

            return;

        case 5://moveDown-key 5

            AddChangePoint(4);

            return;

        default:

            return;

    }

}

 

//////////////////////////////////////////////////////////////////////////////

void newGame()

{

    gameover = FALSE;

    gamepass = FALSE;

    pause = FALSE;

    catch = FALSE;

   

    HeadDirect = 1;

    HeadPos[0] = 4;

    HeadPos[1] = 1;

    EndDirect  = 1;

    EndPos[0] = 1;

    EndPos[1] = 1;

 

    boolbean00[0] = TRUE;

    boolbean00[1] = TRUE;

    boolbean00[2] = TRUE;

    boolbean01[0] = TRUE;

    boolbean10[0] = TRUE;

    boolbean11[0] = TRUE;

 

    ChangePointCount = 0;       //the number    of the change point

 

    addcount=0;

}

///////////////////////////////////////////////////////////////////////////

void pausegame()

{

    if( (gameover == FALSE)&&(gamepass == FALSE) );

    pause = !(pause);

}  

 

//////////////////////////////////////////////////////////////////////////////////////////////

void AddChangePoint(int direction)

{

    int i = 0;

    //已有的转折点在数组中后移一位,把第一位让给新的转折点

    for(i=ChangePointCount; i>0; i--)

        {

            ChangePointPos[i][0] = ChangePointPos[i-1][0];

            ChangePointPos[i][1] = ChangePointPos[i-1][1];

            ChangePointDirect[i] = ChangePointDirect[i-1];

        }

    ChangePointPos[0][0] = HeadPos[0];

    ChangePointPos[0][1] = HeadPos[1];

    ChangePointDirect[0] = direction;

    HeadDirect = direction;

    ChangePointCount++;

}

 

⌨️ 快捷键说明

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