简单的猫捉老鼠游戏_第1页
简单的猫捉老鼠游戏_第2页
简单的猫捉老鼠游戏_第3页
简单的猫捉老鼠游戏_第4页
简单的猫捉老鼠游戏_第5页
已阅读5页,还剩6页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、简单的猫捉老鼠游戏/*这个程序是学数据结构后写的,里面涉及到了栈和队列的一些操作。开始本来只是要做迷宫求解的,但就看着电脑在里面运行,没有互动性也没什么意思,于是加入了由人控制的角色,让电脑控制的角色每走一步都计算一次当前两者的最短距离去追赶人控制的角色。这个程序画地图的部分是得于网上的一个迷宫程序,汉字部分也是用的别别人写好的子程序。其实这个程序还不是很完善,开始做好了一个,却不知那儿弄丢了,假期因为参加电子设计大赛,都玩单片机去了,这方面的也忘差不多了,希望有兴趣的朋友能改好了发给我.我的email:xiangyuan_122163 */*本程序在TURBOC2.0下编译通过,*.h文件可

2、拷在INCLUDE文件夹下.*/#include"stdio.h"#include"graphics.h"#include"conio.h"#include"mprinthz.h"#define N 10#define P 4unsigned *str1="猫捉老鼠!"unsigned *str2="嘿嘿!看你往哪跑!"/*定义迷宫数组*/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,

3、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;/*坐标结构*/typedef struct int xpos; int ypos;postype;/*队列结点结构*/typedef struct queuenode

4、postype seat;/*当前坐标*/ struct queuenode*next;/*指向后继结点*/ struct queuenode*pre;/*指向前驱结点*/queuenode,*link;static link front=NULL;static link rear=NULL;/*栈结点结构*/typedef struct stack_nodepostype sdata;struct stack_node*next;stack_list,*plink;static plink path=NULL;/*入栈操作*/push(postype e)plink new_node;new

5、_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 pop()postype e;plink top;top=path;if(path!=NULL)e.xpos=path->sdata.xpos;e.yp

6、os=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->seat.xpos=value.xpos;newnode->seat.ypos=value.ypos;newnode->next=NULL

7、;if(rear=NULL) front=newnode; rear=newnode; newnode->pre=NULL;else newnode->pre=front; rear->next=newnode; rear=newnode; /*删除队列结点*/postype dequeue(void) postype e; front=front->next; e.xpos=front->seat.xpos; e.ypos=front->seat.ypos; return e;postype nextpos(postype curpos,int v)pos

8、type next_;next_.xpos=curpos.xpos+div;next_.ypos=curpos.ypos+djv;return next_;/*标准延时*/void Delay(int clicks)unsigned int far *clock=(unsigned int far *)0x0000046CL;unsigned int now;now=*clock;while(abs(*clock-now)<clicks)/*画地图*/void picture (int mazeN)int i,j;setbkcolor(BLACK);for(i=0;i<N;i+)f

9、or (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;/*访问标志数组*/plink ptr;link p,qp;postype curpos;postype next,out,imgcurman;int found=0;int i,j,curmanx,curmany;init(

10、);picture(maze);/*画迷宫*/printhz(200,10,str1,2);curmanx=N-2;curmany=N-2;/*设置逃跑者初始位置坐标*/setfillstyle(1,YELLOW);bar(70+curmanx*20,40+curmany*20,88+curmanx*20,58+curmany*20);curpos.xpos=1;curpos.ypos=1;/*设置追赶者初始位置坐标*/while(1)/*游戏循环*/if(kbhit()/*检测按键*/key=bioskey(0);/*按键处理*/ if(key=0x4800)/*UP方向键*/imgcurm

11、an.xpos=curmanx; imgcurman.ypos=curmany;/*复制逃跑者坐标*/ if(mazeimgcurman.ypos-1imgcurman.xpos=0)/*当前位置上方为通道*/ /*上行一步*/ setfillstyle(1,WHITE); bar(70+curmanx*20,40+curmany*20,88+curmanx*20,58+curmany*20); curmany-; setfillstyle(1,YELLOW); bar(70+curmanx*20,40+curmany*20,88+curmanx*20,58+curmany*20); if(k

12、ey=0x5000)/*DOWN方向键*/imgcurman.xpos=curmanx; imgcurman.ypos=curmany;/*复制逃跑者坐标*/ if(mazeimgcurman.ypos+1imgcurman.xpos=0)/*当前位置下方为通道*/ /*下行一步*/ setfillstyle(1,WHITE); bar(70+curmanx*20,40+curmany*20,88+curmanx*20,58+curmany*20); curmany+; setfillstyle(1,YELLOW); bar(70+curmanx*20,40+curmany*20,88+cur

13、manx*20,58+curmany*20); if(key=0x4b00)/*LEFT方向键*/imgcurman.xpos=curmanx; imgcurman.ypos=curmany;/*复制逃跑者坐标*/ if(mazeimgcurman.yposimgcurman.xpos-1=0)/*当前位置左方为通道*/ /*左行一步*/ setfillstyle(1,WHITE); bar(70+curmanx*20,40+curmany*20,88+curmanx*20,58+curmany*20); curmanx-; setfillstyle(1,YELLOW); bar(70+cur

14、manx*20,40+curmany*20,88+curmanx*20,58+curmany*20); if(key=0x4d00)/*RIGHT方向键*/imgcurman.xpos=curmanx; imgcurman.ypos=curmany;/*复制逃跑者坐标*/ if(mazeimgcurman.yposimgcurman.xpos+1=0)/*当前位置右方为通道*/ /*右行一步*/ setfillstyle(1,WHITE); bar(70+curmanx*20,40+curmany*20,88+curmanx*20,58+curmany*20); curmanx+; setfi

15、llstyle(1,YELLOW); bar(70+curmanx*20,40+curmany*20,88+curmanx*20,58+curmany*20); while(bioskey(1) bioskey(0);/*清除键盘缓冲区*/for(i=1;i<=N;i+) for(j=1;j<=N;j+) visitedij=0;/*初始化访问标志*/ found=0;/*初始化追赶上标志*/enqueue(curpos);/*追赶者当前坐标入队列*/visitedcurpos.xposcurpos.ypos=1;/*当前访问标志置1*/while(!found)/*如没有追上循环

16、*/ 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=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.ypos*2

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论