#include<iostream>
#include<stdlib.h>
#include <math.h>
#include <time.h>
#include <gl/glut.h>
#define RIGHT 40 //設定迷宮的長寬
#define BOTTOM 40
class grid //每一格的結構,顯示該格的哪個邊該消失
{
public:
bool left, right, top, bottom, already;
grid()
{
bottom=1;
left=1;
right=1;
top=1;
already=0;
}
void Draw(int x, int y)
{
if(top==1)
{
glBegin(GL_LINES);
glVertex2f(-3.5+ 7.0/RIGHT * x , 3.5- 7.0/BOTTOM * y);
glVertex2f(-3.5+ 7.0/RIGHT * (x+1), 3.5- 7.0/BOTTOM * y);
glEnd();
}
if(bottom==1)
{
glBegin(GL_LINES);
glVertex2f(-3.5+ 7.0/RIGHT * x , 3.5- 7.0/BOTTOM * (y+1));
glVertex2f(-3.5+ 7.0/RIGHT * (x+1), 3.5- 7.0/BOTTOM * (y+1));
glEnd();
}
if(left==1)
{
glBegin(GL_LINES);
glVertex2f(-3.5+ 7.0/RIGHT * x , 3.5- 7.0/BOTTOM * y);
glVertex2f(-3.5+ 7.0/RIGHT * x , 3.5- 7.0/BOTTOM * (y+1));
glEnd();
}
if(right==1)
{
glBegin(GL_LINES);
glVertex2f(-3.5+ 7.0/RIGHT * (x+1), 3.5- 7.0/BOTTOM * y);
glColor4f(0.8f, 0.8f, 0.8f, 1.0f);
glVertex2f(-3.5+ 7.0/RIGHT * (x+1), 3.5- 7.0/BOTTOM * (y+1));
glEnd();
}
}
};
struct node
{
int data;
node *next;
};
class stack //堆疊結構
{
node *top;
public:
node* add(int x)
{
node* newNode = new node;
newNode->next = top;
top = newNode;
set(x);
return newNode;
}
void del()
{
node* temp = top;
top = top->next;
delete temp;
}
node* get()
{
return top;
}
void set(int x)
{
top->data=x;
}
};
grid game[RIGHT][BOTTOM];
stack goal;
void Drawline(int x, int y , int xx, int yy)
{
glColor4f(1.0f, 0,0,1.0f);
glBegin(GL_LINES);
glVertex2f(-3.5+ 7.0/RIGHT * (x+0.5) , 3.5- 7.0/BOTTOM * (y+0.5) );
glVertex2f(-3.5+ 7.0/RIGHT * (xx+0.5), 3.5- 7.0/BOTTOM * (yy+0.5) );
glEnd();
}
void gamelogic()
{
stack save;
srand( time(0) );//亂數初始化
int x=0,y=0,max=0,dead;
game[0][0].already=1;
while(max < RIGHT*BOTTOM-1)
{
dead=0;
(x+1<RIGHT)? dead+=game[x+1][y].already:dead++;
(x-1>=0)? dead+=game[x-1][y].already:dead++;
(y+1<BOTTOM)?dead+=game[x][y+1].already:dead++;
(y-1>=0)? dead+=game[x][y-1].already:dead++;
if(dead>=4)
{
if(x==RIGHT-1 && y==BOTTOM-1)
{
goal.add(y);
goal.add(x);
node* baffer=save.get();
int xx,yy;
do
{
yy = baffer->data;
xx = baffer->next->data;
goal.add(xx);
goal.add(yy);
baffer= baffer->next->next;
}
while(! (xx==0 && yy==0) );
}
y=save.get()->data;
save.del();
x=save.get()->data;
save.del();
}
switch(rand()%4)
{
case 0:
if(y-1 < 0 || game[x][y-1].already==1)
continue;
save.add(x);
save.add(y);
game[x][y].top=0;
//game[x][y].Draw(x,y);
game[x][--y].bottom=0;
game[x][y].already=1;
break;
case 1:
if(y+1 >= BOTTOM || game[x][y+1].already==1)
continue;
save.add(x);
save.add(y);
game[x][y].bottom=0;
//game[x][y].Draw(x,y);
game[x][++y].top=0;
game[x][y].already=1;
break;
case 2:
if(x-1 < 0 || game[x-1][y].already==1)
continue;
save.add(x);
save.add(y);
game[x][y].left=0;
//game[x][y].Draw(x,y);
game[--x][y].right=0;
game[x][y].already=1;
break;
case 3:
if(x+1 >= RIGHT || game[x+1][y].already==1)
continue;
save.add(x);
save.add(y);
game[x][y].right=0;
//game[x][y].Draw(x,y);
game[++x][y].left=0;
game[x][y].already=1;
break;
}
max++;
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);//清除指定的緩衝區
for(int i=0; i<RIGHT; i++)
for(int j=0; j<BOTTOM; j++)
if(game[i][j].already)
game[i][j].Draw(i,j);
glutSwapBuffers();
}
game[0][0].left=0;
game[RIGHT-1][BOTTOM-1].right=0;
}
void Display()
{
static bool yes=0;
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);//清除指定的緩衝區
if(!yes)
{
yes=!yes;
gamelogic();
}
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);//清除指定的緩衝區
for(int i=0;i<RIGHT;i++)
for(int j=0;j<BOTTOM;j++)
game[i][j].Draw(i,j);
while(! (goal.get()->data==BOTTOM-1 && goal.get()->next->data==RIGHT-1) ) //重劃尚有問題
{
int x,y, xx, yy;
y=goal.get()->data;
goal.del();
x=goal.get()->data;
goal.del();
yy=goal.get()->data;
xx=goal.get()->next->data;
Drawline(x,y,xx,yy);
}
glutSwapBuffers();
}
void myReshape(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= h)
glOrtho(-4.0, 4.0, -4.0 * (GLfloat) h / (GLfloat) w,
4.0 * (GLfloat) h / (GLfloat) w, -10.0, 10.0);
else
glOrtho(-4.0 * (GLfloat) w / (GLfloat) h,
4.0 * (GLfloat) w / (GLfloat) h, -4.0, 4.0, -10.0, 10.0);
}
void main()
{
glutInitDisplayMode(GLUT_DEPTH |GLUT_DOUBLE | GLUT_RGBA);//用一個單一緩衝區的視窗,且用RGB的色彩模式
glutInitWindowSize(600, 600); //視窗的大小 x,y
glutInitWindowPosition(250, 100); //建立窗口的位置
glutCreateWindow("maze");//設定視窗標題
glClearColor(0.1f ,0.1f ,0.1f ,1.0f);//依設定的顏色清除視窗
glEnable(GL_DEPTH_TEST);
glutReshapeFunc(myReshape);
glutDisplayFunc(Display);//表示GLUT會在視窗需要繪圖時呼叫此處所傳入的函數指標
glutMainLoop();//讓GLUT架構開始運作,處理所有與作業系統相關的訊息、按鍵等等直到程式結束
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
opengl-hw.zip (11个子文件)
test
╖s╝WñσªrñσÑ≤.plg 1KB
╖s╝WñσªrñσÑ≤.opt 48KB
╖s╝WñσªrñσÑ≤.ncb 123KB
╖s╝WñσªrñσÑ≤.suo 9KB
╖s╝WñσªrñσÑ≤.sln 913B
╖s╝WñσªrñσÑ≤.exe 96KB
╖s╝WñσªrñσÑ≤.cpp 5KB
╖s╝WñσªrñσÑ≤.vcproj 4KB
╖s╝WñσªrñσÑ≤.dsw 549B
╖s╝WñσªrñσÑ≤.dsp 4KB
www.pudn.com.txt 218B
共 11 条
- 1
资源评论
朱moyimi
- 粉丝: 75
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功