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

📄 main.h

📁 这是我在ARM7Samsung3C44B0x芯片的实验板上开发的贪吃蛇游戏的源代码
💻 H
📖 第 1 页 / 共 2 页
字号:
char Draw_Wnd_Caption_8[]="Draw Window";
U16 Line_Button_Caption_16[20];
U16 Circle_Button_Caption_16[20];
U16 Curve_Button_Caption_16[20];
U16 Clear_Button_Caption_16[20];
U16 Draw_Wnd_Caption_16[20];

PDC pdc;

const U32 Line_Button_ID=100;
const U32 Circle_Button_ID=101;
const U32 Curve_Button_ID=102;
const U32 Clear_Button_ID=103;
const U32 Draw_Wnd_ID=104;

OS_STK Touch_Screen_Stack[STACKSIZE]={0, };   //Touch_Screen_Task堆栈
void Touch_Screen_Task(void *Id);             //触摸屏任务
#define Touch_Screen_Task_Prio      9
void TchScr_State_Display(U32 state);

///******************任务定义***************///
OS_STK Main_Stack[STACKSIZE*8]={0, };   //Main_Test_Task堆栈
void Main_Task(void *Id);             //Main_Test_Task
#define Main_Task_Prio     12

OS_STK Led_Flash_Stack[STACKSIZE]= {0, }; //LED闪烁任务堆栈
void Led_Flash_Task(void *Id);            //LED闪烁任务
#define Led_Flash_Prio           50


OS_STK Snake_Stack[STACKSIZE]= {0, }; //LED闪烁任务堆栈
void Snake_Task(void *Id);            //LED闪烁任务
#define Snake_Prio           60

void Touch_Screen_Task(void *Id)             //触摸屏任务
{
	int x,y;
	U32 TchScrAction;
	POSMSG pmsg;

	for (;;){
		int oldx,oldy;
		int i;
		for(;;){
			if(TchScrAction!=TCHSCR_ACTION_DOWN && TchScrAction!=TCHSCR_ACTION_MOVE){
				if(!(rPDATC&ADS7843_PIN_PEN)){							//触摸屏有触摸
					TchScr_GetScrXY(&x, &y);

					TchScrAction=TCHSCR_ACTION_DOWN;
					pmsg=OSCreateMessage(NULL, OSM_TOUCH_SCREEN,(y<<16)|(x&0xffff),TCHSCR_ACTION_DOWN);
					SendMessage(pmsg);

					for(i=0;i<20;i++){
						if(rPDATC&ADS7843_PIN_PEN){						//触摸屏未被触摸
							TchScrAction=TCHSCR_ACTION_UP;
							pmsg=OSCreateMessage(NULL, OSM_TOUCH_SCREEN,(y<<16)|(x&0xffff),TCHSCR_ACTION_UP);
							SendMessage(pmsg);
							break;
						}
						OSTimeDly(15);									//一定时间内,抬起
					}
					if(i<20){												//在规定的双击时间之内抬起,检测是不是及时按下
						for(i=0;i<20;i++){
							if(!(rPDATC&ADS7843_PIN_PEN)){				//触摸屏又有触摸
								TchScrAction=TCHSCR_ACTION_DBCLICK;		//状态:  双击
								break;
								}
							OSTimeDly(15);
							}
						if(i==20)											//没有在规定的时间内按下
							TchScrAction=TCHSCR_ACTION_CLICK;			//如果在抬起之后无接触,触摸屏单击,
																		//单击就是按下再抬起
						}
					else{												// 在规定的时间里,未抬起
						oldx=x;
						oldy=y;
						}
					break;
					}
				}
			else{														//触摸屏已经有接触
				if(rPDATC&ADS7843_PIN_PEN){								//抬起
					TchScrAction=TCHSCR_ACTION_UP;
					x=oldx;
					y=oldy;
					break;
					}
				else{												//处于: 按下或Move的状态
					TchScr_GetScrXY(&x, &y);
					if(ABS(oldx-x)>5 ||ABS( oldy-y)>4){
						TchScrAction=TCHSCR_ACTION_MOVE;			//超出范围,动作改为Move,否则仍为Down
						break;
						}
					}
				}
			OSTimeDly(10);
			}
		oldx=x;
		oldy=y;

		pmsg=OSCreateMessage(NULL, OSM_TOUCH_SCREEN,(y<<16)|(x&0xffff),TchScrAction);
		SendMessage(pmsg);
		OSTimeDly(200);
	}
}


OS_EVENT *Nand_Rw_Sem;                   //Nand_Flash读写控制权旗语

OS_EVENT *Uart_Rw_Sem;                   //Uart读写控制权旗语



void Led_Flash_Task(void *Id)//指示RTOS处于正常工作中
{
  unsigned char led_state;
  for (;;)
  {
    Led_Display(led_state);
    led_state=~led_state;
    OSTimeDly(250);
  }
}//Led_Flash_Task


void initOSGUI()	//初始化操作系统的图形界面
{
	structRECT rect;

	initOSMessage();
	initOSList();
	initOSDC();
	initOSCtrl();
	initOSFile();
}
/////////////////////////////////////////////////////
//                  Main function.                //
////////////////////////////////////////////////////
extern U8 isConfigsysLoad;
extern U8 sysCONFIG[];
extern U32 ConfigSysdata[];


