51单片机在12864上玩起来贪吃蛇.docx
《51单片机在12864上玩起来贪吃蛇.docx》由会员分享,可在线阅读,更多相关《51单片机在12864上玩起来贪吃蛇.docx(46页珍藏版)》请在冰点文库上搜索。
51单片机在12864上玩起来贪吃蛇
/*用你身边的51单片机玩起贪吃蛇,哈哈仅仅是玩玩就好。
。
大家注意管脚可能不同请根据实际情况改写。
。
。
*/
//主程序
#include
#include
#defineup0x21
#definedown0x24
#defineleft0x12
#defineright0x42
#definequit0x81
unsignedcharbdatasnake_flag;
ucharxdatasnake[256][2];
ucharidatasnake_len;
ucharidatasnake_food[2];
sbitgameover=snake_flag^3;
sbitfood=snake_flag^2;
uintmarker;
ucharno_counter;
voidtimer0_init(void);
voidsnake_init(void)
{
marker=0;
no_counter=0;
snake_len=3;
fill_point(31,30);
fill_point(32,30);
fill_point(33,30);
snake[0][0]=33;
snake[1][0]=32;
snake[2][0]=31;
snake[0][1]=30;
snake[1][1]=30;
snake[2][1]=30;
snake_flag=0x10;
line_horizon(0,63,0);
line_horizon(0,63,63);
line_vertical(0,0,127);
line_vertical(63,0,127);
timer0_init();
xprintf(64,0,"分数");
printf_number(64,2,0);
xprintf(64,4,"按退出键");
xprintf(64,6,"退出");
}
voidsnake_refresh()
{
uchark;
for(k=snake_len;k>0;k--)
{
snake[k][0]=snake[k-1][0];
snake[k][1]=snake[k-1][1];
}
}
voidsnake_run(void)
{
uchari;
snake_refresh();
switch(snake_flag>>4)
{
case(0x08):
if(snake[0][1]==1)gameover=1;
elsesnake[0][1]--;
break;
case(0x04):
if(snake[0][1]==62)gameover=1;
elsesnake[0][1]++;
break;
case(0x02):
if(snake[0][0]==1)gameover=1;
elsesnake[0][0]--;
break;
case(0x01):
if(snake[0][0]==62)gameover=1;
elsesnake[0][0]++;
break;
default:
break;
}
for(i=(snake_len);i>0;i--)
{
if((snake[0][0]==snake[i][0])&&(snake[0][1]==snake[i][1]))
{
gameover=1;
break;
}
}
if(!
gameover)
{
if((snake[0][0]==snake_food[0])&&(snake[0][1]==snake_food[1]))
{
food=0;
snake_len++;
no_counter++;
marker=no_counter*100;
printf_number(64,2,marker);
}
else
{
clear_point(snake[snake_len][0],snake[snake_len][1]);
fill_point(snake[0][0],snake[0][1]);
}
}
if(no_counter>9)
gameover=1;
}
uchargetkey(void)
{
ucharcol_code,row_code;
P1=0xf0;
if(P1!
=0xf0)
{
delayms(5);
if(P1!
=0xf0)
{
row_code=0xfe;
while(row_code&0x10)
{
P1=row_code;
if(P1!
=row_code)
{
col_code=(P1&0xf0)|0x0f;
return((~row_code)|(~col_code));
}
elserow_code=(row_code<<1)|0x01;
}
}
}
return(0);
}
bitcompare(ucharx,uchary)
{
ucharcounter;
for(counter=0;counter{
if((snake[counter][0]==x)&&(snake[counter][1]==y))return(0);
}
return
(1);
}
voidset_food(void)
{
uchari,j;
bitcompare_flag;
i=TL0&0x3f;
j=TH0&0x3f;
if(i==0)i++;
if(i==0x4f)i--;
if(j==0x4f)j--;
if(j==0)j++;
do{
compare_flag=compare(i,j);
if(!
compare_flag)
{
i++;
j++;
if(i>63)i=1;
if(j>63)j=1;
}
}
while(!
compare_flag);
fill_point(i,j);
snake_food[0]=i;
snake_food[1]=j;
food=1;
}
voidtimer0(void)interrupt1using3
{
TH0=0x00;
TL0=0x00;
TR0=1;
}
voidtimer0_init(void)
{
TMOD=0x01;
TH0=0x00;
TL0=0x00;
TR0=1;
ET0=1;
EA=1;
}
voiddelay()
{
unsignedinti;
for(i=0;i<15000;i++);
}
voidlongdelay(uchari)
{
ucharx;
for(x=0;x
delay();
}
voidsnake_game(void)
{
ucharkey_code;
cls();
xprintf(24,0,"贪吃蛇游戏");
xprintf(50,2,"ver0.1");
xprintf(50,4,"by无业游民");
xprintf(16,6,"按任意键开始");
while(getkey()==0);
cls();
snake_init();
do{
if(!
food)set_food();
key_code=getkey();
switch(key_code)
{
case0:
break;
caseup:
if((snake_flag&0xf0)==0x40)
break;
snake_flag=(snake_flag&0x0f)|0x80;
break;
casedown:
if((snake_flag&0xf0)==0x80)
break;
snake_flag=(snake_flag&0x0f)|0x40;
break;
caseleft:
if((snake_flag&0xf0)==0x10)
break;
snake_flag=(snake_flag&0x0f)|0x20;
break;
caseright:
if((snake_flag&0xf0)==0x20)
break;
snake_flag=(snake_flag&0x0f)|0x10;
break;
casequit:
cls();
return;
default:
break;
}
snake_run();
delay();
if(gameover&&(no_counter!
=10))
{
cls();
xprintf(24,3,"GAMEOVER");
xprintf(0,5,"按任意键重新开始");
while(getkey()==0);
return;
}
if(gameover&&(no_counter==10))
{
cls();
xprintf(4,3,"Congratulation!
");
xprintf(0,5,"按任意键重新开始");
while(getkey()==0);
return;
}
}
while
(1);
}
voidmain(void)
{
lcm_init();
draw_graph(graph1);
longdelay(10);
while
(1)
snake_game();
}
其他的
graphcode.h
unsignedcharcodegraph1[]=
{
0x01,0x01,0x02,0x02,0x04,0x04,0x08,0x08,0xd0,0x30,0x30,0x30,0x50,0x60,0xdc,0xb2,0x3a,0xdc,0x20,0xa0,0xa0,0xc0,0x0e,0x30,0x7e,0x00,0x40,0x80,0x00,0x00,0x00,0x00,0x60,0x80,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x40,0xa0,0x90,0x88,0xd4,0xe4,0x70,0x72,0xbc,0x78,0x38,0x38,0x3e,0x7c,0xf8,0xf8,0xf8,0xf0,0xf0,0xf0,0xe0,0xc0,0xc0,0x40,0x20,0x00,0x00,0x00,0x00,0xfe,0xfa,0xfe,0xfe,0xfe,0xfe,0xfe,0xfe,0xfe,0xfc,0xfe,0xfe,0x1e,0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x80,0x40,0x40,0x20,0x20,0x10,0x10,0x08,0x08,0x04,0x04,0x02,0x02,0x01,
0x00,0x00,0x80,0x4e,0x31,0x21,0x21,0x21,0x27,0xf8,0x90,0xd4,0xf2,0xe8,0x47,0x41,0x41,0xd1,0x23,0x1f,0x85,0x44,0x28,0x98,0x14,0x93,0x3c,0xb0,0xf1,0x7e,0xce,0xf7,0x63,0xc3,0xc3,0x27,0x3f,0xfe,0xfe,0xfc,0xf0,0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0e,0x07,0xf1,0xfc,0xfe,0xff,0xff,0xfc,0xf8,0x98,0x98,0xe4,0xe7,0xff,0xdc,0xfc,0xcf,0xcf,0xff,0xbf,0xff,0xef,0x1e,0xf8,0x10,0x00,0x40,0x3f,0x8f,0x43,0x71,0x49,0x49,0x89,0x01,0x03,0x0f,0xf3,0x00,0x00,0x88,0x80,0xea,0x40,0x20,0x20,0x10,0x10,0x08,0x08,0x04,0x04,0x02,0x02,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0xc0,0xc0,0xe3,0xd4,0x48,0x88,0x0a,0x05,0x3e,0xc1,0x45,0xd1,0xe1,0xff,0xf8,0xf0,0xf0,0xf8,0x77,0xd1,0x30,0x90,0x00,0xaa,0x00,0x88,0x00,0x21,0x8e,0x70,0x1f,0xfe,0xff,0xff,0xff,0x3f,0x3f,0x3f,0x7f,0x9f,0xff,0x3f,0x20,0x20,0xc0,0xc0,0x80,0x80,0x80,0x80,0x80,0x40,0x40,0x40,0x40,0x40,0x40,0xc0,0x00,0x03,0x0f,0x3f,0x7f,0x7f,0xff,0xbf,0x3f,0x1f,0x0f,0x0f,0x0f,0x8f,0x8f,0xcf,0xe3,0xf7,0x79,0x1f,0x07,0x00,0x00,0x80,0x80,0x60,0xc0,0xa7,0x24,0x11,0x10,0x8c,0x8b,0x44,0x34,0x8e,0x03,0xa9,0x01,0x88,0x00,0xaa,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0xe2,0xd5,0xf7,0xfb,0x6f,0x7a,0x77,0x7c,0x7a,0xff,0x72,0x75,0x77,0x77,0x77,0x65,0xe3,0xea,0xc1,0xca,0x00,0x88,0x00,0xaa,0x00,0x88,0x00,0xa0,0x0f,0xb0,0x40,0x82,0x11,0x2b,0x4a,0x4a,0x72,0x0a,0x13,0x01,0x00,0x00,0x00,0x00,0x01,0x06,0x1e,0xfe,0xff,0xff,0xfe,0xff,0x7f,0x7f,0x7f,0x3f,0x3f,0xff,0x20,0x20,0x40,0x40,0x81,0x81,0x03,0x83,0x9b,0x63,0x63,0x23,0x23,0x33,0x35,0x1d,0x0c,0x84,0x84,0x42,0x22,0x71,0x29,0x46,0x81,0xca,0x40,0x28,0x21,0x39,0x45,0x82,0x82,0x82,0x44,0x78,0x40,0x4a,0x40,0x88,0x00,0xca,0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x2f,0x16,0xfd,0x6d,0xfd,0xfa,0xea,0x62,0x63,0x6a,0xfd,0xfa,0x66,0x62,0x9a,0x9d,0x65,0x65,0x1a,0x2d,0x00,0x88,0x00,0x2a,0x00,0x88,0x00,0x22,0x00,0x88,0x00,0x2a,0x01,0x8a,0x02,0x24,0x04,0x88,0x08,0x10,0x10,0x10,0x20,0xa0,0xa0,0x60,0x40,0x20,0x21,0xa3,0xa5,0x64,0x64,0x24,0x64,0x54,0x52,0xc9,0xa8,0xa4,0x64,0x22,0x22,0x31,0x11,0x11,0x12,0x1a,0x1c,0x1c,0x1c,0x1c,0x12,0x32,0x21,0x60,0x40,0x40,0x80,0x80,0x80,0x80,0x00,0x03,0x04,0x04,0x3c,0xc4,0x82,0x83,0x83,0x47,0xf8,0x08,0x08,0x08,0x18,0x27,0xc0,0x29,0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x08,0x08,0x0f,0x37,0x6f,0x7f,0x7f,0x5f,0x40,0x57,0x7b,0x7d,0x7e,0x7e,0x7f,0x7f,0x3e,0x0e,0x08,0x0a,0x00,0x08,0x08,0x08,0x08,0x08,0x08,0x0a,0x08,0x08,0x08,0x08,0x08,0x88,0x88,0x4a,0x48,0x2c,0x2c,0x1a,0x1a,0x09,0x0d,0x0c,0x14,0x12,0x22,0x21,0x21,0x40,0x40,0x40,0x40,0x40,0x40,0x20,0x20,0x1e,0x21,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x20,0x20,0x20,0x10,0x08,0x0c,0x08,0x08,0x08,0x08,0x08,0x09,0x09,0x0b,0x0b,0x0d,0x0e,0x0a,0x0d,0x18,0x18,0x28,0x28,0x49,0x4e,0x88,0x88,0x08,0x08,0x08,0x09,0x0a,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x80,0x40,0x40,0x20,0x20,0x10,0x10,0x08,0x08,0x04,0x04,0x02,0x02,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x02,0x02,0x04,0x04,0x08,0x08,0x10,0x10,0x20,0x20,0x40,0x40,0x80,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x80,0x80,0x40,0x40,0x20,0x20,0x10,0x10,0x08,0x08,0x04,0x04,0x02,0x02,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x02,0x02,0x04,0x04,0x08,0x08,0x10,0x10,0x20,0x20,0x40,0x40,0x80,0x80};
lcddriver.h
//用心看看
#include
#include
#include
#defineucharunsignedchar
#defineuintunsignedint
#definewrcmdl_addXBYTE[0x80