#include<intrins.h>
//#include<math.h>
#include<stdlib.h>
#include<lcd12864.h>
sbit up = P1^0 //上
sbit down= P1^1 //下
sbit left= P1^2 //蚂腊陆左
sbit right = P1^3 //右
sbit restart = P1^4 //功能键
#define snake_max_body 22
#define Key_Up 1
#define Key_Down 4
#define Key_Left 2
#define Key_Right 3
uchar Key_V_temp=0//上次按键值
uchar Key_Value
const uchar code failure[]={"很局羡遗憾,你输了"}
const uchar code maker[]={"设计者:机协成员"闷顷}
const uchar code huiz[]={"会长:方吉桐"}
const uchar code guanggao[]={"机器人协会欢迎你"}
const uchar code t[]={
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xE7,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xE7,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xE7,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xE7,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF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F,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0x7F,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF
}
//const uchar zhi[]={
//0xFF,0xF3,0x85,0xE8,0xCC,0xED,0x8D,0xEF}
struct //结构体:食物
{
uchar X
uchar Y
uchar Yes
}Food
struct //结构体:小蛇
{
uchar X[snake_max_body] //蛇的X坐标
uchar Y[snake_max_body]//蛇的Y坐标
uchar Log //蛇的长度
uchar Life //蛇的生命
uchar Score//蛇的分数
uchar Level//蛇的等级
}Snake
void Init_time1() //定时器初始化,用来产生随机数
{
EA=1
ET1=1
TMOD|=0X20
TH1=0X01
TL1=0X01
TR1=1
}
void snake_init() //贪吃蛇初始化场地
{
uchar i
line(0,0,0,80)
line(0,80,50,80)
line(50,80,50,0)
line(50,0,0,0)
Snake.Log=2 //定义初始化蛇的长度
Snake.Life=0
Snake.Score=0
// Food.Yes=1
Key_Value=0
for(i=0i<Snake.Logi++) //将蛇给赋值
{
Snake.X[i]=i+2
Snake.Y[i]=2
}
for(i=0i<Snake.Logi++) //将蛇给画出来
{
dot(Snake.X[i],Snake.Y[i])
}
}
key_press()//按键检测
{
// uchar key
up=1
if(!up)
{
delay_ms(5)
if(!up)
{
Key_Value=1
}
while(!up)
}
down=1
if(!down)
{
delay_ms(5)
if(!down)
{
Key_Value=4
}
while(!down)
}
left=1
if(!left)
{
delay_ms(5)
if(!left)
{
Key_Value=2
}
while(!left)
}
right=1
if(!right)
{
delay_ms(5)
if(!right)
{
Key_Value=3
}
while(!up)
}
// return Key_Value
}
void Run_snake(void) //蛇移动
{
uchar i=0
if((Key_Value==Key_Right&&Key_V_temp!=Key_Left)||(Key_Value==Key_Left&&Key_V_temp==Key_Right))
{
offdot(Snake.X[0],Snake.Y[0]) //先把蛇尾给熄灭掉
for(i=0i<Snake.Log-1i++) //蛇身都是它前一段的蛇身的位置
{
Snake.X[i]=Snake.X[i+1]
Snake.Y[i]=Snake.Y[i+1]
}
Snake.X[Snake.Log-1]=Snake.X[Snake.Log-2]+0
Snake.Y[Snake.Log-1]=Snake.Y[Snake.Log-2]+1 //蛇头向要求的方向移动
for(i=0i<Snake.Logi++) //蛇显示函数
{
dot(Snake.X[i],Snake.Y[i])
}
Key_V_temp=Key_Right //保存现在的值,以便下次按下时使用
}
if((Key_Value==Key_Left&&Key_V_temp!=Key_Right)||(Key_Value==Key_Right&&Key_V_temp==Key_Left))
{
offdot(Snake.X[0],Snake.Y[0])
for(i=0i<Snake.Log-1i++)
{
Snake.X[i]=Snake.X[i+1]
Snake.Y[i]=Snake.Y[i+1]
}
Snake.X[Snake.Log-1]=Snake.X[Snake.Log-2]+0
Snake.Y[Snake.Log-1]=Snake.Y[Snake.Log-2]-1
for(i=0i<Snake.Logi++)
{
dot(Snake.X[i],Snake.Y[i])
}
Key_V_temp=Key_Left
}
if(( Key_Value==Key_Up&&Key_V_temp!=Key_Down)||( Key_Value==Key_Down&&Key_V_temp==Key_Up))
{
offdot(Snake.X[0],Snake.Y[0])
for(i=0i<Snake.Log-1i++)
{
Snake.X[i]=Snake.X[i+1]
Snake.Y[i]=Snake.Y[i+1]
}
Snake.X[Snake.Log-1]=Snake.X[Snake.Log-2]-1
Snake.Y[Snake.Log-1]=Snake.Y[Snake.Log-2]+0
for(i=0i<Snake.Logi++)
{
dot(Snake.X[i],Snake.Y[i])
}
Key_V_temp=Key_Up
}
if(( Key_Value==Key_Down&&Key_V_temp!=Key_Up)||( Key_Value==Key_Up&&Key_V_temp==Key_Down))
{
offdot(Snake.X[0],Snake.Y[0])
for(i=0i<Snake.Log-1i++)
{
Snake.X[i]=Snake.X[i+1]
Snake.Y[i]=Snake.Y[i+1]
}
Snake.X[Snake.Log-1]=Snake.X[Snake.Log-2]+1
Snake.Y[Snake.Log-1]=Snake.Y[Snake.Log-2]+0
for(i=0i<Snake.Logi++)
{
dot(Snake.X[i],Snake.Y[i])
}
Key_V_temp=Key_Down
}
if((Snake.X[Snake.Log-1]>=50)||(Snake.Y[Snake.Log-1]>=80)||(Snake.X[Snake.Log-1]==0)||(Snake.Y[Snake.Log-1]==0))//判蛇头是否撞到墙壁
{
Snake.Life=1
}
for(i=3i<Snake.Logi++)//从第三节开始判断蛇头是否咬到自己
{
if((Snake.X[i]==Snake.X[0])&&(Snake.Y[i]==Snake.Y[0]))
{
Snake.Life=1
}
}
/* if(Snake.Life==1)
{
i=Key_Value
// Display_dead( i)
// clear()
write_comd(0x34)
}*/
/* if(Snake.Log==snake_max_body) //判断蛇是否最长了
{
//i=Key_Value
write_comd(0x34)
// clear()
write_comd(0x30)
// Display_Pass(i)
}*/
if(Snake.X[Snake.Log-1]==Food.X&&Snake.Y[Snake.Log-1]==Food.Y)//判蛇是否吃到食物
{
Snake.Log++ //蛇节数加1
Snake.X[Snake.Log-1]=Food.X
Snake.Y[Snake.Log-1]=Food.Y
Snake.Score++
Food.Yes=1 //食物标志置1
}
if(Food.Yes==0) //放果子程序
{
dot(Food.X,Food.Y)
}
if(Food.Yes==1)
{
while(1)
{
Food.X=TL1/5//获得随机数
Food.Y=TL1/4
for(i=0i<Snake.Logi++)//判断产生的食物坐标是否和蛇身重合
{
if((Food.X==Snake.X[i])&&(Food.X==Snake.Y[i]))
break
}
if(i==Snake.Log)
{
Food.Yes=0
break //产生有效的食物坐标
}
}
}
}
void main()
{
lcd_init() //初始化LCD
Init_time1()
clear()
_nop_()_nop_()_nop_()
map(t)
delay_ms(1400)
clear()
snake_init()
// snake_food()
Food.X=TL1/5
Food.Y=TL1/4
dot(Food.X,Food.Y)
// dot(TL1&49,TL1&79)
// dot(TL1&49,TL1&79)
while(1)
{
if(Snake.Life)
{
// clear()
write_comd(0x34)
write_comd(0x30)
delay_ms(100)
display(0x80,7,failure)
display(0x90,8,maker)
display(0x88,6,huiz)
display(0x98,8,guanggao)
}
else
{
key_press()
Run_snake()
write_comd(0x36)
}
if(Snake.Score<15)
delay_ms(150-10*Snake.Score)
else
delay_ms(5)
}
}
int FindSamdData(int *a, int*b, int length){
int i,j
for (i=0,j=0(i<length)&&(j<length))
{
while (*a <*b)//有序排列,挨个比较
{
a++
i++
}
while (*a >逗衫 *b)
{
b++
j++
}
if (*a == *b) return *a
}
}
int main(void)
{
int a[ ]={1,3,5,7,9,13,15,27,29,37}
int b[ ]={2,4,6,8,10,13,14,27,29,37}
int c
c = FindSamdData(a, b ,sizeof(a))
}
增加出错的程序。出错的最好使用一个单独参数,我为了方便没有增加参数。
//有序数组查找相同数据,数组等长
//*length为0未找到相同数据,
int FindSamdData(int *a, int*b, int *length)
{
int i,j
for (i=0,j=0(i<*length)&&(j<*length))
{
while (*a <*b)//有序排列,挨个比较
{
a++
i++
if (i>=*length)break
}
while (*a >高搏 *b)
{
b++
j++
if (j>=*length)break
}
if (*a == *b)
{
*length = 1
return *a
}
}
*length = 0
return 0//
}
//数组不等长
//*al或*bl超出数组长表示出错,
int FindSamdData2(int *a, int*b, int *al, int *bl)
{
int i,j
for (i=0,j=0(i<*al)&&(j<*bl))
{
while (*a <*b)//有序排列,挨个比较
{
a++
i++
if (i>=*al) break
}
while (*a >*b)
{
b++
j++
if (j>=*bl) break
}
if (*a == *b)
{
*al = i
*bl = j
return *a
}
}
*al = i
*bl = j
return 0
}
void main(void)
{
int a[]={1,3,5,7,9,13,15,27,29,37}
int b[]={2,4,6,8,10,13,14,27,29,37}
int la = sizeof(a)
int lb = sizeof(b)
int c
c = FindSamdData(a, b ,&la)
if (la == 0) printf("山念腔No same!")
la = sizeof(a)//la会被函数 FindSamdData 修改.
c = FindSamdData2(a, b ,&la, &lb)
if ((la >=sizeof(a)) || (la >=sizeof(a))) printf("No same!")
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)