簡單的貓捉老鼠游戲._第1頁
簡單的貓捉老鼠游戲._第2頁
簡單的貓捉老鼠游戲._第3頁
簡單的貓捉老鼠游戲._第4頁
簡單的貓捉老鼠游戲._第5頁
已閱讀5頁,還剩2頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

1、簡單的貓捉老鼠游戲.txt23讓我們揮起沉重的鐵錘吧!每一下都砸在最稚嫩的部位,當(dāng)青春逝去,那些部位將生出厚曬太陽的繭,最終成為堅實的石,支撐起我們不再年輕但一定美麗的生命。/*這個程序是學(xué)數(shù)據(jù)結(jié)構(gòu)后寫的,里面涉及到了棧和隊列的一些操作。開始本來只是要做迷宮求解的,但就看著電腦在里面運(yùn)行,沒有互動性也沒什么意思,于是加入了由人控制的角色,讓電腦控制的角色每走一步都計算一次當(dāng)前兩者的最短距離去追趕人控制的角色。這個程序畫地圖的部分是得于網(wǎng)上的一個迷宮程序,漢字部分也是用的別別人寫好的子程序。其實這個程序還不是很完善,開始做好了一個,卻不知那兒弄丟了,假期因為參加電子設(shè)計大賽,都玩單片機(jī)去了,這方

2、面的也忘差不多了,希望有興趣的朋友能改好了發(fā)給我.我的email:xiangyuan_122163 */*本程序在TURBOC2.0下編譯通過,*.h文件可拷在INCLUDE文件夾下.*/#include"stdio.h"#include"graphics.h"#include"conio.h"#include"mprinthz.h"#define N 10#define P 4unsigned *str1="貓捉老鼠!"unsigned *str2="嘿嘿!看你往哪跑!"/

3、*定義迷宮數(shù)組*/int mazeNN=1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,1,1,0,1,1,0,1,1,1,0,1,1,0,1,0,0,0,0,1,0,1,1,0,1,0,1,1,0,1,0,1,1,0,1,0,1,0,0,1,0,1,1,0,1,0,0,0,0,1,0,1,1,0,1,1,1,0,1,1,0,1,1,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1;int diP=0, 1, 0, -1 ;int djP=1, 0, -1, 0 ;static int key;static int step;/*坐標(biāo)結(jié)構(gòu)*

4、/typedef struct int xpos; int ypos;postype;/*隊列結(jié)點(diǎn)結(jié)構(gòu)*/typedef struct queuenode postype seat;/*當(dāng)前坐標(biāo)*/ struct queuenode*next;/*指向后繼結(jié)點(diǎn)*/ struct queuenode*pre;/*指向前驅(qū)結(jié)點(diǎn)*/queuenode,*link;static link front=NULL;static link rear=NULL;/*棧結(jié)點(diǎn)結(jié)構(gòu)*/typedef struct stack_nodepostype sdata;struct stack_node*next;stac

