时间:2021-05-19
本文实例讲述了基于C语言实现迷宫游戏的方法,代码备有较为详尽的注释,便于读者理解。通过该游戏代码可以很好的复习C语言的递归算法与流程控制等知识,相信对于学习游戏开发的朋友有一定的借鉴价值。
完整的实例代码如下:
#include <graphics.h>#include <stdlib.h>#include <stdio.h>#include <conio.h>#include <dos.h>#define N 20int oldmap[N][N];int yes=0;int way[100][2],wayn=0;void Init(void);void Close(void);void DrawPeople(int *x,int *y,int n);void PeopleFind(int (*x)[N]);void WayCopy(int (*x)[N],int (*y)[N]);int FindWay(int (*x)[N],int i,int j);void MapRand(int (*x)[N]);void PrMap(int (*x)[N]);void Result(void);void Find(void);void NotFind(void);void main(void){ int map[N][N]; char ch; clrscr(); printf("\n Please select hand(1) else auto\n"); scanf("%c",&ch); Init(); MapRand(map); PrMap(map); if(ch=='1') PeopleFind(map); else FindWay(map,1,1); Result(); Close();}void Init(void){ int gd=DETECT,gm; initgraph(&gd,&gm,"c:\\tc");}void DrawPeople(int *x,int *y,int n){ setfillstyle(SOLID_FILL,WHITE); bar(100+(*y)*15-6,50+(*x)*15-6,100+(*y)*15+6,50+(*x)*15+6); switch(n) { case 1: (*x)--;break; case 2: (*x)--;(*y)++;break ; case 3: (*y)++;break; case 4: (*x)++;(*y)++;break; case 5: (*x)++;break; case 6: (*x)++;(*y)--;break; case 7: (*y)--;break; case 8: (*x)--;(*y)--;break; } setfillstyle(SOLID_FILL,RED); bar(100+(*y)*15-6,50+(*x)*15-6,100+(*y)*15+6,50+(*x)*15+6);}void PeopleFind(int (*map)[N]){ int x,y; char c=0; x=y=1; setcolor(11); line(500,200,550,200); outtextxy(570,197,"d"); line(500,200,450,200); outtextxy(430,197,"a"); line(500,200,500,150); outtextxy(497,130,"w"); line(500,200,500,250); outtextxy(497,270,"x"); line(500,200,450,150); outtextxy(445,130,"q"); line(500,200,550,150); outtextxy(550,130,"e"); line(500,200,450,250); outtextxy(445,270,"z"); line(500,200,550,250); outtextxy(550,270,"c"); setcolor(YELLOW); outtextxy(420,290,"Press 'Enter' to end"); setfillstyle(SOLID_FILL,RED); bar(100+y*15-6,50+x*15-6,100+y*15+6,50+x*15+6); while(c!=13) { c=getch(); if(c=='w'&&map[x-1][y]!=1) DrawPeople(&x,&y,1); else if(c=='e'&&map[x-1][y+1]!=1) DrawPeople(&x,&y,2); else if(c=='d'&&map[x][y+1]!=1) DrawPeople(&x,&y,3); else if(c=='c'&&map[x+1][y+1]!=1) DrawPeople(&x,&y,4); else if(c=='x'&&map[x+1][y]!=1) DrawPeople(&x,&y,5); else if(c=='z'&&map[x+1][y-1]!=1) DrawPeople(&x,&y,6); else if(c=='a'&&map[x][y-1]!=1) DrawPeople(&x,&y,7); else if(c=='q'&&map[x-1][y-1]!=1) DrawPeople(&x,&y,8); } setfillstyle(SOLID_FILL,WHITE); bar(100+y*15-6,50+x*15-6,100+y*15+6,50+x*15+6); if(x==N-2&&y==N-2) yes=1; }void WayCopy(int (*oldmap)[N],int (*map)[N]){ int i,j; for(i=0;i<N;i++) for(j=0;j<N;j++) oldmap[i][j]=map[i][j];}int FindWay(int (*map)[N],int i,int j){ if(i==N-2&&j==N-2) { yes=1; return; } map[i][j]=1; WayCopy(oldmap,map); if(oldmap[i+1][j+1]==0&&!yes) { FindWay(oldmap,i+1,j+1); if(yes) { way[wayn][0]=i; way[wayn++][1]=j; return; } } WayCopy(oldmap,map); if(oldmap[i+1][j]==0&&!yes) { FindWay(oldmap,i+1,j); if(yes) { way[wayn][0]=i; way[wayn++][1]=j; return; } } WayCopy(oldmap,map); if(oldmap[i][j+1]==0&&!yes) { FindWay(oldmap,i,j+1); if(yes) { way[wayn][0]=i; way[wayn++][1]=j; return; } } WayCopy(oldmap,map); if(oldmap[i-1][j]==0&&!yes) { FindWay(oldmap,i-1,j); if(yes) { way[wayn][0]=i; way[wayn++][1]=j; return; } } WayCopy(oldmap,map); if(oldmap[i-1][j+1]==0&&!yes) { FindWay(oldmap,i-1,j+1); if(yes) { way[wayn][0]=i; way[wayn++][1]=j; return; } } WayCopy(oldmap,map); if(oldmap[i+1][j-1]==0&&!yes) { FindWay(oldmap,i+1,j-1); if(yes) { way[wayn][0]=i; way[wayn++][1]=j; return; } } WayCopy(oldmap,map); if(oldmap[i][j-1]==0&&!yes) { FindWay(oldmap,i,j-1); if(yes) { way[wayn][0]=i; way[wayn++][1]=j; return; } } WayCopy(oldmap,map); if(oldmap[i-1][j-1]==0&&!yes) { FindWay(oldmap,i-1,j-1); if(yes) { way[wayn][0]=i; way[wayn++][1]=j; return; } } return;}void MapRand(int (*map)[N]){ int i,j; cleardevice(); randomize(); for(i=0;i<N;i++) { for(j=0;j<N;j++) { if(i==0||i==N-1||j==0||j==N-1) map[i][j]=1; else if(i==1&&j==1||i==N-2&&j==N-2) map[i][j]=0; else map[i][j]=random(2); } }}void PrMap(int (*map)[N]){ int i,j; for(i=0;i<N;i++) for(j=0;j<N;j++) if(map[i][j]==0) { setfillstyle(SOLID_FILL,WHITE); bar(100+j*15-6,50+i*15-6,100+j*15+6,50+i*15+6); } else { setfillstyle(SOLID_FILL,BLUE); bar(100+j*15-6,50+i*15-6,100+j*15+6,50+i*15+6); }}void Find(void){ int i; setfillstyle(SOLID_FILL,RED); wayn--; for(i=wayn;i>=0;i--) { bar(100+way[i][1]*15-6,50+way[i][0]*15-6,100+ way[i][1]*15+6,50+way[i][0]*15+6); sleep(1); } bar(100+(N-2)*15-6,50+(N-2)*15-6,100+ (N-2)*15+6,50+(N-2)*15+6); setcolor(GREEN); settextstyle(0,0,2); outtextxy(130,400,"Find a way!");}void NotFind(void){ setcolor(GREEN); settextstyle(0,0,2); outtextxy(130,400,"Not find a way!");}void Result(void){ if(yes) Find(); else NotFind(); getch();}void Close(void){ closegraph();}声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例讲述了基于C语言实现的迷宫算法。分享给大家供大家参考,具体如下:利用c语言实现迷宫算法,环境是vc++6.0.#include#include#incl
本文实例讲述了C语言实现简单的走迷宫游戏的方法,代码完整,便于读者理解。学数据结构时用“栈”写的一个走迷宫程序,实际上用到双向队列,方便
本文实例为大家分享了C语言实现24点游戏的具体代码,供大家参考,具体内容如下参考文章:C语言实现经典24点算法将算法实现改成C语言,并可在linux服务器上运行
本文以实例的形式讲述了基于C语言实现的贪吃蛇游戏代码,这是一个比较常见的游戏,代码备有比较详细的注释,对于读者理解有一定的帮助。贪吃蛇完整实现代码如下:#inc
本文实例为大家分享了C语言实现扫雷游戏的具体代码,供大家参考,具体内容如下功能设计扫雷大家应该都玩过,这是一个十分经典的游戏,今天我将给大家讲解如何用C语言实现