oenglD迷宮C實現(xiàn)源代碼_第1頁
oenglD迷宮C實現(xiàn)源代碼_第2頁
oenglD迷宮C實現(xiàn)源代碼_第3頁
已閱讀5頁,還剩15頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

1、#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 S

2、TARTING_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 heading float player_s = 0.0f; / forward speed of the playerfloat player_m = 1.0f; / speed multiplier of the playerfloat pl

3、ayer_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 keystate4=false,false,false,false;char mazedataMAZE_HEIGHTMAZE_WIDTH = riiii iiii i

4、iii iiii iiii iiii iiii iiii iiii iiii iiii iiii iiii iiii iiii iiin 'H','H','H','H','H','H','H','H','H','H','H','H','H','H','H','H','H',' ',' ',

5、9; ',' ',' ',' ',' ','H',' ',' ',' ',' ',' ',' ','H',I I I, j j j j j j ,I I, j j j j j ,I i 門r'H' ' ' 'Ll' ' ' 'Ll' 'Ll' 'Ll' ' ' '

6、Ll' ' ' Lin'H',' ','H',' ','H','H','H',' ','H',' ','H',' ',' ',' ',' ','H',r'H' ' ' 'Ll' ' ' 'Ll' 'Ll' ' 

7、9; 'Ll' ' ' 'Ll' ' ' 'Lin'H',' ','H','H',' ',' ','H',' ','H','H',' ','H',' ','H',' ','H','H',' ',' ',' '

8、,' ',' ','H',' ',' ',' ',' ',' ',' ','H',' ','H',I I I, J J J J ,I I, J J J J J ,I I,I I 門riiii i i iiii iiii iiii iiii iiii iiii iiii iiii i i iiii iiii iiii i i iiin'H',' ','H',&

9、#39;H','H','H','H','H','H','H',' ','H','H','H',' ','H','H',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',&

10、#39;H',' ',' ','H',I I I, J J J J J J J J J J ,I I, J ,I I 門riiii i i iiii iiii iiii iiii iiii i i iiii iiii iiii i i iiii iiii iiii iiin'H',' ','H','H','H','H','H',' ','H','H','H',

11、9; ','H','H','H','H',r'Li' ' ' Lin'H',' ','H',' ',' ',' ','H',' ',' ',' ','H',' ',' ',' ',' ','H', 'Ll' 'Ll&

12、#39; 'Ll' 'Ll' 'Ll' Lin'H',' ',' ',' ','H','H','H','H','H','H','H',' ',' ',' ',' ','H',r'Li' > > > > 'Lin'H',' &

13、#39;,'H',' ',' ',' ','H',' ',' ',' ','H',' ',' ','H',' ','H',riiii i i iiii iiii iiii iiii iiii i i iiii iiii iiii iiii i i iiii i i iiin'H',' ','H','H',

14、9;H','H','H',' ','H','H','H','H',' ','H',' ','H','H',' ',' ',' ',' ',' ','H',' ',' ',' ',' ',' ',' ',

15、9;H',' ','H',I I I, j j j j ,I I, j j j j j ,I I,I i 門riiii iiii iiii iiii i i iiii iiii iiii iiii i i iiii iiii iiii i i iiin'H',' ',' ','H','H',' ','H','H','H','H',' ','H','H'

16、;,'H',' ','H', ' ' Lin'H',' ',' ',' ','H',' ','H',' ',' ',' ',' ','H',' ',' ',' ','H',riiii iiii iiii iiii iiii iiii iiii iiii iiii iiii iii

17、i iiii iiii i i iiii iiin'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(

18、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);glLoadId

19、entity();gluPerspective(60.0, 1.0 , 0.1, 60.0);glMatrixMode(GL_MODELVIEW);glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);/?di 2?± ?D?bool wall(int x,int y) return(x>=0 && y>=0 && x<MAZE_W I DT H && y<MAZE_HEIGHT&&mazedatayx!=' ');bool onopen

20、(int x,int y)if(wall(x,y)return(mazedatayx='H'); void closeit(int x,int y) if(onopen(x,y)mazedatayx= '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; brea

21、k;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;clos

22、eit(x,y);doint 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

23、 = (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,

24、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) mazedatayx= '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 )

25、;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

26、)+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

27、),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.

28、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_CO

29、LOR_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(

30、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);gl

31、LoadIdentity();glutPostRedisplay();void specialKeys(int key,int x,int y)switch (key)case GLUT_KEY_LEFT: keystate2 = true; player_t = -2.0f; break;case GLUT_KEY_RIGHT: keystate3 = true; player_t = 2.0f; break;case GLUT_KEY_UP: keystate0 = true; player_s = 0.01f; break;case GLUT_KEY_DOWN:keystate1 = true;player_s = -0.01f;break;defau

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論