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

📄 main.c

📁 这是我在ARM7Samsung3C44B0x芯片的实验板上开发的吃豆子游戏的源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
#include"..\ucos-ii\includes.h"               /* uC/OS interface */
#include "..\ucos-ii\add\osaddition.h"
#include "..\inc\beep.h"
//#include "..\inc\TCPIP\internet.h"
//#include <string.h>
#include <math.h>

const u8 xmax = 40;
const u8 xmin = 1;
const u8 ymax = 40;
const u8 ymin = 1;

u8 bean00[3][2] = { {2,11}, {18,13}, {15,20} };
u8 bean01[1][2] = { {10,27} };
u8 bean10[1][2] = { {38,3} };
u8 bean11[1][2] = { {27,38} };

BOOLEAN boolbean00[3] = {TRUE, TRUE, TRUE};
BOOLEAN boolbean01[1] = { TRUE };
BOOLEAN boolbean10[1] = { TRUE };
BOOLEAN boolbean11[1] = { TRUE };

U8 Length;//the length of the snake

U8 HeadPos[2];//the position of the head of the snake
U8 EndPos[2]; //the position of the end  of the snake

U8 HeadDirect; //the direction of the headpoint move toward
U8 EndDirect;  //the direction of the endpoint  move toward

U8 ChangePointPos[10][2];  //the position  of the change point
U8 ChangePointDirect[10];  //the direction of the change point
U8 ChangePointCount;       //the number    of the change point

u8 addcount=0;//after eat bean,the length to be added

structRECT snakeRect, rightRect;
structRECT *psnakeRect, *prightRect;

structRECT barRect1, barRect2;
structRECT *pbarRect1, *pbarRect2;

structRECT barRect1_5, barRect2_5;
structRECT *pbarRect1_5, *pbarRect2_5;


BOOLEAN gameover,pause,gamepass;
BOOLEAN catch;

BOOLEAN initfinished;


PDC pdc;


    
///******************任务定义***************///
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           60

OS_STK Transmit_Task_Stack[STACKSIZE*8]={0, };   //Transmit_Task堆栈
void Transmit_Task(void *Id);             //Transmit_Task
#define Transmit_Task_Prio     20

/**************已经定义的OS任务*************
#define SYS_Task_Prio				1
#define Touch_Screen_Task_Prio		9
#define Main_Task_Prio     12
#define Key_Scan_Task_Prio      58
#define Lcd_Fresh_prio           59
#define Led_Flash_Prio           60
#define Transmit_Task_Prio     20

***************************************/////////



///*****************事件定义*****************///

OS_EVENT *Nand_Rw_Sem;                   //Nand_Flash读写控制权旗语
//and you can use it as folloeing:
//    Nand_Rw_Sem=OSSemCreate(1);        //创建Nand-Flash读写控制权旗语,初值为1满足互斥条件//
//    OSSemPend(Nand_Rw_Sem,0,&err);
//    OSSemPost(Nand_Rw_Sem);

OS_EVENT *Uart_Rw_Sem;                   //Uart读写控制权旗语
//and you can use it as folloeing:
//    Uart_Rw_Sem=OSSemCreate(1);        //创建Uart读写控制权旗语,初值为1满足互斥条件//
//    OSSemPend(Uart_Rw_Sem,0,&err);
//    OSSemPost(Uart_Rw_Sem);


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

void Led_Flash_Task(void *Id)//指示RTOS处于正常工作中
{
  unsigned char led_state;
  Uart_Printf(0,"\n10");
  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.                //
////////////////////////////////////////////////////

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模块
	LCD_printf("LCD initialization is OK\n");
	LCD_printf("240 x 128  Text Mode\n");
       initOSGUI();
	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(Transmit_Task,(void *)0,  (OS_STK *)&Transmit_Task_Stack[STACKSIZE-1],  Transmit_Task_Prio);
 
	OSAddTask_Init();
  
	LCD_printf("Starting uCOS-II...\n");
	LCD_printf("Entering graph mode...\n");
	LCD_ChangeMode(DspGraMode);
       
	
	
	InitRtc();
	Nand_Rw_Sem=OSSemCreate(1);   //创建Nand-Flash读写控制权旗语,初值为1满足互斥条件//

	OSStart();              // start the game //

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

//////////////////////////////////////////////////////////////////////////////////////////////
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++;
}

//////////////////////////////////////////////////////////////////////////
void DelChangePoint()
{
	
	EndDirect = ChangePointDirect[ChangePointCount-1];
	EndPos[0] = ChangePointPos[ChangePointCount-1][0];
	EndPos[1] = ChangePointPos[ChangePointCount-1][1];

   	ChangePointPos[ChangePointCount-1][0] = 0;
	ChangePointPos[ChangePointCount-1][1] = 0;
	ChangePointDirect[ChangePointCount-1] = 0;
	
	ChangePointCount--;
}
	
///////////////////////////////////////////////////////////////////////////
void pausegame()
{
    if( (gameover == FALSE)&&(gamepass == FALSE) );
	pause = !(pause);
}	

//////////////////////////////////////////////////////////////////////////////
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 onKey(int nkey, int fnkey)
{
	Uart_Printf("Hellow world!%d \n",nkey);	
	switch(nkey)
	{	
		case 0://New Game-key 1
			newGame();
			return;
		case 8://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 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;

	
	pbarRect1_5 = &barRect1_5;
	barRect1_5.bottom = 15;
	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;
	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));

⌨️ 快捷键说明

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