?? 43.c
字號(hào):
#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; //定義指向該數(shù)組的指針
//****
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("飛機(jī)游戲"
"\r\n
"\r\n游戲控制方法:"
"\r\n 操作者不動(dòng)鍵盤(pán)10秒鐘進(jìn)入演示狀態(tài),按'a'或'4'飛機(jī)"
"\r\n 左移,按'd'或'6'右移,按's'或'0'發(fā)彈,按'-'降低難度,"
"\r\n 按'+'提高難度,按'p'立即進(jìn)入演示狀態(tài)。"
"\r\n 按任何鍵開(kāi)始游戲。");
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級(jí)別: +:高 -:低\r\n5:左移 6:右移 0:發(fā)射");
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("結(jié)束!再玩一遍?(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("游戲自動(dòng)演示中。");
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 代碼
//串口中斷服務(wù)程序,僅需做簡(jiǎn)單調(diào)用即可完成串口輸入輸出的處理
//出入均設(shè)有緩沖區(qū),大小可任意設(shè)置。
//可供使用的函數(shù)名:
//char getbyte(void);從接收緩沖區(qū)取一個(gè)byte,如不想等待則在調(diào)用前檢測(cè)inbufsign是否為1。
//putbyte(char c);放入一個(gè)字節(jié)到發(fā)送緩沖區(qū)
//putbytes(unsigned char *outplace,j);放一串?dāng)?shù)據(jù)到發(fā)送緩沖區(qū),自定義長(zhǎng)度
//putstring(unsigned char code *puts);發(fā)送一個(gè)定義在程序存儲(chǔ)區(qū)的字符串到串口
//puthex(unsigned char c);發(fā)送一個(gè)字節(jié)的hex碼,分成兩個(gè)字節(jié)發(fā)。
//putchar(uchar c,uchar j);發(fā)送一個(gè)字節(jié)數(shù)據(jù)的asc碼表達(dá)方式,需要定義小數(shù)點(diǎn)的位置
//putint(uint ui,uchar j);發(fā)送一個(gè)整型數(shù)據(jù)的asc碼表達(dá)方式,需要定義小數(shù)點(diǎn)的位置
//delay(unsigned char d); 延時(shí)n x 100ns
//getline(char idata *line, unsigned char n); 獲取一行數(shù)據(jù)回車結(jié)束,必須定義最大輸入字符數(shù)
//CR;發(fā)送一個(gè)回車換行
//*************************************************************************
#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; //最后由中斷傳輸出去的字節(jié)位置
unsigned char idata *putlast=outbuf; //最后放入發(fā)送緩沖區(qū)的字節(jié)位置
#define ILEN 2 //size of serial receiving buffer
idata unsigned char inbuf[ILEN];
unsigned char idata *inlast=inbuf; //最后由中斷進(jìn)入接收緩沖區(qū)的字節(jié)位置
unsigned char idata *getlast=inbuf; //最后取走的字節(jié)位置
bit outbufsign; //輸出緩沖區(qū)非空標(biāo)志有=1
bit inbufsign; //接收緩沖區(qū)非空標(biāo)志 有=1
bit inbufful; //輸入緩沖區(qū)滿標(biāo)志 滿=1
#define CR putstring("\r\n") //CR=回車換行
unsigned char code comready[]="com is ready!";
//*****************************
//延時(shí)n x 100ns
void delay(unsigned char d) //在源程序開(kāi)頭定義是否用w77e58或22。1184M晶振
{unsigned char j;
do{ d--;
//110592 & 89c52
#ifndef cpuw77e58
#ifndef xtal221184
j=21; //k=38 cpu80320 100us k=21 cpu 8052
#else
j=42;
#endif
#else
#ifndef xtal221184
j=38;
#else
j=76;
#endif
#endif
do {j--;} while(j!=0);
}while(d!=0);
}
//*****************************
//放入一個(gè)字節(jié)到發(fā)送緩沖區(qū)
putbyte(char c)
{uchar i,j;
ES=0; //暫停串行中斷,以免數(shù)據(jù)比較時(shí)出錯(cuò)?
if (outlast==putlast )
{ i=(0-TH1);
do{i--;j=36; do {j--;}while(j!=0); }while(i!=0); //延時(shí)一個(gè)字節(jié)發(fā)送時(shí)間
}
*putlast=c; //放字節(jié)進(jìn)入緩沖區(qū)
putlast++; //發(fā)送緩沖區(qū)指針加一
if (putlast==outbuf+OLEN) putlast=outbuf; //指針到了頂部換到底部
if (!outbufsign) {outbufsign=1;TI=1; } //緩沖區(qū)開(kāi)始為空置為有,啟動(dòng)發(fā)送
ES=1;
}
//******************************
//放一串?dāng)?shù)據(jù)到發(fā)送緩沖區(qū)
putbytes(unsigned char *outplace,unsigned char j)
{
int i;
for(i=0;i<j;i++)
{putbyte(*outplace);
outplace++;
}
}
//******************************
putchar(uchar c,uchar j)
{
uchar idata free[4];uchar data i;
i=0;
free[i++]=(c/100+0x30);
if (j==3) free[i++]='.';
free[i++]=(c%100)/10+0x30;
if (j==2) free[i++]='.';
if (j==2 && free[i-3]==0x30) free[i-3]=0x20;
free[i++]=(c%10)+0x30;
if (j==1 && free[i-3]==0x30) free[i-3]=0x20;
if (j==1 && free[i-3]==0x20 && free[i-2]==0x30) free[i-2]=0x20;
putbytes(free,i);
}
//******************************
putint(uint ui,uchar j)
{uchar idata free[6];
uchar data i;
i=0;
free[i++]=(ui/10000+0x30);
if (j==5) free[i++]='.';
free[i++]=((ui%10000)/1000+0x30);
if (j==4) free[i++]='.';
if (j==4 && free[i-3]==0x30) free[i-3]=0x20;
free[i++]=((ui%1000)/100+0x30);
if (j==3) free[i++]='.';
if (j==3 && free[i-4]==0x30) free[i-4]=0x20;
if (j==3 && free[i-4]==0x20 && free[i-3]==0x30) free[i-3]=0x20;
free[i++]=((ui%100)/10+0x30);
if (j==2) free[i++]='.';
if (j==2 && free[i-5]==0x30) free[i-5]=0x20;
if (j==2 && free[i-5]==0x20 && free[i-4]==0x30) free[i-4]=0x20;
if (j==2 && free[i-5]==0x20 && free[i-4]==0x20 && free[i-3]==0x30) free[i-3]=0x20;
free[i++]=(ui%10+0x30);
if (j==1 && free[i-5]==0x30) free[i-5]=0x20;
if (j==1 && free[i-5]==0x20 && free[i-4]==0x30) free[i-4]=0x20;
if (j==1 && free[i-5]==0x20 && free[i-4]==0x20 && free[i-3]==0x30) free[i-3]=0x20;
if (j==1 && free[i-5]==0x20 && free[i-4]==0x20 && free[i-3]==0x20 && free[i-2]==0x30) free[i-2]=0x20;
putbytes(free,i);
}
//***************************************
//發(fā)送一個(gè)定義在程序存儲(chǔ)區(qū)的字符串到串口
putstring(unsigned char *puts)
{for (;*puts!=0;puts++) //遇到停止符0結(jié)束
putbyte(*puts);
}
//*************************************
//發(fā)送一個(gè)字節(jié)的hex碼,分成兩個(gè)字節(jié)發(fā)。
unsigned char code hex_[]={"0123456789ABCDEF"};
puthex(unsigned char c)
{int ch;
ch=(c>>4)&0x0f;
putbyte(hex_[ch]);
ch=c&0x0f;
putbyte(hex_[ch]);
}
//*************************************
//從接收緩沖區(qū)取一個(gè)byte,如不想等待則在調(diào)用前檢測(cè)inbufsign是否為1。
uchar getbyte (void)
{ char idata c ;
while (!inbufsign); //緩沖區(qū)空等待
ES=0;
c= *getlast; //取數(shù)據(jù)
getlast++; //最后取走的數(shù)據(jù)位置加一
inbufful=0; //輸入緩沖區(qū)的滿標(biāo)志清零
if (getlast==inbuf+ILEN) getlast=inbuf; //地址到頂部回到底部
if (getlast==inlast) inbufsign=0; //地址相等置接收緩沖區(qū)空空標(biāo)志,再取數(shù)前要檢該標(biāo)志
ES=1;
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -