oenglD迷宫 C实现 源代码

合集下载

迷宫(direction)C语言代码

迷宫(direction)C语言代码
/* 其中 1<=x1,x2<=M-2 , 1<=y1,y2<=N-2 */
voidmazePath(intmaze[][N],intdirection[][2],intx1,inty1,intx2,inty2) {
inti, j, k, g, h;
PSeqStackst;
DataTypeelement;
1,1,1,1,1,1,1,1,1,1,1
};
mazePath(maze,direction,1,1,6,9);
getchar();
return 0;
}
#include<stdio.h>
#include<conio.h>
intmigong[10][10]= //设置迷宫,最外围1为墙 里边0为可走路径 1为障碍
find=1;
}
if(find==1){ //判断是否找得到
lj[top].d=d;
top++;
lj[top].x=x;
lj[top].y=y;
d=-1;find=0; //重新调整方向
migong[x][y]=-1;}
else{
migong[lj[top].x][lj[top].y]=0;
top--;d=lj[top].d; //找不到的话退栈
case1:x=lj[top].x; y=lj[top].y+1;break;//方向为右
case2:x=lj[top].x+1; y=lj[top].y; break;//方向为下
case3:x=lj[top].x; y=lj[top].y-1;}//方向为左
if(migong[x][y]==0)

迷宫游戏C语言代码讲解

迷宫游戏C语言代码讲解
HANDLE WINAPI GetStdHandle( //获得输入、输出/错误的屏幕缓冲区的句柄。__in DWORD nStdHandle );
而其参数nStdHandle的值可以为下面几种类型的一种:STD_INPUT_HANDLE标准输入的句柄STD_OUTPUT_HANDLE标准输出的句柄STD_ERROR_HANDLE标准错误的句柄
void game() //游戏相关操作
先不要管main函数里的代码,我们先逐个分析一下函数
先来看看gotoxy()接受两个参数,代码如下
void gotoxy(int x,int y) //移动坐标
{
COORD coord;
coord.X=x;
coord.Y=y;
SetConsoleCursorPosition( GetStdHandle( STD_OUTPUT_HANDLE ), coord );
if(c==Esc)
{
gotoxy(0,24);
5
回复
1楼
2011-12-07 15:46
举报|
我也说一句
未来未来
追忆曾经_
团子家族
10
break;
}
switch(c)
{
case Up: //向上走
if(map[x-1][y]!=Wall)
{
paint(x,y);
x--;
}
break;
case Down: //向下走
map[i][Width+1]=Wall;
}
for(j=0;j<=Width+1;j++) //边界处理
{
map[0][j]=Wall;

c语言迷宫问题代码实现

c语言迷宫问题代码实现
}SEAT;
structStackList
{
SEAT stack[MAXSIZE];
inttop;
}*Stack;
intEmptyStack(StackList*Stack)//判断是否为空栈
{
if(Stack->top==0)
return 0;
else
return 1;
}
intMove[4][2]={{0,1},{1,0},{0,-1},{-1,0}};//分别表示向东、西、南、北需要加上的坐标
CurSeat=temp;
find =1;
}
}
}
}
}while(EmptyStack(Stack));
return false;
}
voidPrintStack(StackList*Stack)//输出路线
{
if(Stack->top==0)
printf("There is no route can be out of the maze\n");
Mark(CurSeat);
if(CurSeat.x==end.x+1&&CurSeat.y==end.y+1)//如果找到出口,返回
{
return true;
}
else
{
intfind=0;
while(CurSeat.di<3&&find==0)//找下一个结点的方向
{
CurSeat.di++;
SEAT temp;
scanf("%d",&n);
printf("Please enter the labyrinth of the coordinates of the wall unit(0<=row,column):\n");

用c语言实现迷宫求解完美源代码

用c语言实现迷宫求解完美源代码

优先队列:用于存储待扩展节点,按照 f(n)值从小到大排序
A*搜索算法的C语言实现
算法流程:包括初始化、搜索、更新父节点等步骤 数据结构:使用优先队列来存储待搜索节点和已访问节点 实现细节:包括如何计算启发式函数、如何选择下一个节点等 性能优化:可以采用多线程、缓存等技术来提高算法的效率
A*搜索算法在迷宫求解中的应用
C语言实现A*搜 索算法
A*搜索算法的基本原理
定义:A*搜索算法是一种启发式搜索 算法,结合了最佳优先搜索和Dijkstra 算法的优点
基本思想:使用启发函数来评估节点的 重要性,优先选择最有希望的节点进行 扩展,从而有效地缩小搜索范围
关键参数:g(n):从起点经过节点n的 实际代价;h(n):从n到目标的估计代 价(启发式函数);f(n)=g(n)+h(n)
最短路径搜索
优化技巧:为了 提高搜索效率和 精度,可以采用 一些优化技巧, 如限制搜索范围、 使用优先队列等
C语言实现 Dijkstra算法
Dijkstra算法的基本原理
Dijkstra算法是一种用于求解最短路径问题的贪心算法 该算法通过不断选择当前最短路径的节点来逼近最短路径 Dijkstra算法适用于带权重的图,其中权重表示节点之间的距离 Dijkstra算法的时间复杂度为O((V+E)logV),其中V是节点数,E是边数
算法复杂度分析:时间复杂 度和空间复杂度分析
感谢您的观看
汇报人:XX
迷宫求解算法的C语言实现流程
初始化迷宫和路径
定义四个方向的移动方向
遍历迷宫,找到起点和终点
使用深度优先搜索或广度优先 搜索算法求解路径
C语言实现深度 优先搜索算法
深度优先搜索算法的基本原理

c语言走迷宫代码

c语言走迷宫代码

c语言走迷宫代码以下是一个简单的 C 语言走迷宫代码示例,使用了递归算法: ```c#include <stdio.h>#include <stdlib.h>int main() {int size, row, col, choice, i, j;maze[0][0] = "*";maze[1][0] = "O";maze[0][1] = "O";maze[1][1] = "*";size = 2;row = 2;col = 2;while (1) {printf("Enter row: ");scanf("%d", &row);printf("Enter col: ");scanf("%d", &col);if (row < 0 || row > size || col < 0 || col > size) { printf("Invalid row or col. Try again.");}if (maze[row][col] != "*") {printf("Current cell is free. Try again. ");continue;}maze[row][col] = "X";printf("Enter a choice (1-6): ");scanf("%d", &choice);switch (choice) {case 1:row++;col = 0;break;case 2:row--;col = 0;break;case 3:col++;break;col--;break;case 5:row--;col = 1;break;case 6:row++;col = 1;break;default:printf("Invalid choice. Try again. ");continue;}}printf(" maze: ");for (i = 0; i < size * size; i++) { for (j = 0; j < size; j++) {if (maze[i][j] == "*")printf("*");elseprintf("%c", maze[i][j]);}printf("");}return 0;}```该程序首先初始化了一个 2x2 的迷宫,其中 `maze[0][0]` 和`maze[1][0]` 分别标记为 `"O"` 和 `"*"`,其他地方都为空。

C语言迷宫源代码

C语言迷宫源代码

#include <graphics.h>#include <stdlib.h>#include <bios.h>/*定义几个功能按键*/#define ESC 0x11b /*强行退出游戏*/#define UP 0x4800 /*上下左右四个按键移动小人*/#define DOWN 0x5000#define LEFT 0x4b00#define RIGHT 0x4d00int a[50][50]={0}; /* 存放地图信息的数组0:不可走的障碍物1:可以走的路*/ int qdx=1,qdy=1,zdx=20,zdy=20; /* 起点和终点的坐标*/int renx,reny; /* 小人坐标*/int d=20; /* 小格子的间距*/int kk=0,rr=2;main(){int i,j,m=30,n=16,d=20,k;int gdriver = DETECT, gmode;randtu(200);renx=qdx,reny=qdy;registerbgidriver(gdriver);initgraph(&gdriver, &gmode, "c:\\turboc2");redraw();while(1) /* 反复从键盘获得程序需要的按键*/{if(bioskey(1)) /* 判断是否有按键*/{k=bioskey(0); /* 将按键存入变量k */switch(k) /* 对按键进行分情况处理*/{case ESC: /* ESC键退出*/printf("%d times\n",kk);exit(0); break;case UP: /* UP键向上移动光标*/if(a[renx][reny-1]==1){reny-=1;redraw();}break;case DOWN: /* DOWN键向下移动光标*/if(a[renx][reny+1]==1){reny+=1;redraw();}break;case LEFT: /* LEFT键向左移动光标*/if(a[renx-1][reny]==1){renx-=1;redraw();}break;case RIGHT: /* RIGHT键向右移动光标*/if(a[renx+1][reny]==1){renx+=1;redraw();}break;}}}getch();closegraph();}/*redraw重画函数在用户有操作后,重画游戏画面*/redraw(){int i,j;for(i=qdx;i<=zdx;i++)for(j=qdy;j<=zdy;j++){if(i<renx+rr && i>renx-rr && j<reny+rr && j>reny-rr)if(a[i][j]==0) geziza(i*d,j*d);else gezilu(i*d,j*d);else gezibk(i*d,j*d);}drawren(renx*d,reny*d);}/*随机地图(randlei)函数用于随机生成地图*/randtu(int num){int i,j,xx,yy,sum,t,m,n;srand(time(NULL));loop: sum=0;m=zdy-qdy+1;n=zdx-qdx+1;a[qdx][qdy]=1;a[zdx][zdy]=1;do{t=random(m*n);xx=t/m+1;yy=t%m+1;if(a[xx][yy]==0){a[xx][yy]=1;sum++;}}while(sum<num);if(ok()) return;for(i=0;i<50;i++)for(j=0;j<50;j++)a[i][j]=0;kk++;goto loop;}ok(){ int b[50][50]={0};b[qdx][qdy]=1;tansuo(qdx,qdy,b);return(b[zdx][zdy]);}tansuo(int x,int y,int *b[50][50]) /* 如果当前格子为空白无雷情况,向周围探索相类似的情况,并打开周围的数字*/{if(a[x][y-1]==1 && b[x][y-1]==0){b[x][y-1]=1;tansuo(x,y-1,b);}if(a[x+1][y]==1 && b[x+1][y]==0){b[x+1][y]=1;tansuo(x+1,y,b);}if(a[x-1][y]==1 && b[x-1][y]==0){b[x-1][y]=1;tansuo(x-1,y,b);}if(a[x][y+1]==1 && b[x][y+1]==0){b[x][y+1]=1;tansuo(x,y+1,b);}}/*绘制障碍物小格子(geziza)int x : 格子左上角点横坐标int y :格子左上角点纵坐标*/geziza(int x,int y){int i;setcolor(8);rectangle(x,y,x+d,y+d);setcolor(RED);setfillstyle(2,RED);bar(x+1,y+1,x+d-1,y+d-1);/* 设置深灰色为格子边框*/ }/*绘制障碍物小格子(geziza)int x : 格子左上角点横坐标int y :格子左上角点纵坐标*/gezibk(int x,int y){int i;setcolor(8);rectangle(x,y,x+d,y+d);setcolor(BLUE);setfillstyle(2,BLUE);bar(x+1,y+1,x+d-1,y+d-1);/* 设置深灰色为格子边框*/}gezilu(int x,int y){int i;setcolor(8); /* 设置深灰色为格子边框*/ rectangle(x,y,x+d,y+d);setcolor(BLUE);setfillstyle(8,BLUE);bar(x+1,y+1,x+d-1,y+d-1);}/*画小人(drawren)函数用于绘制给定坐标位置的小人小人画在格子的正中心格子背景色为红色int x : 所在格子左上角点横坐标int y :所在格子左上角点纵坐标*/drawren(int x,int y){int i;setcolor(8); /* 设置深灰色为格子边框*/ rectangle(x,y,x+d,y+d);setcolor(YELLOW);for(i=1;i<d/2-3;i++)circle(x+d/2,y+d/2,i);}/*over(over)函数用于判断游戏是否结束*/over(){}。

c语言版 迷宫 完整版代码

c语言版 迷宫 完整版代码
xtxy(550,130,"e");
line(500,200,450,250);
outtextxy(445,270,"z");
line(500,200,550,250);
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); }
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); /*左下*/
WayCopy(oldmap,map);
if(oldmap[i+1][j]==0&&!yes)/*判断下方是否可以走,如果标志yes已经是1也不用找下去了*/
{ FindWay(oldmap,i+1,j);
{ FindWay(oldmap,i,j+1);
if(yes) { way[wayn][0]=i; way[wayn++][1]=j; return; } }
WayCopy(oldmap,map);
#include <graphics.h>

迷宫游戏C语言小游戏源代码

迷宫游戏C语言小游戏源代码
void FootPri nt(PosType curpos);
PosType NextPos(PosType seat,i nt di);
Status MazePath(PosType start,PosType end);
void CreatMaze(void)
/* Forቤተ መጻሕፍቲ ባይዱ the maze. */
case ''''''''''''''''''''''''''''''''2'''''''''''''''''''''''''''''''':Maze[j][k]=2・break・
case ''''''''''''''''''''''''''''''''3'''''''''''''''''''''''''''''''':Maze[j][k]=1・

C语言自动生成查找迷宫最短路径的代码

C语言自动生成查找迷宫最短路径的代码

C语言自动生成查找迷宫最短路径的代码#include#include#include#include#includeusing namespace std;#define OVERFLOW 0#define OK 1#define ERROE 0#define TRUE 1#define FALSE 0#define SIZE 102//迷宫的最大范围typedef int Status;typedef struct{int x;int y;}PosType;//坐标位置typedef struct {PosType seat; //通道块在迷宫中的"坐标位置"int di; //从上一通道块走向此通道块的"方向"}SElemType;void Random(int (*mg)[SIZE],int size,PosType start,PosType end);/*随机生成迷宫的函数/*为了能够让尽量能通过,将能通过的块和不能通过的块数量比大致为3:1*/Status Pass(PosType e,int (*mg)[SIZE]);//当前块可否通过Status FootPrint(PosType e,int (*mg)[SIZE]);//留下通过的足迹PosType NextPos(PosType e,int dir);//下一步Status Equal(PosType e1,PosType e2);//e1与e2的位置坐标是否相同Status MarkPath(PosType e,int (*mg)[SIZE],int di);//对最短可行路径上的“通道块”进行标记PosType FrontPos(PosType e,int dir);//寻找当前通道块的上一步的位置Status PathPrint(stack s,int (*mg)[SIZE]);//迷宫最短路径的标记Status PathClean(int (*mg)[SIZE],stack s);//路径清除Status MazePath(PosType start,PosType end,int (*mg)[SIZE],stack &s);/*迷宫函数/* 若迷宫maze中从入口start到出口end的通道,则求得一条存放在栈中/* 并返回TRUE;否则返回FALSE*/void PrintMaze(int (*mg)[SIZE],int size);//打印迷宫Status Check(char &choice);//确认输入正确int main(){stack s;int mg[SIZE][SIZE]={1},size;PosType start,end;char choice;system("mode con cols=220 lines=220");printf("\n==================迷宫最短路径游戏==================");printf("\n说明:■不能走的区域");printf("\n '空格'代表可通过的区域");printf("\n默认起点为左上角位置,默认终点为右下角位置\n");printf("\n================================ ============\n");printf("请输入迷宫边长(3~%d),系统将为你产生一个随机迷宫:",SIZE-2);scanf("%d",&size);while((size>SIZE-2)||(size<1)){printf("输入有误!\n");printf("请输入迷宫边长(3~%d),系统将为你产生一个随机迷宫:",SIZE-2);scanf("%d",&size);}size+=2;//补上外围getchar();//跳过'\n'start.x=1;start.y=1; //起点坐标end.x=size-2;end.y=size-2; //终点坐标Random(mg,size,start,end);PrintMaze(mg,size);while(!((choice=='Q')||(choice=='q'))){printf("是否使用该迷宫?(y/n)\n");Check(choice);if((choice=='Y')||(choice=='y')){PathClean(mg,s);}while((choice=='n')||(choice=='N')){while(!s.empty())s.pop();choice=' ';printf("请输入迷宫边长(3~%d),系统将为你产生一个随机迷宫:",SIZE-2);scanf("%d",&size);while((size>SIZE-2)||(size<1)){printf("输入有误!\n");printf("请输入迷宫边长(3~%d),系统将为你产生一个随机迷宫:",SIZE-2);scanf("%d",&size);}size+=2;//补上外围start.x=1;start.y=1;//起点坐标end.x=size-2;end.y=size-2; //终点坐标getchar();//跳过'\n'Random(mg,size,start,end);PrintMaze(mg,size);printf("是否使用该迷宫?(y/n)\n");Check(choice);}printf("是否人工选择起点和终点(y/n)?【默认:起点(1,1),终点(%d,%d)】\n",size-2,size-2);Check(choice);if((choice=='y')||(choice=='Y')){printf("请输入“起点”坐标(1~%d)用空格分隔:",size-2);scanf("%d %d",&start.x,&start.y);while(((start.x>size-2)||start.x<1)||((start.y>size-2)||(start.y<1))||!Pass(start,mg)){if(!Pass(start,mg)) printf("些位置不能为“起点”!\n");else printf("输入有误!\n");printf("请输入“起点”坐标(1~%d)用空格分隔:",size-2);scanf("%d %d",&start.x,&start.y);}printf("请输入“终点”坐标(1~%d)用空格分隔:",size-2);scanf("%d %d",&end.x,&end.y);while(((end.x>size-2)||end.x<1)||((end.y>size-2)||(end.y<1))||!Pass(end,mg)||Equal(start,end)){if(!Pass(end,mg)) printf("些位置不能为“终点”!\n");else if(Equal(start,end)) printf("该位置已为起点!\n");else printf("输入有误!\n");printf("请输入“终点”坐标(1~%d)用空格分隔:",size-2);scanf("%d %d",&end.x,&end.y);}getchar();//跳过'\n'}MazePath(start,end,mg,s);PrintMaze(mg,size);printf("退出游戏请输入\"Q\"否则继续游戏!\n");choice=getchar();getchar();//跳过'\n'}printf("\n==========程序退出,感谢使用!==========\n");return 0;}void Random(int (*mg)[SIZE],int size,PosType start,PosType end){int i,j,k;srand(time(NULL));for(j=0;j<size;j++)mg[0][j]=mg[size-1][j]=1; /*设置迷宫外围"不可走",保证只有一个出口和入口*/for(i=1;i<size-1;i++)mg[i][0]=mg[i][size-1]=1; /*设置迷宫外围"不可走",保证只有一个出口和入口*/for(i=1;i<size-1;i++)for(j=1;j<size-1;j++){k=rand()%4; //随机生成0、1、2、4三个数if(k)mg[i][j]=0;else{mg[i][j]=1;}//else}mg[start.y][start.x]=0;mg[end.y][end.x]=0; //将入口、出口设置为"0"即可通过}Status Pass(PosType e,int (*mg)[SIZE]){if (mg[e.y][e.x]==0) //0时可以通过return OK; // 如果当前位置是可以通过,返回1 return OVERFLOW; // 其它情况返回0}Status FootPrint(PosType e,int (*mg)[SIZE]){mg[e.y][e.x]=7;return OK;}PosType NextPos(PosType e,int dir){PosType E;switch(dir){case 1:E.x=e.x+1; //向右E.y=e.y;break;case 2:E.x=e.x; //向下E.y=e.y+1;break;case 3:E.x=e.x-1; //向左E.y=e.y;break;case 4:E.x=e.x; //向上E.y=</size-1;j++){</size-1;i++)</size-1;i++)</size;j++)e.y-1;break;}return E;}Status Equal(PosType e1,PosType e2){if((e1.x==e2.x)&&(e1.y==e2.y))return TRUE;return FALSE;}Status MarkPath(PosType e,int (*mg)[SIZE],int di) {switch(di){case 1://向右mg[e.y][e.x]=11;break;case 2://向下mg[e.y][e.x]=12;break;case 3://向左mg[e.y][e.x]=13;break;case 4://向上mg[e.y][e.x]=14;break;}return OK;}PosType FrontPos(PosType e,int dir) {PosType E;switch(dir){case 1:E.x=e.x-1; //向左E.y=e.y;break;case 2:E.x=e.x; //向上E.y=e.y-1;break;case 3:E.x=e.x+1; //向右E.y=e.y;break;case 4:E.x=e.x; //向下E.y=e.y+1;break;}return E;}Status PathPrint(stack s,int (*mg)[SIZE]) {SElemType e,front,tail;int di;e=s.top();tail=e;s.pop();MarkPath(e.seat,mg,1);while(!s.empty()){front=s.top();s.pop();if(Equal(front.seat,FrontPos(e.seat,e.di))) {di=e.di;e=front;MarkPath(e.seat,mg,di);}}mg[tail.seat.y][tail.seat.x]=20;mg[e.seat.y][e.seat.x]=10;return OK;Status PathClean(int (*mg)[SIZE],stack s){SElemType e;while(!s.empty()){e=s.top();s.pop();mg[e.seat.y][e.seat.x]=0;}return OK;}Status MazePath(PosType start,PosType end,int (*mg)[SIZE],stack &s){queue q;SElemType e;int di=0;e.di=di;e.seat=start;// 设定"当前位置"为"入口位置"q.push(e);s.push(e);do{e=q.front();q.pop();for(di=1;di<=4;di++)e.seat=NextPos(e.seat,di);e.di=di;if(Pass(e.seat,mg)){q.push(e);s.push(e);FootPrint(e.seat,mg);if(Equal(e.seat,end)){PathPrint(s,mg);return TRUE;}}e.seat=FrontPos(e.seat,di);}}while(!q.empty());printf("\n\n囧 ! 不能到达终点!"); return FALSE;}void PrintMaze(int (*mg)[SIZE],int size) {int i,j;printf("\n");for(i=0;i<size;i++){for(j=0;j<size;j++){switch(mg[i][j]){case 0: case 7: printf(" "); break; case 1: printf("■"); break; case 10: printf("起"); break; case 20: printf("终"); break; case 11: printf("→"); break; case 12: printf("↓"); break; case 13: printf("←"); break; case 14: printf("↑"); break;}}printf("\n"); }printf("\n");}Status Check(char &choice){while(!(((choice=getchar())=='y')||(choice=='n')||(choice=='Y ')||(choice=='N')))//非正确输入{if(choice!='\n'){printf("请输入确定选择(y/n)\n");getchar();}}getchar();//跳过'\n'return OK;}</size;j++){</size;i++){。

简单的迷宫小游戏C语言程序源代码

简单的迷宫小游戏C语言程序源代码

简单的迷宫小游戏C语言程序源代码#include <stdio.h>#include <conio.h>#include <windows.h>#include <time.h>#define Height 31 //迷宫的高度,必须为奇数#define Width 25 //迷宫的宽度,必须为奇数#define Wall 1#define Road 0#define Start 2#define End 3#define Esc 5#define Up 1#define Down 2#define Left 3#define Right 4int map[Height+2][Width+2];void gotoxy(int x,int y) //移动坐标{COORD coord;coord.X=x;coord.Y=y;SetConsoleCursorPosition( GetStdHandle( STD_OUTPUT_HA NDLE ), coord );}void hidden()//隐藏光标{HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);CONSOLE_CURSOR_INFO cci;GetConsoleCursorInfo(hOut,&cci); cci.bVisible=0;//赋1为显示,赋0为隐藏SetConsoleCursorInfo(hOut,&cci);}void create(int x,int y) //随机生成迷宫{int c[4][2]={0,1,1,0,0,-1,-1,0}; //四个方向int i,j,t;//将方向打乱for(i=0;i<4;i++){j=rand()%4;t=c[i][0];c[i][0]=c[j][0];c[j][0]=t;t=c[i][1];c[i][1]=c[j][1];c[j][1]=t;}map[x][y]=Road;for(i=0;i<4;i++)if(map[x+2*c[i][0]][y+2*c[i][1]]==Wall) {map[x+c[i][0]][y+c[i][1]]=Road; create(x+2*c[i][0],y+2*c[i][1]);}}int get_key() //接收按键{char c;while(c=getch()){if(c==27) return Esc; //Escif(c!=-32)continue;c=getch();if(c==72) return Up; //上if(c==80) return Down; //下if(c==75) return Left; //左if(c==77) return Right; //右}return 0;}void paint(int x,int y) //画迷宫{gotoxy(2*y-2,x-1);switch(map[x][y]){case Start:printf("入");break; //画入口case End:printf("出");break; //画出口case Wall:printf("※");break; //画墙case Road:printf(" ");break; //画路}}void game(){int x=2,y=1; //玩家当前位置,刚开始在入口处int c; //用来接收按键while(1){gotoxy(2*y-2,x-1);printf("☆"); //画出玩家当前位置if(map[x][y]==End) //判断是否到达出口{gotoxy(30,24);printf("到达终点,按任意键结束"); getch();break;}c=get_key();if(c==Esc){gotoxy(0,24);break;}switch(c){case Up: //向上走if(map[x-1][y]!=Wall){paint(x,y);x--;}break;case Down: //向下走if(map[x+1][y]!=Wall){paint(x,y);x++;}break;case Left: //向左走if(map[x][y-1]!=Wall){paint(x,y);y--;}break;case Right: //向右走if(map[x][y+1]!=Wall){paint(x,y);y++;}break;}}}int main(){int i,j;srand((unsigned)time(NULL)); //初始化随即种子hidden(); //隐藏光标for(i=0;i<=Height+1;i++)for(j=0;j<=Width+1;j++)if(i==0||i==Height+1||j==0||j==Width+1) //初始化迷宫map[i][j]=Road;else map[i][j]=Wall;create(2*(rand()%(Height/2)+1),2*(rand()%(Width/2)+1)); //从随机一个点开始生成迷宫,该点行列都为偶数for(i=0;i<=Height+1;i++) //边界处理{map[i][0]=Wall;map[i][Width+1]=Wall;}for(j=0;j<=Width+1;j++) //边界处理{map[0][j]=Wall;map[Height+1][j]=Wall;}map[2][1]=Start; //给定入口map[Height-1][Width]=End; //给定出口for(i=1;i<=Height;i++)for(j=1;j<=Width;j++) //画出迷宫paint(i,j);game(); //开始游戏getch();return 0;}。

用c语言实现迷宫求解完美源代码

用c语言实现迷宫求解完美源代码

用c语言实现迷宫求解完美源代码#include#include#include#define TRUE 1#define FALSE 0#define OK 1#define ERROR 0#define OVERFLOW -2#define UNDERFLOW -2typedef int Status;//-----栈开始-----typedef struct{//迷宫中r行c列的位置int r;int c;}PostType;//坐标位置类型typedef struct{int ord;// 当前位置在路径上的序号PostType seat;// 当前坐标int di;// 从此通块走向下一通块的“方向”}SElemType;// 栈的元素类型//定义链式栈的存储结构struct LNode{SElemType data;//数据域struct LNode *next;//指针域};struct LStack{struct LNode *top;//栈顶指针};Status InitStack(LStack &s)//操作结果:构造一个空栈S {struct LNode *p;p=(LNode *)malloc(sizeof(LNode));if(!p){printf("分配失败,退出程序");exit(ERROR);}s.top=p;p->next=NULL;return OK;}Status StackEmpty(LStack s)//若栈s为空栈,则返回TRUE,否则FALSE{if(s.top->next==NULL) return TRUE;return FALSE;}Status Push(LStack &s,SElemType e)//插入元素e成为新的栈顶元素{struct LNode *p;p=(LNode *)malloc(sizeof(LNode));if(!p) exit(OVERFLOW);s.top->data=e;p->next=s.top;s.top=p;return OK;}Status Pop(LStack &s,SElemType &e)//删除s的栈顶元素,并且用e返回其值{struct LNode *p;if(!(s.top->next)) exit(UNDERFLOW);p=s.top;s.top=p->next;e=s.top->data;free(p);return OK;}Status DestroyStack(LStack &s)//操作结果:栈s被销毁{struct LNode *p;p=s.top;while(p){s.top=p->next;free(p);p=s.top;}return OK;}//-----栈结束------//-----迷宫开始-------#define MAXLEN 10// 迷宫包括外墙最大行列数typedef struct{int r;int c;char adr[MAXLEN][MAXLEN];// 可取' ''*' '@' '#'}MazeType;// 迷宫类型Status InitMaze(MazeType&maze){// 初始化迷宫,成功返回TRUE,否则返回FALSE int m,n,i,j;printf("输入迷宫行数和列数(包括了外墙): ");scanf("%d%d",&maze.r,&maze.c); // 输入迷宫行数和列数for(i=0;i<=maze.c+1;i++){// 迷宫行外墙maze.adr[0][i]='#';maze.adr[maze.r+1][i]='#';}//forfor(i=0;i<=maze.r+1;i++){// 迷宫列外墙maze.adr[i][0]='#';maze.adr[i][maze.c+1]='#';}for(i=1;i<=maze.r;i++)for(j=1;j<=maze.c;j++)maze.adr[i][j]=' ';// 初始化迷宫printf("输入障碍的坐标((-1 -1)结束): ");scanf("%d%d",&m,&n);// 接收障碍的坐标while(m!=-1){if(m>maze.r || n>maze.c)// 越界exit(ERROR);maze.adr[m][n]='#';// 迷宫障碍用#标记printf("输入障碍的坐标((-1,-1)结束): ");scanf("%d%d",&m,&n);}//whilereturn OK;}//InitMazeStatus Pass(MazeType maze,PostType curpos){// 当前位置可同则返回TURE,否则返回FALSEif(maze.adr[curpos.r][curpos.c]==' ')// 可通return TRUE;elsereturn FALSE;}//PassStatus FootPrint(MazeType &maze,PostType curpos) {// 若走过并且可通,则返回TRUE,否则返回FALSE maze.adr[curpos.r][curpos.c]='*';//"*"表示可通return OK;}//FootPrintPostType NextPos(PostType &curpos,int i){// 指示并返回下一位置的坐标PostType cpos;cpos=curpos;switch(i){//1.2.3.4 分别表示东南西北方向case 1 : cpos.c+=1; break;case 2 : cpos.r+=1; break;case 3 : cpos.c-=1; break;case 4 : cpos.r-=1; break;default: exit(ERROR);}return cpos;}//NextposStatus MarkPrint(MazeType &maze,PostType curpos) {// 曾走过,但不是通路标记,并返回OKmaze.adr[curpos.r][curpos.c]='@';//"@" 表示曾走过但不通return OK;}//MarkPrintStatus MazePath(MazeType &maze,PostType start,PostType end){// 若迷宫maze存在通路,则求出一条同路放在栈中,并返回TRUE,否则返回FALSE struct LStack S;PostType curpos;int curstep;// 当前序号,1,2,3,4分别表示东南西北方向SElemType e;InitStack(S);curpos=start; //设置"当前位置"为"入口位置"curstep=1;// 探索第一位printf("以三元组形式表示迷宫路径:\n");do{if(Pass(maze,curpos)){// 当前位置可以通过FootPrint(maze,curpos);// 留下足迹e.ord=curstep;e.seat=curpos;e.di=1;printf("%d %d %d-->",e.seat.r,e.seat.c,e.di);Push(S,e);// 加入路径if(curpos.r==end.r&&curpos.c==end.c)if(!DestroyStack(S))// 销毁失败exit(OVERFLOW);elsereturn TRUE; // 到达出口else{curpos=NextPos(curpos,1);// 下一位置是当前位置的东邻curstep++;// 探索下一步}//else}//ifelse{// 当前位置不通时if(!StackEmpty(S)){Pop(S,e);while(e.di==4&& !StackEmpty(S)){MarkPrint(maze,e.seat);Pop(S,e);// 留下不能通过的标记,并退一步}//whileif(e.di < 4){e.di++;// 换一个方向探索Push(S,e);curpos=NextPos(e.seat,e.di);// 设定当前位置是该方向上的相邻printf("%d %d %d-->",e.seat.r,e.seat.c,e.di);}//if}//if}//else}while(!StackEmpty(S));if(!DestroyStack(S))// 销毁栈exit(OVERFLOW);elsereturn FALSE;}//MazePathvoid PrintMaze(MazeType &maze){// 将标记路径信息的迷宫输出到终端int i,j;printf("\n输出迷宫(*表示通路):\n\n");printf("");for(i=0;i<=maze.r+1;i++)// 打印列数名printf("%4d",i);printf("\n\n");for(i=0;i<=maze.r+1;i++){printf("%2d",i);// 打印行名for(j=0;j<=maze.c+1;j++)printf("%4c",maze.adr[i][j]);// 输出迷宫当前位置的标记printf("\n\n");}}//PrintMazeint main(){// 主函数MazeType maze;PostType start,end;char cmd;do{printf("-------创建迷宫--------\n");if(!InitMaze(maze)){printf("Initialization errors\n");exit(OVERFLOW);// 初始化失败}do{// 输入迷宫入口坐标printf("\n输入迷宫入口坐标: ");scanf("%d%d",&start.r,&start.c);if(start.r>maze.r ||start.c>maze.c){printf("\nBeyond the maze\n"); continue;}}while(start.r>maze.r ||start.c>maze.c);do{// 输入迷宫出口坐标printf("\n输入迷宫出口坐标: ");scanf("%d%d",&end.r,&end.c);if(end.r>maze.r ||end.c>maze.c){printf("\nBeyond the maze\n"); continue;}}while(end.r>maze.r ||end.c>maze.c);if(!MazePath(maze,start,end))//迷宫求解printf("\nNo path from entranceto exit!\n"); elsePrintMaze(maze);// 打印路径printf("\n需要继续创建新的迷宫吗?(y/n): "); scanf("%s",&cmd);}while(cmd=='y' || cmd=='Y');}。

opengl 3D迷宫 C 实现 源代码精编版

opengl 3D迷宫 C  实现 源代码精编版

#include "stdafx.h"#include <stdio.h>#include <stdlib.h>#include <GL/glut.h>#include <math.h>#include <iostream>using namespace std;void drawwalls(void);void drawtop(void);void drawball(void);#define IDM_APPLICATION_EXIT (101)#define IDM_APPLICATION_TEXTURE (102)#define IDM_APPLICATION_BANK (103)#define MAZE_HEIGHT (16)#define MAZE_WIDTH (16)#define STARTING_POINT_X (13.5f);#define STARTING_POINT_Y (1.5f);#define STARTING_HEADING (90.0f);float player_x = STARTING_POINT_X ;float player_y = STARTING_POINT_Y ;float player_h = STARTING_HEADING ; // player's headingfloat player_s = 0.0f; // forward speed of the playerfloat player_m = 1.0f; // speed multiplier of the playerfloat player_t = 0.0f; // player's turning (change in heading)float player_b = 0.0f; // viewpoint bank (roll)static float texcoordX=0.0f;int walllist=0;int mazelist=0;int balllist=0;int status=1;bool searchroute=false;bool keystate[4]={false,false,false,false};char mazedata[MAZE_HEIGHT][MAZE_WIDTH] = {{'H','H','H','H','H','H','H','H','H','H','H','H','H','H','H','H '},{'H',' ',' ',' ',' ',' ',' ',' ','H',' ',' ',' ',' ',' ',' ','H'}, {'H',' ','H',' ','H','H','H',' ','H',' ','H',' ',' ',' ',' ','H'}, {'H',' ','H','H',' ',' ','H',' ','H','H',' ','H',' ','H',' ','H'}, {'H',' ',' ',' ',' ',' ','H',' ',' ',' ',' ',' ',' ','H',' ','H'},{'H',' ','H','H','H','H','H','H','H','H',' ','H','H','H',' ','H'},{'H',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','H',' ',' ','H'}, {'H',' ','H','H','H','H','H',' ','H','H','H',' ','H','H','H','H'},{'H',' ','H',' ',' ',' ','H',' ',' ',' ','H',' ',' ',' ',' ','H'}, {'H',' ',' ',' ','H','H','H','H','H','H','H',' ',' ',' ',' ','H'}, {'H',' ','H',' ',' ',' ','H',' ',' ',' ','H',' ',' ','H',' ','H'}, {'H',' ','H','H','H','H','H',' ','H','H','H','H',' ','H',' ','H'},{'H',' ',' ',' ',' ',' ','H',' ',' ',' ',' ',' ',' ','H',' ','H'}, {'H',' ',' ','H','H',' ','H','H','H','H',' ','H','H','H',' ','H'}, {'H',' ',' ',' ','H',' ','H',' ',' ',' ',' ','H',' ',' ',' ','H'}, {'H','H','H','H','H','H','H','H','H','H','H','H','H','','H','H'},};void myinit(){glClearColor(0.5f, 0.5f, 0.5f, 0.0f);glColor3f(1.0,1.0,1.0);glEnable(GL_DEPTH_TEST);glEnable(GL_TEXTURE_2D);walllist=glGenLists(2);mazelist=walllist+1;balllist=walllist+2;glNewList(walllist,GL_COMPILE);drawwalls();glEndList();glNewList(mazelist,GL_COMPILE);drawtop();glEndList();glNewList(balllist,GL_COMPILE);drawball();glEndList();glMatrixMode(GL_PROJECTION);glLoadIdentity();gluPerspective(60.0, 1.0 , 0.1, 60.0);glMatrixMode(GL_MODELVIEW);glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);//ʹÎÆÀí²»±äÐÎ}bool wall(int x,int y) {return (x>=0 && y>=0 && x<MAZE_WIDTH && y<MAZE_HEIGHT && mazedata[y][x]!=' ');}bool onopen(int x,int y){if(wall(x,y)){return(mazedata[y][x]=='H');}}void closeit(int x,int y) {if(onopen(x,y)){mazedata[y][x]= 'X';}}bool neighbor(int x,int y,int w,int *nx,int *ny){ switch(w) {case 0:*nx = x-1; *ny=y; break;case 1:*nx = x; *ny=y+1; break;case 2:*nx = x+1; *ny=y; break;case 3:*nx = x; *ny=y-1; break;default:break;}return wall(*nx,*ny);}bool diagnal(int x,int y,int w,int *nx,int *ny){ switch(w) {case 0:*nx = x-1; *ny=y-1; break;case 1:*nx = x-1; *ny=y+1; break;case 2:*nx = x+1; *ny=y+1; break;case 3:*nx = x+1; *ny=y-1; break;default:break;}return wall(*nx,*ny);}void dw(int x,int y,int p) {int w=p;closeit(x,y);do{int x2=0;int y2=0;if(neighbor(x,y,w,&x2,&y2)){if(onopen(x2,y2)) {dw(x2,y2,(w+3)%4);}else {if((w+1)%4 ==p){return ;}}}else {float fx;float fy;if(diagnal(x,y,w,&x2,&y2) && onopen(x2,y2)) { dw(x2,y2,(w+2)%4);}texcoordX=(texcoordX<0.5)?1.0f:0.0f;fx = (float)x+((w==1||w==2)?1.0f:0.0f);fy = (float)y+((w==0||w==1)?1.0f:0.0f);glTexCoord2f(texcoordX,0.0f);glVertex3f(fx,fy,0.0f);glTexCoord2f(texcoordX,1.0f);glVertex3f(fx,fy,1.0f);}w++;w%=4;}while (w!=p);return ;}void drawwalls() {glEnable(GL_TEXTURE_2D);glBegin(GL_QUAD_STRIP);glColor3f(1.0,1.0,1.0);glVertex3f(0.0f, 0.0f, 0.0f);glVertex3f(0.0f, 0.0f, 1.0f);dw(0,0,0);glEnd();}void drawtop() {int x,y;glBegin(GL_QUADS);for(y=0;y<MAZE_HEIGHT;y++) {for(x=0;x<MAZE_WIDTH;x++) {if(wall(x,y)) {mazedata[y][x]= 'X';glVertex3f(x+0.0f ,y+0.0f ,1.0f ); glVertex3f(x+1.0f ,y+0.0f ,1.0f ); glVertex3f(x+1.0f ,y+1.0f ,1.0f ); glVertex3f(x+0.0f ,y+1.0f ,1.0f ); }}}glEnd();}void forward(float px,float py,float bf) { int x = ((int)player_x);int y = ((int)player_y);int h=0;if((px> x+1.0f - bf) && wall(x+1,y)) { px = (float)(x)+1.0f-bf;h++;}if(py> y+1.0f-bf && wall(x,y+1)) {py = (float)(y)+1.0f-bf;h++;}if(px< x+bf && wall(x-1,y)) {px = (float)(x)+bf;h++;}if(py< y+bf && wall(x,y-1)) {py = (float)(y)+bf;h++;}player_x=px;player_y=py;}void drawball(){glDisable(GL_TEXTURE_2D);glColor3f(1.0,0.0,0.0);glutSolidSphere(0.2f,15,15);}void navmaze1(){forward(player_x+player_s*(float)sin(player_h*3.14/180),player_y+player_s*(float)cos(player_h*3.14/180),0.2f);cout<<player_x<<player_y<<endl;player_h+=player_t;player_b = 3*player_b/4 + player_t/4;glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);glLoadIdentity();glPushMatrix();glRotatef(-90.0f,1.0f,0.0f,0.0f);glRotatef(player_h,0.0f,0.0f,1.0f);glTranslatef(-player_x,-player_y,-0.5f);glCallList(walllist);glPopMatrix();}void navmaze2(){forward(player_x+player_m*player_s*(float)sin(player_h*3.14/180 ),player_y+player_m*player_s*(float)cos(player_h*3.14/180),0.2f);cout<<player_x<<player_x<<endl;player_h+=player_t;player_b = 3*player_b/4 + player_t/4;glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);glLoadIdentity();glOrtho(-16.0,16.0,-16.0,16.0,-2.0,20.0);glPushMatrix();glRotatef(90.0f,0.0f,0.0f,1.0f);glTranslatef(-MAZE_WIDTH /2,-MAZE_HEIGHT/2,-0.5f);glCallList(walllist);glCallList(mazelist);glPushMatrix();glTranslatef(player_x,player_y,0.5f);glCallList(balllist);glPopMatrix();glPopMatrix();}void myDisplay(){if(status==1){if(searchroute==true){}else navmaze1();}if(status==3){if(searchroute==true){}else navmaze2();}glFlush();glutSwapBuffers();}void myReshape(int w, int h){glViewport(0,0,w,h);glMatrixMode(GL_PROJECTION);glLoadIdentity();glMatrixMode(GL_MODELVIEW);glLoadIdentity();glutPostRedisplay();}void specialKeys(int key,int x,int y) {switch (key){case GLUT_KEY_LEFT:keystate[2] = true;player_t = -2.0f;break;case GLUT_KEY_RIGHT:keystate[3] = true;player_t = 2.0f;break;case GLUT_KEY_UP:keystate[0] = true;player_s = 0.01f;break;case GLUT_KEY_DOWN:keystate[1] = true;player_s = -0.01f;break;default:break;}}void keyboard(unsigned char key,int x,int y){switch (key){case '1':status=1;break;case '3':status=3;break;default:break;}glutPostRedisplay();}void upSpecialKeyboard(int key,int x,int y){switch (key){case GLUT_KEY_LEFT:keystate[2] = false;player_t = 0.0f;break;case GLUT_KEY_RIGHT:keystate[3] = false;player_t = 0.0f;break;case GLUT_KEY_UP:keystate[0] = false;player_s = 0.0f;break;case GLUT_KEY_DOWN:keystate[1] = false;player_s = 0.0f;break;default:break;}// glutPostRedisplay();}void idle(){if(keystate[0]||keystate[1]||keystate[2]||keystate[3]) glutPostRedisplay();else{}}//ÊÍ·Å°´¼üºó¾Í½øÈë¿ÕÏÐ״̬£¬Èç¹û¿ÕÏÐ״̬²»Ò»Ö±Öظ´»æͼ£¬¾Í»á»-ÃæÍ£ÖÍ¡£´Î·½·¨È±µãÊÇ¿ªÏúºÜ´óvoid main(int argc, char** argv){glutInit(&argc,argv);glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);glutInitWindowSize(500, 500);glutCreateWindow("polygon modeler");glutDisplayFunc(myDisplay);myinit ();glutSpecialFunc(specialKeys);glutKeyboardFunc(keyboard);glutSpecialUpFunc(upSpecialKeyboard);glutIdleFunc(idle);glutMainLoop();}。

opengl 3D迷宫 C 实现 源代码之欧阳法创编

opengl 3D迷宫 C  实现 源代码之欧阳法创编

#include "stdafx.h"#include <stdio.h>#include <stdlib.h>#include <GL/glut.h>#include <math.h>#include <iostream>using namespace std;void drawwalls(void);void drawtop(void);void drawball(void);#define IDM_APPLICATION_EXIT (101)#define IDM_APPLICATION_TEXTURE (102) #define IDM_APPLICATION_BANK (103) #define MAZE_HEIGHT (16)#define MAZE_WIDTH (16)#define STARTING_POINT_X (13.5f);#define STARTING_POINT_Y (1.5f);#define STARTING_HEADING (90.0f);float player_x = STARTING_POINT_X ;float player_y = STARTING_POINT_Y ;float player_h = STARTING_HEADING ; // player's headingfloat player_s = 0.0f; // forward speed of the playerfloat player_m = 1.0f; // speed multiplier of the player float player_t = 0.0f; // player's turning (change in heading)float player_b = 0.0f; // viewpoint bank (roll)static float texcoordX=0.0f;int walllist=0;int mazelist=0;int balllist=0;int status=1;bool searchroute=false;bool keystate[4]={false,false,false,false};char mazedata[MAZE_HEIGHT][MAZE_WIDTH] = { {'H','H','H','H','H','H','H','H','H','H','H','H','H','H','H',' H'},{'H',' ',' ',' ',' ',' ',' ',' ','H',' ',' ',' ',' ',' ',' ','H'},{'H',' ','H',' ','H','H','H',' ','H',' ','H',' ',' ',' ',' ','H'},{'H',' ','H','H',' ',' ','H',' ','H','H',' ','H',' ','H',' ','H'},{'H',' ',' ',' ',' ',' ','H',' ',' ',' ',' ',' ',' ','H',' ','H'},{'H',' ','H','H','H','H','H','H','H','H',' ','H','H','H',' ','H'}, {'H',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','H',' ',' ','H'},{'H',' ','H','H','H','H','H',' ','H','H','H',' ','H','H','H','H'}, {'H',' ','H',' ',' ',' ','H',' ',' ',' ','H',' ',' ',' ',' ','H'},{'H',' ',' ',' ','H','H','H','H','H','H','H',' ',' ',' ',' ','H'},{'H',' ','H',' ',' ',' ','H',' ',' ',' ','H',' ',' ','H',' ','H'},{'H',' ','H','H','H','H','H',' ','H','H','H','H',' ','H',' ','H'},{'H',' ',' ',' ',' ',' ','H',' ',' ',' ',' ',' ',' ','H',' ','H'},{'H',' ',' ','H','H',' ','H','H','H','H',' ','H','H','H',' ','H'},{'H',' ',' ',' ','H',' ','H',' ',' ',' ',' ','H',' ',' ',' ','H'},{'H','H','H','H','H','H','H','H','H','H','H','H','H',' ','H','H'}, };void myinit(){glClearColor(0.5f, 0.5f, 0.5f, 0.0f);glColor3f(1.0,1.0,1.0);glEnable(GL_DEPTH_TEST);glEnable(GL_TEXTURE_2D);walllist=glGenLists(2);mazelist=walllist+1;balllist=walllist+2;glNewList(walllist,GL_COMPILE);drawwalls();glEndList();glNewList(mazelist,GL_COMPILE);drawtop();glEndList();glNewList(balllist,GL_COMPILE);drawball();glEndList();glMatrixMode(GL_PROJECTION);glLoadIdentity();gluPerspective(60.0, 1.0 , 0.1, 60.0);glMatrixMode(GL_MODELVIEW);glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL _NICEST);//ʹÎÆÀí²»±äÐÎ}bool wall(int x,int y) {return (x>=0 && y>=0 && x<MAZE_WIDTH && y<MAZE_HEIGHT && mazedata[y][x]!=' ');}bool onopen(int x,int y){if(wall(x,y)){return(mazedata[y][x]=='H');}}void closeit(int x,int y) {if(onopen(x,y)){mazedata[y][x]= 'X';}}bool neighbor(int x,int y,int w,int *nx,int *ny){switch(w) {case 0:*nx = x1; *ny=y; break;case 1:*nx = x; *ny=y+1; break;case 2:*nx = x+1; *ny=y; break;case 3:*nx = x; *ny=y1; break;default:break;}return wall(*nx,*ny);}bool diagnal(int x,int y,int w,int *nx,int *ny){ switch(w) {case 0:*nx = x1; *ny=y1; break;case 1:*nx = x1; *ny=y+1; break;case 2:*nx = x+1; *ny=y+1; break;case 3:*nx = x+1; *ny=y1; break;default:break;}return wall(*nx,*ny);}void dw(int x,int y,int p) {int w=p;closeit(x,y);do{int x2=0;int y2=0;if(neighbor(x,y,w,&x2,&y2)){if(onopen(x2,y2)) {dw(x2,y2,(w+3)%4);}else {if((w+1)%4 ==p){return ;}}}else {float fx;float fy;if(diagnal(x,y,w,&x2,&y2) && onopen(x2,y2)) {dw(x2,y2,(w+2)%4);}texcoordX=(texcoordX<0.5)?1.0f:0.0f;fx = (float)x+((w==1||w==2)?1.0f:0.0f);fy = (float)y+((w==0||w==1)?1.0f:0.0f);glTexCoord2f(texcoordX,0.0f);glVertex3f(fx,fy,0.0f);glTexCoord2f(texcoordX,1.0f);glVertex3f(fx,fy,1.0f);}w++;w%=4;}while (w!=p);return ;}void drawwalls() {glEnable(GL_TEXTURE_2D); glBegin(GL_QUAD_STRIP);glColor3f(1.0,1.0,1.0);glVertex3f(0.0f, 0.0f, 0.0f);glVertex3f(0.0f, 0.0f, 1.0f);dw(0,0,0);glEnd();}void drawtop() {int x,y;glBegin(GL_QUADS);for(y=0;y<MAZE_HEIGHT;y++) { for(x=0;x<MAZE_WIDTH;x++) { if(wall(x,y)) {mazedata[y][x]= 'X';glVertex3f(x+0.0f ,y+0.0f ,1.0f );glVertex3f(x+1.0f ,y+0.0f ,1.0f ); glVertex3f(x+1.0f ,y+1.0f ,1.0f ); glVertex3f(x+0.0f ,y+1.0f ,1.0f ); }}}glEnd();}void forward(float px,float py,float bf) { int x = ((int)player_x);int y = ((int)player_y);int h=0;if((px> x+1.0f bf) && wall(x+1,y)) { px = (float)(x)+1.0fbf;h++;}if(py> y+1.0fbf && wall(x,y+1)) { py = (float)(y)+1.0fbf;h++;}if(px< x+bf && wall(x1,y)) {px = (float)(x)+bf;h++;}if(py< y+bf && wall(x,y1)) {py = (float)(y)+bf;h++;}player_x=px;player_y=py;}void drawball(){glDisable(GL_TEXTURE_2D);glColor3f(1.0,0.0,0.0);glutSolidSphere(0.2f,15,15);}void navmaze1(){forward(player_x+player_s*(float)sin(player_h*3.14/180),player_y+player_s*(float)cos(player_h*3.14/180),0.2f);cout<<player_x<<player_y<<endl;player_h+=player_t;player_b = 3*player_b/4 + player_t/4;glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);glLoadIdentity();glPushMatrix();glRotatef(90.0f,1.0f,0.0f,0.0f);glRotatef(player_h,0.0f,0.0f,1.0f);glTranslatef(player_x,player_y,0.5f);glCallList(walllist);glPopMatrix();}void navmaze2(){forward(player_x+player_m*player_s*(float)sin(playe r_h*3.14/180),player_y+player_m*player_s*(float)cos(player_h*3.14/180) ,0.2f);cout<<player_x<<player_x<<endl;player_h+=player_t;player_b = 3*player_b/4 + player_t/4;glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);glLoadIdentity();glOrtho(16.0,16.0,16.0,16.0,2.0,20.0);glPushMatrix();glRotatef(90.0f,0.0f,0.0f,1.0f);glTranslatef(MAZE_WIDTH/2,MAZE_HEIGHT/2,0.5f);glCallList(walllist);glCallList(mazelist);glPushMatrix();glTranslatef(player_x,player_y,0.5f);glCallList(balllist);glPopMatrix();glPopMatrix();}void myDisplay(){if(status==1){if(searchroute==true){}else navmaze1();}if(status==3){if(searchroute==true){}else navmaze2();}glFlush();glutSwapBuffers();}void myReshape(int w, int h){glViewport(0,0,w,h);glMatrixMode(GL_PROJECTION);glLoadIdentity();glMatrixMode(GL_MODELVIEW);glLoadIdentity();glutPostRedisplay();}void specialKeys(int key,int x,int y){switch (key){case GLUT_KEY_LEFT:keystate[2] = true;player_t = 2.0f;break;case GLUT_KEY_RIGHT:keystate[3] = true;player_t = 2.0f;break;case GLUT_KEY_UP:keystate[0] = true;player_s = 0.01f;break;case GLUT_KEY_DOWN:keystate[1] = true;player_s = 0.01f;break;default:break;}}void keyboard(unsigned char key,int x,int y){switch (key){case '1':status=1;break;case '3':status=3;break;default:break;}glutPostRedisplay();}void upSpecialKeyboard(int key,int x,int y){switch (key){case GLUT_KEY_LEFT:keystate[2] = false;player_t = 0.0f;break;case GLUT_KEY_RIGHT:keystate[3] = false;player_t = 0.0f;break;case GLUT_KEY_UP:keystate[0] = false;player_s = 0.0f;break;case GLUT_KEY_DOWN:keystate[1] = false;player_s = 0.0f;break;default:break;}// glutPostRedisplay();}void idle(){if(keystate[0]||keystate[1]||keystate[2]||keystate[3]) glutPostRedisplay();else{}}//ÊÍ·Å°´¼üºó¾Í½øÈë¿ÕÏÐ״̬£¬Èç¹û¿ÕÏÐ״̬²»Ò»Ö±Öظ´»æͼ£¬¾Í»á»­ÃæÍ£ÖÍ¡£´Î·½·¨È±µãÊÇ¿ªÏúºÜ´óvoid main(int argc, char** argv){glutInit(&argc,argv);glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);glutInitWindowSize(500, 500);glutCreateWindow("polygon modeler");glutDisplayFunc(myDisplay);myinit ();glutSpecialFunc(specialKeys);glutKeyboardFunc(keyboard);glutSpecialUpFunc(upSpecialKeyboard);glutIdleFunc(idle);glutMainLoop();}。

openglD迷宫C实现源代码完整版

openglD迷宫C实现源代码完整版

o p e n g l D迷宫C实现源代码HUA system office room 【HUA16H-TTMS2A-HUAS8Q8-HUAH1688】#include <iostream>using namespace std;void drawwalls(void);void drawtop(void);void drawball(void);#define IDM_APPLICATION_EXIT (101) #define IDM_APPLICATION_TEXTURE (102) #define IDM_APPLICATION_BANK (103) #define MAZE_HEIGHT (16)#define MAZE_WIDTH (16)#define STARTING_POINT_X (13.5f);#define STARTING_POINT_Y (1.5f);#define STARTING_HEADING (90.0f); float player_x = STARTING_POINT_X ; float player_y = STARTING_POINT_Y ;float player_h = STARTING_HEADING ; // player's headingfloat player_s = 0.0f; // forward speed of the playerfloat player_m = 1.0f; // speed multiplier of the playerfloat player_t = 0.0f; // player's turning (change in heading)float player_b = 0.0f; // viewpoint bank (roll)static float texcoordX=0.0f;int walllist=0;int mazelist=0;int balllist=0;int status=1;bool searchroute=false;bool keystate[4]={false,false,false,false};char mazedata[MAZE_HEIGHT][MAZE_WIDTH] = {{'H','H','H','H','H','H','H','H','H','H','H','H','H','H','H','H'}, {'H',' ',' ',' ',' ',' ',' ',' ','H',' ',' ',' ',' ',' ',' ','H'}, {'H',' ','H',' ','H','H','H',' ','H',' ','H',' ',' ',' ',' ','H'},{'H',' ','H','H',' ',' ','H',' ','H','H',' ','H',' ','H',' ','H'}, {'H',' ',' ',' ',' ',' ','H',' ',' ',' ',' ',' ',' ','H',' ','H'}, {'H',' ','H','H','H','H','H','H','H','H',' ','H','H','H',' ','H'}, {'H',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','H',' ',' ','H'}, {'H',' ','H','H','H','H','H',' ','H','H','H',' ','H','H','H','H'}, {'H',' ','H',' ',' ',' ','H',' ',' ',' ','H',' ',' ',' ',' ','H'}, {'H',' ',' ',' ','H','H','H','H','H','H','H',' ',' ',' ',' ','H'}, {'H',' ','H',' ',' ',' ','H',' ',' ',' ','H',' ',' ','H',' ','H'}, {'H',' ','H','H','H','H','H',' ','H','H','H','H',' ','H',' ','H'}, {'H',' ',' ',' ',' ',' ','H',' ',' ',' ',' ',' ',' ','H',' ','H'}, {'H',' ',' ','H','H',' ','H','H','H','H',' ','H','H','H',' ','H'}, {'H',' ',' ',' ','H',' ','H',' ',' ',' ',' ','H',' ',' ',' ','H'}, {'H','H','H','H','H','H','H','H','H','H','H','H','H',' ','H','H'}, };void myinit(){glClearColor(0.5f, 0.5f, 0.5f, 0.0f); glColor3f(1.0,1.0,1.0);glEnable(GL_DEPTH_TEST);glEnable(GL_TEXTURE_2D);walllist=glGenLists(2);mazelist=walllist+1;balllist=walllist+2;glNewList(walllist,GL_COMPILE);drawwalls();glEndList();glNewList(mazelist,GL_COMPILE);drawtop();glEndList();glNewList(balllist,GL_COMPILE);drawball();glEndList();glMatrixMode(GL_PROJECTION);glLoadIdentity();gluPerspective(60.0, 1.0 , 0.1, 60.0);glMatrixMode(GL_MODELVIEW);glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);//ê1àí2±D}bool wall(int x,int y) {return (x>=0 && y>=0 && x<MAZE_WIDTH && y<MAZE_HEIGHT && mazedata[y][x]!=' ');}bool onopen(int x,int y){if(wall(x,y)){return(mazedata[y][x]=='H');}}void closeit(int x,int y) {if(onopen(x,y)){mazedata[y][x]= 'X';}}bool neighbor(int x,int y,int w,int *nx,int *ny){ switch(w) {case 0:*nx = x-1; *ny=y; break;case 1:*nx = x; *ny=y+1; break;case 2:*nx = x+1; *ny=y; break;case 3:*nx = x; *ny=y-1; break;default:break;}return wall(*nx,*ny);}bool diagnal(int x,int y,int w,int *nx,int *ny){ switch(w) {case 0:*nx = x-1; *ny=y-1; break;case 1:*nx = x-1; *ny=y+1; break;case 2:*nx = x+1; *ny=y+1; break;case 3:*nx = x+1; *ny=y-1; break;default:break;}return wall(*nx,*ny);}void dw(int x,int y,int p) {int w=p;closeit(x,y);do{int x2=0;int y2=0;if(neighbor(x,y,w,&x2,&y2)){if(onopen(x2,y2)) {dw(x2,y2,(w+3)%4);}else {if((w+1)%4 ==p){return ;}}}else {float fx;float fy;if(diagnal(x,y,w,&x2,&y2) && onopen(x2,y2)) {dw(x2,y2,(w+2)%4);}texcoordX=(texcoordX<0.5)1.0f:0.0f;fx = (float)x+((w==1||w==2)1.0f:0.0f);fy = (float)y+((w==0||w==1)1.0f:0.0f);glTexCoord2f(texcoordX,0.0f);glVertex3f(fx,fy,0.0f);glTexCoord2f(texcoordX,1.0f);glVertex3f(fx,fy,1.0f);}w++;w%=4;}while (w!=p);return ;}void drawwalls() {glEnable(GL_TEXTURE_2D);glBegin(GL_QUAD_STRIP);glColor3f(1.0,1.0,1.0);glVertex3f(0.0f, 0.0f, 0.0f); glVertex3f(0.0f, 0.0f, 1.0f); dw(0,0,0);glEnd();}void drawtop() {int x,y;glBegin(GL_QUADS);for(y=0;y<MAZE_HEIGHT;y++) {for(x=0;x<MAZE_WIDTH;x++) {if(wall(x,y)) {mazedata[y][x]= 'X';glVertex3f(x+0.0f ,y+0.0f ,1.0f ); glVertex3f(x+1.0f ,y+0.0f ,1.0f ); glVertex3f(x+1.0f ,y+1.0f ,1.0f ); glVertex3f(x+0.0f ,y+1.0f ,1.0f ); }}}glEnd();}void forward(float px,float py,float bf) {int x = ((int)player_x);int y = ((int)player_y);int h=0;if((px> x+1.0f - bf) && wall(x+1,y)) { px = (float)(x)+1.0f-bf;h++;}if(py> y+1.0f-bf && wall(x,y+1)) {py = (float)(y)+1.0f-bf;h++;}if(px< x+bf && wall(x-1,y)) {px = (float)(x)+bf;h++;}if(py< y+bf && wall(x,y-1)) {py = (float)(y)+bf;h++;}player_x=px;player_y=py;}void drawball(){glDisable(GL_TEXTURE_2D);glColor3f(1.0,0.0,0.0);glutSolidSphere(0.2f,15,15);}void navmaze1(){forward(player_x+player_s*(float)sin(player_h*3.14/180),player_y+player_s*(float)cos(player_h*3.14/180),0.2f);player_h+=player_t;player_b = 3*player_b/4 + player_t/4;glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);glLoadIdentity();glPushMatrix();glRotatef(-90.0f,1.0f,0.0f,0.0f);glRotatef(player_h,0.0f,0.0f,1.0f);glTranslatef(-player_x,-player_y,-0.5f);glCallList(walllist);glPopMatrix();}void navmaze2(){forward(player_x+player_m*player_s*(float)sin(player_h*3.14/180),player_y+player_m*player_s*(float)cos(player_h*3.14/180),0.2f);player_h+=player_t;player_b = 3*player_b/4 + player_t/4;glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);glLoadIdentity();glOrtho(-16.0,16.0,-16.0,16.0,-2.0,20.0);glPushMatrix();glRotatef(90.0f,0.0f,0.0f,1.0f);glTranslatef(-MAZE_WIDTH /2,-MAZE_HEIGHT/2,-0.5f);glCallList(walllist);glCallList(mazelist);glPushMatrix();glTranslatef(player_x,player_y,0.5f);glCallList(balllist);glPopMatrix();glPopMatrix();}void myDisplay(){if(status==1){if(searchroute==true){}else navmaze1();}if(status==3){if(searchroute==true){}else navmaze2();glFlush();glutSwapBuffers();}void myReshape(int w, int h){glViewport(0,0,w,h);glMatrixMode(GL_PROJECTION); glLoadIdentity();glMatrixMode(GL_MODELVIEW); glLoadIdentity();glutPostRedisplay();}void specialKeys(int key,int x,int y) {switch (key)case GLUT_KEY_LEFT:keystate[2] = true;player_t = -2.0f;break;case GLUT_KEY_RIGHT:keystate[3] = true;player_t = 2.0f;break;case GLUT_KEY_UP:keystate[0] = true;player_s = 0.01f;break;case GLUT_KEY_DOWN:keystate[1] = true;player_s = -0.01f;break;default:break;}}void keyboard(unsigned char key,int x,int y) {switch (key){case '1':status=1;break;case '3':status=3;break;default:break;}glutPostRedisplay();}void upSpecialKeyboard(int key,int x,int y){switch (key){case GLUT_KEY_LEFT:keystate[2] = false;player_t = 0.0f;break;case GLUT_KEY_RIGHT:keystate[3] = false;player_t = 0.0f;break;case GLUT_KEY_UP:keystate[0] = false;player_s = 0.0f;break;case GLUT_KEY_DOWN:keystate[1] = false;player_s = 0.0f;break;default:break;}// glutPostRedisplay();}void idle(){if(keystate[0]||keystate[1]||keystate[2]||keystate[3]) glutPostRedisplay();else{}}//êí·°′üoóíèD×′ì£è1D×′ì2ò±′í£íá-í£í£′··¨è±μêaúoü′óvoid main(int argc, char** argv){glutInit(&argc,argv);glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB); glutInitWindowSize(500, 500);glutCreateWindow("polygon modeler");glutDisplayFunc(myDisplay);myinit ();glutSpecialFunc(specialKeys);glutKeyboardFunc(keyboard);glutSpecialUpFunc(upSpecialKeyboard);glutIdleFunc(idle);glutMainLoop();}。

2021年opengl 3D迷宫 C 实现 源代码之令狐采学创编

2021年opengl 3D迷宫 C  实现 源代码之令狐采学创编

#include "stdafx.h"欧阳光明(2021.03.07)#include <stdio.h>#include <stdlib.h>#include <GL/glut.h>#include <math.h>#include <iostream>using namespace std;void drawwalls(void);void drawtop(void);void drawball(void);#define IDM_APPLICATION_EXIT (101)#define IDM_APPLICATION_TEXTURE (102)#define IDM_APPLICATION_BANK (103)#define MAZE_HEIGHT (16)#define MAZE_WIDTH (16)#define STARTING_POINT_X (13.5f);#define STARTING_POINT_Y (1.5f);#define STARTING_HEADING (90.0f);float player_x = STARTING_POINT_X ;float player_y = STARTING_POINT_Y ;float player_h = STARTING_HEADING ; // player's headingfloat player_s = 0.0f; // forward speed of the playerfloat player_m = 1.0f; // speed multiplier of the playerfloat player_t = 0.0f; // player's turning (change in heading) float player_b = 0.0f; // viewpoint bank (roll)static float texcoordX=0.0f;int walllist=0;int mazelist=0;int balllist=0;int status=1;bool searchroute=false;bool keystate[4]={false,false,false,false};char mazedata[MAZE_HEIGHT][MAZE_WIDTH] = { {'H','H','H','H','H','H','H','H','H','H','H','H','H','H','H','H'}, {'H',' ',' ',' ',' ',' ',' ',' ','H',' ',' ',' ',' ',' ',' ','H'},{'H',' ','H',' ','H','H','H',' ','H',' ','H',' ',' ',' ',' ','H'},{'H',' ','H','H',' ',' ','H',' ','H','H',' ','H',' ','H',' ','H'},{'H',' ',' ',' ',' ',' ','H',' ',' ',' ',' ',' ',' ','H',' ','H'},{'H',' ','H','H','H','H','H','H','H','H',' ','H','H','H',' ','H'},{'H',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','H',' ',' ','H'},{'H',' ','H','H','H','H','H',' ','H','H','H',' ','H','H','H','H'},{'H',' ','H',' ',' ',' ','H',' ',' ',' ','H',' ',' ',' ',' ','H'},{'H',' ',' ',' ','H','H','H','H','H','H','H',' ',' ',' ',' ','H'},{'H',' ','H',' ',' ',' ','H',' ',' ',' ','H',' ',' ','H',' ','H'},{'H',' ','H','H','H','H','H',' ','H','H','H','H',' ','H',' ','H'},{'H',' ',' ',' ',' ',' ','H',' ',' ',' ',' ',' ',' ','H',' ','H'},{'H',' ',' ','H','H',' ','H','H','H','H',' ','H','H','H',' ','H'},{'H',' ',' ',' ','H',' ','H',' ',' ',' ',' ','H',' ',' ',' ','H'},{'H','H','H','H','H','H','H','H','H','H','H','H','H',' ','H','H'}, };void myinit(){glClearColor(0.5f, 0.5f, 0.5f, 0.0f);glColor3f(1.0,1.0,1.0);glEnable(GL_DEPTH_TEST);glEnable(GL_TEXTURE_2D);walllist=glGenLists(2);mazelist=walllist+1;balllist=walllist+2;glNewList(walllist,GL_COMPILE);drawwalls();glEndList();glNewList(mazelist,GL_COMPILE);drawtop();glEndList();glNewList(balllist,GL_COMPILE);drawball();glEndList();glMatrixMode(GL_PROJECTION);glLoadIdentity();gluPerspective(60.0, 1.0 , 0.1, 60.0);glMatrixMode(GL_MODELVIEW);glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);//ʹÎÆÀí²»±äÐÎ}bool wall(int x,int y) {return (x>=0 && y>=0 && x<MAZE_WIDTH && y<MAZE_HEIGHT && mazedata[y][x]!=' ');}bool onopen(int x,int y){if(wall(x,y)){return(mazedata[y][x]=='H');}}void closeit(int x,int y) {if(onopen(x,y)){mazedata[y][x]= 'X';}}bool neighbor(int x,int y,int w,int *nx,int *ny){ switch(w) {case 0:*nx = x1; *ny=y; break;case 1:*nx = x; *ny=y+1; break;case 2:*nx = x+1; *ny=y; break;case 3:*nx = x; *ny=y1; break;default:break;}return wall(*nx,*ny);}bool diagnal(int x,int y,int w,int *nx,int *ny){ switch(w) {case 0:*nx = x1; *ny=y1; break;case 1:*nx = x1; *ny=y+1; break;case 2:*nx = x+1; *ny=y+1; break;case 3:*nx = x+1; *ny=y1; break;default:break;}return wall(*nx,*ny);}void dw(int x,int y,int p) {int w=p;closeit(x,y);do{int x2=0;int y2=0;if(neighbor(x,y,w,&x2,&y2)){if(onopen(x2,y2)) {dw(x2,y2,(w+3)%4);}else {if((w+1)%4 ==p){return ;}}}else {float fx;float fy;if(diagnal(x,y,w,&x2,&y2) && onopen(x2,y2)) {dw(x2,y2,(w+2)%4);}texcoordX=(texcoordX<0.5)?1.0f:0.0f;fx = (float)x+((w==1||w==2)?1.0f:0.0f);fy = (float)y+((w==0||w==1)?1.0f:0.0f);glTexCoord2f(texcoordX,0.0f);glVertex3f(fx,fy,0.0f);glTexCoord2f(texcoordX,1.0f);glVertex3f(fx,fy,1.0f);}w++;w%=4;}while (w!=p);return ;}void drawwalls() {glEnable(GL_TEXTURE_2D);glBegin(GL_QUAD_STRIP);glColor3f(1.0,1.0,1.0);glVertex3f(0.0f, 0.0f, 0.0f);glVertex3f(0.0f, 0.0f, 1.0f);dw(0,0,0);glEnd();}void drawtop() {int x,y;glBegin(GL_QUADS);for(y=0;y<MAZE_HEIGHT;y++) { for(x=0;x<MAZE_WIDTH;x++) { if(wall(x,y)) {mazedata[y][x]= 'X';glVertex3f(x+0.0f ,y+0.0f ,1.0f ); glVertex3f(x+1.0f ,y+0.0f ,1.0f ); glVertex3f(x+1.0f ,y+1.0f ,1.0f ); glVertex3f(x+0.0f ,y+1.0f ,1.0f ); }}}glEnd();}void forward(float px,float py,float bf) {int x = ((int)player_x);int y = ((int)player_y);int h=0;if((px> x+1.0f bf) && wall(x+1,y)) { px = (float)(x)+1.0fbf;h++;}if(py> y+1.0fbf && wall(x,y+1)) { py = (float)(y)+1.0fbf;h++;}if(px< x+bf && wall(x1,y)) {px = (float)(x)+bf;h++;}if(py< y+bf && wall(x,y1)) {py = (float)(y)+bf;h++;}player_x=px;player_y=py;}void drawball(){glDisable(GL_TEXTURE_2D);glColor3f(1.0,0.0,0.0);glutSolidSphere(0.2f,15,15);}void navmaze1(){forward(player_x+player_s*(float)sin(player_h*3.14/180),player_y+player_s*(float)cos(player_h*3.14/180),0.2f);cout<<player_x<<player_y<<endl;player_h+=player_t;player_b = 3*player_b/4 + player_t/4;glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity();glPushMatrix();glRotatef(90.0f,1.0f,0.0f,0.0f);glRotatef(player_h,0.0f,0.0f,1.0f);glTranslatef(player_x,player_y,0.5f);glCallList(walllist);glPopMatrix();}void navmaze2(){forward(player_x+player_m*player_s*(float)sin(player_h*3.14/18 0),player_y+player_m*player_s*(float)cos(player_h*3.14/180),0.2f);cout<<player_x<<player_x<<endl;player_h+=player_t;player_b = 3*player_b/4 + player_t/4;glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity();glOrtho(16.0,16.0,16.0,16.0,2.0,20.0);glPushMatrix();glRotatef(90.0f,0.0f,0.0f,1.0f);glTranslatef(MAZE_WIDTH /2,MAZE_HEIGHT/2,0.5f);glCallList(walllist);glCallList(mazelist);glPushMatrix();glTranslatef(player_x,player_y,0.5f);glCallList(balllist);glPopMatrix();glPopMatrix();}void myDisplay(){if(status==1){if(searchroute==true){}else navmaze1();}if(status==3){if(searchroute==true){}else navmaze2();}glFlush();glutSwapBuffers();}void myReshape(int w, int h){glViewport(0,0,w,h);glMatrixMode(GL_PROJECTION); glLoadIdentity();glMatrixMode(GL_MODELVIEW);glLoadIdentity();glutPostRedisplay();}void specialKeys(int key,int x,int y){switch (key){case GLUT_KEY_LEFT:keystate[2] = true;player_t = 2.0f;break;case GLUT_KEY_RIGHT:keystate[3] = true;player_t = 2.0f;break;case GLUT_KEY_UP:keystate[0] = true;player_s = 0.01f;break;case GLUT_KEY_DOWN:keystate[1] = true;player_s = 0.01f;break;default:break;}}void keyboard(unsigned char key,int x,int y){switch (key){case '1':status=1;break;case '3':status=3;break;default:break;}glutPostRedisplay();}void upSpecialKeyboard(int key,int x,int y){switch (key){case GLUT_KEY_LEFT:keystate[2] = false;player_t = 0.0f;break;case GLUT_KEY_RIGHT:keystate[3] = false;player_t = 0.0f;break;case GLUT_KEY_UP:keystate[0] = false;player_s = 0.0f;break;case GLUT_KEY_DOWN:keystate[1] = false;player_s = 0.0f;break;default:break;}// glutPostRedisplay();}void idle(){if(keystate[0]||keystate[1]||keystate[2]||keystate[3]) glutPostRedisplay();else{}}//ÊÍ·Å°´¼üºó¾Í½øÈë¿ÕÏÐ״̬£¬Èç¹û¿ÕÏÐ״̬²»Ò»Ö±Öظ´»æͼ£¬¾Í»á»-ÃæÍ£ÖÍ¡£´Î·½·¨È±µãÊÇ¿ªÏúºÜ´óvoid main(int argc, char** argv){glutInit(&argc,argv);glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);glutInitWindowSize(500, 500);glutCreateWindow("polygon modeler");glutDisplayFunc(myDisplay);myinit ();glutSpecialFunc(specialKeys);glutKeyboardFunc(keyboard);glutSpecialUpFunc(upSpecialKeyboard);glutIdleFunc(idle);glutMainLoop();}。

opengl 3D迷宫 C 实现 源代码之令狐采学创编之欧阳家百创编

opengl 3D迷宫 C  实现 源代码之令狐采学创编之欧阳家百创编

#include "stdafx.h"欧阳家百(2021.03.07)#include <stdio.h>#include <stdlib.h>#include <GL/glut.h>#include <math.h>#include <iostream>using namespace std;void drawwalls(void);void drawtop(void);void drawball(void);#define IDM_APPLICATION_EXIT (101)#define IDM_APPLICATION_TEXTURE (102)#define IDM_APPLICATION_BANK (103)#define MAZE_HEIGHT (16)#define MAZE_WIDTH (16)#define STARTING_POINT_X (13.5f);#define STARTING_POINT_Y (1.5f);#define STARTING_HEADING (90.0f);float player_x = STARTING_POINT_X ;float player_y = STARTING_POINT_Y ;float player_h = STARTING_HEADING ; // player's headingfloat player_s = 0.0f; // forward speed of the playerfloat player_m = 1.0f; // speed multiplier of the playerfloat player_t = 0.0f; // player's turning (change in heading) float player_b = 0.0f; // viewpoint bank (roll)static float texcoordX=0.0f;int walllist=0;int mazelist=0;int balllist=0;int status=1;bool searchroute=false;bool keystate[4]={false,false,false,false};char mazedata[MAZE_HEIGHT][MAZE_WIDTH] = { {'H','H','H','H','H','H','H','H','H','H','H','H','H','H','H','H'}, {'H',' ',' ',' ',' ',' ',' ',' ','H',' ',' ',' ',' ',' ',' ','H'},{'H',' ','H',' ','H','H','H',' ','H',' ','H',' ',' ',' ',' ','H'},{'H',' ','H','H',' ',' ','H',' ','H','H',' ','H',' ','H',' ','H'},{'H',' ',' ',' ',' ',' ','H',' ',' ',' ',' ',' ',' ','H',' ','H'},{'H',' ','H','H','H','H','H','H','H','H',' ','H','H','H',' ','H'},{'H',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','H',' ',' ','H'},{'H',' ','H','H','H','H','H',' ','H','H','H',' ','H','H','H','H'},{'H',' ','H',' ',' ',' ','H',' ',' ',' ','H',' ',' ',' ',' ','H'},{'H',' ',' ',' ','H','H','H','H','H','H','H',' ',' ',' ',' ','H'},{'H',' ','H',' ',' ',' ','H',' ',' ',' ','H',' ',' ','H',' ','H'},{'H',' ','H','H','H','H','H',' ','H','H','H','H',' ','H',' ','H'},{'H',' ',' ',' ',' ',' ','H',' ',' ',' ',' ',' ',' ','H',' ','H'},{'H',' ',' ','H','H',' ','H','H','H','H',' ','H','H','H',' ','H'},{'H',' ',' ',' ','H',' ','H',' ',' ',' ',' ','H',' ',' ',' ','H'},{'H','H','H','H','H','H','H','H','H','H','H','H','H',' ','H','H'}, };void myinit(){glClearColor(0.5f, 0.5f, 0.5f, 0.0f);glColor3f(1.0,1.0,1.0);glEnable(GL_DEPTH_TEST);glEnable(GL_TEXTURE_2D);walllist=glGenLists(2);mazelist=walllist+1;balllist=walllist+2;glNewList(walllist,GL_COMPILE);drawwalls();glEndList();glNewList(mazelist,GL_COMPILE);drawtop();glEndList();glNewList(balllist,GL_COMPILE);drawball();glEndList();glMatrixMode(GL_PROJECTION);glLoadIdentity();gluPerspective(60.0, 1.0 , 0.1, 60.0);glMatrixMode(GL_MODELVIEW);glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);//ʹÎÆÀí²»±äÐÎ}bool wall(int x,int y) {return (x>=0 && y>=0 && x<MAZE_WIDTH && y<MAZE_HEIGHT && mazedata[y][x]!=' ');}bool onopen(int x,int y){if(wall(x,y)){return(mazedata[y][x]=='H');}}void closeit(int x,int y) {if(onopen(x,y)){mazedata[y][x]= 'X';}}bool neighbor(int x,int y,int w,int *nx,int *ny){ switch(w) {case 0:*nx = x1; *ny=y; break;case 1:*nx = x; *ny=y+1; break;case 2:*nx = x+1; *ny=y; break;case 3:*nx = x; *ny=y1; break;default:break;}return wall(*nx,*ny);}bool diagnal(int x,int y,int w,int *nx,int *ny){ switch(w) {case 0:*nx = x1; *ny=y1; break;case 1:*nx = x1; *ny=y+1; break;case 2:*nx = x+1; *ny=y+1; break;case 3:*nx = x+1; *ny=y1; break;default:break;}return wall(*nx,*ny);}void dw(int x,int y,int p) {int w=p;closeit(x,y);do{int x2=0;int y2=0;if(neighbor(x,y,w,&x2,&y2)){if(onopen(x2,y2)) {dw(x2,y2,(w+3)%4);}else {if((w+1)%4 ==p){return ;}}}else {float fx;float fy;if(diagnal(x,y,w,&x2,&y2) && onopen(x2,y2)) {dw(x2,y2,(w+2)%4);}texcoordX=(texcoordX<0.5)?1.0f:0.0f;fx = (float)x+((w==1||w==2)?1.0f:0.0f);fy = (float)y+((w==0||w==1)?1.0f:0.0f);glTexCoord2f(texcoordX,0.0f);glVertex3f(fx,fy,0.0f);glTexCoord2f(texcoordX,1.0f);glVertex3f(fx,fy,1.0f);}w++;w%=4;}while (w!=p);return ;}void drawwalls() {glEnable(GL_TEXTURE_2D);glBegin(GL_QUAD_STRIP);glColor3f(1.0,1.0,1.0);glVertex3f(0.0f, 0.0f, 0.0f);glVertex3f(0.0f, 0.0f, 1.0f);dw(0,0,0);glEnd();}void drawtop() {int x,y;glBegin(GL_QUADS);for(y=0;y<MAZE_HEIGHT;y++) { for(x=0;x<MAZE_WIDTH;x++) { if(wall(x,y)) {mazedata[y][x]= 'X';glVertex3f(x+0.0f ,y+0.0f ,1.0f ); glVertex3f(x+1.0f ,y+0.0f ,1.0f ); glVertex3f(x+1.0f ,y+1.0f ,1.0f ); glVertex3f(x+0.0f ,y+1.0f ,1.0f ); }}}glEnd();}void forward(float px,float py,float bf) {int x = ((int)player_x);int y = ((int)player_y);int h=0;if((px> x+1.0f bf) && wall(x+1,y)) { px = (float)(x)+1.0fbf;h++;}if(py> y+1.0fbf && wall(x,y+1)) { py = (float)(y)+1.0fbf;h++;}if(px< x+bf && wall(x1,y)) {px = (float)(x)+bf;h++;}if(py< y+bf && wall(x,y1)) {py = (float)(y)+bf;h++;}player_x=px;player_y=py;}void drawball(){glDisable(GL_TEXTURE_2D);glColor3f(1.0,0.0,0.0);glutSolidSphere(0.2f,15,15);}void navmaze1(){forward(player_x+player_s*(float)sin(player_h*3.14/180),player_y+player_s*(float)cos(player_h*3.14/180),0.2f);cout<<player_x<<player_y<<endl;player_h+=player_t;player_b = 3*player_b/4 + player_t/4;glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity();glPushMatrix();glRotatef(90.0f,1.0f,0.0f,0.0f);glRotatef(player_h,0.0f,0.0f,1.0f);glTranslatef(player_x,player_y,0.5f);glCallList(walllist);glPopMatrix();}void navmaze2(){forward(player_x+player_m*player_s*(float)sin(player_h*3.14/1 80),player_y+player_m*player_s*(float)cos(player_h*3.14/180),0.2f);cout<<player_x<<player_x<<endl;player_h+=player_t;player_b = 3*player_b/4 + player_t/4;glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity();glOrtho(16.0,16.0,16.0,16.0,2.0,20.0);glPushMatrix();glRotatef(90.0f,0.0f,0.0f,1.0f);glTranslatef(MAZE_WIDTH /2,MAZE_HEIGHT/2,0.5f);glCallList(walllist);glCallList(mazelist);glPushMatrix();glTranslatef(player_x,player_y,0.5f);glCallList(balllist);glPopMatrix();glPopMatrix();}void myDisplay(){if(status==1){if(searchroute==true){}else navmaze1();}if(status==3){if(searchroute==true){}else navmaze2();}glFlush();glutSwapBuffers();}void myReshape(int w, int h){glViewport(0,0,w,h);glMatrixMode(GL_PROJECTION); glLoadIdentity();glMatrixMode(GL_MODELVIEW);glLoadIdentity();glutPostRedisplay();}void specialKeys(int key,int x,int y){switch (key){case GLUT_KEY_LEFT:keystate[2] = true;player_t = 2.0f;break;case GLUT_KEY_RIGHT:keystate[3] = true;player_t = 2.0f;break;case GLUT_KEY_UP:keystate[0] = true;player_s = 0.01f;break;case GLUT_KEY_DOWN:keystate[1] = true;player_s = 0.01f;break;default:break;}}void keyboard(unsigned char key,int x,int y){switch (key){case '1':status=1;break;case '3':status=3;break;default:break;}glutPostRedisplay();}void upSpecialKeyboard(int key,int x,int y){switch (key){case GLUT_KEY_LEFT:keystate[2] = false;player_t = 0.0f;break;case GLUT_KEY_RIGHT:keystate[3] = false;player_t = 0.0f;break;case GLUT_KEY_UP:keystate[0] = false;player_s = 0.0f;break;case GLUT_KEY_DOWN:keystate[1] = false;player_s = 0.0f;break;default:break;}// glutPostRedisplay();}void idle(){if(keystate[0]||keystate[1]||keystate[2]||keystate[3]) glutPostRedisplay();else{}}//ÊÍ·Å°´¼üºó¾Í½øÈë¿ÕÏÐ״̬£¬Èç¹û¿ÕÏÐ״̬²»Ò»Ö±Öظ´»æͼ£¬¾Í»á»-ÃæÍ£ÖÍ¡£´Î·½·¨È±µãÊÇ¿ªÏúºÜ´óvoid main(int argc, char** argv){glutInit(&argc,argv);glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);glutInitWindowSize(500, 500);glutCreateWindow("polygon modeler");glutDisplayFunc(myDisplay);myinit ();glutSpecialFunc(specialKeys);glutKeyboardFunc(keyboard);glutSpecialUpFunc(upSpecialKeyboard);glutIdleFunc(idle);glutMainLoop();}。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

o e n g l D迷宫C实现源代码集团标准化办公室:[VV986T-J682P28-JP266L8-68PNN]#include <iostream>using namespace std;void drawwalls(void);void drawtop(void);void drawball(void);#define IDM_APPLICATION_EXIT (101)#define IDM_APPLICATION_TEXTURE (102)#define IDM_APPLICATION_BANK (103)#define MAZE_HEIGHT (16)#define MAZE_WIDTH (16)#define STARTING_POINT_X ;#define STARTING_POINT_Y ;#define STARTING_HEADING ;float player_x = STARTING_POINT_X ;float player_y = STARTING_POINT_Y ;float player_h = STARTING_HEADING ; // player's headingfloat player_s = ; // forward speed of the playerfloat player_m = ; // speed multiplier of the playerfloat player_t = ; // player's turning (change in heading)float player_b = ; // viewpoint bank (roll)static float texcoordX=;int walllist=0;int mazelist=0;int balllist=0;int status=1;bool searchroute=false;bool keystate[4]={false,false,false,false};char mazedata[MAZE_HEIGHT][MAZE_WIDTH] = {{'H','H','H','H','H','H','H','H','H','H','H','H','H','H','H','H'}, {'H',' ',' ',' ',' ',' ',' ',' ','H',' ',' ',' ',' ',' ',' ','H'}, {'H',' ','H',' ','H','H','H',' ','H',' ','H',' ',' ',' ',' ','H'}, {'H',' ','H','H',' ',' ','H',' ','H','H',' ','H',' ','H',' ','H'},{'H',' ',' ',' ',' ',' ','H',' ',' ',' ',' ',' ',' ','H',' ','H'}, {'H',' ','H','H','H','H','H','H','H','H',' ','H','H','H',' ','H'}, {'H',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','H',' ',' ','H'}, {'H',' ','H','H','H','H','H',' ','H','H','H',' ','H','H','H','H'}, {'H',' ','H',' ',' ',' ','H',' ',' ',' ','H',' ',' ',' ',' ','H'}, {'H',' ',' ',' ','H','H','H','H','H','H','H',' ',' ',' ',' ','H'}, {'H',' ','H',' ',' ',' ','H',' ',' ',' ','H',' ',' ','H',' ','H'}, {'H',' ','H','H','H','H','H',' ','H','H','H','H',' ','H',' ','H'}, {'H',' ',' ',' ',' ',' ','H',' ',' ',' ',' ',' ',' ','H',' ','H'}, {'H',' ',' ','H','H',' ','H','H','H','H',' ','H','H','H',' ','H'}, {'H',' ',' ',' ','H',' ','H',' ',' ',' ',' ','H',' ',' ',' ','H'}, {'H','H','H','H','H','H','H','H','H','H','H','H','H',' ','H','H'}, };void myinit(){glClearColor, , , ;glColor3f,,;glEnable(GL_DEPTH_TEST);glEnable(GL_TEXTURE_2D);walllist=glGenLists(2);mazelist=walllist+1;balllist=walllist+2;glNewList(walllist,GL_COMPILE);drawwalls();glEndList();glNewList(mazelist,GL_COMPILE);drawtop();glEndList();glNewList(balllist,GL_COMPILE);drawball();glEndList();glMatrixMode(GL_PROJECTION);glLoadIdentity();gluPerspective, , , ;glMatrixMode(GL_MODELVIEW);glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);//ê1àí2±D}bool wall(int x,int y) {return (x>=0 && y>=0 && x<MAZE_WIDTH && y<MAZE_HEIGHT && mazedata[y][x]!=' ');}bool onopen(int x,int y){if(wall(x,y)){return(mazedata[y][x]=='H');}}void closeit(int x,int y) {if(onopen(x,y)){mazedata[y][x]= 'X';}}bool neighbor(int x,int y,int w,int *nx,int *ny){ switch(w) {case 0:*nx = x-1; *ny=y; break;case 1:*nx = x; *ny=y+1; break;case 2:*nx = x+1; *ny=y; break;case 3:*nx = x; *ny=y-1; break;default:break;}return wall(*nx,*ny);}bool diagnal(int x,int y,int w,int *nx,int *ny){ switch(w) {case 0:*nx = x-1; *ny=y-1; break;case 1:*nx = x-1; *ny=y+1; break;case 2:*nx = x+1; *ny=y+1; break;case 3:*nx = x+1; *ny=y-1; break;default:break;}return wall(*nx,*ny);}void dw(int x,int y,int p) {int w=p;closeit(x,y);do{int x2=0;int y2=0;if(neighbor(x,y,w,&x2,&y2)){if(onopen(x2,y2)) {dw(x2,y2,(w+3)%4);}else {if((w+1)%4 ==p){return ;}}}else {float fx;float fy;if(diagnal(x,y,w,&x2,&y2) && onopen(x2,y2)) {dw(x2,y2,(w+2)%4);}texcoordX=(texcoordX<:;fx = (float)x+((w==1||w==2):;fy = (float)y+((w==0||w==1):;glTexCoord2f(texcoordX,;glVertex3f(fx,fy,;glTexCoord2f(texcoordX,;glVertex3f(fx,fy,;}w++;w%=4;}while (w!=p);return ;}void drawwalls() {glEnable(GL_TEXTURE_2D); glBegin(GL_QUAD_STRIP);glColor3f,,;glVertex3f, , ;glVertex3f, , ;dw(0,0,0);glEnd();}void drawtop() {int x,y;glBegin(GL_QUADS);for(y=0;y<MAZE_HEIGHT;y++) {for(x=0;x<MAZE_WIDTH;x++) {if(wall(x,y)) {mazedata[y][x]= 'X';glVertex3f(x+ ,y+ , );glVertex3f(x+ ,y+ , );glVertex3f(x+ ,y+ , );glVertex3f(x+ ,y+ , );}}}glEnd();}void forward(float px,float py,float bf) { int x = ((int)player_x);int y = ((int)player_y);int h=0;if((px> x+ - bf) && wall(x+1,y)) { px = (float)(x)+;h++;}if(py> y+ && wall(x,y+1)) {py = (float)(y)+;h++;}if(px< x+bf && wall(x-1,y)) {px = (float)(x)+bf;h++;}if(py< y+bf && wall(x,y-1)) {py = (float)(y)+bf;h++;}player_x=px;player_y=py;}void drawball(){glDisable(GL_TEXTURE_2D);glColor3f,,;glutSolidSphere,15,15);}void navmaze1(){forward(player_x+player_s*(float)sin(player_h*180), player_y+player_s*(float)cos(player_h*180),;cout<<player_x<<player_y<<endl;player_h+=player_t;glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);glLoadIdentity();glPushMatrix();glRotatef,,,;glRotatef(player_h,,,;glTranslatef(-player_x,-player_y,;glCallList(walllist);glPopMatrix();}void navmaze2(){forward(player_x+player_m*player_s*(float)sin(player_h*180), player_y+player_m*player_s*(float)cos(player_h*180),;cout<<player_x<<player_x<<endl;player_h+=player_t;glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity();glOrtho,,,,,;glPushMatrix();glRotatef,,,;glTranslatef(-MAZE_WIDTH /2,-MAZE_HEIGHT/2,;glCallList(walllist);glCallList(mazelist);glPushMatrix();glTranslatef(player_x,player_y,;glCallList(balllist);glPopMatrix();glPopMatrix();}void myDisplay(){if(status==1){if(searchroute==true){}else navmaze1();}if(status==3){if(searchroute==true){}else navmaze2();}glFlush();glutSwapBuffers();}void myReshape(int w, int h){glViewport(0,0,w,h);glMatrixMode(GL_PROJECTION);glLoadIdentity();glMatrixMode(GL_MODELVIEW);glLoadIdentity();glutPostRedisplay();}void specialKeys(int key,int x,int y){switch (key){case GLUT_KEY_LEFT:keystate[2] = true;player_t = ;break;case GLUT_KEY_RIGHT:keystate[3] = true;player_t = ;break;case GLUT_KEY_UP:keystate[0] = true;player_s = ;break;case GLUT_KEY_DOWN:keystate[1] = true;player_s = ;break;default:break;}}void keyboard(unsigned char key,int x,int y) {switch (key){case '1':status=1;break;case '3':status=3;break;default:break;}glutPostRedisplay();}void upSpecialKeyboard(int key,int x,int y){switch (key){case GLUT_KEY_LEFT:keystate[2] = false;player_t = ;break;case GLUT_KEY_RIGHT:keystate[3] = false;player_t = ;break;case GLUT_KEY_UP:keystate[0] = false;player_s = ;break;case GLUT_KEY_DOWN:keystate[1] = false;player_s = ;break;default:break;}// glutPostRedisplay();}void idle(){if(keystate[0]||keystate[1]||keystate[2]||keystate[3]) glutPostRedisplay();else{}}//êí·°′üoóíèD×′ì£è1D×′ì2ò±′í£íá-í£í£′··¨è±μêaúoü′óvoid main(int argc, char** argv){glutInit(&argc,argv);glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB); glutInitWindowSize(500, 500);glutCreateWindow("polygon modeler");glutDisplayFunc(myDisplay);myinit ();glutSpecialFunc(specialKeys);glutKeyboardFunc(keyboard);glutSpecialUpFunc(upSpecialKeyboard);glutIdleFunc(idle);glutMainLoop();}。

相关文档
最新文档