51單片機的貪吃蛇代碼(直接代碼)
#includ

e <12864.h>
#include <tcs_resource.h>
#define num 15
//ó?à′?èê?é??úòo?§é???ò???μ?μ??á11ì?
struct she
{
unsigned char x,y,value;
unsigned char direction;
};

struct loop_queue? //à?ó??-?·êy×éêμ???-?·?óáD£?1?μ?2??á3?1y15???£?ùò?′óD???15×?ò??£
{? ??
unsigned int front;
unsigned int rear;
unsigned int count;
struct she dat[num];
};
//2?êì?¤êy×éêμ???-?·?óáDμ??éò??ú??é??-ò???2ù×÷1y3ì
struct she tou,wei,food; //é?μ?í·?2?úμ?? oíê3??μ??

struct loop_queue queue;//?-?·?óáD? é?×aí??òD?3é1?μ? ??1?μ?è??óáD? ?2°íμ?′?1?μ??ò1?μ?3??óáD?
unsigned char flag_exit_game; //·μ??±ê??? ?a1ê±? ′óì°3?é?ó??··μ??2?μ¥
unsigned char flag_game_over; //?áê?±ê??? ?a1ê±? ??ê?ó??·?áê??-?? í?ê±ê1·μ??±ê???a1
unsigned char tcs_stop;//?Yí£±ê??
unsigned char tcs_nd; //???è±ê??
unsigned char tcs_dt; //μ?í?±ê??

unsigned char tcs_key_flag;? //ì°3?é??úò?′?ò??ˉ?D? ????êDí2???ò?′?°′?ü? ò2?íê????ü2úéúò???1?μ?
unsigned char tcs_key_state; //ó?óú°′?ü×′ì??úμ?êμ??
unsigned int? tcs_df;//μ?·?
unsigned int? tcs_highest_df;//×???μ?·??¤?¤?¤??óDê1ó??a??? òò?a2??aμàòa???′?ú??DDê±D′μ¥???úμ?ROM

??
void tcs_game();
void tcs_bianjie(unsigned int );? //ì°3?é?±?????
void tcs_game_initial(); //ó??·3?ê??ˉ
void timer0_initial(); ? //?¨ê±?÷03?ê??ˉ
void timer1_initial(); ??
void food_produce(); ? //2úéúê3??

unsigned char test_point_exist(unsigned char x, unsigned char y, unsigned char value);
void queue_initial(struct loop_queue *q);??
void queue_in(struct loop_queue *q,struct she h)? ;
struct she queue_out(struct loop_queue *q)? ;

