贪吃蛇新.docx
《贪吃蛇新.docx》由会员分享,可在线阅读,更多相关《贪吃蛇新.docx(14页珍藏版)》请在冰豆网上搜索。
![贪吃蛇新.docx](https://file1.bdocx.com/fileroot1/2023-1/23/806f8802-c0b3-4aa8-9769-8d941a54affd/806f8802-c0b3-4aa8-9769-8d941a54affd1.gif)
贪吃蛇新
贪吃蛇新.txt男人应该感谢20多岁陪在自己身边的女人。
因为20岁是男人人生的最低谷,没钱,没事业;而20岁,却是女人一生中最灿烂的季节。
只要锄头舞得好,哪有墙角挖不到?
/***************************************************************************\
\***************************************************************************/
/***************************************************************************\
#说明:
Cmain函数,ucos-ii初始化等定义
----------------------------------Bug--------------------------------------
----------------------------------TODOlist--------------------------------------
----------------------------------修正--------------------------------------
2010-1-12创建
\***************************************************************************/
#include"../ucos-ii/includes.h"/*uC/OSinterface*/
#include"../ucos-ii/add/osaddition.h"
#include"../inc/drivers.h"
#include"../inc/sys/lib.h"
#include"../src/gui/gui.h"
#include
#include
#pragmaimport(__use_no_semihosting_swi)//ensurenofunctionsthatusesemihosting
OS_EVENT*Mbox;
///******************任务定义***************///
OS_STKMain_Stack[STACKSIZE]={0,};//Main_Test_Task堆栈
voidMain_Task(void*Id);//Main_Test_Task
#defineMain_Task_Prio12
OS_STKBioskey_Stack[STACKSIZE]={0,};//bioskey_Test_Task堆栈
voidBioskey_Task(void*Id);//bioskey_Test_Task
#defineBioskey_Task_Prio15
OS_STKLED_Stack[STACKSIZE]={0,};
voidLED_Task(void*Id);
#defineLED_Task_Prio18
//***************************************/////////
#defineWHITE0x0FFF
#defineBLACK0x0000
#defineSIZE16//定义SIZE将原坐标系简化
#defineBGCOLORBLACK//背景色
#defineFORECOLORWHITE//前景色,用来标志游戏已被填充的部分
#defineSCOREINTERVAL30//升级间隔的分数
#defineGPFCON(*(volatileunsigned*)0x56000050)
#defineGPFUP(*(volatileunsigned*)0x56000058)
///*****************游戏定义*****************///
#defineN30
#defineROW25
#defineCOLUMN25
U16speed=0,score=0;
intx=0,y=0;
intboard[ROW][COLUMN];/*记录游戏板当前状态的数组*/
///*****************结构定义*****************///
structSnake
{
intx[N];
inty[N];
intnode;
intdirection;
intlife;
}snake;
structBean
{
intx;
inty;
intyes;
}bean;
///*****************功能函数定义*****************///
///***************画基本块******************///
voidDrawBeanSnake(PDCdc,intx,inty,intcolor)
{
if(y<1)return;
FillRect(dc,SIZE*(x+9)+1,SIZE*(y+4)+1,SIZE*(x+10)-1,SIZE*(y+5)-1,0,color);
}
///*****************画蛇*******************///
voidDrawSnake(PDCdc)
{
inti=0;
DrawBeanSnake(dc,snake.x[0],snake.y[0],FORECOLOR);
for(i=1;iDrawBeanSnake(dc,snake.x[i],snake.y[i],FORECOLOR);
}
}
///*****************画豆子*******************///
voidDrawBean(PDCdc)
{
inti;
if(bean.yes==1)
{
DrawBeanSnake(dc,bean.x,bean.y,FORECOLOR);
}
else
{
for(;;)
{
bean.x=rand()%23+1;
bean.y=rand()%23+1;
bean.yes=1;
for(i=0;i{
if(snake.x[i]==bean.x&&snake.y[i]==bean.y)
{
bean.yes=0;
break;
}
}
if(bean.yes==1)
{
DrawBeanSnake(dc,bean.x,bean.y,FORECOLOR);
break;
}
}
}
}
///**************蛇的移动******************///
voidSnakeMove(PDCdc){
inti=0;
DrawBeanSnake(dc,snake.x[snake.node-1],snake.y[snake.node-1],BGCOLOR);
for(i=snake.node-1;i>0;i--)
{
snake.x[i]=snake.x[i-1];
snake.y[i]=snake.y[i-1];
}
switch(snake.direction)
{
case1:
//left
if(snake.x[0]-1!
=snake.x[2])
{
snake.x[0]=snake.x[0]-1;
}else{
snake.x[0]=snake.x[0]+1;
}
break;
case2:
//right
if(snake.x[0]+1!
=snake.x[2])
{
snake.x[0]=snake.x[0]+1;
}else{
snake.x[0]=snake.x[0]-1;
}
break;
case3:
//up
if(snake.y[0]-1!
=snake.y[2])
{
snake.y[0]=snake.y[0]-1;
}else{
snake.y[0]=snake.y[0]+1;
}
break;
case4:
//down
if(snake.y[0]+1!
=snake.y[2])
{
snake.y[0]=snake.y[0]+1;
}else{
snake.y[0]=snake.y[0]-1;
}
break;
}
}
///*****************吃豆子*******************///
voidEatBean()
{
if(snake.x[0]==bean.x&&snake.y[0]==bean.y)
{
bean.yes=0;
score++;
speed=score/3;
if(snake.nodesnake.x[snake.node]=snake.x[snake.node-1]*2-snake.x[snake.node-2];
snake.y[snake.node]=snake.y[snake.node-1]*2-snake.y[snake.node-2];
snake.node++;
}
}
}
///**************新游戏******************///
voidNewGame(PDCdc)
{
inti=0,j=0;
ClearScreen();
for(j=ROW-2;j>0;j--)
for(i=COLUMN-2;i>0;i--)
DrawBeanSnake(dc,i,j,BGCOLOR);
snake.node=4;
snake.life=1;//1为活着,0为死亡
score=0;
speed=0;
snake.direction=1;
for(i=0;i{
snake.x[i]=11+i;
snake.y[i]=12;
}
DrawSnake(dc);
}
///**************蛇生命判断******************///
voidSnakeLife(){
inti;
if(snake.x[0]==0||snake.x[0]==24||snake.y[0]==0||snake.y[0]==24)
snake.life=0;
for(i=4;i{
if((snake.x[0]==snake.x[i])&&(snake.y[0]==snake.y[i]))
{
snake.life=0;
break;
}
}
}
///**************游戏结束******************///
voidGameOver(PDCdc)
{
U16ch6[]={'G','A','M','E','O','V','E','R',0};
U16ch7[]={'E','N','T','E','R',':
','N','E','W','G','A','M','E',0};
ClearScreen();
TextOut(dc,SIZE*30,SIZE*12,ch6,TRUE,FONTSIZE_MIDDLE);
TextOut(dc,SIZE*30,SIZE*14,ch7,TRUE,FONTSIZE_MIDDLE);
OSTimeDly(6000);
}
///*****************事件定义*****************///
/////////////////////////////////////////////////////
//Mainfunction.//
////////////////////////////////////////////////////
intmain(void)
{
ARMTargetInit();//dotarget(uHALbasedARMsystem)initialisation//
IIC_init();
OSInit();//neededbyuC/OS-II//
OSInitUart();
Mbox=OSMboxCreate((void*)0);
initOSFile();
#ifUSE_MINIGUI==0
initOSMessage();
initOSDC();
LoadFont();
#endif
loadsystemParam();
//createthetasksinuC/OSandassignincreasing//
//prioritiestothemsothatTask3attheendof//
//thepipelinehasthehighestpriority.//
LCD_printf("CreatetaskonuCOS-II...\n");
OSTaskCreate(Main_Task,(void*)0,(OS_STK*)&Main_Stack[STACKSIZE-1],Main_Task_Prio);
OSTaskCreate(Bioskey_Task,(void*)0,(OS_STK*)&Bioskey_Stack[STACKSIZE-1],Bioskey_Task_Prio);
OSTaskCreate(LED_Task,(void*)0,(OS_STK*)&LED_Stack[STACKSIZE-1],LED_Task_Prio);
OSAddTask_Init
(1);
LCD_printf("StartinguCOS-II...\n");
LCD_printf("Enteringgraphmode...\n");
LCD_ChangeMode(DspGraMode);
OSStart();//starttheOS//
//neverreached//
return0;
}//main
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
voidMain_Task(void*Id)//Main_Test_Task
{
U8*msg;
INT8Uerr;
intoldx=0,oldy=0;//保存原来坐标系位置
PDCpdc;//定义绘图设备上下文结构
//intx,y;//坐标
//doubleoffset=0;//x坐标偏移量
ClearScreen();//清屏
pdc=CreateDC();//创建绘图设备上下文
SetDrawOrg(pdc,0,0,&oldx,&oldy);
//OSTimeDly(3000);//将任务挂起3秒
do{
msg=(U8*)OSMboxPend(Mbox,0,&err);
}while(msg[0]!
=5);
for(;;)
{
OSTimeDly(3000);
NewGame(pdc);
for(;;)
{
if(600-50*speed>0)
OSTimeDly(600-50*speed);
else
OSTimeDly(50);
DrawBean(pdc);
msg=(U8*)OSMboxPend(Mbox,0,&err);
switch(msg[0])
{
case4:
snake.direction=1;
break;
case6:
snake.direction=2;
break;
case8:
snake.direction=3;
break;
case2:
snake.direction=4;
break;
}
SnakeMove(pdc);
SnakeLife();
if(snake.life==1)
{
EatBean();
DrawSnake(pdc);
}
else
break;
}
//break;
//可以询问是否重新开始
GameOver(pdc);
msg=(U8*)OSMboxPend(Mbox,0,&err);
if(msg[0]!
=5)
break;
}
DestoryDC(pdc);//删除绘图设备上下文
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
voidBioskey_Task(void*Id)//bioskey_Test_Task
{
U8key;
unsignedcharKeyBoard_Map[]={0,0,11,0,0,0,0,0,0,0,0
14,0,0,0,0,0,10,0,13,0
12,0,0,0,0,0,22,0,0,0
0,0,1,0,3,17,2,0,0,0
4,0,6,0,5,0,0,0,7,0
9,15,8,0,0,0,0,0,0,0
0,0,0
};//64,键值映射表
for(;;)
{
key=IIC_Read(0x70,1);
hudelay(10);
if(key==0)
continue;
key=KeyBoard_Map[key];
OSMboxPost(Mbox,&key);
OSTimeDly(80);
}//for
}//bioskey
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
voidLED_Task(void*Id)
{
U8key,key1,value,value1;
inti;
value=0;
value1=0;
for(;;)
{
key=score;
key1=speed;
hudelay(3000);
IIC_Write(0x70,0x07,0x60);
IIC_Write(0x70,0x08,key);
hudelay(3000);
IIC_Write(0x70,0x07,0x64);
IIC_Write(0x70,0x08,key1);
/*if(key>=10)
{
value=key/10;
key=key%10;
}
hudelay(30);
IIC_Write(0x70,0x07,0x60);
IIC_Write(0x70,0x08,key);
IIC_Write(0x70,0x07,0x61);
IIC_Write(0x70,0x08,value);
hudelay(30);
if(key1>=10)
{
value1=key1/10;
key1=key1%10;
}
hudelay(30);
IIC_Write(0x70,0x07,0x64);
IIC_Write(0x70,0x08,key1);
IIC_Write(0x70,0x07,0x65);
IIC_Write(0x70,0x08,value1);
*/
OSTimeDly(80);
}
}