求51单片机led8*8点阵贪吃蛇程序 5
1个回答
展开全部
#include<reg52.h>
#include<stdlib.h>
#define uchar unsigned char
sbit B0=P1^0;
sbit B1=P1^1;
sbit B2=P1^2;
sbit B3=P1^3;
uchar table[31]={0,10,20};
uchar count;
uchar way;
uchar T;
int tt;
uchar flag,flag2,flag3,flag4,flag5;
uchar u;
void delay(int n)
{
while(n--);
}
void reinit()
{
TR0=0;
TH0=(65535-50000)/256;
TL0=(65535-50000)%256;
T=0;
way=0;
count=3;
table[0]=0;
table[1]=10;
table[2]=20;
P0=0x00;
P2=0xff;
delay(50000);
flag2=1;
flag4=2;
flag5=1;
table[count]=0;
}
void display()
{
int x,y;
int j;
for (j=0;j<=count;j++)
{
x=table[j]/10;
y=table[j]%10;
switch (x)
{
case 0:
P0=0x01;
break;
case 1:
P0=0x02;
break;
case 2:
P0=0x04;
break;
case 3:
P0=0x08;
break;
case 4:
P0=0x10;
break;
case 5:
P0=0x20;
break;
case 6:
P0=0x40;
break;
case 7:
P0=0x80;
break;
default:
break;
}
switch (y)
{
case 0:
P2=0xfe;
break;
case 1:
P2=0xfd;
break;
case 2:
P2=0xfb;
break;
case 3:
P2=0xf7;
break;
case 4:
P2=0xef;
break;
case 5:
P2=0xdf;
break;
case 6:
P2=0xbf;
break;
case 7:
P2=0x7f;
break;
default:
break;
}
delay(50);
P2=0xff;
}
}
void init()
{
flag5=1;
flag4=2;
tt=0;
T=0;
flag=0;
flag2=1;
P1=0xff;
P0=0x00;
count=3;
EA=1;
EX0=1;
IT0=1;
ET0=1;
ET1=1;
TMOD=0x21;
TH0=(65535-50000)/256;
TL0=(65535-50000)%256;
TH1=0x00;
TH1=0x00;
way=0;
TR0=0;
TR1=1;
}
void keyscanf()
{
if (B0==0)
way=0;
if (B1==0)
way=1;
if (B2==0)
way=2;
if (B3==0)
way=3;
}
void dule()
{
int k;
if (flag5)
for (k=0;k<count-1;k++)
table[k]=table[k+1];
switch (way)
{
case 0:
if (table[count-1]/10<7)
table[count-1]=table[count-1]+10;
else
flag2=0;
break;
case 1:
if (table[count-1]%10<7)
table[count-1]=table[count-1]+1;
else
flag2=0;
break;
case 2:
if (table[count-1]/10>0)
table[count-1]=table[count-1]-10;
else
flag2=0;
break;
case 3:
if (table[count-1]%10>0)
table[count-1]=table[count-1]-1;
else
flag2=0;
break;
default:
break;
}
flag=0;
if (flag2)
for (k=0;k<count-1;k++)
if (table[count-1]==table[k])
{
flag2=0;
break;
}
if (!flag2)
reinit();
else if (table[count-1]==table[count])
{
if (count<30)
{
count++;
flag5=0;
}
flag4=1;
table[count]=88;
}
else
flag5=1;
}
void random()
{
int k;
flag4=0;
do
{
flag3=1;
u=rand()%64;
u=u/8*10+u%8;
for (k=0;k<count;k++)
if (u==table[k])
{
flag3=0;
break;
}
}
while(!flag3);
table[count]=u;
}
void main()
{
init();
while(1)
{
if (flag4==1)
random();
if (flag)
dule();
if (TR0)
keyscanf();
display();
}
}
void rup0() interrupt 0
{
if (flag2)
{
TR0=~TR0;
if (flag4==2)
flag4=1;
}
}
void time0() interrupt 1
{
TH0=(65535-50000)/256;
TL0=(65535-50000)%256;
T++;
if (T>=10)
{
T=0;
flag=1;
}
}
void time1() interrupt 3
{
tt++;
srand(tt);
if (tt>=2157)
tt=0;
}
#include<stdlib.h>
#define uchar unsigned char
sbit B0=P1^0;
sbit B1=P1^1;
sbit B2=P1^2;
sbit B3=P1^3;
uchar table[31]={0,10,20};
uchar count;
uchar way;
uchar T;
int tt;
uchar flag,flag2,flag3,flag4,flag5;
uchar u;
void delay(int n)
{
while(n--);
}
void reinit()
{
TR0=0;
TH0=(65535-50000)/256;
TL0=(65535-50000)%256;
T=0;
way=0;
count=3;
table[0]=0;
table[1]=10;
table[2]=20;
P0=0x00;
P2=0xff;
delay(50000);
flag2=1;
flag4=2;
flag5=1;
table[count]=0;
}
void display()
{
int x,y;
int j;
for (j=0;j<=count;j++)
{
x=table[j]/10;
y=table[j]%10;
switch (x)
{
case 0:
P0=0x01;
break;
case 1:
P0=0x02;
break;
case 2:
P0=0x04;
break;
case 3:
P0=0x08;
break;
case 4:
P0=0x10;
break;
case 5:
P0=0x20;
break;
case 6:
P0=0x40;
break;
case 7:
P0=0x80;
break;
default:
break;
}
switch (y)
{
case 0:
P2=0xfe;
break;
case 1:
P2=0xfd;
break;
case 2:
P2=0xfb;
break;
case 3:
P2=0xf7;
break;
case 4:
P2=0xef;
break;
case 5:
P2=0xdf;
break;
case 6:
P2=0xbf;
break;
case 7:
P2=0x7f;
break;
default:
break;
}
delay(50);
P2=0xff;
}
}
void init()
{
flag5=1;
flag4=2;
tt=0;
T=0;
flag=0;
flag2=1;
P1=0xff;
P0=0x00;
count=3;
EA=1;
EX0=1;
IT0=1;
ET0=1;
ET1=1;
TMOD=0x21;
TH0=(65535-50000)/256;
TL0=(65535-50000)%256;
TH1=0x00;
TH1=0x00;
way=0;
TR0=0;
TR1=1;
}
void keyscanf()
{
if (B0==0)
way=0;
if (B1==0)
way=1;
if (B2==0)
way=2;
if (B3==0)
way=3;
}
void dule()
{
int k;
if (flag5)
for (k=0;k<count-1;k++)
table[k]=table[k+1];
switch (way)
{
case 0:
if (table[count-1]/10<7)
table[count-1]=table[count-1]+10;
else
flag2=0;
break;
case 1:
if (table[count-1]%10<7)
table[count-1]=table[count-1]+1;
else
flag2=0;
break;
case 2:
if (table[count-1]/10>0)
table[count-1]=table[count-1]-10;
else
flag2=0;
break;
case 3:
if (table[count-1]%10>0)
table[count-1]=table[count-1]-1;
else
flag2=0;
break;
default:
break;
}
flag=0;
if (flag2)
for (k=0;k<count-1;k++)
if (table[count-1]==table[k])
{
flag2=0;
break;
}
if (!flag2)
reinit();
else if (table[count-1]==table[count])
{
if (count<30)
{
count++;
flag5=0;
}
flag4=1;
table[count]=88;
}
else
flag5=1;
}
void random()
{
int k;
flag4=0;
do
{
flag3=1;
u=rand()%64;
u=u/8*10+u%8;
for (k=0;k<count;k++)
if (u==table[k])
{
flag3=0;
break;
}
}
while(!flag3);
table[count]=u;
}
void main()
{
init();
while(1)
{
if (flag4==1)
random();
if (flag)
dule();
if (TR0)
keyscanf();
display();
}
}
void rup0() interrupt 0
{
if (flag2)
{
TR0=~TR0;
if (flag4==2)
flag4=1;
}
}
void time0() interrupt 1
{
TH0=(65535-50000)/256;
TL0=(65535-50000)%256;
T++;
if (T>=10)
{
T=0;
flag=1;
}
}
void time1() interrupt 3
{
tt++;
srand(tt);
if (tt>=2157)
tt=0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询