設(shè)計(jì)要求
①制作一個(gè)的貪吃蛇游戲,系統(tǒng)以單片機(jī)為控制器,用四個(gè)輸入端表示四個(gè)控制鍵(上下左右)。
②游戲初始化蛇的節(jié)數(shù),以及障礙墻壁。
③當(dāng)蛇的頭碰到障礙墻壁或蛇的身體時(shí)自動(dòng)結(jié)束。
it D0=P2^0;
sbit D1=P2^1;
sbit D2=P2^2;
sbit D3=P2^3;
sbit KEY1=P3^5;
sbit KEY2=P3^4;
sbit KEY3=P3^3;
sbit KEY4=P3^2;
#define X1 P0
#define X2 P1
u8 Date[32]=
{
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,
};//點(diǎn)陣屏顯示數(shù)據(jù)
u8 snake_turn_point[20]={0};//存儲(chǔ)蛇尾的轉(zhuǎn)折點(diǎn)
u32 snake_turn=0;//相應(yīng)轉(zhuǎn)點(diǎn)為左轉(zhuǎn)還是右轉(zhuǎn) 0為左轉(zhuǎn)1為右轉(zhuǎn)
u8 snake_cd=2;//蛇的長度 初始值為2
u8 smake_head_x=10;//蛇頭的x軸坐標(biāo)
u8 smake_head_y=10;//蛇頭的y軸坐標(biāo)
u8 smake_tail_x=10;//蛇尾的x軸坐標(biāo)
u8 smake_tail_y=11;//蛇尾的y軸坐標(biāo)
u8 snake_fx=3;//蛇的運(yùn)動(dòng)方向 1:上 2:下 3:左 4:右
u8 snake_sd=10;//運(yùn)動(dòng)速度
u8 snake_turn_time=0;//蛇的尾巴剩余轉(zhuǎn)點(diǎn)
u8 snake_tail_fx=3;//蛇尾運(yùn)動(dòng)方向
u8 food_x=5;//食物的x軸坐標(biāo)
u8 food_y=5;//食物的y軸坐標(biāo)
u8 food_x_next=10;//下一個(gè)食物的x軸坐標(biāo)
u8 food_y_next=10;//下一個(gè)食物的y軸坐標(biāo)
void Display(void);//顯示函數(shù)
void InitTimer0(void);//定時(shí)器中斷初始化 10ms
void Key_Scan(void);//按鍵掃描函數(shù)
void Write_Date(u8 x,u8 y,u8 a);//對(duì)點(diǎn)的寫操作
void snake_move(void);//蛇的移動(dòng)
void game_over(void);//游戲結(jié)束 顯示完
void creat_food();//生產(chǎn)食物
void main(void)
{
InitTimer0();//初始化定時(shí)器
Write_Date(food_x,food_y,1);
while(1)
{
Display();
Key_Scan();
}
}
void InitTimer0(void)
{
TMOD = 0x01;
TH0 = 0x0D8;
TL0 = 0x0F0;
EA = 1;
ET0 = 1;
TR0 = 1;
}
void Display(void)
{
X1=0x00; X2=0x00; D3=0; D2=0; D1=0; D0=0; X1=Date[0]; X2=Date[1];
X1=0x00; X2=0x00; D3=0; D2=0; D1=0; D0=1; X1=Date[2]; X2=Date[3];
X1=0x00; X2=0x00; D3=0; D2=0; D1=1; D0=0; X1=Date[4]; X2=Date[5];
X1=0x00; X2=0x00; D3=0; D2=0; D1=1; D0=1; X1=Date[6]; X2=Date[7];
X1=0x00; X2=0x00; D3=0; D2=1; D1=0; D0=0; X1=Date[8]; X2=Date[9];
X1=0x00; X2=0x00; D3=0; D2=1; D1=0; D0=1; X1=Date[10]; X2=Date[11];
X1=0x00; X2=0x00; D3=0; D2=1; D1=1; D0=0; X1=Date[12]; X2=Date[13];
X1=0x00; X2=0x00; D3=0; D2=1; D1=1; D0=1; X1=Date[14]; X2=Date[15];
X1=0x00; X2=0x00; D3=1; D2=0; D1=0; D0=0; X1=Date[16]; X2=Date[17];
X1=0x00; X2=0x00; D3=1; D2=0; D1=0; D0=1; X1=Date[18]; X2=Date[19];
X1=0x00; X2=0x00; D3=1; D2=0; D1=1; D0=0; X1=Date[20]; X2=Date[21];
X1=0x00; X2=0x00; D3=1; D2=0; D1=1; D0=1; X1=Date[22]; X2=Date[23];
X1=0x00; X2=0x00; D3=1; D2=1; D1=0; D0=0; X1=Date[24]; X2=Date[25];
X1=0x00; X2=0x00; D3=1; D2=1; D1=0; D0=1; X1=Date[26]; X2=Date[27];
X1=0x00; X2=0x00; D3=1; D2=1; D1=1; D0=0; X1=Date[28]; X2=Date[29];
X1=0x00; X2=0x00; D3=1; D2=1; D1=1; D0=1; X1=Date[30]; X2=Date[31];
}
void Key_Scan(void)
{
static fx=3;
if(KEY1==0)
snake_fx=1;
if(KEY2==0)
snake_fx=2;
if(KEY3==0)
snake_fx=3;
if(KEY4==0)
snake_fx=4;
if(fx!=snake_fx)
{
snake_turn_point[snake_turn_time]|=snake_fx*16;//保存蛇尾拐點(diǎn)方向
if(snake_fx==1||snake_fx==2)
snake_turn_point[snake_turn_time]+=smake_head_y;
else if(snake_fx==3||snake_fx==4)
snake_turn_point[snake_turn_time]+=smake_head_x; //保存蛇尾拐點(diǎn)的坐標(biāo)
snake_turn_time++;
fx=snake_fx;
}
}
void tail_turn()
{
u8 i;
for(i=0;i<snake_turn_time;i++)
{
snake_turn_point[i]=snake_turn_point[i+1];
}
snake_turn_time-=1;
}
void res_food()
{
food_x=food_x_next;
food_y=food_y_next;
Write_Date(food_x,food_y,1);
}
void snake_move(void)
{
u8 dat=0x01;
u8 bj_get_food=0;
if(smake_head_y==0&&snake_fx==3)
game_over();
if(smake_head_x==0&&snake_fx==1)
game_over(); //判斷蛇頭是否碰到上、左墻壁
if(snake_fx==1)
{
if(!((smake_head_x-1==food_x)&&(smake_head_y==food_y)))
{
Write_Da
資料借鑒于此紛傳
閱讀全文