




已阅读5页,还剩14页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
共分为4个.h文件和5个.cpp文件1、 keyListener.h,keyListener.cpp2、 map.h,map.cpp3、 texture.h,texture.cpp4、 textview.h,textview.cpp5、 Main.cpp1、 Main.cpp#include #include #include #include #include #include map.h#include Texture.h#include keyListener.h#include textview.h#pragma comment(linker,/subsystem:Windows /entry:mainCRTStartup)Texture texture; / 文理加载类Map map; / 地图加载类KeyListener keylistener; /键盘监听事件类TextView textview;int* image;int *mp;int window; /窗口int is_pass = 0; / 是否过关标志int *pass = &is_pass;int level = 1; / 关卡int *lv = &level;int pre_lv = 1;/颜色的RGB值int black3 = 0 , 0 , 0;int white3 = 255 , 255 , 255;int orange3 = 255, 128, 0;/窗口大小变化时调用的函数 void ChangeSize(GLsizei w, GLsizei h) if(h = 0) h = 1; glViewport(0, 0, w, h); /定义视口大小,宽高一致 int width = 550; int height = 550; glMatrixMode(GL_PROJECTION); /重置坐标系统,使投影变换复位 glLoadIdentity(); /将当前的用户坐标系的原点移到了屏幕中心 GLfloat aspect; if(w h) aspect = (GLfloat) w / h; glOrtho(0, width * aspect, 0, height, 0, width); void display_text()glColor3ub(orange0,orange1,orange2);glRasterPos2i(250, 570); /起始位置 textview.drawString(推箱子); /输出的字符串 glRasterPos2i(400, 570); char c10 = 第;char d5 ;sprintf(d, %d关, level);strcat(c, d); textview.drawString(c); glRasterPos2i(120, 80); textview.drawString(1、ESC键退出游戏); glRasterPos2i(120, 60); textview.drawString(2、BackSpace后退); glRasterPos2i(120, 40); textview.drawString(3、PageUp、PageDown切换关卡); glRasterPos2i(120, 20); textview.drawString(4、键盘上、下、左、右键控制方向); /画长方形void draw_rect(int leftX, int leftY)glBegin(GL_POLYGON); glColor3ub(black0,black1,black2); glVertex2d(leftX,leftY); /左下角 glVertex2d(leftX+100,leftY); /右下角 glVertex2d(leftX+100,leftY+100); /右上角 glVertex2d(leftX,leftY+100); /左上角 glEnd();/结束画线void drawmap()glColor3ub(white0,white1,white2); /清理颜色,为黑色,(也可认为是背景颜色) glEnable(GL_TEXTURE_2D); for (int i = 50; i 500; i += 30)for (int j = 100; j 550; j += 30)int y = (i - 50) / 30;int x =14 - (j - 100) / 30;int pos = mpxy;if(pos = 6)keylistener.get_man_location(x, y);texture.display_texture(imagepos, i, j);display_text();if(pre_lv != level)pre_lv = level;draw_rect(400, 570);/glutSwapBuffers();glFlush();if(is_pass)MessageBox(NULL, 恭喜过关, 提示, 0);is_pass = 0;level +;map.load_map(level);keylistener.stay_origin_map(); / 保留原地图void load_map_and_picture()mp = new int*15;for(int i = 0; i 15; i+)mpi = new int15;for(int j = 0; j 15; j+)mpij = 0;image = new int10;for(int i = 0; i 10; i+)imagei = 0;/ 加载地图map.getmap(mp);map.load_map(level); /键盘相应事件keylistener.get_map(mp);keylistener.get_level(lv);keylistener.get_window(window);keylistener.get_pass_flag(pass);/加载图片texture.get_image(image);texture.load_texture();int main(int argc, char *argv)glutInit(&argc, argv);glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);glutInitWindowSize(550, 600); /设置窗口大小glutInitWindowPosition(100, 100); / 设置初始窗口的位置window = glutCreateWindow(推箱子);glutDisplayFunc(&drawmap);load_map_and_picture();glutIdleFunc(&drawmap); / 对键盘事件的响应glutSpecialFunc(keylistener.SpecialKeys); glutKeyboardFunc(keylistener.keyPressed);/参数为一个函数,当窗口大小改变时会被调用 glutReshapeFunc(ChangeSize);glutMainLoop();2、keylistener.h#include #include #include #include class KeyListenerpublic:KeyListener();KeyListener();static void SpecialKeys(GLint key, GLint x, GLint y);/键盘的上下左右键static void keyPressed(unsigned char key, int x, int y);/键盘响应事件static void get_man_location(int x, int y);static void get_window(int wnd);static void get_map(int *map);static void move(int dir); /移动一步static bool is_pass(); /判断是否过关static void get_pass_flag(int *ispass);static void get_level(int *lv);static void stay_origin_map();static int window;static int dir_x4 ;static int dir_y4 ;static int direction; /当前方向static int manX, manY; /人所处的位置 static int *mp;static int map1515;static int* level;static int* pass;2、 keylistener.cpp#include keyListener.h#include map.h#include #include #include #define ESCAPE 27#define BACKSPACE 8#define UP 0#define DOWN 1#define LEFT 2#define RIGHT 3int KeyListener:window = 0;/上下左右int KeyListener:dir_x4 = -1, 1, 0, 0;int KeyListener:dir_y4 = 0, 0, -1, 1;int KeyListener:direction = 1; /当前方向int KeyListener:manX = 0;int KeyListener:manY = 0;int *KeyListener:mp = NULL;int KeyListener:map1515 = 0;int *KeyListener:level = NULL;int *KeyListener:pass = NULL;Map MAP;std:stack s;KeyListener:KeyListener()KeyListener:KeyListener()void KeyListener:keyPressed(unsigned char key, int x, int y)switch(key)case : break;case ESCAPE:glutDestroyWindow(window); exit(0); break;case BACKSPACE: /后退一步if(s.size() = 1 )break;int cur_dir = s.top(); s.pop(); /当前坐标的方向/当前坐标int mx = s.top(); s.pop();int my = s.top(); s.pop();int cur_picture = s.top(); s.pop(); /当前坐标cur_dir方向的图片int next_picture = s.top(); s.pop(); /前一个坐标以前图片int pre_dir = s.top();int dx = dir_xcur_dir;int dy = dir_ycur_dir;mpmxmy = cur_picture;mpmx + dxmy + dy = next_picture;mpmx - dxmy - dy = pre_dir + 5;manX = mx - dx;manY = my - dy;break;default:break;/键盘的上下左右键void KeyListener:SpecialKeys(GLint key, GLint x, GLint y) switch(key)case GLUT_KEY_UP: /向上移动 direction = UP;move(direction);break;case GLUT_KEY_DOWN: /向下移动direction = DOWN;move(direction);break;case GLUT_KEY_LEFT: /向左移动direction = LEFT;move(direction);break;case GLUT_KEY_RIGHT: /向右移动direction = RIGHT;move(direction);break;case GLUT_KEY_PAGE_UP: /下一关(*level) +;if(*level) 13)(*level) -;break;/ 加载地图MAP.getmap(mp);MAP.load_map(*level); stay_origin_map();break;case GLUT_KEY_PAGE_DOWN: /上一关(*level) -;if(*level) 1)(*level) +;break;/ 加载地图MAP.getmap(mp);MAP.load_map(*level); stay_origin_map();break;default:break;void KeyListener:move(int dir)int x = manX;int y = manY;int dx = dir_xdir;int dy = dir_ydir;manX += dx;manY += dy;int origin_picture = mpmanXmanY;int next_picture = mpmanX + dxmanY + dy;/dir方向是箱子,箱子dir方向是墙或箱子if(mpmanXmanY = 4 | mpmanXmanY = 3) & (mpmanX + dxmanY + dy = 2 | mpmanX + dxmanY + dy = 3 | mpmanX + dxmanY + dy = 4)manX -= dx, manY -= dy;return ;/dir方向是墙if(mpmanXmanY = 2)manX -= dx, manY -= dy;return ;/dir方向是草地或者目的地if(mpmanXmanY = 1 | mpmanXmanY = 9) mpmanXmanY = dir + 5;if(mapxy = 9) /原来位置是目的地mpxy = 9; else mpxy = 1; /原来位置是草地/dir方向是箱子,箱子dir方向是草地if(mpmanXmanY = 4 | mpmanXmanY = 3) & mpmanX + dxmanY + dy = 1)mpmanXmanY = dir + 5;if(mpmanXmanY = 3) /若是移动已达目的地的箱子,原箱子位置值为9mpmanXmanY = 9;if(mapxy = 9) /原来位置是目的地mpxy = 9;else /原来位置是草地mpxy = 1;mpmanX + dxmanY + dy = 4; /草地位置变箱子 /dir方向是箱子,箱子dir方向是目的地if(mpmanXmanY = 4 | mpmanXmanY = 3) & mpmanX + dxmanY + dy = 9) mpmanXmanY = dir + 5;if(mpmanXmanY = 3) /若是移动一大目的地的箱子,原箱子位置值为9mpmanXmanY = 9;if(mapxy = 9) /原来位置是目的地mpxy = 9;else /原来位置是草地mpxy=1;mpmanX + dxmanY + dy = 3; /目的地位置变箱子if(is_pass()*pass = 1;/每走一步保存当前位置的坐标、以前的图片、s.push(next_picture);s.push(origin_picture);s.push(manY);s.push(manX);s.push(dir);void KeyListener:get_man_location(int x, int y)manX = x;manY = y;void KeyListener:get_window(int wnd)window = wnd;void KeyListener:get_level(int *lv)level = lv;void KeyListener:get_map(int *Mp)mp = Mp;stay_origin_map();void KeyListener:stay_origin_map()while(!s.empty()s.pop();direction = 1;s.push(direction);for(int i = 0; i 15; i+)for(int j = 0; j 15; j+)mapij = mpij;if(mapij = 3)mapij = 9;void KeyListener:get_pass_flag(int *ispass)pass = ispass;bool KeyListener:is_pass()int p = 0;for (int i = 0; i 15; i+)for (int j = 0; j 15; j+)if(mpij = 4) p+;if (p = 0) return true;return false;3、 map.hclass Mappublic :int *mp;Map();Map();void get_map(char *filepath);void load_map(int level);void getmap(int *map);4、 map.cpp#include #include #include #include map.hchar root_directory100 = F:source; /地图、图片根目录Map:Map() Map:Map() delete mp;void Map:get_map(char *filepath)char buffer1024 = ;FILE *fp = fopen(filepath, r);if(fp = NULL) MessageBox(NULL, 地图加载失败, ERROR, 0); return ; int row = 0;while(!feof(fp)char *ch = fgets(buffer, 1024, fp);if(ch = NULL)return ;for(int i = 0; i 15; i+)mprowi = bufferi- 48;row+;void Map:load_map(int level)char path100 = ;strcpy(path, root_directory);char d6;itoa(level, d, 10);strcat(d,.txt);strcat(path, d);get_map(path);void Map:getmap(int *map)mp = map;5、 textview.hclass TextViewpublic:TextView();TextView();void drawString(char *str);6、 textview.cpp#include textview.h#include #include #include TextView:TextView()TextView:TextView()void TextView:drawString(char *str)int len, i; wchar_t* wstring; HDC hDC = wglGetCurrentDC(); GLuint list = glGenLists(1); / 计算字符的个数 / 如果是双字节字符的(比如中文字符),两个字节才算一个字符 / 否则一个字节算一个字符 len = 0; for(i=0; stri!=0; +i) if( IsDBCSLeadByte(stri) ) +i; +len; / 将混合字符转化为宽字符 wstring = (wchar_t*)malloc(len+1) * sizeof(wchar_t); MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, str, -1, wstring, len); wstringlen = L0; / 逐个输出字符 for(i=0; ilen; +i) wglUseFontBitmapsW(hDC, wstringi, 1, list); glCallList(list); / 回收所有临时资源 free(wstring); glDeleteLists(list, 1);7、 texture.hclass Texturepublic:int* image;Texture();Texture();int get_texture(char *filename);void load_texture();void display_texture(int img, int x, int y);void get_image(int *img);8、 texture.cpp#include #include #include #include Texture.h#pragma comment(lib,glaux.lib)char root100 = F:source; /地图、图片根目录Texture:Texture()Texture:Texture()delete image;int Texture:get_texture(char *filename)BITMAP bm; GLuint idTexture ; CImage img; /需要头文件atlimage.h HRESULT hr = img.Load(filename); if ( !SUCCEEDED(hr) ) /文件加载失败 MessageBox(NULL, 图片加载失败, ERROR, 0); return NULL; HBITMAP hbmp = img; if(!GetObject(hbmp, sizeof(bm), &bm) return 0; glGenTextures(1, &idTexture); glBindTexture(GL_TEXTURE_2D, idTexture); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexPa
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 跟骨骨折经皮复位与置钉知识2025
- 中国古代文学作品选辅导
- DeepSeek大模型在医学影像诊断智能识别中的应用方案
- 2025年四川省绵阳市安州八年级中考一模生物试题 (含答案)
- 2025年甘肃省武威市凉州区松树、永昌九年制学校中考三模语文试题(含答案)
- 2025年广东省初中毕业生学业考试英语模拟试题(文字版无答案)
- 后置埋件安装垂直度技术专题
- 2025合同能源管理EMC(EPC)融资及信用保障
- 2025年北京市二手车交易合同
- 2025艺人经纪合同范文
- 四川省(科大讯飞大数据)2025届高三第二次教学质量联合测评物理试题及答案
- 抽水台班记录表
- 2019年房地产估价师试题及答案:《理论与方法》
- 中油即时通信安装手册(二厂)
- 临床医生做科研(课堂PPT)
- 西北工业大学台湾交换生入学申请表
- 【精品】沟渠整治工程施工方案
- 新员工入职试岗考核表
- 2010年全国辐射环境监测方案
- 隧道工程测量毕业设计论文
- 一次函数应用题专项练习(含答案)
评论
0/150
提交评论