oenglD迷宫 C实现 源代码

合集下载

c语言~走迷宫

c语言~走迷宫

本程序代码为C语言解决数据结构(严蔚敏)中关于迷宫的问题。

程序不仅实现迷宫路径查找,还实现文字描述路径功能可以直接粘贴到vc6.0中运行【代码如下】# include <stdio.h> # include <malloc.h> # define null 0typedef struct{int (*base)[2];int (*top)[2];int listlen;}sqlist;int topelem[2]; //栈顶元素void creatstack(sqlist *mazepath); //创建一个存储路径的栈void creatmap(int (*mazemap)[10]); //创建迷宫图纸void printmap(int (*mazemap)[10]);void footprint(int x,int y,int k,int (*mazemap)[10]);int position(int x,int y); //判断是否到终点int passroad(int x,int y,int (*mazemap)[10]);void findpath(int (*mazemap)[10],sqlist *mazepath); //在mazemap当中寻找mazepahtvoid printpath(sqlist *mazepath);void roadinwords(sqlist *mazepath); //文字叙述如何走迷宫void push(int x,int y,sqlist *mazepath); //栈操作void pop(sqlist *mazepath);void gettop(sqlist *mazepath);void main(){sqlist mazepath;creatstack(&mazepath); //创建一个存储路径的栈int mazemap[10][10]={1,1,1,1,1,1,1,1,1,1,1,0,0,1,0,0,0,1,0,1,1,0,0,1,0,0,0,1,0,1,1,0,0,0,0,1,1,0,0,1,1,0,1,1,1,0,0,0,0,1,1,0,0,0,1,0,0,0,0,1,1,0,1,0,0,0,1,0,0,1,1,0,1,1,1,0,1,1,0,1,1,1,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1};// creatmap(mazemap); //创建迷宫图纸printf("迷宫原图为:\n");printmap(mazemap);findpath(mazemap,&mazepath); //在mazemap当中寻找mazepaht printf("走出迷宫图纸为:\n");printmap(mazemap);printf("走出迷宫文字叙述为:\n");roadinwords(&mazepath);// printpath(&mazepath);}void findpath(int (*mazemap)[10],sqlist *mazepath){int x,y,flag=0,k=0,next; //位置是否可通,flag=0通,1墙,2通但不可走x=1;y=1; //获取初始位置push(x,y,mazepath); //起点位置进栈footprint(x,y,6,mazemap);while(flag==0 && k!=162) //flag==1到达终点,0未到达终点{if(passroad(x,y+1,mazemap)==0)push(x,y+1,mazepath),y=y+1,footprint(x,y,6,mazemap);else if(passroad(x+1,y,mazemap)==0)push(x+1,y,mazepath),x=x+1,footprint(x,y,6,mazemap);else if(passroad(x,y-1,mazemap)==0)push(x,y-1,mazepath),y=y-1,footprint(x,y,6,mazemap);else if(passroad(x-1,y,mazemap)==0)push(x-1,y,mazepath),x=x-1,footprint(x,y,6,mazemap);elsefootprint(x,y,2,mazemap),pop(mazepath),gettop(mazepath),x=topelem[0],y= topelem[1];// printmap(mazemap);k++;flag=position(x,y); //判断是否到达终点// printf("flag==%d\n",flag);}}void creatstack(sqlist *mazepath){mazepath->base=(int (*)[2])malloc(120*sizeof(int (*)[2]));mazepath->top=mazepath->base;mazepath->listlen=120;}void push(int x,int y,sqlist *mazepath){**(mazepath->top)=x;*(*(mazepath->top)+1)=y;mazepath->top++;}void pop(sqlist *mazepath){if(mazepath->top!=mazepath->base)mazepath->top--;}void printmap(int (*mazemap)[10]){int (*p)[10];p=mazemap;int i,j;printf(" \n\n\n");for(i=0;i<10;i++){for(j=0;j<10;j++){if(j==0)printf(" ");if(*(*(p+i)+j)==0)printf("▇");else if(*(*(p+i)+j)==1)printf("□");else if(*(*(p+i)+j)==6)printf("★");elseprintf("▇");if(j==9)printf("\n");}}printf("\n\n");}void printpath(sqlist *mazepath){int (*p)[2];p=mazepath->base;while(p!=mazepath->top){printf("x=%d,y=%d\n",**p,*(*p+1));p++;}}void gettop(sqlist *mazepath){int (*p)[2];int (*q)[2];p=mazepath->base;while(p!=mazepath->top){q=p;p++;}topelem[0]=**q;topelem[1]=*(*q+1);}void footprint(int x,int y,int k,int (*mazemap)[10]){if(x<10 && y<10)*(*(mazemap+x)+y)=k;}int position(int x,int y){int flag;if(x==8 && y==8)flag=1;elseflag=0;return(flag);}int passroad(int x,int y,int (*mazemap)[10]) {int num=1;if(x<10 && y<10)num=*(*(mazemap+x)+y);return(num);}void roadinwords(sqlist *mazepath){int x=1,y=1,i=0;int (*p)[2];p=mazepath->base;p++;while(p!=mazepath->top){if(x==**p && y+1==*(*p+1))printf("向右走→→"),x=**p,y=*(*p+1);else if(x+1==**p && y==*(*p+1))printf("向下走→→"),x=**p,y=*(*p+1);else if(x==**p && y-1==*(*p+1))printf("向左走→→"),x=**p,y=*(*p+1);else if(x-1==**p && y==*(*p+1))printf("向上走→→"),x=**p,y=*(*p+1);i++;if(i%3==0)printf("\n");p++;}printf("\n");}。

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语言实现深度 优先搜索算法
深度优先搜索算法的基本原理

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();}。

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语言版)

