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

📄 main.c

📁 这是我在ARM7Samsung3C44B0x芯片的实验板上开发的贪吃蛇游戏的源代码
💻 C
字号:
#include"..\ucos-ii\includes.h"               /* uC/OS interface */
#include "..\ucos-ii\add\osaddition.h"
#include "..\inc\TCPIP\internet.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "..\inc\Maro.h"
#include "main.h"



int Main(int argc, char **argv)
{
	ARMTargetInit();        // do target (uHAL based ARM system) initialisation //

	OSInit();               // needed by uC/OS-II //
	uHALr_ResetMMU();

	LCD_Init();             //初始化LCD模块
	TchScr_init();
	LCD_printf("LCD initialization is OK\n");
	LCD_printf("240 x 128  Text Mode\n");

	LoadFont();
	LoadConfigSys();

  // create the tasks in uC/OS and assign increasing //
  // priorities to them so that Task3 at the end of  //
  // the pipeline has the highest priority.          //
	LCD_printf("Create task on uCOS-II...\n");
	OSTaskCreate(Main_Task,  (void *)0,  (OS_STK *)&Main_Stack[STACKSIZE*8-1],  Main_Task_Prio);
//	OSTaskCreate(Led_Flash_Task,  (void *)0,  (OS_STK *)&Led_Flash_Stack[STACKSIZE-1],  Led_Flash_Prio  );
    OSTaskCreate(Touch_Screen_Task,  (void *)0,  (OS_STK *)&Touch_Screen_Stack[STACKSIZE-1],  Touch_Screen_Task_Prio);
	OSTaskCreate(Snake_Task,  (void *)0,  (OS_STK *)&Snake_Stack[STACKSIZE-1],  Snake_Prio  );

    OSAddTask_Init();
  
	LCD_printf("Starting uCOS-II...\n");
	LCD_printf("Entering graph mode...\n");
	LCD_ChangeMode(DspGraMode);
       
	initOSGUI();
	InitRtc();
	Nand_Rw_Sem=OSSemCreate(1);   //创建Nand-Flash读写控制权旗语,初值为1满足互斥条件//

	OSStart();              // start the game //

	// never reached //
	return 0;
}//main

