电路图和单片机仿真文件下载:http://www.51hei.com/bbs/dpj-20604-1.html
下面是源代码:
#include
#define uchar unsigned char
#define SNAKE 20 //最大长度
#define TIME 50 //显现延时时刻
#define SPEED 71 //速度操控
//#define keyenable 1
sbit keyenable=P3^6;//方向使能
/*sbit keyx=P0^1;//左右
sbit keyy=P0^2;//上下*/
//sbit up=P0^0;
sbit up=P3^3; //down
sbit down=P3^1;
sbit right=P3^2;
sbit left=P3^4;
uchar x[SNAKE+1];
uchar y[SNAKE+1];
uchar time,n,i,e; //延时时刻,当时蛇长,通用循环变量,当时速度
char addx,addy; //位移偏移量
/********************
延时程序
*********************/
void delay(char MS)
{
char us,usn;
while(MS!=0)
{
usn = 0;
while(usn!=0)
{
us=0xff;
while (us!=0){us–;};
usn–;
}
MS–;
}
}
/*******************************************
判别磕碰
*******************************************/
bit knock()
{bit k;
k=0;
if(x[1]>7||y[1]>7)k=1; //撞墙
for(i=2;i
}
/*****************
上下左右键位处理
*****************/
void turnkey()// interrupt 0 using 2
{//up=1;
if(keyenable)
{
if(left){addy=0;if(addx!=1)addx=-1; else addx=1;}
if(right){addy=0;if(addx!=-1)addx=1; else addx=-1;}
if(up){addx=0;if(addy!=-1)addy=1; else addy=-1;}
if(down){addx=0;if(addy!=1)addy=-1; else addy=1;}
/*if(keyy)
{addy=0; //左右
if(keyx)if(addx==-1)addx=-1; else addx=1; //左
else
if(addx==1)addx=1; else addx=-1; //右
}
else
{addx=0; //上下
if(keyx)if(addy==1)addy=1; else addy=-1; //下
else
if(addy==-1)addy=-1; else addy=1; //上
} */
}
}