51单片机的贪吃蛇代码(直接代码)
#includ

e <12864.h>
#include <tcs_resource.h>
#define num 15
//ÓÃÀ´ÃèÊöÉßÔÚÒº¾§ÉÏÃæÒ»¸öµãµÄ½á¹¹Ìå
struct she
{
unsigned char x,y,value;
unsigned char direction;
};

struct loop_queue //ÀûÓÃÑ»·Êý×éʵÏÖÑ»·¶ÓÁУ¬¹Õµã²»»á³¬¹ý15¸ö¡£ËùÒÔ´óСѡ15×ãÒÔ¡£
{
unsigned int front;
unsigned int rear;
unsigned int count;
struct she dat[num];
};
//²»ÊìϤÊý×éʵÏÖÑ»·¶ÓÁеĿÉÒÔÔÚÖ½ÉϻһϲÙ×÷¹ý³Ì
struct she tou,wei,food; //ÉßµÄͷβ½Úµã ºÍʳÎïµã

struct loop_queue queue;//Ñ»·¶ÓÁÐ ÉßתÍäÔòÐγɹյ㠽«¹ÕµãÈë¶ÓÁРβ°Íµ½´ï¹ÕµãÔò¹Õµã³ö¶ÓÁÐ
unsigned char flag_exit_game; //·µ»Ø±êÖ¾ Ϊ1ʱ ´Ó̰³ÔÉßÓÎÏ··µ»Ø²Ëµ¥
unsigned char flag_game_over; //½áÊø±êÖ¾ Ϊ1ʱ ÏÔʾÓÎÏ·½áÊø»Ãæ ͬʱʹ·µ»Ø±ê־Ϊ1
unsigned char tcs_stop;//ÔÝÍ£±êÖ¾
unsigned char tcs_nd; //ÄѶȱêÖ¾
unsigned char tcs_dt; //µØÍ¼±êÖ¾

unsigned char tcs_key_flag; //̰³ÔÉßÔÚÒ»´ÎÒÆ¶¯ÖÐ Ö»ÔÊÐí²¶»ñÒ»´Î°´¼ü Ò²¾ÍÊÇÖ»ÄܲúÉúÒ»¸ö¹Õµã
unsigned char tcs_key_state; //ÓÃÓÚ°´¼ü״̬»úµÄʵÏÖ
unsigned int tcs_df;//µÃ·Ö
unsigned int tcs_highest_df;//×î¸ßµÃ·Ö¡¤¡¤¡¤Ã»ÓÐʹÓÃÕâ¸ö ÒòΪ²»ÖªµÀÒªÔõôÔÚÔËÐÐʱдµ¥Æ¬»úµÄROM

