版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
#ifndef_glb_h_#define_glb_h_#defineucharunsignedchar#defineuintunsignedint#defineLOW0#defineHIGH1//初始化指令#defineCLEAR_SCREEN0x01//清屏指令:清屏且AC值為00H#defineAC_INIT0x02//將AC設(shè)置為00H。且游標(biāo)移到原點(diǎn)位置#defineCURSE_ADD0x06//設(shè)定游標(biāo)移到方向及圖像整體移動方向〔默認(rèn)游標(biāo)右移,圖像整體不動〕#defineFUN_MODE0x30//工作模式:8位根本指令集#defineDISPLAY_ON0x0c//顯示開,顯示游標(biāo),且游標(biāo)位置反白#defineDISPLAY_OFF0x08//顯示關(guān)#defineCURSE_DIR0x14//游標(biāo)向右移動:AC=AC+1#defineSET_CG_AC0x40//設(shè)置AC,范圍為:00H~3FH#defineSET_DD_AC0x80#defineFUN_MODEK0x36//擴(kuò)展指令集,8位指令#defineX1address0x80//上半屏X軸起始地址#defineX2address0x88//下半屏X軸起始地址#defineYaddress0x80//Y軸起始地址/*--------------------------------------------------------------------------------------------------- 液晶屏----------------------------------------------------------------------------------------------------*/#defineDataPortP0sbitRS=P3^5;sbitRW=P3^6;sbitE=P3^4;sbitPSB=P3^7;sbitWELA=P2^7;sbitDULA=P2^6;sbitspeed=P2^0;sbitbeep=P2^3;/*#defineDataPortP1sbitRS=P2^0;sbitRW=P2^1;sbitE=P2^3;sbitPSB=P2^4;//sbitWELA=P2^7;//sbitDULA=P2^6;*//*--------------------------------------------------------------------------------------------------- 貪吃蛇----------------------------------------------------------------------------------------------------*///定義復(fù)位不自動清零全局變量//#pragmaDATA_SEGNOINIT_RAMsbitLEFT=P2^4;//左sbitRIGHT=P2^1;//右sbitUP=P2^5;//上sbitDOWN=P2^2;//下//sbitLed1=P1^7;//sbitLed2=P1^6;/*sbitLEFT=P0^2;//左sbitRIGHT=P0^3;//右sbitUP=P0^0;//上sbitDOWN=P0^1;//下*/sbitLed7=P1^0;sbitLed6=P1^1;sbitLed5=P1^3;sbitLed4=P1^4;//sbitLed3=P3^3;//sbitLed2=P3^2;//sbitLed1=P3^1;//sbitLed0=P3^0;#endif#ifndef_Init_h_#define_Init_h_/*------------------------------------------------------------------------------------------------------------ Init_12864-------------------------------------------------------------------------------------------------------------*/voidDelayus(unsignedchari);voidDelayms(unsignedinti);voidCheck_Busy();voidWrite_Cmd(unsignedcharCmd);voidWrite_Data(unsignedcharData);//ucharRead_Status();ucharRead_Data();voidInit_IO();voidInit_12864();//voiddisp();voidCGRAM();voidLCD_PutString(unsignedcharx,unsignedchary,unsignedcharcode*s);voidClrScreen();voidClr_GDRAM(void);voidLCD_PutGraphic(unsignedcharcode*img);voidSetGraphicMode();voidKeyscan();voidstatuschange();voidDraw_Point(ucharX,ucharY,ucharclour);voidDraw_zhi(ucharX1,ucharX2,ucharY);voidDraw_shu(ucharX,ucharY1,ucharY2);voidDraw_line(ucharx0,uchary0,ucharx1,uchary1);voidDraw_all_lines();voidStat_Point();voidLast_Point();voidAdd_Point();voidBump_Wall();voidSelf_Bump();/*------------------------------------------------------------------------------------------------------------ Init_Time-------------------------------------------------------------------------------------------------------------*///voidInit_Time();//voidexter0()interrupt1#endif#include<reg52.h>#include"glb.h"uinttimestatus=0;voidInit_Time(){ TMOD=0x01; TH0=(65536-50000)/256; TL0=(65536-50000)%256; EA=1; ET0=1; TR0=1; }voidTimer0()interrupt1{ TH0=(65536-50000)/256; TL0=(65536-50000)%256; timestatus++; if(speed==0) { if(timestatus>2) timestatus=0; } if(timestatus>10) timestatus=0;}#ifndef_Interrupt_h_#define_Interrupt_h_voidInit_Time();#endif#include<reg52.h>#include"glb.h"/*游戲的初始化值*/uchargamestatus=0;ucharflag=1; //折點(diǎn)個數(shù)標(biāo)志uchara[]={20,0,0,0,0,0,0,0}; //其中〔a[0],b[0]〕為初始蛇頭坐標(biāo),〔a[1],b[1]〕為初始蛇尾坐標(biāo),之后蛇尾坐標(biāo)為〔a[flag],b[flag]〕。ucharb[]={32,32,0,0,0,0,0,0}; //后面〔a[i],b[i]〕記錄拐點(diǎn)坐標(biāo),其中〔a[flag-1],b[flag-1]〕為第一個拐點(diǎn)的坐標(biāo)ucharc[]={0,0,0,0,0,0,0,0}; //c[i]記錄方向,其中a[0]為當(dāng)前方向ucharx=54,y=32,pointstatus,allpoints;/*---------------------------------------------------------------------------------延時函數(shù)---------------------------------------------------------------------------------*/voidDelayus(unsignedchari){ intj; for(j=2;j>0;j--) for(;i>0;i--);}voidDelayms(unsignedinti){ unsignedintj; for(;i>0;i--) for(j=110;j>0;j--);}/*---------------------------------------------------------------------------------檢測忙位---------------------------------------------------------------------------------*/voidCheck_Busy(){E=0; RS=0; RW=1; E=1; DataPort=0xff; while((DataPort&0x80)==0x80); //忙那么等待,忙標(biāo)志檢測BF,可以將BF讀到DB7進(jìn)行檢測 E=0;}/*---------------------------------------------------------------------------------寫命令---------------------------------------------------------------------------------*/voidWrite_Cmd(unsignedcharCmd){ Check_Busy(); RS=0; RW=0; DataPort=Cmd; Delayus(2); E=1; Delayus(5); E=0;}/*---------------------------------------------------------------------------------寫數(shù)據(jù)---------------------------------------------------------------------------------*/voidWrite_Data(unsignedcharData){ Check_Busy(); RS=1; RW=0; E=1; DataPort=Data; Delayus(2); E=0; Delayus(5);}/*---------------------------------------------------------------------------------讀狀態(tài)---------------------------------------------------------------------------------*//*ucharRead_Status(){ uchari;DataPort=0xff;RW=1;RS=0;E=0;Delayus(2);E=1;Delayus(5);i=DataPort;Delayus(5);E=0;return(i);}*//*---------------------------------------------------------------------------------讀數(shù)據(jù)---------------------------------------------------------------------------------*/ucharRead_Data(){ uchari;//while((Read_Status()&0x80)!=0x00);//忙檢測 Check_Busy();RW=1;RS=1;E=0;Delayus(2);E=1;Delayus(5);i=DataPort;Delayus(5);E=0;returni;}/*---------------------------------------------------------------------------------計算并顯示得分---------------------------------------------------------------------------------*/voidWrite_Score(ucharx,uchary,ucharshu){uchard1,d2,add,ah;d1=shu/10;d2=shu%10;if(x==0)ah=0x80;elseif(x==1)ah=0x90;elseif(x==2)ah=0x88;elseah=0x98;add=ah+y; //某一行顯示分?jǐn)?shù)Write_Cmd(add);Write_Data(d1+'0');Write_Data(d2+'0');}/*---------------------------------------------------------------------------------IO口初始化---------------------------------------------------------------------------------*/voidInit_IO(){ Led4=1; Led5=1; Led6=1; Led7=1;}/*---------------------------------------------------------------------------------液晶屏初始化---------------------------------------------------------------------------------*/voidInit_12864(){ //WELA=0; //DULA=1; Delayms(1); PSB=1; Delayms(1); Write_Cmd(0x30); Delayms(1); Write_Cmd(0x30); Delayms(1); Write_Cmd(0x0f); Delayms(1); Write_Cmd(0x01); Delayms(1); //Write_Cmd(0x06); //DelayVs2x(50);}/*voiddisp(){ unsignedchari; unsignedchara=0x10; Write_Cmd(0x80); for(i=0;i<16;i++) { Write_Data(a); a++; Delayms(2); }}*//*---------------------------------------------------------------------------------用戶自定義字符---------------------------------------------------------------------------------*//*voidCGRAM(){ inti; Write_Cmd(0x30); Write_Cmd(0x40); for(i=0;i<16;i++) { Write_Data(0x10); Write_Data(0x10); }}*//*---------------------------------------------------------------------------------顯示字符x:橫坐標(biāo)值,范圍0-8y:縱坐標(biāo)值,范圍1-4---------------------------------------------------------------------------------*/voidLCD_PutString(unsignedcharx,unsignedchary,unsignedcharcode*s){ switch(y) { case1:Write_Cmd(0x80+x);break; case2:Write_Cmd(0x90+x);break; case3:Write_Cmd(0x88+x);break; case4:Write_Cmd(0x98+x);break; default:break; } while(*s>0) { Write_Data(*s); s++; Delayms(5); }}/*---------------------------------------------------------------------------------清屏---------------------------------------------------------------------------------*/voidClrScreen(){ Write_Cmd(0x01); Delayus(5);}/*---------------------------------------------------------------------------------去除液晶GDRAM內(nèi)部的隨機(jī)數(shù)據(jù)---------------------------------------------------------------------------------*/voidClr_GDRAM(void){ucharx,y,i,j;Write_Cmd(0x34);//鎵撳紑鎵╁睍鎸囦護(hù)闆?y=0x80;x=0x80;for(i=0;i<32;i++){Write_Cmd(y);Write_Cmd(x);for(j=0;j<16;j++){Write_Data(0x00);}y++;}y=0x80;x=0x88;for(i=0;i<32;i++){Write_Cmd(y);Write_Cmd(x);for(j=0;j<16;j++){Write_Data(0x00);}y++;}Write_Cmd(0x30);//鍥炲埌鍩烘湰鎸囦護(hù)闆?}/*---------------------------------------------------------------------------------顯示圖片---------------------------------------------------------------------------------*//*voidLCD_PutGraphic(unsignedcharcode*img){ inti,j; //顯示上半屏內(nèi)容設(shè)置 for(i=0;i<16;i++) { Write_Cmd(0x80); for(j=0;j<16;j++) { Write_Data(*img); img++; } Write_Cmd(0x90); for(j=0;j<16;j++) { Write_Data(*img); img++; } }//顯示下半屏內(nèi)容設(shè)置 for(i=0;i<32;i++) { Write_Cmd(0x88); for(j=0;j<16;j++) { Write_Data(*img); img++; } Write_Cmd(0x98); for(j=0;j<16;j++) { Write_Data(*img); img++; } }}*//*---------------------------------------------------------------------------------設(shè)置到繪圖模式---------------------------------------------------------------------------------*//*voidSetGraphicMode(){ Write_Cmd(0x36); Delayms(2);}*//*------------------------------------------------------------------------------------------------------------ 按鍵函數(shù)-------------------------------------------------------------------------------------------------------------*/voidKeyscan(){ Led4=1; Led5=1; Led6=1; Led7=1; if(RIGHT==0) { Delayms(5); if(RIGHT==0) { Led4=0; gamestatus=0; } }if(DOWN==0) { Delayms(5); if(DOWN==0) { Led5=0; gamestatus=1; } } if(UP==0) { Delayms(5); if(UP==0) { Led6=0; gamestatus=3; } } if(LEFT==0) { Delayms(5); if(LEFT==0) { Led7=0; gamestatus=2; } }}/*------------------------------------------------------------------------------------------------------------ 狀態(tài)發(fā)生改變處理函數(shù)必須跟在按鍵涵數(shù)后面?。。。?------------------------------------------------------------------------------------------------------------*/voidstatuschange(){uchari;if((gamestatus!=c[0])&&((gamestatus%2)!=(c[0]%2))) //只有g(shù)amestatus由奇數(shù)變?yōu)榕紨?shù)或由偶數(shù)變?yōu)槠鏀?shù)時,才改變方向{ flag++; //方向改變,折點(diǎn)個數(shù)加1 for(i=flag;i>0;i--) { a[i]=a[i-1];b[i]=b[i-1];c[i]=c[i-1];}c[0]=gamestatus;}}/*------------------------------------------------------------------------------------------------------------ 畫點(diǎn)函數(shù),在屏幕上畫一個點(diǎn)-------------------------------------------------------------------------------------------------------------*/voidDraw_Point(ucharX,ucharY,ucharclour) //clour為0時畫點(diǎn),為其它去除這個點(diǎn){ucharx_pos,y_pos,x_bit;uintread_H,read_L,data_H,data_L; //計算LCD位置x_pos=X/16; //取16*16首地址if(Y>31) x_pos+=8; //計算該點(diǎn)所屬LCD液晶中X坐標(biāo)位置,上半屏0-7,下半屏8-15x_bit=X%16; //該點(diǎn)位于所在16bit中的第幾位y_pos=Y%32; //y坐標(biāo)32行的第幾行Write_Cmd(0x34); //翻開擴(kuò)展指令集操作GDRAM是擴(kuò)展指令集 Write_Cmd(0x36); //使用擴(kuò)充指令集,繪圖顯示ONWrite_Cmd(0x80+y_pos); // 寫入第一行8個32*16模塊的垂直起始地址,在寫入地址時,要先輸入垂直地址,再輸入水平地址這是規(guī)定好的Write_Cmd(0x80+x_pos); //第一行水平的起始地址為80H //以后寫入數(shù)據(jù)后,地址計數(shù)器〔AC〕會自動加一Read_Data(); //此處為什么要中上不知道為什么????read_H=Read_Data(); //高八位!read_L=Read_Data(); //低八位!if(x_bit<8) //該點(diǎn)位于高八位{ if(clour==0) //畫點(diǎn) { data_H=read_H|(1<<(7-x_bit)); //把1左移7-x_bit位 } else { data_H=read_H&~(1<<(7-x_bit)); } data_L=read_L; }else //該點(diǎn)位于低八位 { data_H=read_H; if(clour==0) { data_L=read_L|(1<<(15-x_bit)); } else { data_L=read_L&~(1<<(15-x_bit));}} Write_Cmd(0x80+y_pos);Write_Cmd(0x80+x_pos);Write_Data(data_H); //寫高八位數(shù)據(jù)Write_Data(data_L); //寫低八位數(shù)據(jù)Write_Cmd(0x30); //回到根本指令集 Write_Cmd(0x0c);}/*------------------------------------------------------------------------------------------------------------ 在整個屏幕上畫一條直線-------------------------------------------------------------------------------------------------------------*/voidDraw_zhi(ucharX1,ucharX2,ucharY) //X1代表線首橫坐標(biāo),X2代表線尾橫坐標(biāo),Y代表縱坐標(biāo){ uchari,temp; if(X1>X2) //X軸上,起點(diǎn)大于終點(diǎn),交換數(shù)據(jù) { temp=X2; X2=X1; X1=temp; }for(i=X1;i<=X2;i++) { Draw_Point(i,Y,0); }}/*------------------------------------------------------------------------------------------------------------ 在整個屏幕上畫一條豎線-------------------------------------------------------------------------------------------------------------*/voidDraw_shu(ucharX,ucharY1,ucharY2) //Y1代表線首縱坐標(biāo),Y2代表線尾縱坐標(biāo),X代表橫坐標(biāo){uchari,temp; if(Y1>Y2) //X軸上,起點(diǎn)大于終點(diǎn),交換數(shù)據(jù) { temp=Y2; Y2=Y1; Y1=temp; } for(i=Y1;i<=Y2;i++) { Draw_Point(X,i,0); }}/*------------------------------------------------------------------------------------------------------------ 在整個屏幕上畫一條線-------------------------------------------------------------------------------------------------------------*/voidDraw_line(ucharx0,uchary0,ucharx1,uchary1) //只能是橫線或者是豎線,〔x0,y0〕為線首坐標(biāo)。(x1,y1)為線尾坐標(biāo){ intdx,dy; //定義X.Y軸上增加的變量值 dx=x1-x0; //X軸方向上的增量 dy=y1-y0; //Y軸方向上的增量 if(dx==0) Draw_shu(x0,y0,y1); if(dy==0) Draw_zhi(x0,x1,y0);}/*------------------------------------------------------------------------------------------------------------ 畫邊界函數(shù)-------------------------------------------------------------------------------------------------------------*/voidDraw_all_lines(){ uchari; for(i=0;i<flag;i++) { Draw_line(a[i],b[i],a[i+1],b[i+1]); }}/*------------------------------------------------------------------------------------------------------------ 開始點(diǎn),蛇頭-------------------------------------------------------------------------------------------------------------*/voidStat_Point(){ if(c[0]==0) { a[0]=a[0]+1; //如果向右,直線長度加1 } elseif(c[0]==1) { b[0]=b[0]+1; } elseif(c[0]==2) { a[0]=a[0]-1; } else { b[0]=b[0]-1; }}/*------------------------------------------------------------------------------------------------------------ 最后點(diǎn),蛇尾-------------------------------------------------------------------------------------------------------------*/voidLast_Point(){ if(c[flag-1]==0) //如果第一次拐之前,方向向右 { a[flag]=a[flag]+1; //蛇尾橫坐標(biāo)加1 } elseif(c[flag-1]==1) { b[flag]=b[flag]+1; } elseif(c[flag-1]==2) { a[flag]=a[flag]-1; }else { b[flag]=b[flag]-1; } if((a[flag]==a[flag-1])&&(b[flag]==b[flag-1])) { flag--; }}/*------------------------------------------------------------------------------------------------------------ 加點(diǎn),如果吃到食物,就產(chǎn)生新的食物,并畫上該點(diǎn),把該點(diǎn)作為蛇頭-------------------------------------------------------------------------------------------------------------*/voidAdd_Point(){ if(c[0]==0) { if((a[0]+1)==x&&(b[0]==y)) { a[0]=x; pointstatus=1; allpoints++; } } elseif(c[0]==1) { if((b[0]+1)==y&&(a[0]==x)) { b[0]=y; pointstatus=1; allpoints++; } } elseif(c[0]==2) { if((a[0]-1)==x&&(b[0]==y)) { a[0]=x; pointstatus=1; allpoints++; } } else { if((a[0])==x&&(b[0]-1)==y) { b[0]=y; pointstatus=1; allpoints++; } }Draw_Point(x,y,0); //畫上一個點(diǎn),即產(chǎn)生食物 if(pointstatus) { beep=0; Delayms(10); beep=1; pointstatus=0; //Draw_Point(x,y,1);//去除這個點(diǎn) //Delayms(5000); //設(shè)置斷點(diǎn),用于測試 x=x+16; y=y+18; if(x>127) { x=1; } if(y>63) { y=0; }}}/*------------------------------------------------------------------------------------------------------------ 判斷是否撞邊框-------------------------------------------------------------------------------------------------------------*/voidBump_Wall(){ if((a[0]>127||a[0]<0)||(b[0]<0||b[0]>63)) { TR0=0; ClrScreen(); //Disp_HZ(0x98,hang4,8); LCD_PutString(0,3,"撞墻死亡了"); Write_Score(3,7,allpoints); }}/*------------------------------------------------------------------------------------------------------------ 判斷是否自身相撞-------------------------------------------------------------------------------------------------------------*/voidSelf_Bump(){ uchari,j; for(j=flag-1;j>=3;j--) { if(c[j]==0) { for(i=a[j+1];i<a[j];i++) { if((a[0]==i)&&(b[0]==b[j])) { TR0=0; ClrScreen(); //Disp_HZ(0x98,hang3,7); LCD_PutString(0,4,"死亡"); Write_Score(3,7,allpoints); } } } elseif(c[j]==1) { for(i=b[j+1];i<b[j];i++) { if((a[0]==a[j])&&(b[0]==i)) { TR0=0; ClrScreen(); //Disp_HZ(0x98,hang3,7); LCD_PutString(0,4,"死亡"); Write_Score(3,7,allpoints); }
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025至2030年中國鋅合金分體鎖數(shù)據(jù)監(jiān)測研究報告
- 2025至2031年中國自動氣壓紙管開槽機(jī)行業(yè)投資前景及策略咨詢研究報告
- 2024自然人之間借款合同范本
- 2025年度城市綜合體場地合作經(jīng)營合同
- 2025年度文化產(chǎn)業(yè)園物業(yè)管理與文化活動策劃服務(wù)協(xié)議3篇
- 2024版教育機(jī)構(gòu)裝潢工程合同樣本
- 二零二四年度9A文智能家居系統(tǒng)定制開發(fā)合同
- 2024版環(huán)評工程服務(wù)合同范本大全
- 2025年度生態(tài)農(nóng)業(yè)用地承包種植合作合同規(guī)范文本3篇
- 二零二四年度BIM可視化展示與演示合同
- 離職分析報告
- 春節(jié)家庭用電安全提示
- 醫(yī)療糾紛預(yù)防和處理?xiàng)l例通用課件
- 廚邦醬油推廣方案
- 乳腺癌診療指南(2024年版)
- 高三數(shù)學(xué)寒假作業(yè)1
- 保險產(chǎn)品創(chuàng)新與市場定位培訓(xùn)課件
- (完整文本版)體檢報告單模版
- 1例左舌鱗癌手術(shù)患者的圍手術(shù)期護(hù)理體會
- (完整)100道兩位數(shù)加減兩位數(shù)口算題(難)
- 鋼結(jié)構(gòu)牛腿計算
評論
0/150
提交評論