#include <at89x51.h>
#include "12864.h"
#include "zf.h"
uchar length[62]={0,6,1,1}; //´æ·ÅÉßÉíµÄÊý×é;
bit flag2,flag4,flag=0; //flagÊÇÐÐÁбêÖ¾,0±íʾÐУ¬1±íʾÁÐ;flag1ÊDz½½ø±êÖ¾ flag2±íʾÊÇ·ñÉßÍ·Óöµ½ÉßÉí 1±íʾûÓÐ;
bit flag3,flag5=0;
bit flag1=0; //flag5ÊÇÍⲿÖжÏ1µÄ±ê־룬flag3±íʾʳÎïÊÇ·ñÔÚÉßÉíÉíÉÏ;flag4ÊÇÉßÊÇ·ñ³ÔʳÎïµÄ±êÖ¾
uchar k,dengji=2; //flag3ÊÇʳÎïÊÇ·ñÖØÑ¡µÄ±êÖ¾¡£kÊÇ°´¼üºÅ;
uchar direction=1; //direction±íʾ·½Ïò;
uchar p=10; //¶¨Ê±´ÎÊý
sbit LED=P1;
sbit REST=P3^5;
/*ÖжÏÁ˳ÌÐò*/
/*ͨ¹ý°´¼üµÄÖµÅжÏÉßÍ·ÐÐ×ߵķ½Ïò*/
sbit SPK=P3^4;
#define uchar unsigned char
#define uint unsigned int
//ÓÎÏ·½áÊøÌáʾÒô
uchar code SONG_TONE[]=
{
212,212,190,212,159,169,0
};
uchar code SONG_LONG[]=
{
9,3,12,12,12,24,0
};
void delay(unsigned char n)
{
unsigned char a;
for(a=0;a<n;a++);
}
void PlayMusic()
{
uint i =0,j,k;
while(SONG_LONG[i]!=0||SONG_TONE[i]!=0)
{
for(j=0;j<SONG_LONG[i]*20;j++)
{
SPK = ~SPK;
for(k=0;k<SONG_TONE[i]/3;k++);
}
delay(5);
i++;
}
}
//°´¼üÒô
void DelayMS(uint x)
{
uchar t;
while(x--)
{
for(t=0;t<120;t++);
}
}
void Play(uchar t)
{
uchar i;
for(i=0;i<100;i++)
{
SPK = ~SPK;
DelayMS(t);
}
SPK = 0;
}
unsigned char counter;
void zhongduan() interrupt 0 using 0
{
k=(P2>>6);
k=k&0x03;
if(flag)
{
if(k==1) direction=3; Play(1);//×ó
if(k==2) direction=1;Play(1);//ÓÒ
}
else
{
if(k==0) direction=4;Play(1);//ÉÏ
if(k==3) direction=2;Play(1);//ÏÂ
}
if(k==0)P1=0xfe;
else if(k==1)P1=0xfd;
else if(k==2)P1=0xfb;
else if(k==3)P1=0xf7;
else P1=0xff;
}
/*¿ªÊ¼ÔÝÍ£¼üµÄÅжÏ*/
void zhongduan1() interrupt 2 using 2
{
if(!flag5)
{
flag5=1;
Play(1);
P1=0xef;
}
else
{
flag5=0;
Play(1);
P1=0xef;
}
}
void dingshi() interrupt 1 using 1 //¶¨Ê±³ÌÐò.²úÉú²½½øʱ¼ä
{
if(p--)
{
TL0=0;
TH0=0xa0;
flag1=0;
}
else
{
flag1=1;
TL0=0;
TH0=0x00;
p=10-(dengji>>1);
}
}
main()
{
uchar food[2]={12,8};
uchar i,x,y; //x,yÊÇʳÎïµÄ»º´æÇø
choose12864(2);
init12864();
clear12864();
length[62]=2;
delay1ms(350);
delay1ms(350);
choose12864(2);
clear12864();
vertical(1,61,30);
vertical(1,61,127);
for(i=0;i<98;i++)
{
dot(30+i,1);//ÉèÖÃǽÌå
dot(30+i,62);
}
play16(0,0,0,tan);
play16(0,0,1,chi);
play16(0,0,2,she);
change(length,(length+1));
change((length+2),(length+3));
change(food,food+1);
TMOD=1; //¶¨Ê±Æ÷¹¤×÷·½Ê½
IT0=1; //±ßÑÓÓÐЧ
IT1=1;
EA=1; //¿ªCPUÖжÏ
ET0=1; //¿ª¶¨Ê±Æ÷ÖжÏ
EX0=1; //¿ªÍⲿÖжÏ
EX1=1;
TL0=0x00;
TH0=0x00; //¶¨Ê±Æ÷³õÖµ
TR0=1; //Æô¶¯¶¨Ê±Æ÷
do
{
while(flag5|!flag1);
x=*(length);
y=*(length+1);
switch(direction) //ÓÒ
{
case 1:
{
for(i=0;i<dengji-1;i++)
{
*(length+(i<<1))=*(length+(i<<1)+2);
*(length+(i<<1)+1)=*(length+(i<<1)+3);
}
(*(length+(dengji<<1)-2))++;
flag=0;
break;
}
case 2: //ÏÂ
{
for(i=0;i<dengji-1;i++)
{
*(length+(i<<1))=*(length+(i<<1)+2);
*(length+(i<<1)+1)=*(length+(i<<1)+3);
}
(*(length+(dengji<<1)-1))++;
flag=1;
break;
}
case 3: //×ó
{
for(i=0;i<dengji-1;i++)
{
*(length+(i<<1))=*(length+(i<<1)+2);
*(length+(i<<1)+1)=*(length+(i<<1)+3);
}
(*(length+(dengji<<1)-2))--;
flag=0;
break;
}
case 4:
{ //ÉÏ
for(i=0;i<dengji-1;i++)
{
*(length+(i<<1))=*(length+(i<<1)+2);
*(length+(i<<1)+1)=*(length+(i<<1)+3);
}
(*(length+(dengji<<1)-1))--;
flag=1;
break;
}
}
flag4=((*(length+(dengji<<1)-2))==food[0])&&((*(length+(dengji<<1)-1))==food[1]);
if(flag4)
{
for(i=dengji;i>0;i--)
{
*(length+(i<<1))=*(length+(i<<1)-2);
*(length+(i<<1)+1)=*(length+(i<<1)-1);
}
*length=x;
*(length+1)=y;
dengji++;
do
{
flag3=0;
food[0]=TL0%24;
food[1]=TL0%15;
for(i=0;i<dengji-1;i++) //ʳÎïÊÇ·ñÔÚÉßÉíÉíÉÏ
{
if((*(length+(i<<1)))==food[0]&&((*(length+(i<<1)+1))==food[1]))
{
flag3=1;
break;
}
}
}while(flag3);
change(food,food+1);
}
flag2=1; //ÉßͷûÓÐÓöµ½ÉßÉí
for(i=0;i<dengji-1;i++)
{
if(*(length+(i<<1))==*(length+(dengji<<1)-2)&&(*(length+(i<<1)+1)==*(length+(dengji<<1)-1)))
{
flag2=0;
break;
}
}
flag2=flag2&&*(length+(dengji<<1)-2)>=0&&*(length+(dengji<<1)-2)<24;//ÉßÍ·ÊÇ·ñ³ö±ß½ç
flag2=flag2&&*(length+(dengji<<1)-1)>=0&&*(length+(dengji<<1)-1)<15;
if(flag2)
{
clear(&x,&y);
{
change(length+(dengji<<1)-2,length+(dengji<<1)-1);
}
if(flag4)
{
change(&x,&y);
}
flag1=0;
play8(0,0,3,shu0+((dengji/10)<<4));
play8(0,1,3,shu0+(((dengji)%10)<<4));
}
}
//while(REST);
// delay1ms(450);
// delay1ms(450);
// choose12864(2);
// clear12864();
// delay1ms(450);
// delay1ms(450);
// init12864();
// clear12864();
//while(1);
while(flag2);
delay1ms(450);
delay1ms(450);
choose12864(2);
clear12864();
play16(0,4,1,over);
play16(0,6,1,over+32);
play16(1,0,1,over+64);
play16(1,2,1,over+96);
PlayMusic();
SPK=1;
//while(1);
delay1ms(450);
delay1ms(450);
if(flag5!=flag)
{
(*(void(*)())0)(); //ÈÃPCÖóÌÐò¼ÆÊýÆ÷µØÖ·Áã
}
choose12864(2);
init12864();
clear12864();
}
|