void tcs_game();
void tcs_bianjie(unsigned int ); //̰³ÔÉ߽߱ç
void tcs_game_initial(); //ÓÎÏ·³õʼ»¯
void timer0_initial(); //¶¨Ê±Æ÷0³õʼ»¯
void timer1_initial();
void food_produce(); //²úÉúʳÎï

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; //ÓÃÓëÔÝ´æÉß¼´½«Òƶ¯µÄµã ´Ó¶ø½øÐÐÅжÏ
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]); //̰³ÔÉßÓÎÏ·»Ãæ
delay_ms(2000);
clear_lcd();//ÇåÆÁ12864
tcs_bianjie(tcs_dt); //̰³ÔÉßÓÎÏ·±ß½ç
tcs_game_initial();//̰³ÔÉß³õʼ»¯ Ö÷ÒªÊÇÉ趨ÉßÍ·Éßβ ²¢ÏÔʾÉß
timer0_initial(); //¶¨Ê±Æ÷0¹¤×÷ÔÚ·½Ê½2 8λ×Ô¶¯×°³õÖµ Ö»¼ÆÊý²»ÖÐ¶Ï ÓÃÀ´²úÉúËæ»úÊý
timer1_initial(); //¶¨Ê±Æ÷1¹¤×÷ÔÚ·½Ê½1 16λ 10msÖжÏÒ»´ÎɨÃè¼üÅÌ
do
{
food_produce();//²úÉúÒ»¸öËæ»úÊý ¼´Ëæ¼´²úÉúx y value3¸öֵȷ¶¨Ò»¸öµãΪʳÎï
he=test_point_exist(food.x,food.y,food.value);//¼ì²â²úÉúµÄµãµÄµØ·½ÊÇ·ñÒѾÓÐµã´æÔÚ
}
while(he==food.value);//Èç¹ûËæ»úÊý²úÉúÔÚÉßµÄÉíÌåÒÔ¼°±ß½çÉÏ ÔòÖØÐÂÔÙ²úÉúÒ»¸ö
draw_lcd_point(food.x,food.y,food.value);//ÏÔʾʳÎï ÏȶÁÈ¡Õâ¸öµãËùÔÚ8λµÄÇé¿ö ÒÔÃâÆÆ»µÏÖ³¡
queue_initial(&queue);//³õʼ»¯Ñ»·¶ÓÁРʹ¶ÓÁÐ¿Õ front rear ¼´Í·Î²Ö¸ÕëΪ0
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)//Èç¹ûÓÎÏ·¹ý³ÌÖа´Ï·µ»Ø¼ü ÔòÁ¢¼´½áÊøÓÎÏ· ·µ»Ø²Ëµ¥
break;
if(flag_game_over==1)
break;
ET1=1; //ÔÊÐíɨÃè°´¼ü ̰³ÔÉßÓÎÏ·ÄѶȾö¶¨Éß2´ÎÒÆ¶¯Ö®¼äµÄ¼ä¸ôʱ¼ä Ò²¾ÍÊǼì²â°´¼üµÄʱ¼ä
tcs_key_flag=0; //ÔÊÐí²¶»ñ°´¼ü ÔÚ¼ì²â°´¼üʱ¼äÄÚ Ö»ÔÊÐíÉú²úÒ»¸ö¹Õµã ¼´²¶»ñµ½Ò»¸öÓÐЧµÄ
//°´¼üÒÔºó ¾ÍÖÃÒ»Õâ¸ö±äÁ¿ ²»ÔÊÐíÔÙÔö¼Ó¹Õµã
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)//Õâ¸öswitchÓÃÀ´¸ù¾ÝÉßÍ·µÄx y valueÖµÒÔ¼°µ±Ç°ÉßÍ·ÒÆ¶¯µÄ·½Ïò
//È¡³öÉßÍ·Òªµ½´ïµÄÏÂÒ»¸öµã ´æÈëcheckÖÐ À´ÅжÏÊÇ·ñײǽ ³Ôµ½Ê³Îï ʲôµÄ¡¤¡¤¡¤
{
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 ÆäËû²»±ä
case 1: { //¡ý ÏòÏÂy²»±ä
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 ÆäËû²»±ä
case 3: { //¡ü ÏòÉÏy²»±ä
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); // ¼ì²âÉßÍ·ÒÆ¶¯µÄÏÂÒ»¸öµãÊÇ·ñ¼¸¾ÓÐµã´æÔÚ
if(he==check.value) //Èç¹ûÇ°ÃæµÄµãÒѾ´æÔÚ Ôò¿ÉÒÔÊÇʳÎï »òÕßÊÇǽºÍÉßÉí
{
if( (check.x==food.x) && (check.y==food.y) && (check.value==food.value) )//³Ôµ½Ê³Îï
{
tcs_df++;
cs1=1; cs2=0;
write_lcd_shuzi(2,32,tcs_shuzi[tcs_df/100] ); //ÏÔʾ·ÖÊýµÄ°Ùλ
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;//ʳÎﱾûÓз½Ïò µ«ÊÇΪÁËÏÂÃæµÄÓï¾ä
tou=food; //ʳÎï¾Í³ÉÁËÉßÍ·¡¤¡¤¡¤ÉßÍ·µÄ·½ÏòÒÀÈ»²»±ä
hui=wei.direction; //ÏȼǼÏÂβ°Í±¾À´µÄÒÆ¶¯·½Ïò
wei.direction=611;//Õâ´ÎÐж¯Î²°Í²»¶¯ ÕâÑùÉßÉí¾Í×ÔÈ»¼ÓÒ»ÁË¡¤¡¤¡¤611´ú±íÏÂÃæµÄβ°Í²»¶¯
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);//ÉÏÃæÓÐ˵¹ý Õâ¸öÊDzúÉú²¢ÏÔʾʳÎï
}
else//ײµ½ÉíÌå»òÕßǽ
{
flag_game_over=1; // ÓÎÏ·½áÊø
}
}
else //ʲô¶¼Ã´ÓÐÓöµ½ ¼ÌÐøÇ°½ø
{
check.direction=tou.direction;//Õâô×öµÄÀíÓÉͬÉÏ ÎªÁ˱£³ÖÉßÍ·µ±Ç°µÄÒÆ¶¯·½Ïò
tou=check;
draw_lcd_point(tou.x,tou.y,tou.value);//»³ö´ËʱµÄÉßÍ·
}
//ÏÂÃæÊǶÔβ°ÍµÄ²Ù×÷
if ( (wei.x == queue.dat[queue.front].x) && ( wei.y==queue.dat[queue.front].y ) && ( wei.value== queue.dat[queue.front].value) ) //Èç¹ûβ°Íµ½´ï¹Õµã
{
check=queue_out(&queue);
wei.direction=check.direction; //β°Í°´ÕÕ¹ÕµãָʾµÄ·½Ïò×ß
}
else //Èç¹ûûÓе½´ï¹Õµã β°ÍÒÆ¶¯µÄ·½Ïò²»±ä
{
}
switch( wei.direction ) //Õâ¸öÊÇβ°ÍµÄÒÆ¶¯ ÒÔ¼°Î²°Í½ÚµãÊý¾ÝµÄÐÞ¸Ä
{
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;} //Õâ´ÎÒÆ¶¯ ³Ôµ½ÊµÎï »¹Ôβ°Í±¾À´µÄÒÆ¶¯·½Ïò
}
}
TR0=0;
TR1=0;
ET1=0; //Í˳öÓÎϷʱ ¹Ø±ÕÓÎÏ·¹ý³ÌÖÐÓõ½µÄ¶¨Ê±Æ÷0ºÍ1 È»ºó¶¨Ê±Æ÷ÖжÏÒ²Òª¹Ø±Õ
}
void timer0_initial()
{
TMOD=(0xf0 & TMOD) | 0x01; //¶¨Ê±Æ÷0 8λ×Ô¶¯×°³õÖµ(TH0->TL0)¶¨Ê±Æ÷ģʽ ÓÃÀ´²úÉúËæ»úÊý
TH0=0x00;
TL0=0x00;
TR0=1;
}
void timer1_initial()
{
EA=1;
TMOD= (0x0f & TMOD) | 0x10 ; //¶¨Ê±Æ÷Ò»16λ¶¨Ê±Æ÷ģʽ 10msÖжÏÒ»´Î ÓÃÀ´É¨Ãè¼üÅÌ
TH1=0xDC;//10msÖжÏÒ»´Î
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: { // °´¼ü³õʼ̬
if ( (P2 & 0x7f) != 0x7f ) tcs_key_state = 1; // ¼ü±»°´Ï£¬×´Ì¬×ª»»µ½¼üÈ·ÈÏ̬
else tcs_key_state = 0;
break;
}
case 1: { // °´¼üÈ·ÈÏ̬
if ( (P2 & 0x7f) != 0x7f)
{
// °´¼üÈÔ°´Ï£¬´Ëʱȷ¶¨¼Ç¼һ´ÎÓÐЧ°´¼ü
tcs_key_state = 2; // ״̬ת»»µ½¼üÊÍ·Å̬
if(tcs_key_flag==0)
{
tou_last_direction=tou.direction;
switch(P2 & 0x7f)
{
case 0x6f: break; // È·¶¨¼ü ²»½øÐÐÈκβÙ×÷
case 0x5f: { flag_exit_game=1; break; }//·µ»Ø¼ü ½áÊøÓÎÏ· ·µ»Ø²Ëµ¥
case 0x7e: { tou.direction=3;break; }//¡ü
case 0x7b: { tou.direction=2;break; }//¡û
case 0x7d: { tou.direction=1;break; }//¡ý
case 0x77: { tou.direction=0;break; }//->
case 0x3f: { if(tcs_stop==0) tcs_stop=1; else tcs_stop=0; break;}//ÔÝÍ£¼ü
}
if( (tou.direction==tou_last_direction) || (tou.direction+tou_last_direction==2) || (tou.direction+tou_last_direction==4) )
//Èç¹û±¾À´¾ÍÊǰ´´Ë·½ÏòÒÆ¶¯ »òÕß°´¼ü¸øµÄ·½ÏòÓëÉßÒÆ¶¯·½ÏòÏà·´ Ôò²»×öÈκβÙ×÷ ´Ë´Î°´¼üÎÞЧ
{
tou.direction=tou_last_direction; // »¹ÔÉßÍ·µÄÒÆ¶¯·½Ïò
}
else //·ñÔò¼Ç¼´ËÍ·½áµã³ÉΪ¹Õµã
{
queue_in(&queue,tou);
tcs_key_flag=1; //ÒѾ²¶»ñµ½Ò»´Î°´¼üÐÅÏ¢ Õâ´ÎÒÆ¶¯¹ÕµãÒѾ²úÉú ¹Ø±Õ²¶»ñ¼üÅÌ
}
}
}
else
{
tcs_key_state = 0; // °´¼üÒŅ̃Æð£¬×ª»»µ½°´¼ü³õʼ̬
break;
}
}
case 2: { //µÈ´ý°´¼üµ¯Æð״̬
if ( (P2 & 0x7f) == 0x7f) tcs_key_state=0; //°´¼üÒÑÊÍ·Å£¬×ª»»µ½°´¼ü³õʼ̬
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µÄÊýÈ·¶¨Ê³ÎïµÄvalueÖµ
j2 = (TL0 & 0xf0)>>4; //È¡TL0µÄ¸ß4λ
food.x=b[j2]; //ͬÀíÈ·¶¨Ê³ÎïµÄxÖµ
j2=j1+j2; // 0-30
food.y=j2 +( TL0 & 0x0f ) + ( (TL0 & 0xf0)>>4 ) + 1;//3´Î0-15µÄÖµºÍÒ»´Î1-16µÄÖµÏà¼Ó£¬²úÉú1-61µÄÊý
}
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; //Èç¹ûÒº¾§ÉÏÕâ¸öµãÊÇÁÁµÄ ÔòheΪvalue Èô²»ÁÁÔòΪ 0
return (he);
}
void tcs_game_initial()
{
unsigned char i;
cs1=0; cs2=1;
tou.x=0;
tou.y=5;
tou.value=0x08; //³õʼ»¯ÉßÍ·
tou.direction=0; // ³õʼ»¯ÉßÍ·ÒÆ¶¯·½ÏòΪÏòÓÒ
wei.x=0;
wei.y=1;
wei.value=0x08; //³õʼ»¯Éßβ
wei.direction=0;
for(i=wei.y;i<=tou.y;i++)
draw_lcd_point(tou.x,i,tou.value);//ÐγÉÉß
}
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] ); //ÏÔʾ·ÖÊýµÄ°Ùλ
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); //È¡µ±Ç°Î»ÖõÄÖµ£¬È¥µôβ°ÍÕâ¸öµã£¬È»ºóÏÔʾ
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); //È¡µ±Ç°Î»ÖõÄÖµ£¬È¥µôβ°ÍÕâ¸öµã£¬È»ºóÏÔʾ
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;
}
}