/*注:本程序探索迷宫的优先顺序=> 1-下、2-右、3-上、4-左<=总体趋势:下右,逆时针方向。

因为出口就在右边下方*/#include<stdio.h>#include<stdlib.h>#include<time.h>#define stack_init_size 200#define stack_increment 10#define OVERFLOW 0#define OK 1#define ERROE 0#define TRUE 1#define FALSE 0typedef int Status;typedef struct{int x;int y;}PosType;typedef struct{int ord; // 通道块在路径上的“序号”PosType seat; //通道块在迷宫中的“坐标位置”int di; //从此通道块走向下一通道块的“方向”}SElemType;typedef struct{SElemType *base;SElemType *top;int stacksize;}SqStack;int mg[20][20];/*随机生成迷宫的函数/*为了能够让尽量能通过,将能通过的块和不能通过的块数量比大致为2:1*/ void Random(){int i,j,k;srand(time(NULL));mg[1][0]=mg[1][1]=mg[18][19]=0; //将入口、出口设置为“0”即可通过for(j=0;j<20;j++)mg[0][j]=mg[19][j]=1; /*设置迷宫外围“不可走”,保证只有一个出口和入口*/for(i=2;i<19;i++)mg[i][0]=mg[i-1][19]=1; /*设置迷宫外围“不可走”,保证只有一个出口和入口*/for(i=1;i<19;i++)for(j=1;j<19;j++){k=rand()%3; //随机生成0、1、2三个数if(k)mg[i][j]=0;else{if((i==1&&j==1)||(i==18&&j==18)) /*因为距入口或出口一步的路是必经之路,故设该通道块为“0”加大迷宫能通行的概率*/mg[i][j]=0;elsemg[i][j]=1;}}}//构造一个空栈Status InitStack(SqStack &s){s.base =(SElemType *)malloc(stack_init_size *sizeof(SElemType));if(!s.base) return OVERFLOW;s.top=s.base;s.stacksize=stack_init_size;return OK;}//当前块可否通过Status Pass(PosType e){if (mg[e.x][e.y]==0) //0时可以通过return OK; // 如果当前位置是可以通过,返回1return OVERFLOW; // 其它情况返回0}//留下通过的足迹Status FootPrint(PosType e){mg[e.x][e.y]=7;return OK;}//压入栈Status Push(SqStack &s,SElemType e){if(s.top-s.base>=s.stacksize){s.base=(SElemType *)realloc(s.base,(s.stacksize+stack_increment) *sizeof(SElemType));if(!s.base)exit(OVERFLOW);s.top=s.base+s.stacksize;s.stacksize+=stack_increment;}*s.top++=e;return OK;}//出栈Status Pop(SqStack &s,SElemType &e){ if(s.top==s.base)return ERROE;e=*--s.top;return OK;}//下一步PosType NextPos(PosType &e,int dir){ PosType E;switch(dir){case 1:E.x=e.x; //向下E.y=e.y+1;break;case 2:E.x=e.x+1; //向右E.y=e.y;break;case 3:E.x=e.x; //向上E.y=e.y-1;break;case 4:E.x=e.x-1; //向左E.y=e.y;break;}return E;}//是否空栈Status StackEmpty(SqStack s){if (s.top==s.base)return OK;return OVERFLOW;}//留下不能通过的足迹Status MarkPrint(PosType e){mg[e.x][e.y]=3;return OK;}//迷宫函数// 若迷宫maze中从入口start到出口end的通道,则求得一条存放在栈中// (从栈底到栈顶),并返回TRUE;否则返回FALSEStatus MazePath(int mg,PosType start,PosType end,SqStack &s){PosType curpos;InitStack(s);SElemType e;int curstep;curpos=start; // 设定"当前位置"为"入口位置"curstep=1; // 探索第一步do{if(Pass(curpos)){// 当前位置可通过,即是未曾走到过的通道块FootPrint(curpos); // 留下足迹e.di =1;e.ord = curstep;e.seat= curpos;Push(s,e); // 加入路径if(curpos.x==end.x&&curpos.y==end.y){printf("\n\n0∩_∩0 能到达终点!");return TRUE;}curpos=NextPos(curpos,1); // 下一位置是当前位置的东邻curstep++; // 探索下一步}else{// 当前位置不能通过if(!StackEmpty(s)){Pop(s,e);while(e.di==4&&!StackEmpty(s)){MarkPrint(e.seat);Pop(s,e);}if(e.di<4){e.di++;Push(s,e); // 留下不能通过的标记,并退回一步curpos=NextPos(e.seat,e.di); /* 当前位置设为新方向的相邻块*/}//if}//if}//else}while(!StackEmpty(s));printf("\n\n囧! 不能到达终点!");return FALSE;}//打印迷宫void PrintMaze(){int i,j;printf("运行路径:\n\n");for(i=0;i<20;i++){for(j=0;j<20;j++){if(mg[i][j]==0)printf(" ");else if(mg[i][j]==1) printf("■"); //迷宫的“墙”else if(mg[i][j]==3) printf("◇"); //不通的路else if(mg[i][j]==7)printf("○"); //通过的路径}printf("\n");}printf("\n");}void main(){SqStack S;PosType start,end;start.x=1;start.y=0; //起点坐标end.x=18;end.y=19; //终点坐标printf("\n==================迷宫游戏==================");printf("\n说明:■不能走的区域\t◇走不通的区域");printf("\n “空格”代表未到过的区域");printf("\n ○代表能通过的路径,指向终点");printf("\n============================================");Random();printf("\n\nTest 1:");MazePath(mg[20][20],start,end,S);PrintMaze();system("pause");Random();printf("\nTest 2:");MazePath(mg[20][20],start,end,S);PrintMaze();system("pause");Random();printf("\nTest 3:");MazePath(mg[20][20],start,end,S);PrintMaze();printf("\n==========程序退出,感谢使用!==========\n");}。