5、k_list,*plink;static plink path=NULL;/*入棧操作*/push(postype e)plink new_node;new_node=(plink)malloc(sizeof(stack_list);if(!new_node)printf("error:not enough memory!n");exit(0);new_node->sdata.xpos=e.xpos;new_node->sdata.ypos=e.ypos;new_node->next=path;path=new_node;/*出棧操作*/postype p

6、op()postype e;plink top;top=path;if(path!=NULL)e.xpos=path->sdata.xpos;e.ypos=path->sdata.ypos;path=path->next;free(top);return e; /*建立隊列*/void enqueue(postype value)link newnode;newnode=(link)malloc(sizeof(queuenode);if(newnode=NULL)printf("not enough memory!n");exit(0);newnode-&

7、gt;seat.xpos=value.xpos;newnode->seat.ypos=value.ypos;newnode->next=NULL;if(rear=NULL) front=newnode; rear=newnode; newnode->pre=NULL;else newnode->pre=front; rear->next=newnode; rear=newnode; /*刪除隊列結(jié)點(diǎn)*/postype dequeue(void) postype e; front=front->next; e.xpos=front->seat.xpos;

8、 e.ypos=front->seat.ypos; return e;postype nextpos(postype curpos,int v)postype next_;next_.xpos=curpos.xpos+div;next_.ypos=curpos.ypos+djv;return next_;/*標(biāo)準(zhǔn)延時*/void Delay(int clicks)unsigned int far *clock=(unsigned int far *)0x0000046CL;unsigned int now;now=*clock;while(abs(*clock-now)<click

9、s)/*畫地圖*/void picture (int mazeN)int i,j;setbkcolor(BLACK);for(i=0;i<N;i+)for (j=0;j<N;j+) if(mazeij=1) setfillstyle(1,LIGHTBLUE); bar(70+j*20,40+i*20,88+j*20,58+i*20); else setfillstyle(1,WHITE); bar(70+j*20,40+i*20,88+j*20,58+i*20); main()int visitedN+1N+1;/*訪問標(biāo)志數(shù)組*/plink ptr;link p,qp;posty

10、pe curpos;postype next,out,imgcurman;int found=0;int i,j,curmanx,curmany;init();picture(maze);/*畫迷宮*/printhz(200,10,str1,2);curmanx=N-2;curmany=N-2;/*設(shè)置逃跑者初始位置坐標(biāo)*/setfillstyle(1,YELLOW);bar(70+curmanx*20,40+curmany*20,88+curmanx*20,58+curmany*20);curpos.xpos=1;curpos.ypos=1;/*設(shè)置追趕者初始位置坐標(biāo)*/while(1)/*

11、游戲循環(huán)*/if(kbhit()/*檢測按鍵*/key=bioskey(0);/*按鍵處理*/ if(key=0x4800)/*UP方向鍵*/imgcurman.xpos=curmanx; imgcurman.ypos=curmany;/*復(fù)制逃跑者坐標(biāo)*/ if(mazeimgcurman.ypos-1imgcurman.xpos=0)/*當(dāng)前位置上方為通道*/ /*上行一步*/ setfillstyle(1,WHITE); bar(70+curmanx*20,40+curmany*20,88+curmanx*20,58+curmany*20); curmany-; setfillstyle

12、(1,YELLOW); bar(70+curmanx*20,40+curmany*20,88+curmanx*20,58+curmany*20); if(key=0x5000)/*DOWN方向鍵*/imgcurman.xpos=curmanx; imgcurman.ypos=curmany;/*復(fù)制逃跑者坐標(biāo)*/ if(mazeimgcurman.ypos+1imgcurman.xpos=0)/*當(dāng)前位置下方為通道*/ /*下行一步*/ setfillstyle(1,WHITE); bar(70+curmanx*20,40+curmany*20,88+curmanx*20,58+curmany

13、*20); curmany+; setfillstyle(1,YELLOW); bar(70+curmanx*20,40+curmany*20,88+curmanx*20,58+curmany*20); if(key=0x4b00)/*LEFT方向鍵*/imgcurman.xpos=curmanx; imgcurman.ypos=curmany;/*復(fù)制逃跑者坐標(biāo)*/ if(mazeimgcurman.yposimgcurman.xpos-1=0)/*當(dāng)前位置左方為通道*/ /*左行一步*/ setfillstyle(1,WHITE); bar(70+curmanx*20,40+curmany

14、*20,88+curmanx*20,58+curmany*20); curmanx-; setfillstyle(1,YELLOW); bar(70+curmanx*20,40+curmany*20,88+curmanx*20,58+curmany*20); if(key=0x4d00)/*RIGHT方向鍵*/imgcurman.xpos=curmanx; imgcurman.ypos=curmany;/*復(fù)制逃跑者坐標(biāo)*/ if(mazeimgcurman.yposimgcurman.xpos+1=0)/*當(dāng)前位置右方為通道*/ /*右行一步*/ setfillstyle(1,WHITE);

15、 bar(70+curmanx*20,40+curmany*20,88+curmanx*20,58+curmany*20); curmanx+; setfillstyle(1,YELLOW); bar(70+curmanx*20,40+curmany*20,88+curmanx*20,58+curmany*20); while(bioskey(1) bioskey(0);/*清除鍵盤緩沖區(qū)*/for(i=1;i<=N;i+) for(j=1;j<=N;j+) visitedij=0;/*初始化訪問標(biāo)志*/ found=0;/*初始化追趕上標(biāo)志*/enqueue(curpos);/*

16、追趕者當(dāng)前坐標(biāo)入隊列*/visitedcurpos.xposcurpos.ypos=1;/*當(dāng)前訪問標(biāo)志置1*/while(!found)/*如沒有追上循環(huán)*/ for(i=0;i<P;i+) next=nextpos(curpos,i);if(mazenext.xposnext.ypos=0)&&(visitednext.xposnext.ypos=0) visitednext.xposnext.ypos=1; if(next.xpos=curmanx&&next.ypos=curmany) found=1; enqueue(next); curpos=

17、dequeue(); p=rear; step=2; while(p) push(p->seat); p=p->pre; while(rear) p=rear; rear=rear->next; free(p); while(path&&step) out=pop(); setfillstyle(1,GREEN); bar(70+out.xpos*20,40+out.ypos*20,88+out.xpos*20,58+out.ypos*20); Delay(2); setfillstyle(1,WHITE); bar(70+out.xpos*20,40+out.yp

溫馨提示

  • 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)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論