单片机打地鼠代码.docx
《单片机打地鼠代码.docx》由会员分享,可在线阅读,更多相关《单片机打地鼠代码.docx(12页珍藏版)》请在冰豆网上搜索。
单片机打地鼠代码
单片机打地鼠代码
/**********************************************************
单片机为at89s52,显示用12864液晶显示屏。
黄书敏
2012.5
#include
#include#include#defineucharunsignedchar#defineuintunsignedint#include"stdlib.h"
#defineFSOC12000000L//晶振频率
#defineTIMS1(65536-FSOC/12/1000*5)//每5ms溢出
全局变量
#defineTIMS0(65536-200)//计数器每中断200次溢出一次
////////////////////////////
ucharscore=0;//uinttop=0;//ucharlevel=1;//ucharlife=3;
ucharpos=0;//ucharprepos=0;
ucharflag=0;//
uintcount;//ucharremtime=33;sbittriger=P3^3;
//////////////////////////////
分数
最高分
关数
//生命数
地鼠/地雷位置
//上一只地鼠/地雷位置
地鼠or地雷标志位
计时器
//每局剩余秒数30s
//单片机P3^3口接P3^4口
/////////////////////////////////////////////////////////
数声明/////////////////////////////////voidDelayUs2x(uintt);
voidDelayMs(uintt);
voiddelay_50us(uintt);
voiddelay_1s(uintt);
voiddelay_0_5s(uintt);
voidCheckBusy(void);
voidwrite_12864com(ucharcom);
voidwrite_12864date(uchardate);
voidinitinal(void);
voidClrScreen();
voidSet_Cursor(ucharx,uchary);
voidwriteCGRAM(ucharpos,ucharcode*pic);
voiddisplayCGRAM(ucharx,uchary,ucharpos);
voidLCD_PutString(unsignedcharx,unsignedchary,unsignedcharcode*s);
voidLCD_PutGraphic(unsignedcharcode*img);//显示满屏图片
voidshowpic(ucharx,uchary,ucharcode*pic);voidshow_welcome();
在指定位置显示老鼠
在指定位置显示锤子
在指定位置显示炸弹
在指定位置显示方框
voidshow_life(ucharlife);voidshow_level(ucharlevel);voidshow_time(uchartime);voidshow_score(uintscore);voidIniScreen();voidshow_mouse(ucharpos);//voidshow_strike(ucharpos);//voidshow_bomb(ucharpos);//voidshow_squre(ucharpos);//voidshow_level_up(ucharlevel,uintscore);
voidshow_gameover(uintscore);voidshow_congratulation(uintscore);
voidshow_win_newrecord(uintscore,uinttop);voidshow_lose_newrecord(uintscore,uinttop);
/////////////////////////voidTimers_Init();voidTimer1reset();voidTimer0reset();
voidTimer0_ISR();//中断处理函数voidTimer1_ISR();
voidclose_timers();voiddelay_ms(uintt);unsignedcharKey_Scan();uintlong_delay(uchars);uintStarAGame();voidlevel_going();unsignedcharend();
/////////////////////////////////////////12864
队友写的,未经允许不做公开////////////////////////////////
///////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////voidTimers_Init()//配置Timer1为计时器,timer0为计数器,timer1每5ms送出一个interrupt3,使count++,timer0++
}
voidTimer0reset()
{
TR0=1;//timer0允许记时remtime=33;
}
voidTimer1reset()
{
TR1=1;//timer1允许计时count=0;
}
voidTimer0_ISR()interrupt1//用于每局30s计时{
remtime--;//时间减一秒
show_time(remtime-3);//打印时间TH0=TIMS0>>8;
TL0=TIMS0;
}
voidTimer1_ISR()interrupt3//用于每只老鼠时间计时,具体时间依关数不同。
{
triger=0;
triger=1;
count++;
TH1=TIMS1>>8;
TL1=TIMS1;
}
voidclose_timers()
{
TR0=0;
TR1=0;
}
///////////////////////////矩阵键盘
///////////////////////////
voiddelay_ms(uintt)
{
uinti;
for(;t>0;t--)
{
i=120;
while(i--);
}
}
unsignedcharKey_Scan()
{
unsignedcharX=0,Y=0,key=255;P1=0x0f;
if(P1!
=0x0f)
{delay_ms(10);//消抖if(P1!
=0x0f)
{
X=P1;
P1=0xf0;
Y=P1;key=X|Y;switch(key){
对键值赋值
case0xee:
key=12;break;//case0xde:
key=8;break;case0xbe:
key=4;break;case0x7e:
key=0;break;case0xed:
key=13;break;case0xdd:
key=9;break;case0xbd:
key=5;break;case0x7d:
key=1;break;case0xeb:
key=14;break;case0xdb:
key=10;break;case0xbb:
key=6;break;case0x7b:
key=2;break;case0xe7:
key=15;break;case0xd7:
key=13;break;case0xb7:
key=7;break;case0x77:
key=3;break;
default:
key=255;break;
}
while(P1!
=0xf0);
}
}
returnkey;
}
///////////////////游戏进行时///////////////////
uintlong_delay(uchars)//延时函数
{
uchari,j;
while(s--)
{
for(i=0;i<10;i++)
{
for(j=0;j<250;j++)
{if(Key_Scan()!
=255)//按键可结束延时,并将rand_seed值定格
{
srand(j);//种随即种子
return1;
}
}
}
}
return0;//开机超时未响应
}
uintStarAGame()
initinal();
writeCGRAM(0,mouse);writeCGRAM(1,squre);writeCGRAM(2,strike);writeCGRAM(3,bomb);
ClrScreen();show_welcome();life=3;/////////
score=0;level=1;
if(long_delay(1000)==1)
{
Timers_Init();
return1;
}
else
{
return0;
}
}
voidcreat_a_mouse()//处理一只老鼠
{
uchargetkey;
show_squre(prepos);//清除上一次图像Timer1reset();
if(flag!
=0)
{
show_mouse(pos);//画出一只老鼠while(count<=400-5*level)
{
getkey=Key_Scan();
if(getkey!
=255)
{
if(getkey==pos)
{
score++;
show_strike(pos);
return;
}
else
{
//life--;
show_squre(pos);
return;
}
}
}
//life--;//超时扣分show_squre(pos);return;
}
else
{
show_bomb(pos);//出现炸弹,几率五分之一while(count<=400-5*level)
{
getkey=Key_Scan();
if(getkey==pos)
life--;
return;
}
}
show_squre(pos);
return;
}
}
voidlevel_going()
{if(life<3){
life++;//每关生命值加一}Timer0reset();
IniScreen();show_level(level);delay_ms(1000);
while(remtime>3){show_life(life);
show_score(score);delay_ms(500-8*level);
if(life>0)
{
prepos=pos;pos=rand()%16;flag=rand()%7;
creat_a_mouse();
}else
{close_timers();
return;
}
}close_timers();return;
}unsignedcharend()
{
if(level<20)//游戏失败{
if(score<=top)
{ClrScreen();
show_gameover(score);
}
else
{
ClrScreen();show_lose_newrecord(score,top);top=score;
}
}
else//通关
{
if(score<=top)
ClrScreen();
show_congratulation(score);
}
else
{ClrScreen();show_win_newrecord(score,top);
top=score;
}
}
for(remtime=60;remtime>0;)选择是退出还是再来一局。
{
unsignedchartemp;
Timer0reset();
temp=Key_Scan();
if(temp<=7)
{
return0;//
}
if(temp>=8&&temp<=15)
{
return1;//
}
}
return0;//
}
//扫描键盘一分钟,等待
按右侧任一键,退出
按左侧任一键,再来一局
超时未选,退出
voidmain()
{top=0;
while
(1){if(StarAGame()==1)//进入键被按下{
level_going();
while(score>=13*level&&level<20&&life>0){
level++;ClrScreen();
show_level_up(level,score);delay_1s(8);
level_going();
}
if(end()==1)
{close_timers();continue;
}
else
{close_timers();
ClrScreen();break;}}else{
ClrScreen();
break;
}
}
while
(1);
}