//小程序,获取方向值
int setdir(int x, int y, structRECT rec)
{
    int dis = 1000000;
    int cc;
    int dir;
    cc = pow(x - (rec.left + rec.right)/2,2) + pow(y - rec.top,2); 
    if(cc < dis)
    {//上
        dis = cc;
        dir = DIR_UP;
    }

    cc = pow(x - (rec.left + rec.right)/2,2) + pow(y - rec.bottom,2); 
    if(cc < dis)
    {//下
        dis = cc;
        dir = DIR_DOWN;
    }

    cc = pow(x - rec.left,2) + pow(y - (rec.top + rec.bottom)/2,2); 
    if(cc < dis)
    {//左
        dis = cc;
        dir = DIR_LEFT;
    }

    cc = pow(x - rec.right,2) + pow(y - (rec.top + rec.bottom)/2,2); 
    if(cc < dis)
    {//右
        dis = cc;
        dir = DIR_RIGHT;
    }

    if((curdir == DIR_RIGHT && dir == DIR_LEFT)
        || (curdir == DIR_LEFT && dir == DIR_RIGHT)
        || (curdir == DIR_UP && dir == DIR_DOWN)
        || (curdir == DIR_DOWN && dir == DIR_UP)
        )
    {
        dir = curdir;
    }
    return dir;
}


void printRectArt(structRECT *bd)
{
}

struct Snake_Position getNextAheadPosition(struct Snake_Position sp, int dir)
{
    struct Snake_Position res = sp;
    switch(dir)
    {
    case DIR_UP:
        res.y--;
        break;
    case DIR_DOWN:
        res.y++;
        break;
    case DIR_LEFT:
        res.x--;
        break;
    case DIR_RIGHT:
        res.x++;
        break;
    }
    return res;
}

int getNext(int index)
{
    return ((index + 1)>=len?0:(index + 1));
}

int isInGround(int x, int y)
{
    return x>0 && y>0 && x <= GRID_COUNT && y <= GRID_COUNT; 
}

int isOnSnakeOrFuitOrShit(x,y)
{
    int i;
    for(i = 0;i< len; i++)
    {
        if(sp[i].x == x && sp[i].y == y)
            return 1 == 1;
    }

    if(fruit.x == x && fruit.y == y)
        return 1 == 1;

    if(shit.x == x && shit.y == y)
        return 1 == 1;

    return 2 == 1;
}


int isInEmptyGround(int x, int y)
{
    return isInGround(x,y) && !isOnSnakeOrFuitOrShit(x,y);
}


void drawFruit(struct Fruit f)
{
    structRECT rec =  getRecByCoor(f.x,f.y);
    PDC pdc = CreateDC();

    Circle(pdc,(rec.left + rec.right)/2, (rec.bottom + rec.top)/2,(rec.bottom - rec.top)/2);
    Circle(pdc,(rec.left + rec.right)/2, (rec.bottom + rec.top)/2,(rec.bottom - rec.top)/3);
    Circle(pdc,(rec.left + rec.right)/2, (rec.bottom + rec.top)/2,(rec.bottom - rec.top)/4);
    Circle(pdc,(rec.left + rec.right)/2, (rec.bottom + rec.top)/2,(rec.bottom - rec.top)/5);

    DestoryDC(pdc);
}


void drawShit(struct Shit s)
{
    structRECT rec =  getRecByCoor(s.x,s.y);
    PDC pdc = CreateDC();


    Circle(pdc,(rec.left + rec.right)/2, (rec.bottom + rec.top)/2,(rec.bottom - rec.top)/2);
    
    DrawRectFrame(pdc,rec.left,rec.top ,	rec.right, rec.bottom);
    MoveTo(pdc,rec.left,rec.top);
    LineTo(pdc,rec.right,rec.bottom);

    MoveTo(pdc,rec.left,rec.bottom);
    LineTo(pdc,rec.right,rec.top);

    DestoryDC(pdc);
}

structRECT getRecByCoor(int x, int y)
{
    structRECT res;

    int dx = (ground.right - ground.left)/GRID_COUNT;
    int dy = (ground.bottom - ground.top)/GRID_COUNT;

    res.bottom = dy * y + ground.top;
    res.right  = dx * x + ground.left;

    res.left = res.right - dx;
    res.top = res.bottom - dy;

    return res;
}

void checkShit()
{
    if(shit.x != 0 && shit.y != 0)
    {
        if(shit.ttl-- < 0)
        {
            struct Snake_Position pp;
            pp.x = shit.x;
            pp.y = shit.y;
            drawOneBody(&pp,COLOR_WHITE);
            shit.x = 0;
            shit.y = 0;
        }
    }
}

void onKey(int nkey, int fnkey )
{
	POSMSG pMsg=0;
       switch(nkey){
		case 1:		//按键2,UP
            dir = DIR_UP;
			break;
		case 4:		//按键4,LEFT
            dir = DIR_LEFT;
			break;
		case 6:		//按键6,RIGHT
            dir = DIR_RIGHT;
			break;
		case 9:		//按键8,DOWN
            dir = DIR_DOWN;
			break;	
		default: 
			break;
		}

    //不允许往回走
       if((curdir == DIR_RIGHT && dir == DIR_LEFT)
        || (curdir == DIR_LEFT && dir == DIR_RIGHT)
        || (curdir == DIR_UP && dir == DIR_DOWN)
        || (curdir == DIR_DOWN && dir == DIR_UP)
        )
    {
        dir = curdir;
    }
}

⌨️ 快捷键说明

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