C语言编写迷宫

C语言编写迷宫

迷宫源代码:#include <stdio.h>#include <slib.h>#include <malloc.h>#include <conio.h>#include <dos.h>#define OK 1#define ERROR 0#define TRUE 1#define FALSE 0#define F9 0x43#define Esc 0x1b#define Del 0x53#define Home 0x47#define End 0x4f#define Space 0x20#define Up 0x48#define Down 0x50#define Left 0x4b#define Right 0x4d#define Enter 0x0d#define F2 0x3c#define F3 0x3d#define STACK_INIT_SIZE 200 #define STACKINCREMENT 10 typedef int Boolean;typedef int Status;typedef suct {int x;int y;} PosType;typedef suct {int ord;PosType seat;int di;} SElemType;typedef suct {int ;int foot;int mark;} MazeType;typedef suct {SElemType *base; SElemType *top;int stacksize;} Stack;int Maze[20][30];MazeType maze[20][30];PosType StartPlace;PosType EndPlace;int count;int m,n;Boolean b_start=FALSE,b_end=FALSE;void CreatMaze(void);Status SaveMaze(char *filename);Status LoadMaze(char *filename);void Error(char *message);Status InitStack(Stack *s);Status DesoyStack(Stack *s);Status ClearStack(Stack *s);Boolean StackEmpty(Stack *s);int StackLength(Stack *s);Status Push(Stack *s,SElemType e);SElemType Pop(Stack *s,SElemType e);Status GetTop(Stack *s,SElemType *e);Status StackTraverse(Stack *s,Status (* visit)(SElemType *se)); Boolean Pass(PosType curpos);void MarkPrint(PosType seat);void FootPrint(PosType curpos);PosType NextPos(PosType seat,int di);Status MazePath(PosType start,PosType end);void CreatMaze(void)/* Form the maze. */{void Error(char *message);Status SaveMaze(char *filename);Status LoadMaze(char *filename);int i,j;int x,y;char c;char savename[12],loadname[12];Boolean flag=FALSE,load=FALSE;clrscr();printf("Menu:\n\n");printf("1.Load Mazefile:(*.dd)\n\n");printf("2.Input Maze:\n\n");printf("Input your choice: ");do{c=getch();switch(c){case ''''''''''''''''''''''''''''''''1'''''''''''''''''''''''''''''''': putch(''''''''''''''''''''''''''''''''1''''''''''''''''''''''''''''''''); break;case ''''''''''''''''''''''''''''''''2'''''''''''''''''''''''''''''''': putch(''''''''''''''''''''''''''''''''2''''''''''''''''''''''''''''''''); break;case Esc: sleep(1); exit(1);default: break;}}while(c!=''''''''''''''''''''''''''''''''1''''''''''''''''''''''''''''''''&&c!=''''''''''''''''''''''''''''''''2'''''''''''''''''''''''''''''''') ; if(c==''''''''''''''''''''''''''''''''1''''''''''''''''''''''''''''''''){printf("\n\nLoadName: ");scanf("%s",loadname);if(LoadMaze(loadname)){sleep(1); load=TRUE;}else { gotoxy(1,9); printf("Load fail! "); }}if(!load){printf("\nInput the maze''''''''''''''''''''''''''''''''s size:\n");printf("\nInput Length :\n");scanf("%d",&m);printf("\nInput Width :\n");scanf("%d",&n);if(m<4||n<4) Error("Input");if(m>30||n>20) Error("Maze too large");for(i=0;i<30;i )for(j=0;j<20;j )Maze[j][i]=2;StartPlace.x=0;StartPlace.y=0;EndPlace.x=0;EndPlace.y=0;clrscr();BR> for(i=1;i<=n;i ){for(j=1;j<=m;j ){printf(" #");Maze[i-1][j-1]=0;}printf("\n");}}gotoxy(65,5);printf("''''''''''''''''''''''''''''''''#'''''''''''''''''''''''''''''''':Wall");gotoxy(65,7);printf("Start:Home");gotoxy(65,9);printf("End:End");gotoxy(65,11);printf("Delete Wall:Del");gotoxy(65,13);printf("Enter Wall:Enter");gotoxy(65,15);printf("Save Maze:F2");gotoxy(65,17);printf("Complete:F9");gotoxy(65,19);printf("Exit:Esc");gotoxy(4,3);x=4;y=3;do{c=getch();switch(c){case Up: if(y>3) { y--; gotoxy(x,y); }break;case Down: if(y<n) { y ; gotoxy(x,y); }break;case Left: if(x>4) { x-=2; gotoxy(x,y); }break;case Right: if(x<2*m-2) { x =2; gotoxy(x,y); }break;case Del: if(y-2==StartPlace.y&&x/2-1==StartPlace.x) b_start=FALSE; if(y-2==EndPlace.y&&x/2-1==EndPlace.x) b_end=FALSE;putch('''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''''); Maze[y-2][x/2-1]=1; gotoxy(x,y); break;case Enter: if(y-2==StartPlace.y&&x/2-1==StartPlace.x) break;if(y-2==EndPlace.y&&x/2-1==EndPlace.x) break;putch(''''''''''''''''''''''''''''''''#''''''''''''''''''''''''''''''''); Maze[y-2][x/2-1]=0; gotoxy(x,y); break;case Home: if(Maze[y-2][x/2-1]&&!b_start){StartPlace.x=x/2-1;StartPlace.y=y-2;putch(''''''''''''''''''''''''''''''''S'''''''''''''''''''''''''''''''');gotoxy(x,y);b_start=TRUE;}break;case End: if(Maze[y-2][x/2-1]&&!b_end){EndPlace.x=x/2-1;EndPlace.y=y-2;putch(''''''''''''''''''''''''''''''''E'''''''''''''''''''''''''''''''');gotoxy(x,y);b_end=TRUE;}break;case Esc: gotoxy(2,22); printf("exit"); sleep(1); exit(1); case F9: if(b_start&&b_end) flag=TRUE; break;case F2: gotoxy(2,22);printf("Savename:");scanf("%s",savename);gotoxy(2,22);if(SaveMaze(savename)) printf("Save OK! ");else printf("Save fail! ");sleep(1);gotoxy(2,22);printf(" ");gotoxy(x,y);break;default: break;}}while(!flag);for(i=0;i<30;i )for(j=0;j<20;j ){maze[j][i].=Maze[j][i];maze[j][i].mark=0;maze[j][i].foot=0;}}Status LoadMaze(char *file)/* The maze has been loaded. */{FILE *fp;char *buffer;char ch;int i=0,j,k;Boolean len=FALSE,wid=FALSE;if((fp=fopen(file,"r"))==NULL)return ERROR;buffer=(char *)malloc(600*sizeof(char));ch=fgetc(fp);while(ch!=EOF){buffer[i]=ch;i ;ch=fgetc(fp);}m=30;n=20;for(i=0;i<600;i ){j=i/30; k=i0;if(buffer[i]==''''''''''''''''''''''''''''''''2''''''''''''''''''''''''''''''''&&!len){ m=i; len=TRUE; }if(k==0&&buffer[i]==''''''''''''''''''''''''''''''''2''''''''''''''''''''''''''''''''&&!wid){ n=j; wid=TRUE; } switch(buffer[i]){case ''''''''''''''''''''''''''''''''0'''''''''''''''''''''''''''''''': Maze[j][k]=0; break;case ''''''''''''''''''''''''''''''''1'''''''''''''''''''''''''''''''': Maze[j][k]=1; break;case ''''''''''''''''''''''''''''''''2'''''''''''''''''''''''''''''''': Maze[j][k]=2; break;case ''''''''''''''''''''''''''''''''3'''''''''''''''''''''''''''''''': Maze[j][k]=1;StartPlace.x=k;StartPlace.y=j;b_start=TRUE;break;case ''''''''''''''''''''''''''''''''4'''''''''''''''''''''''''''''''': Maze[j][k]=1;EndPlace.x=k;EndPlace.y=j;b_end=TRUE;break;default : break;}}fclose(fp);clrscr();for(i=0;i<30;i )for(j=0;j<20;j ){maze[j][i].=Maze[j][i];maze[j][i].foot=0;maze[j][i].mark=0;if(Maze[j][i]==0){gotoxy(2*i 2,j 2);putch(''''''''''''''''''''''''''''''''#'''''''''''''''''''''''''''''''');}}gotoxy(2*StartPlace.x 2,StartPlace.y 2);putch(''''''''''''''''''''''''''''''''S'''''''''''''''''''''''''''''''');gotoxy(2*EndPlace.x 2,EndPlace.y 2);putch(''''''''''''''''''''''''''''''''E'''''''''''''''''''''''''''''''');return OK;}Status SaveMaze(char *filename)/* The maze has been saved. */{FILE *fp;char *buffer;int i,j,k;fp=fopen(filename,"wb");buffer=(char *)malloc(600*sizeof(char));for(i=0;i<600;i ){j=i/30; k=i0;switch(Maze[j][k]){case 0: buffer[i]=''''''''''''''''''''''''''''''''0''''''''''''''''''''''''''''''''; break;case 1: buffer[i]=''''''''''''''''''''''''''''''''1''''''''''''''''''''''''''''''''; break;case 2: buffer[i]=''''''''''''''''''''''''''''''''2''''''''''''''''''''''''''''''''; break;default : Error("Write"); break;}if(k==StartPlace.x&&j==StartPlace.y) buffer[i]=''''''''''''''''''''''''''''''''3''''''''''''''''''''''''''''''''; if(k==EndPlace.x&&j==EndPlace.y) buffer[i]=''''''''''''''''''''''''''''''''4''''''''''''''''''''''''''''''''; }fwrite(buffer,600,1,fp);free(buffer);fclose(fp);return OK;}void Error(char *message){clrscr();fprintf(serr,"Error:%s\n",message);exit(1);} /* Error */Status InitStack(Stack *s)/* The stack s has been created and is initialized to be empty. */{s->base=(SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType)); if(!s->base) Error("Overflow");s->top=s->base;s->stacksize=STACK_INIT_SIZE;return OK;} /* InitStack */Status DesoyStack(Stack *s)/* The stack s has been desoyed. */{s->top=NULL;s->stacksize=0;free(s->base);s->base=NULL;return OK;} /* DesoyStack */Status ClearStack(Stack *s)/* The stack has been clear to be maximum. */{s->top=s->base;s->stacksize=STACK_INIT_SIZE;return OK;} /* ClearStack */Boolean StackEmpty(Stack *s)/* Check if the stack s is empty. */{if(s->top==s->base) return TRUE;else return FALSE;} /* StackEmpty */int StackLength(Stack *s)/* Gain the length of the stack s. */{if(s->top>s->base) return (int)(s->top-s->base);else return 0;} /* StackLength */Status Push(Stack *s,SElemType e)/* The element e has been pushed into the stack s. */{if(s->top-s->base>=s->stacksize){s->base=(SElemType *)realloc(s->base,(s->stacksize STACKINCREMENT)*sizeof(SElemType));if(!s->base) Error("Overflow");s->top=s->base s->stacksize;s->stacksize =STACKINCREMENT;}*s->top =e;return OK;} /* Push */SElemType Pop(Stack *s,SElemType e)/* The element e has been removed from the stack s. */{if(s->top==s->base) Error("Pop");e=*--s->top;return e;} /* Pop */Status GetTop(Stack *s,SElemType *e)/* The element e has got to the top of the stack s.*/{if(s->top==s->base) Error("GetTop");*e=*(s->top-1);return OK;} /* GetTop *//* Traverse the stack s using ''''''''''''''''''''''''''''''''visiting'''''''''''''''''''''''''''''''' function. */ /* Status StackTraverse(Stack *s,Status (* visit)(SElemType *se)){SElemType p;int result;if(s->top==s->base) return ERROR;p=s->base;while(!(p==s->top)){result=(*visit)(p);p ;}return OK;} */Boolean Pass(PosType curpos)/* Check if the current position can be passed. */{if(maze[curpos.x][curpos.y].==1&&maze[curpos.x][curpos.y].foot==0&&maze[curpos.x][curpos.y].mark==0) return TRUE;else return FALSE;} /* Pass */void MarkPrint(PosType seat)/* Mark the position seat. */{maze[seat.x][seat.y].mark=-1;/* Marking ''''''''''''''''''''''''''''''''-1'''''''''''''''''''''''''''''''' symbolize the current position cannot be put. */} /* MarkPrint */void FootPrint(PosType curpos)/* The foot of the curpos of the maze has been set''''''''''''''''''''''''''''''''ue''''''''''''''''''''''''''''''''. */{maze[curpos.x][curpos.y].foot=1;} /* FootPrint */PosType NextPos(PosType seat,int di){switch(di){case 1: seat.y ; return seat; /* Eastward */case 2: seat.x ; return seat; /* Southward */case 3: seat.y--; return seat; /* Westward */case 4: seat.x--; return seat; /* Northward */default: seat.x=0; seat.y=0; return seat;}} /* NextPos *//* The key to the program. *//* Pre: The maze array & the startplace & the endplace.Post: Find the one averse of the maze and perform the mazepath.Uses: The ADT stack class.*/Status MazePath(PosType start,PosType end){PosType curpos;int curstep;SElemType e;Stack *s,stack;stack.base=(SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType));if(!stack.base) Error("Overflow");stack.top=stack.base;stack.stacksize=STACK_INIT_SIZE;s=&stack;curpos=start;cursteprintf("\n");<'0''''''''''''''''''''''''''''''''; break;case 1: buffer[i]=''''''''''''''''''''''''''''''''1''''''''''''''''''''''''''''''''; break;case 2: buffer[i]=''''''''''''''''''''''''''''''''2''''''''''''''''''''''''''''''''; break;default : Error("Write"); break;}if(k==StartPlace.x&&j==StartPlace.y) buffer[i]=''''''''''''''''''''''''''''''''3''''''''''''''''''''''''''''''''; if(k==EndPlace.x&&j==EndPlace.y) buffer[i]=''''''''''''''''''''''''''''''''4''''''''''''''''''''''''''''''''; }fwrite(buffer,600,1,fp);free(buffer);fclose(fp);return OK;}void Error(char *message){clrscr();fprintf(serr,"Error:%s\n",message);exit(1);} /* Error */Status InitStack(Stack *s)/* The stack s has been created and is initialized to be empty. */{s->base=(SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType));if(!s->base) Error("Overflow");s->top=s->base;s->stacksize=STACK_INIT_SIZE;return OK;} /* InitStack */Status DesoyStack(Stack *s)/* The stack s has been desoyed. */{s->top=NULL;s->stacksize=0;free(s->base);s->base=NULL;return OK;} /* DesoyStack */Status ClearStack(Stack *s)/* The stack has been clear to be maximum. */{s->top=s->base;s->stacksize=STACK_INIT_SIZE;return OK;} /* ClearStack */Boolean StackEmpty(Stack *s)/* Check if the stack s is empty. */{if(s->top==s->base) return TRUE;else return FALSE;} /* StackEmpty */int StackLength(Stack *s)/* Gain the length of the stack s. */{if(s->top>s->base) return (int)(s->top-s->base);else return 0;} /* StackLength */Status Push(Stack *s,SElemType e)/* The element e has been pushed into the stack s. */ {if(s->top-s->base>=s->stacksize){s->base=(SElemType *)realloc(s->base,(s->stacksize STACKINCREMENT)*sizeof(SElemType)); if(!s->base) Error("Overflow");s->top=s->base s->stacksize;s->stacksize =STACKINCREMENT;}*s->top =e;return OK;} /* Push */SElemType Pop(Stack *s,SElemType e)/* The element e has been removed from the stack s. */ {if(s->top==s->base) Error("Pop");e=*--s->top;return e;} /* Pop */Status GetTop(Stack *s,SElemType *e)/* The element e has got to the top of the stack s.*/{if(s->top==s->base) Error("GetTop");*e=*(s->top-1);return OK;} /* GetTop *//* Traverse the stack s using ''''''''''''''''''''''''''''''''visiting'''''''''''''''''''''''''''''''' function. *//* Status StackTraverse(Stack *s,Status (* visit)(SElemType *se)){SElemType p;int result;if(s->top==s->base) return ERROR;p=s->base;while(!(p==s->top)){result=(*visit)(p);p ;}return OK;} */Boolean Pass(PosType curpos)/* Check if the current position can be passed. */{if(maze[curpos.x][curpos.y].==1&&maze[curpos.x][curpos.y].foot==0&&maze[curpos.x][curpos.y].mark==0) return TRUE;else return FALSE;} /* Pass */void MarkPrint(PosType seat)/* Mark the position seat. */{maze[seat.x][seat.y].mark=-1;/* Marking ''''''''''''''''''''''''''''''''-1'''''''''''''''''''''''''''''''' symbolize the current position cannot be put. */} /* MarkPrint */void FootPrint(PosType curpos)/* The foot of the curpos of the maze has been set''''''''''''''''''''''''''''''''ue''''''''''''''''''''''''''''''''. */{maze[curpos.x][curpos.y].foot=1;} /* FootPrint */PosType NextPos(PosType seat,int di){switch(di){case 1: seat.y ; return seat; /* Eastward */case 2: seat.x ; return seat; /* Southward */case 3: seat.y--; return seat; /* Westward */case 4: seat.x--; return seat; /* Northward */default: seat.x=0; seat.y=0; return seat;}} /* NextPos *//* The key to the program. *//* Pre: The maze array & the startplace & the endplace.Post: Find the one averse of the maze and perform the mazepath.Uses: The ADT stack class.*/Status MazePath(PosType start,PosType end){PosType curpos;int curstep;SElemType e;Stack *s,stack;stack.base=(SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType)); if(!stack.base) Error("Overflow");stack.top=stack.base;stack.stacksize=STACK_INIT_SIZE;s=&stack;curpos=start;curstep=1;do{if(Pass(curpos)){FootPrint(curpos);e.ord=curstep; e.seat=curpos; e.di=1;gotoxy((curpos.y 1)*2,curpos.x 2);putch(''''''''''''''''''''''''''''''''@'''''''''''''''''''''''''''''''');delay(8000); /* pospone time. */Push(s,e);if(curpos.x==end.x&&curpos.y==end.y) /* Proceed recursively. */{DesoyStack(s);return TRUE;}curpos=NextPos(curpos,1); /* Try next position. */curstep ;}else{if(!StackEmpty(s)){e=Pop(s,e); /* Removed e from s. */while(e.di==4&&!StackEmpty(s)) /* Four directions have been checked and s is not empty. */{MarkPrint(e.seat);gotoxy((e.seat.y 1)*2,e.seat.x 2);putch(''''''''''''''''''''''''''''''''@'''''''''''''''''''''''''''''''');delay(8000); /* Pospone time. */gotoxy((e.seat.y 1)*2,e.seat.x 2);putch('''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''');e=Pop(s,e); /* Remove e from s. */curstep--;}if(e.di<4) /* The current position hasnot been checked. */{e.di ;Push(s,e); /* Insert e into s. */curpos=NextPos(e.seat,e.di); /* Try next position. */}}}}while(!StackEmpty(s));DesoyStack(s);return FALSE;} /* MazePath */void main(){PosType start,end;CreatMaze();start.x=StartPlace.y;start.y=StartPlace.x;end.x=EndPlace.y;end.y=EndPlace.x;if(MazePath(start,end)){gotoxy(2,22);printf("Path found\n");}else{gotoxy(2,22);printf("Path not found\n");}。

