#include <serial.c> //包含串行处理模块
bit showclocksign;
bit showblocksign;
bit showfensign;
uchar count;
uint second;
uchar paoplace;
uint zongfen;
uchar speed=32;
idata uint free[16]; //定义行首处理空间
unsigned char idata *freep; //定义指向该数组的指针
//****
gotoxy(uchar x,y)
{putbyte(0x1b);putbyte('['
);
putbyte((y%100)/10+0x30);
putbyte((y%10)+0x30);
putbyte(';');
putbyte((x%100)/10+0x30);
putbyte((x%10)+0x30);
putbyte('H');
}
//******************
clrscr()
{gotoxy(0,0); putbyte(0x1b); putstring("[J"); }
//**************
showpao()
{gotoxy(1,16); putbyte(0x1b); putstring("[K");
gotoxy(paoplace,16); putbyte('|');
gotoxy(1,17); putbyte(0x1b); putstring("[K");
gotoxy(paoplace-1,17);
putbyte('/');putbyte('H');putbyte('\\');
gotoxy(0,24);
}
//***************
uint bdata showp;
sbit outblock=showp^15;
showblock() //showblock
{uchar i,j;uint bdata p;
uint st;
gotoxy(0,0);
for(i=15;i!=0;i--)
{putbyte('|');
showp=free[i];
st=0x8000;
for(j=0;j!=16;j++)
{ if ((showp&st)!=0) putbyte('@');
else putbyte(' ');
st>>=1;
//showp=showp>>1;
}
putbyte('|');CR;
}
gotoxy(0,24);
showblocksign=0;
}
//*************
showfen()
{gotoxy(6,19);
//putstring("得分: ");
putint(zongfen,1);
showfensign=0;
//putbyte(' ');
//puthex(free[15]/256); puthex(free[15]%256);
//gotoxy(0,24);
}
//******************************************************
void main(void)
{uchar idata c,i,j;bit k;
uint temp,notype;
serial_init(); // ET0=0;
putstring("\r\nSerial ready!\r\n");
start: ET0=1;clrscr();
putstring("飞机游戏"
"\r\n
"\r\n游戏控制方法:"
"\r\n 操作者不动键盘10秒钟进入演示状态,按'a'或'4'飞机"
"\r\n 左移,按'd'或'6'右移,按's'或'0'发弹,按'-'降低难度,"
"\r\n 按'+'提高难度,按'p'立即进入演示状态。"
"\r\n 按任何键开始游戏。");
while(!inbufsign);
zongfen=0;showblock();showfen();notype=1000;
gotoxy(0,19); //putstring("lever: h-hard e-easy\r\n5-left 6-right 0-shot");
putstring("得分:\r\n级别: +:高 -:低\r\n5:左移 6:右移 0:发射");
gotoxy(7,20);putchar(speed,1);
for(i=0;i!=16;i++)
{free[i]=0;
}
paoplace=9;showpao();
while(1) {
if (showblocksign) showblock();
if (showfensign) showfen();
if (free[0]!=0)
{ET0=0;notype=1000;free[0]=0;gotoxy(0,22);putstring("结束!再玩一遍?(y/n)");
while(getbyte()!='y');goto start;
}
notype--;if(notype==10){notype=1000;goto autoplay;}
delay(100);
if (notype>900) {gotoxy(0,22);putbyte(0x1b); putstring("[K");}
if (inbufsign)
{ c=getbyte();notype=1000;
switch (c)
{case '5': //left move
case 'a':
paoplace--; if (paoplace==1) paoplace=2;
showpao();
break;
case '6': //right move
case 'd':
paoplace++; if (paoplace==18) paoplace=17;
showpao();
break;
case '=':
case '+':
speed--;
if (speed==0)speed=1;
gotoxy(7,20);
putchar(speed,1);
break;
case '_':
case '-':
speed++;
if (speed==255)speed=254;
gotoxy(7,20);
putchar(speed,1);
break;
case 's':
case '0': //shot
for(i=15,j=1;i!=0;i--,j=16-i)
{gotoxy(paoplace,i); putbyte('o');delay(50);
putbyte(8);putbyte(' ');
//free[i]=0xffff;
temp=0x0001<<(17-paoplace);
if((free[j]&temp)!=0)
{free[j]&= (temp^0xffff);zongfen+=10;showfensign=1;break;}
}
break;
case 'y': goto start;
case 'p':
autoplay: gotoxy(0,22);putbyte(0x1b); putstring("[K");putstring("游戏自动演示中。");
ET0=1;speed=20;gotoxy(7,20);putchar(speed,1);
do
{
if (showblocksign) showblock();
if (showfensign) showfen();
for(i=15,j=1;i!=0;i--,j=16-i)
{gotoxy(paoplace,i); putbyte('o');delay(50);
putbyte(8);putbyte(' ');
temp=0x0001<<(17-paoplace);
if((free[j]&temp)!=0)
{free[j] &= (temp^0xffff);zongfen+=10;showfensign=1;goto again;}
}
again:
if (!(((free[5]&temp)!=0)||((free[4]&temp)!=0)))
{if (k) {paoplace--;}
else {paoplace++;}
}
if (paoplace==1){ paoplace=2; k=0;}
if (paoplace==18) {paoplace=17;k=1;}
if (free[0]!=0) //if dead?
{ET0=0;notype=1000;free[0]=0;
//while(getbyte()!='y');goto start;
break;
}
showpao();
}while(!inbufsign);
}
}
}
}
//freep=free;
//putbytes(freep,1);
//getline(freep,1);
uint code *getcode;
//******************
void timer0(void) interrupt 1
{
uchar i;
TH0=31;TL0=00;
//1/32S
count++;
if (count==speed) //11059200
{count=0; //1s
for(i=0;i<16;i++)
{free[i]=free[i+1];
}
free[15]=*getcode;
getcode++;if (getcode==1000) getcode=0;
showblocksign=1;
showclocksign=1;
second++; //1S
if (second==3600) {second=0;showclocksign=1;}
// if (iftimeover) timeovercount++;
//else timeovercount=0;
}
}
//serial.c 代码
//串口中断服务程序,仅需做简单调用即可完成串口输入输出的处理
//出入均设有缓冲区,大小可任意设置。
//可供使用的函数名:
//char getbyte(void);从接收缓冲区取一个byte,如不想等待则在调用前检测inbufsign是否为1。
//putbyte(char c);放入一个字节到发送缓冲区
//putbytes(unsigned char *outplace,j);放一串数据到发送缓冲区,自定义长度
//putstring(unsigned char code *puts);发送一个定义在程序存储区的字符串到串口
//puthex(unsigned char c);发送一个字节的hex码,分成两个字节发。
//putchar(uchar c,uchar j);发送一个字节数据的asc码表达方式,需要定义小数点的位置
//putint(uint ui,uchar j);发送一个整型数据的asc码表达方式,需要定义小数点的位置
//delay(unsigned char d); 延时n x 100ns
//getline(char idata *line, unsigned char n); 获取一行数据回车结束,必须定义最大输入字符数
//CR;发送一个回车换行
//*************************************************************************
#include <w77e58.h>
#define uchar unsigned char
#define uint unsigned int
#define OLEN 10 //size of serial transmission buffer
idata unsigned char outbuf[OLEN]; // storage for transmission buffer
unsigned char idata *outlast=outbuf; //最后由中断传输出去的字节位置
unsigned char idata *putlast=outbuf; //最后放入发送缓冲区的字节位置
#define ILEN 2 //size of serial receiving buffer
idata unsigned char inbuf[ILEN];
unsigned char idata *inlast=inbuf; //最后由中断进入接收缓冲区的字节位置
unsigned char idata *getlast=inbuf; //最后取走的字节位置
bit outbufsign; //输出缓冲区非空标志有=1
bit inbufsign; //接收缓冲区非空标志 有=1
bit inbufful; //输入缓冲区满标志 满=1
#define CR putstring("\r\n") //CR=回车换行
unsigned char code comready[]="com is ready!";
//*****************************
//延时n x 100ns
void delay(unsigned char d) //在源程序开头定义是否用w77e58或22。1184M晶振
{unsigned char j;
do{ d--;
//110592 & 89c52
#ifndef cpuw77e58
#ifndef xtal221184
j=21; //k=38