void wei_you();
void wei_xia();?
void wei_zuo();
void wei_shang();
void tcs_game()
{
struct she check;? //ó?ó??Y′?é??′??ò??ˉμ?μ?? ′ó????DD?D??
unsigned char he;
unsigned char hui,i;
flag_exit_game=0;?
flag_game_over=0;
tcs_key_state=0;
tcs_df=0;
tcs_stop=1;
draw_lcd_picture(&tcs_picture[0]); //ì°3?é?ó??·?-??
delay_ms(2000);
clear_lcd();//???á12864
tcs_bianjie(tcs_dt); //ì°3?é?ó??·±???
tcs_game_initial();//ì°3?é?3?ê??ˉ? ?÷òaê?éè?¨é?í·é??2? 2¢??ê?é?
timer0_initial(); //?¨ê±?÷01¤×÷?ú·?ê?2? 8??×??ˉ×°3??μ? ????êy2??D?? ó?à′2úéú???úêy
timer1_initial(); //?¨ê±?÷11¤×÷?ú·?ê?1 16??? ? ?10ms?D??ò?′?é¨?è?ü?ì
do
{
food_produce();//2úéúò??????úêy ?′???′2úéúx y value3???μè·?¨ò???μ??aê3??
he=test_point_exist(food.x,food.y,food.value);//?ì2a2úéúμ?μ?μ?μ?·?ê?·?ò??-óDμ?′??ú
}
while(he==food.value);//è?1????úêy2úéú?úé?μ?éíì?ò??°±???é? ?ò??D??ù2úéúò?????
draw_lcd_point(food.x,food.y,food.value);//??ê?ê3?? ?è?áè??a??μ??ù?ú8??μ??é?? ò??a???μ??3?
queue_initial(&queue);//3?ê??ˉ?-?·?óáD? ê1?óáD??? front rear ?′í·?2?????a0
check.x=0;
check.y=0;
check.value=0;
check.direction=0;
for(i=0;i<num;i++)
queue.dat[i]=check;
while(1)
{
if(flag_exit_game==1)//è?1?ó??·1y3ì?D°′??·μ???ü? ?òá¢?′?áê?ó??·? ·μ??2?μ¥
break;
if(flag_game_over==1)
break;
ET1=1;? ?//?êDíé¨?è°′?ü ? ì°3?é?ó??·???è???¨é?2′?ò??ˉ????μ?????ê±??? ò2?íê??ì2a°′?üμ?ê±??
tcs_key_flag=0; //?êDí2???°′?ü? ?ú?ì2a°′?üê±???ú? ???êDíéú2úò???1?μ?? ?′2???μ?ò???óDD§μ?
//°′?üò?oó? ?í??ò??a??±?á? 2??êDí?ù???ó1?μ?
while(tcs_stop==0);
switch(tcs_nd)
{
case 0:? {delay_ms(10); break;}
case 1:? {delay_ms(50); break;}
case 2:? {delay_ms(100); break;}
}
ET1=0;?
switch(tou.direction)//?a??switchó?à′?ù?Yé?í·μ?x y value?μò??°μ±?°é?í·ò??ˉμ?·??ò?
//è?3?é?í·òaμ?′?μ???ò???μ?? ′?è?check?D? à′?D??ê?·?×2?? 3?μ?ê3?? ê2?′μ??¤?¤?¤
{
case 0: {
check.x=tou.x;?
if(tou.y==63)??
check.y=0;
else
check.y=tou.y+1;
check.value=tou.value;
break ;
}//->?òóòy+1 ????2?±?
case 1: { //?y ?ò??y2?±?
check.y=tou.y;?
if(tou.value==0x80)// value==0x80 ?ò x+1 value=0x01
{
if(tou.x==7)
check.x=0;
else
check.x=tou.x+1;
check.value=0x01;
}
else
{
check.x=tou.x;
check.value=tou.value<<1;
}
? ? break ;
}
case 2: {
check.x=tou.x;?
if(tou.y==0)??
check.y=63;
else
check.y=tou.y-1;
check.value=tou.value;?
break ;
}//<-?ò×óy-1 ????2?±?
case 3: { ? ?//?ü ?òé?y2?±?
check.y=tou.y;?
if(tou.value==0x01)// value==0x01 ?ò x-1 value=0x80
{
if(tou.x==0)
check.x=7;
else
check.x=tou.x-1;
check.value=0x80;
}
else
{
check.x=tou.x;
check.value=tou.value>>1;
}
? ? break ;
}
}
he=test_point_exist(check.x,check.y,check.value); // ?ì2aé?í·ò??ˉμ???ò???μ?ê?·????-óDμ?′??ú
if(he==check.value) //è?1??°??μ?μ?ò??-′??ú ?ò?éò?ê?ê3?? ?ò??ê???oíé?éí
{
if( (check.x==food.x) && (check.y==food.y) && (check.value==food.value) )//3?μ?ê3??
{
tcs_df++;
cs1=1; cs2=0;
write_lcd_shuzi(2,32,tcs_shuzi[tcs_df/100]? ? ? ?); //??ê?·?êyμ?°ù??
write_lcd_shuzi(2,40,tcs_shuzi[ (tcs_df%100)/10] ); // ê?
write_lcd_shuzi(2,48,tcs_shuzi[tcs_df%10]? ? ? ? ); //? ? ? ? ? ??
cs1=0; cs2=1;
food.direction=tou.direction;//ê3??±???óD·??ò μ?ê??aá?????μ?ó???
tou=food; //ê3???í3éá?é?í·?¤?¤?¤é?í·μ?·??òòàè?2?±?
hui=wei.direction; //?è???????2°í±?à′μ?ò??ˉ·??ò??
wei.direction=611;//?a′?DD?ˉ?2°í2??ˉ? ?a?ùé?éí?í×?è??óò?á??¤?¤?¤611′ú±í????μ??2°í2??ˉ
do
{
food_produce();
he=test_point_exist(food.x,food.y,food.value);
}
while(he==food.value);??
draw_lcd_point(food.x,food.y,food.value);//é???óD?μ1y? ?a??ê?2úéú2¢??ê?ê3??
}
else//×2μ?éíì??ò????
{
flag_game_over=1; // ó??·?áê?
}
}
else? //ê2?′???′óDó?μ? ?ìD??°??
{
check.direction=tou.direction;//?a?′×?μ?àíóéí?é? ?aá?±£3?é?í·μ±?°μ?ò??ˉ·??ò
tou=check;
draw_lcd_point(tou.x,tou.y,tou.value);//?-3?′?ê±μ?é?í·
}
//????ê????2°íμ?2ù×÷
if (? (wei.x == queue.dat[queue.front].x) && ( wei.y==queue.dat[queue.front].y ) && ( wei.value== queue.dat[queue.front].value)? ) //è?1??2°íμ?′?1?μ?
{
check=queue_out(&queue);
wei.direction=check.direction; //?2°í°′??1?μ???ê?μ?·??ò×?
}
else //è?1???óDμ?′?1?μ? ?2°íò??ˉμ?·??ò2?±?
{
}
switch( wei.direction ) //?a??ê??2°íμ?ò??ˉ? ò??°?2°í?úμ?êy?Yμ?DT??
{
case 0: { wei_you(); break; }
case 1: { wei_xia(); break; }
case 2: { wei_zuo(); break; }
case 3: { wei_shang(); break; }
case 611: { wei.direction=hui;? break;}? //?a′?ò??ˉ 3?μ?êμ??? ??1?-?2°í±?à′μ?ò??ˉ·??ò
? }
}
TR0=0;
TR1=0;
ET1=0;? //í?3?ó??·ê± 1?±?ó??·1y3ì?Dó?μ?μ??¨ê±?÷0oí1? è?oó?¨ê±?÷?D??ò2òa1?±?
}
void timer0_initial()
{
TMOD=(0xf0 & TMOD) | 0x01; //?¨ê±?÷0 8??×??ˉ×°3??μ(TH0->TL0)?¨ê±?÷?£ê?? ?ó?à′2úéú???úêy
TH0=0x00;
TL0=0x00;
TR0=1;? ?
}
void timer1_initial()
{
EA=1;
TMOD= (0x0f & TMOD) | 0x10 ; //?¨ê±?÷ò?16???¨ê±?÷?£ê? 10ms?D??ò?′?? ó?à′é¨?è?ü?ì
TH1=0xDC;//10ms?D??ò?′?
TL1=0x00;
TR1=1;? ?
}
void timer1() interrupt 3
{
unsigned char tou_last_direction; //????é?í·?-à′μ?ò??ˉ·??ò
TR1=0;
TH1=0xDC;
TL1=0x00;
switch (tcs_key_state)
{
case 0: { // °′?ü3?ê?ì?
if ( (P2 & 0x7f) != 0x7f )? ?tcs_key_state = 1; // ?ü±?°′??£?×′ì?×a??μ??üè·è?ì?
else ? tcs_key_state = 0;
break;
}
case 1: { // °′?üè·è?ì?
if ( (P2 & 0x7f) != 0x7f)
{
// °′?üè?°′??£?′?ê±è·?¨????ò?′?óDD§°′?ü
tcs_key_state = 2; // ×′ì?×a??μ??üêí·?ì?
if(tcs_key_flag==0)
{
tou_last_direction=tou.direction;?
switch(P2 & 0x7f)
{
case 0x6f: break; // è·?¨?ü? 2???DDè?o?2ù×÷
case 0x5f: {? flag_exit_game=1;? ?break; }//·μ???ü? ?áê?ó??· ·μ??2?μ¥
case 0x7e: {? tou.direction=3;break; }//?ü
case 0x7b: {? tou.direction=2;break; }//??
case 0x7d: {? tou.direction=1;break; }//?y
case 0x77: {? tou.direction=0;break; }//-> ?
case 0x3f:? {? if(tcs_stop==0) tcs_stop=1; else tcs_stop=0; break;}//?Yí£?ü
}
if( (tou.direction==tou_last_direction) || (tou.direction+tou_last_direction==2) || (tou.direction+tou_last_direction==4) )
//è?1?±?à′?íê?°′′?·??òò??ˉ? ??ò??°′?ü??μ?·??òó?é?ò??ˉ·??ò?à·′ ?ò2?×?è?o?2ù×÷ ′?′?°′?ü?TD§
{
tou.direction=tou_last_direction; //? ??1?-é?í·μ?ò??ˉ·??ò
}
else //·??ò????′?í·?áμ?3é?a1?μ?? ? ? ? ? ?
{
queue_in(&queue,tou);
tcs_key_flag=1; //ò??-2???μ?ò?′?°′?üD??¢ ?a′?ò??ˉ1?μ?ò??-2úéú 1?±?2????ü?ì?
}
}
}
else
{
tcs_key_state = 0; // °′?üò?ì§?e£?×a??μ?°′?ü3?ê?ì?
break;
}
}
case 2: {? ? ? ? ? ?//μè′y°′?üμˉ?e×′ì?
if ( (P2 & 0x7f) == 0x7f) tcs_key_state=0; //°′?üò?êí·?£?×a??μ?°′?ü3?ê?ì?
else ? tcs_key_state = 2;
break;
}
}
TR1=1;
}
void food_produce()
{
unsigned char j1,j2;
unsigned char code a[]={0x01,0x02,0x04,0x08, 0x10,0x20,0x40,0x80,0x01,0x02,0x04,0x08, 0x10,0x20,0x40,0x80};
unsigned char code b[]={0,1,2,3,4,5,6,7,0,1,2,3,4,5,6,7};
? ?
j1 = TL0 & 0x0f;? //è?TL0μ?μí????
food.value = a[j1];? //ó?0-15μ?êyè·?¨ê3??μ?value?μ
j2 = (TL0 & 0xf0)>>4; //è?TL0μ???4??
food.x=b[j2]; //í?àíè·?¨ê3??μ?x?μ
j2=j1+j2; // 0-30? ? ? ? ? ? ?
food.y=j2 +( TL0 & 0x0f ) + ( (TL0 & 0xf0)>>4 ) + 1;//3′?0-15μ??μoíò?′?1-16μ??μ?à?ó£?2úéú1-61μ?êy
}
unsigned char test_point_exist(unsigned char x, unsigned char y, unsigned char value)
{
unsigned char he;
set_lcd_xy(x,y);
he=read_lcd_dat();
he=read_lcd_dat();
he=he&value; //è?1?òo?§é??a??μ?ê?ááμ? ?òhe?avalue è?2?áá?ò?a 0
return (he);
}
void tcs_game_initial()
{
unsigned char i;
cs1=0; cs2=1;
tou.x=0;
tou.y=5;
tou.value=0x08;? ? ? //3?ê??ˉé?í·
tou.direction=0;? ? // 3?ê??ˉé?í·ò??ˉ·??ò?a?òóò?
wei.x=0;
wei.y=1;
wei.value=0x08; //3?ê??ˉé??2
wei.direction=0;
for(i=wei.y;i<=tou.y;i++)
draw_lcd_point(tou.x,i,tou.value);//D?3éé?
}
void tcs_bianjie(unsigned int he)
{
unsigned char i;
cs1=0; cs2=1;
set_lcd_xy(0,0);
for(i=0;i<=63;i++)
write_lcd_dat(0x01);?
set_lcd_xy(7,0);
for(i=0;i<=63;i++)
write_lcd_dat(0x80);?
for(i=0;i<=7;i++)
{
set_lcd_xy(i,0);
write_lcd_dat(0xff);
set_lcd_xy(i,63);
write_lcd_dat(0xff);
}
if(he==2)
{
unsigned char i,j;
for(i=0;i<8;i++)
{?
cs1=0;cs2=1;
set_lcd_xy(i,0);
for(j=0;j<64;j++)?
write_lcd_dat(tcs_dt2[i*64+j]);
}
}
if(he==3)
{
unsigned char i,j;
for(i=0;i<8;i++)
{?
cs1=0;cs2=1;
set_lcd_xy(i,0);
for(j=0;j<64;j++)?
write_lcd_dat(tcs_dt3[i*64+j]);
}
}
if(he==4)
{
unsigned char i,j;
for(i=0;i<8;i++)
{?
cs1=0;cs2=1;
set_lcd_xy(i,0);
for(j=0;j<64;j++)?
write_lcd_dat(tcs_dt4[i*64+j]);
}
}
cs1=1; cs2=0;
write_lcd_hanzi(0,0,df[0]);
write_lcd_hanzi(0,16,df[1]);
write_lcd_shuzi(2,32,tcs_shuzi[tcs_df/100]? ? ? ?); //??ê?·?êyμ?°ù??
write_lcd_shuzi(2,40,tcs_shuzi[ (tcs_df%100)/10] ); // ê?
write_lcd_shuzi(2,48,tcs_shuzi[tcs_df%10]? ? ? ? ); //? ? ? ? ? ??
write_lcd_hanzi(4,0,jb[0]);
write_lcd_hanzi(4,16,jb[1]);
switch(tcs_nd)
{?
case 0 :? { write_lcd_hanzi(6,32,tcs_bt[0]); write_lcd_hanzi(6,48,tcs_bt[1]);? break;}
case 1 :? { write_lcd_hanzi(6,32,tcs_kn[0]); write_lcd_hanzi(6,48,tcs_kn[1]);? break;}
case 2 :? { write_lcd_hanzi(6,32,tcs_jd[0]); write_lcd_hanzi(6,48,tcs_jd[1]);? break;}
}
}
void queue_initial(struct loop_queue *q)? ?
{? ?
? ? q->front =q->rear=0;? ?
? ? q->count = 0;? ?
}? ?
void queue_in(struct loop_queue *q,struct she h)??
{? ?
? ? ? ? q->dat[q->rear] = h;? ?
? ? ? ? q->rear = (q->rear + 1) % num;? ?
? ? ? ? q->count = q->count + 1;? ?
}? ?
??
struct she queue_out(struct loop_queue *q)? ?
{? ?
? ? ? ? struct she he;
he = q->dat[q->front];? ?
? ? ? ? q->front = (q->front + 1) % num;? ?
? ? ? ? q->count = q->count - 1;
return (he);? ?
}??
void wei_you()
{
unsigned char he;
set_lcd_xy(wei.x,wei.y);
he=read_lcd_dat();
he=read_lcd_dat();?
? ?
set_lcd_xy(wei.x,wei.y);
write_lcd_dat(he-wei.value);? //è?μ±?°????μ??μ£?è¥μ??2°í?a??μ?£?è?oó??ê?
if(wei.y==63)
wei.y=0;
else
wei.y++;
}
void wei_zuo()
{
unsigned char he;
set_lcd_xy(wei.x,wei.y);
he=read_lcd_dat();
he=read_lcd_dat();?
? ?
set_lcd_xy(wei.x,wei.y);
write_lcd_dat(he-wei.value); //è?μ±?°????μ??μ£?è¥μ??2°í?a??μ?£?è?oó??ê?
if(wei.y==0)
wei.y=63;
else
wei.y--;
}
void wei_shang()
{
? unsigned int he;
set_lcd_xy(wei.x,wei.y);
he=read_lcd_dat();
he=read_lcd_dat();
set_lcd_xy(wei.x,wei.y);
write_lcd_dat(he-wei.value);
if(wei.value==0x01)
{
if(wei.x==0)
wei.x=7;
else
wei.x--;?
wei.value=0x80;
}
else?
{
wei.value=wei.value>>1;
}
}
void wei_xia()
{
unsigned int he;
set_lcd_xy(wei.x,wei.y);
he=read_lcd_dat();
he=read_lcd_dat();
set_lcd_xy(wei.x,wei.y);
write_lcd_dat(he-wei.value);
if(wei.value==0x80)
{
if(wei.x==7)
wei.x=0;
else
wei.x++;?
wei.value=0x01;
}
else?
{
wei.value=wei.value<<1;
}
}