单片机源程序如下:
#include <12864.h>
#include
#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
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
『本文转载自网络,版权归原作者所有,如有侵权请联系删除』