void Main_Task(void *Id)             //Main_Test_Task
{
	pTouch_Position=&Touch_Position;
	pLine_Button=&Line_Button;
	pCircle_Button=&Circle_Button;
	pCurve_Button=&Curve_Button;
	pClearButton=&Clear_Button;
	pDraw_Wnd=&Draw_Wnd;
	
	pLine_Button_RECT=&Line_Button_RECT;
	pCircle_Button_RECT=&Circle_Button_RECT;
	pCurve_Button_RECT=&Curve_Button_RECT;
	pClear_Button_RECT=&Clear_Button_RECT;
	pDraw_Wnd_RECT=&ground;
	
	strChar2Unicode(Line_Button_Caption_16, Begin_Buttorn_Caption);
	strChar2Unicode(Circle_Button_Caption_16, Pause_Button_Caption);
	strChar2Unicode(Curve_Button_Caption_16, Stop_Button_Caption);
	strChar2Unicode(Clear_Button_Caption_16, Help_Button_Caption);
	strChar2Unicode(Draw_Wnd_Caption_16, Draw_Wnd_Caption_8);

	Line_Button_RECT.bottom=70-5;
	Line_Button_RECT.left=10;
	Line_Button_RECT.right=60;
	Line_Button_RECT.top=30-5;

	Circle_Button_RECT.bottom=120-5;
	Circle_Button_RECT.left=10;
	Circle_Button_RECT.right=60;
	Circle_Button_RECT.top=80-5;

	Curve_Button_RECT.bottom=170-5;
	Curve_Button_RECT.left=10;
	Curve_Button_RECT.right=60;
	Curve_Button_RECT.top=130-5;

	Clear_Button_RECT.bottom=220-5;
	Clear_Button_RECT.left=10;
	Clear_Button_RECT.right=60;
	Clear_Button_RECT.top=180-5;

	ground.bottom=230-10;
	ground.left=65;
	ground.right=310;//	310,220
	ground.top=10+10;

	pLine_Button=CreateButton(Line_Button_ID, pLine_Button_RECT, FONTSIZE_SMALL, CTRL_STYLE_3DDOWNFRAME, Line_Button_Caption_16, NULL);
	pCircle_Button=CreateButton(Circle_Button_ID, pCircle_Button_RECT, FONTSIZE_SMALL, CTRL_STYLE_3DDOWNFRAME, Circle_Button_Caption_16, NULL);
	pCurve_Button=CreateButton(Curve_Button_ID, pCurve_Button_RECT, FONTSIZE_SMALL, CTRL_STYLE_3DDOWNFRAME, Curve_Button_Caption_16, NULL);
	pClearButton=CreateButton(Clear_Button_ID, pClear_Button_RECT, FONTSIZE_SMALL, CTRL_STYLE_3DDOWNFRAME, Clear_Button_Caption_16, NULL);

	pDraw_Wnd=CreateWindow(Draw_Wnd_ID, pDraw_Wnd_RECT, FONTSIZE_SMALL,WND_STYLE_MODE, Draw_Wnd_Caption_16, NULL);

	DrawButton(pLine_Button);
	DrawButton(pCircle_Button);
	DrawButton(pCurve_Button);
	DrawButton(pClearButton);
	DrawWindow(pDraw_Wnd);

	pdc=CreateDC();
	pdc->DrawRect=ground;
    cmd = 0;
	for(;;){
    		pMsg=WaitMessage(0);
	        switch(pMsg->Message){
    		case OSM_KEY:							//键盘的消息
	    		onKey(pMsg->WParam,pMsg->LParam);	//键盘消息响应函数
		    break;
            case OSM_TOUCH_SCREEN://OSM_TOUCH_SCREEN:
				Touch_Position.x=pMsg->WParam&0xffff;
				Touch_Position.y=pMsg->WParam>>16;
				//代码已删除,请对照流程图完成代码  
			    if(IsInRect(&Clear_Button_RECT,Touch_Position.x,Touch_Position.y))
			    {
                    cmd = CMD_BEGIN;
			    }
			    if(IsInRect(&Curve_Button_RECT,Touch_Position.x,Touch_Position.y))
			    {
                    cmd = CMD_PAUSE;
			    }
			    if(IsInRect(&Circle_Button_RECT,Touch_Position.x,Touch_Position.y))
			    {
                    cmd = CMD_HELP;
			    }
			    if(IsInRect(&Line_Button_RECT,Touch_Position.x,Touch_Position.y))
			    {
                    cmd = CMD_STOP;
			    }
			    if(IsInRect(&ground,Touch_Position.x,Touch_Position.y))
			    {//判断触摸点代表的上下左右
                    dir = setdir(Touch_Position.x, Touch_Position.y, ground);
			    }
                
		}
		DeleteMessage(pMsg);
              OSTimeDly(100);
	}
	DestoryDC(pdc);
	
}


//处理贪吃蛇的主任务
void Snake_Task(void *Id)
{
    int tick = DEFAULT_TICK;
    Game_ini();
    cmd = CMD_STOP;
    lastcmd = CMD_NULL;
    for (;;)
    {
        OSTimeDly(tick);
        curcmd = cmd;
        curdir = dir;

       switch(curcmd)
        {
        case CMD_BEGIN:
            if(lastcmd != CMD_BEGIN)
            {
                if(lastcmd != CMD_PAUSE)
                    cmd_begin();
                lastcmd = curcmd;
            }
            break;
        case CMD_PAUSE:
            if(lastcmd == CMD_BEGIN || lastcmd == CMD_NULL)
            {
                cmd_pause();
                lastcmd = curcmd;
            }
            break;
        case CMD_HELP:
            if(lastcmd == CMD_STOP || lastcmd == CMD_NULL)
            {
                cmd_help();
                lastcmd = curcmd;
            }
            break;
        case CMD_STOP:
            if(lastcmd == CMD_PAUSE || lastcmd == CMD_BEGIN)
            {
               cmd_stop();
               lastcmd = curcmd;
            }
            break;
        }
        cmd = CMD_NULL;

        if(lastcmd != CMD_BEGIN)
        {
            continue;
        }

        //蛇前进
        switch(checkAhead())
        {
        case ADOBE:
        case SHIT:
            cmd_stop();
            lastcmd = CMD_STOP;
            continue;
            break;
        case NOTHING:
            goAhead();
            break;
        case FRUIT:
            grownUp();
            goAhead();
            fruit.x = fruit.y = 0;
			if(tick>0)
				tick-=20;
            break;
        }

        genFruitAndShit();

        checkShit();

    }
}




⌨️ 快捷键说明

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