迷宫(direction)C语言代码

迷宫(direction)C语言代码
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为障碍
voidpush_seq(PSeqStackpastack,DataTypex ) {
if(pastack->t >= MAXNUM - 1 )
printf( "Overflow! \n" );
else {
pastack->t++;
pastack->s[pastack->t] = x;
}
}
/* 删除栈顶元素 */
element =top_seq(st);
pop_seq(st);
i=element.x; j =element.y;
for (k =element.d+ 1; k <= 3; k++) { /* 依次试探每个方向 */
g =i+ direction[k][0];h = j + direction[k][1];
lj[top].x=1;
lj[top].y=1;
migong[1][1]=-1;
find=0;d=-1;
while(top>-1){
if(lj[top].x==8&&lj[top].y==8)
{
printf("迷宫路径如下:\n");

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语言迷宫代码

c语言迷宫代码C语言迷宫代码是指用C语言编写的程序,用于生成和解决迷宫问题的算法。

迷宫通常由一个矩形网格组成,其中包含墙壁和通道。

目标是找到从迷宫的起点到终点的路径,同时避开墙壁。

下面是一个简单的示例代码,用于生成迷宫:```c#include <stdio.h>#include <stdlib.h>#include <stdbool.h>#define ROWS 10#define COLS 10typedef struct {int x;int y;} Point;void generateMaze(int maze[ROWS][COLS]) {for (int i = 0; i < ROWS; i++) {for (int j = 0; j < COLS; j++) {if (i % 2 == 0 || j % 2 == 0) { maze[i][j] = 1; // 墙壁} else {maze[i][j] = 0; // 通道}}}}void printMaze(int maze[ROWS][COLS]) {for (int i = 0; i < ROWS; i++) {for (int j = 0; j < COLS; j++) {printf('%d ', maze[i][j]);}printf('');}}int main() {int maze[ROWS][COLS];generateMaze(maze);printMaze(maze);return 0;}```在上面的代码中,我们使用一个二维数组来表示迷宫。

数组中的值为1表示墙壁,值为0表示通道。

使用generateMaze函数,我们将迷宫的墙壁和通道初始化为适当的值。

然后使用printMaze函数打印迷宫。

通过运行上面的代码,我们可以得到一个简单的迷宫的表示:```1 1 1 1 1 1 1 1 1 11 0 1 0 1 0 1 0 1 11 1 1 1 1 1 1 1 1 11 0 1 0 1 0 1 0 1 11 1 1 1 1 1 1 1 1 11 0 1 0 1 0 1 0 1 11 1 1 1 1 1 1 1 1 11 0 1 0 1 0 1 0 1 11 1 1 1 1 1 1 1 1 11 0 1 0 1 0 1 0 1 1```当然,上述代码只是生成了一个简单的迷宫,还没有解决迷宫问题。

迷宫游戏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 <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');}。

迷宫c语言

迷宫c语言

迷宫c语言#include<stdio.h>#include<conio.h>#include<windows.h>#include<time.h>#定义权31//迷宫的高度必须是奇数#定义的高度25//迷宫的宽度必须是奇数#定义的高度1#定义的高度0#定义的高度3#定义的高度5#定义的宽度1#定义的宽度2#定义的宽度3#定义的宽度4intmap[height+2][width+2];voidgotoxy(intx,inty)//移动坐标{合作社;库德。

x=x;库德。

y=y;setconsolecursorposition(getstdhandle(std_output_handle),coord);}Void hidden()//隐藏光标{handlehout=getstdhandle(std_output_handle);console_cursor_infocci;GetConsoleUrsorInfo(hout和cci);cci。

bvisible=0;//指定1显示,指定0隐藏设置控制台或信息(hout,&CCI);}voidcreate(intx,inty)//随机生成迷宫{intc[4][2]={0,1,1,0,0,-1,-1,0};//四个方向inti,J,t//分解(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++)如果(映射[x+2*c[i][0][y+2*c[i][1]]==墙){map[x+c[i][0]][y+c[i][1]]=road;create(x+2*c[i][0],y+2*c[i][1]);}}intget_uokey()//接收密钥{charc;while(C=getch()){if(c==27)returnesc;//escif(c!=-32)continue;c=getch();如果(c==72)返回;//上if(C==80)returndown//下if(C==75)returneft//左if(c==77)returnright;//右}return0;}虚空绘制(intx,inty)//绘制迷宫{gotoxy(2*y-2,x-1);switch(map[x][y]){casestart:Printf(“in”);中断;//图纸输入框结束:printf("出");break;//画出口casewall:printf(“※”);中断;//画墙凯斯路:printf("");break;//画路}}虚空游戏(){intx=2,y=1;//玩家当前位置,刚开始在入口处intc;//接收键(1时){gotoxy(2*y-2,x-1);printf(“☆“”;//如果(map[x][y]==end)//判断是否到达出口,则绘制玩家的当前位置{gotoxy(30,24);Printf(“到达终点时,按任意键结束”);getch();打破;}c=get_key();if(c==esc){gotoxy(0,24);打破;}开关(c){caseup://向上走if(map[x-1][y]!=wall)。

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();}。

C++迷宫游戏源代码

C++迷宫游戏源代码
for(int j=1;j<=n;j++){
if(maze[i][j]==-1)
maze[i][j]=0;
}
}
}
int path1(int **maze,int m,int n,int c,int d,int x1,int y1)//最短路径
{ //m,n为迷宫的ze为迷宫;
front++; //当前点搜索完,取下一个点搜索
} //while
G:cout<<"无路径。"<<endl;
return 0;
}
void path(int **maze,int a,int b,int m,int n)
{
item move[8]={{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1},{-1,0},{-1,1}};
#include<iostream>
#include<stack>
#include<stdio.h>
#include<time.h>
#include<string>
using namespace std;
typedef struct
{
int x,y;
}item;
typedef struct
}
if(i==x1&&j==y1){
cout<<"最短路径为:"<<endl;
printpath(sq,rear); //输出路径;
restore(maze,m,n); //恢复迷宫;

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();}。

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

o e n g l D迷宫C实现源代码The latest revision on November 22, 2020#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();}。

相关文档
最新文档