拾芝麻丢芝麻 单片机 游戏 c语言代码Word文档下载推荐.docx
- 文档编号:18243746
- 上传时间:2022-12-14
- 格式:DOCX
- 页数:14
- 大小:17.71KB
拾芝麻丢芝麻 单片机 游戏 c语言代码Word文档下载推荐.docx
《拾芝麻丢芝麻 单片机 游戏 c语言代码Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《拾芝麻丢芝麻 单片机 游戏 c语言代码Word文档下载推荐.docx(14页珍藏版)》请在冰豆网上搜索。
sbitIRIN=P3^2;
//红外接口
ucharIrValue[4]={0};
//用来存放读取到的红外值,只用IrValue[2]
unsignedcharTime;
ucharreceive;
ucharfoodcount=0;
uchardying=0;
//--点阵显示数组--//
ucharcodetab0[]=
{0x00,0x01,0x00,0x02,0x00,0x04,0x00,0x08,
0x00,0x10,0x00,0x20,0x00,0x40,0x00,0x80,
0x01,0x00,0x02,0x00,0x04,0x00,0x08,0x00,
0x10,0x00,0x20,0x00,0x40,0x00,0x80,0x00};
uchartab[]=//动态的bitmap
{255,255,1,128,1,128,1,128,1,128,1,128,1,128,1,128,1,128,1,128,1,128,1,128,1,128,1,128,1,128,255,255};
ucharcodetabsave[]=//tab的副本
ucharcodedx[4]={1,2,1,0};
ucharcodedy[4]={0,1,2,1};
//--动态数码管--//
ucharCODE[16]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71};
sbitLSA=P2^2;
sbitLSB=P2^3;
//带译码器,动态数码管接口
sbitLSC=P2^4;
uintn=1;
ucharcount=0;
ucharspeed=SPEED;
voidgivefood();
voidrestart();
voidenlight(uchari);
voidshow(uintn,uintm);
voidInitTimer0(void);
voidxych(ucharx,uchary);
ucharxytell(ucharx,uchary);
voidxyset(ucharx,uchary,ucharval);
voidIrInit();
voidDelayMs(unsignedintx);
voidHC595SendData(ucharBT3,ucharBT2,ucharBT1,ucharBT0);
voidgameloop(void);
//*********************//main
voidmain()
{
//HC595SendData(0xff,0xff,0,0);
//清屏
InitTimer0();
IrInit();
gameinit();
//xyset(10,11,1);
restart();
while
(1)gameloop();
}
//********************//gameloop
voidgameloop(void)
intk;
for(k=0;
k<
16;
k++)
{
HC595SendData(~tab[2*k+1],~tab[2*k],tab0[2*k],tab0[2*k+1]);
show(n,foodcount);
}
voidTimer0Interrupt(void)interrupt1
TH0=0x0D8;
TL0=0x0F0;
count++;
if(count==speed)
if(direction&
&
dying)
{
if(dying==1)restart();
else{dying--;
direction=0;
}
if(direction)n++;
count=0;
move();
givefood();
}
if(receive&
IrValue[2])
//n+=10;
n+=100,n+=200,n+=300,n+=400,n+=500,
if(IrValue[2]==0x18)direction=1;
elseif(IrValue[2]==0x5A)direction=2;
elseif(IrValue[2]==0x52)direction=3;
elseif(IrValue[2]==0x08)direction=4;
elseif(IrValue[2]==0x45)
if(direction!
=0)directionsave=direction,direction=0;
elsedirection=directionsave;
IrValue[2]=0;
receive=0;
if(n>
=30*(50/speed))n=0,speed=speed*5/6;
voidReadIr()interrupt0
unsignedcharj,k;
unsignedinterr;
Time=0;
DelayMs(70);
//gameloop();
if(IRIN==0)//确认是否真的接收到正确的信号
{
//1000->
500
err=1000;
//1000*10us=10ms,超过说明接收到错误的信号
/*当两个条件都为真是循环,如果有一个条件为假的时候跳出循环,免得程序出错的时
侯,程序死在这里*/
while((IRIN==0)&
(err>
0))//等待前面9ms的低电平过去
{
DelayMs
(1);
err--;
//gameloop();
//////////////////
}
if(IRIN==1)//如果正确等到9ms低电平
err=500;
while((IRIN==1)&
0))//等待4.5ms的起始高电平过去
{
DelayMs
(1);
err--;
//gameloop();
////////////
}
receive=1;
//**************************//
for(k=0;
4;
k++)//共有4组数据
{
for(j=0;
j<
8;
j++)//接收一组数据
{
err=60;
while((IRIN==0)&
0))//等待信号前面的560us低电平过去
{
DelayMs
(1);
err--;
//gameloop();
///////////
}
err=500;
while((IRIN==1)&
0))//计算高电平的时间长度。
//0.14ms
Time++;
if(Time>
30)
{
EX0=1;
return;
}
IrValue[k]>
>
=1;
//k表示第几组数据
if(Time>
=8)//如果高电平出现大于565us,那么是1
IrValue[k]|=0x80;
Time=0;
//用完时间要重新赋值
}
if(IrValue[2]!
=~IrValue[3])
return;
ucharxytell(ucharx,uchary)
//0<
=x,y<
=15
if(x<
8)returnBITVAL(tab[2*y+0],x);
else{x-=8;
returnBITVAL(tab[2*y+1],x);
voidxyset(ucharx,uchary,ucharval)
8)BITSET(tab[2*y+0],x,val);
BITSET(tab[2*y+1],x,val);
/*voidxydel(ucharx,uchary)
//
//{
//if(x<
8)BITSET(tab[2*y+0],x,0);
//else{x-=8;
BITSET(tab[2*y+1],x,0);
//}
//voidxych(ucharx,uchary)
//xyset(x,y,!
xytell(x,y));
voidIrInit()
IT0=1;
//下降沿触发
EX0=1;
//打开中断0允许
EA=1;
//打开总中断
IRIN=1;
//初始化端口
receive=0;
//**********//
voidenlight(uchari)
switch(i)//位选,选择点亮的数码管,
case(0):
LSA=0;
LSB=0;
LSC=0;
break;
//显示第0位
case
(1):
LSA=1;
//显示第1位
case
(2):
LSB=1;
//显示第2位
case(3):
//显示第3位
case(4):
LSC=1;
//显示第4位
case(5):
//显示第5位
case(6):
//显示第6位
case(7):
//显示第7位
voidshow(uintn,uintm)
uchari,j;
for(i=0,j=10;
n!
=0&
i<
i++,j=10)
enlight(7-i);
P0=CODE[n%10];
n/=10;
while(j-->
0);
P0=0;
if(m==0)
enlight(3);
P0=CODE[0];
m/=10;
j=10;
else
m!
enlight(3-i);
P0=CODE[m%10];
voidInitTimer0(void)
TMOD=0x01;
TH0=0x0FF;
TL0=0x38;
EA=1;
ET0=1;
TR0=1;
voidHC595SendData(ucharBT3,ucharBT2,ucharBT1,ucharBT0)
{
//从高位到低位依次送给MOSIO
uchari;
for(i=0;
i++)
MOSIO=BT3>
7;
BT3<
=1;
S_CLK=0;
S_CLK=1;
MOSIO=BT2>
7;
BT2<
MOSIO=BT1>
BT1<
MOSIO=BT0>
BT0<
R_CLK=0;
R_CLK=1;
voidDelayMs(unsignedintx)//0.14ms误差0us
unsignedchari;
while(x--)
for(i=0;
i<
13;
i++)
{}
voidmove()
ucharx=xx,y=yy;
if(direction!
=0)
x+=dx[direction-1]-1;
y+=dy[direction-1]-1;
if(!
(0<
=x&
x<
16&
0<
=y&
y<
16)){dead();
return;
elseif(x==aimx&
y==aimy)aimx=17,foodcount+=1,push(x,y,1);
elseif(xytell(x,y)){dead();
elsepush(x,y,0);
voidpush(ucharx,uchary,ucharadd)
/*
//uchartlen;
//if(len>
head)tlen=len-head;
//elsetlen=LENMAX-head+len;
if(add)
xx[len]=x;
yy[len]=y;
len++;
if(len==LENMAX)len=0;
head++;
if(head+1==LENMAX)len=0;
xyset(x,y,1);
elseif(!
add)
xyset(xx[head],yy[head],0);
if(head==LENMAX)head=0;
*/
if(!
add)xyset(xx,yy,0);
xyset(x,y,1);
xx=x;
yy=y;
voidrestart()
32;
i++)tab[i]=tabsave[i];
direction=2;
n=1;
speed=SPEED;
foodcount=0;
dying=0;
srand(n);
xx=rand()%7+1;
yy=rand()%16;
aimx=rand()%6+8;
aimy=rand()%16;
xyset(xx,yy,1);
xyset(aimx,aimy,1);
voiddead()
direction=0;
dying=2;
n=0;
voidgameinit()
//len=1;
head=0;
voidgivefood()
ucharx,y;
if(aimx>
=16||aimy>
=16)
srand(n);
x=rand()%14+1;
y=rand()%14+1;
xytell(x,y))
aimx=x,aimy=y;
xyset(x,y,1);
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 拾芝麻丢芝麻 单片机 游戏 c语言代码 芝麻 语言 代码