OpenGL实现3D魔方游戏源代码

本文分享了一款使用OpenGL编写的3D魔方游戏程序,详细介绍了项目设置,包括配置属性和附加依赖项。游戏通过键盘输入控制魔方旋转,提供cubemanage.h、wcgcube.h等源文件。文章最后展示了游戏的最终效果,并推荐了作者的人工智能教程。
摘要由CSDN通过智能技术生成

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

               

        首先这个程序是建立的是Windows应用程序,建立控制台程序是不能运行的,另外,项目——项目属性——配置属性——常规-----使用多字节字符集,这样编译才能够通过的,否则如果选择使用 Unicode 字符集,编译会有错误提示:error C2440: “初始化”: 无法从“const char [8]”转换为“LPCTSTR”,另外,链接器----输入----附加依赖项要加入:“opengl32.lib glu32.lib”的lib库。。

cubemanage.h文件为:

#ifndef CUBEMANAGE_H#define CUBEMANAGE_H#include <windows.h>#include <gl/gl.h>#include <gl/glu.h>#include <math.h>#include "wcgcube.h"#define CUBE_SIZE  3#define ORIENTX 0#define ORIENTY 0#define ORIENTZ 0class CubeManage {
    public:  CubeManage(); ~CubeManage();  void turn(int rotateType)void turnByXShun(int x)void turnByXNi(int x)void turnByYShun(int y)void turnByYNi(int y)void turnByZShun(int z)void turnByZNi(int z)void output(int scr,int site)void output()void draw(int rotateType,GLfloat rotate)private: WcgCube *cubes[CUBE_SIZE][CUBE_SIZE][CUBE_SIZE];  void goStep(int *leftLeg,int *rightLeg,int *goDirection,int step,int leftEdge,int rightEdge);};#endif

wcgcube.h文件为:

#ifndef WCGCUBE_H#define WCGCUBE_H#include <windows.h>#include <gl/gl.h>#include <gl/glu.h>#include <math.h>#include "iostream"using namespace std;#define X 1#define Y 2#define Z 3class WcgCube {
    public: WcgCube();  ~WcgCube();  void turnByXShun(int x)void turnByXNi(int x)void turnByYShun(int y)void turnByYNi(int y)void turnByZShun(int z)void turnByZNi(int z)void output(int sign)void output()void draw(GLfloat x0,GLfloat y0,GLfloat z0)privateint direct[6]; GLfloat sideColor[6][3];  void turnByX(int x,int sign)void turnByY(int y,int sign)void turnByZ(int z,int sign);};#endif

CubeGame.cpp文件为:

#include <windows.h>#include <winuser.h>#include <gl/gl.h>#include <gl/glu.h>#include <math.h>#include "iostream"using namespace std;#include "cubemanage.h"#include "wcgcube.h"static GLfloat PI=3.1415f;// Rotation amountsstatic GLfloat xRot = 0.0f;static GLfloat yRot = 0.0f;static GLfloat rotate=0.0f;static int rotateType=0;static int rotateOK=0;static int rotateRate=100;static GLfloat rotateStep=5*PI/180;CubeManage cm;HPALETTE hPalette = NULL;// Keep track of windows changing width and heightGLfloat windowWidth;GLfloat windowHeight;static LPCTSTR lpszAppName = "WcgCube";void exitGame(HWND hWnd,HDC hDC,HGLRC hRC);// Declaration for Window procedureLRESULT CALLBACK WndProc( HWND  hWnd,       UINT message,       WPARAM wParam,       LPARAM lParam);// Set Pixel Format function - forward declarationvoid SetDCPixelFormat(HDC hDC);void ChangeSize(GLsizei w, GLsizei h) { GLfloat nRange = 350.0f// Prevent a divide by zero if(h == 0)  h = 1// Set Viewport to window dimensions    glViewport(0, 0, w, h); // Reset coordinate system glMatrixMode(GL_PROJECTION); glLoadIdentity(); // Establish clipping volume (left, right, bottom, top, near, far)    if (w <= h)   glOrtho (-nRange, nRange, -nRange*h/w, nRange*h/w, -nRange, nRange);    else   glOrtho (-nRange*w/h, nRange*w/h, -nRange, nRange, -nRange, nRange); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); }// Called by timer routine to effect movement of the rectangle.void IdleFunction(void) if (rotate>=PI/2) {  cm.turn(rotateType);  rotateType=0;  rotateOK=0;  rotate=0.0f;  // Refresh the Window//  glutPostRedisplay();  return; } rotate+=rotateStep;  // Refresh the Window// glutPostRedisplay(); }// Called by AUX library to draw scenevoid RenderScene(void) // Clear the window with current clearing color glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);   glPushMatrix();  glRotatef(xRot, 1.0f, 0.0f, 0.0f); glRotatef(yRot, 0.0f, 1.0f, 0.0f); cm.draw(rotateType,rotate);  glPopMatrix(); // Show the graphics// glutSwapBuffers(); }// If necessary, creates a 3-3-2 palette for the device context listed.HPALETTE GetOpenGLPalette(HDC hDC) { HPALETTE hRetPal = NULL// Handle to palette to be created PIXELFORMATDESCRIPTOR pfd; // Pixel Format Descriptor LOGPALETTE *pPal;   // Pointer to memory for logical palette int nPixelFormat;   // Pixel format index int nColors;    // Number of entries in palette int i;      // Counting variable BYTE RedRange,GreenRange,BlueRange;        // Range for each color entry (7,7,and 3) // Get the pixel format index and retrieve the pixel format description nPixelFormat = GetPixelFormat(hDC); DescribePixelFormat(hDC, nPixelFormat, sizeof(PIXELFORMATDESCRIPTOR), &pfd); // Does this pixel format require a palette?  If not, do not create a // palette and just return NULL if(!(pfd.dwFlags & PFD_NEED_PALETTE))  return NULL// Number of entries in palette.  8 bits yeilds 256 entries nColors = 1 << pfd.cColorBits;  // Allocate space for a logical palette structure plus all the palette entries pPal = (LOGPALETTE*)malloc(sizeof(LOGPALETTE) +nColors*sizeof(PALETTEENTRY)); // Fill in palette header  pPal->palVersion = 0x300;  // Windows 3.0 pPal->palNumEntries = nColors; // table size // Build mask of all 1's.  This creates a number represented by having // the low order x bits set, where x = pfd.cRedBits, pfd.cGreenBits, and // pfd.cBlueBits.   RedRange = (1 << pfd.cRedBits) -1; GreenRange = (1 << pfd.cGreenBits) - 1; BlueRange = (1 << pfd.cBlueBits) -1
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
作者对游戏的说明: 首先,您应当以一种批判的眼光来看待本程序。这个游戏是我制作 的第一部RPG游戏,无任何经验可谈,完全按照自己对游戏的理解进 行设计的。当我参照了《圣剑英雄2》的源码之后,才体会到专业游 戏引擎的博大精深。 该程序的内核大约有2000余行,能够处理人物的行走、对话、战斗, 等等。由于该程序的结构并不适于这种规模的程序,故不推荐您详 细研究该程序。所附地图编辑器的源程序我已经添加了详细的注释, 其程序结构也比较合理,可以作为初学VC的例子。 该程序在VC的程序向导所生成的SDI框架的基础上修改而成。它没有 使用任何关于VC底层的东西。程序的绝大部分都是在CgameView类中 制作的,只有修改窗口特征的一段代码在CMainFrm类中。其他的类 统统没有用到。另外添加的一个类是CEnemy类。 整个游戏的故事情节分成8段,分别由Para1.h ~ Para8.h八个文件 实现。由于程序仅仅能够被动的处理各种各样的消息,所以情节的 实现也只能根据系统的一些参数来判断当前应当做什么。在程序中 使用了冗长的if……else if……结构来实现这种判断。 当然,在我的记录本上,详细的记录了每个事件的判断条件。这种 笨拙的设计当然是不可取的。成都金点所作《圣剑英雄II》采用了 剧本解读的方式,这才是正统的做法。但这也需要更多的编程经验 和熟练的code功夫。 下面列举的是程序编制过程中总结出来的经验和教训。 第一,对话方式应该采用《圣剑英雄II》的剧本方式。 现在的方式把一个段落中所有的对话都混在一个文件中,然后给每 句话一个号码相对应。这样做虽然降低了引擎的难度,却导致剧情的 编写极其繁琐。 第二,运动和显示应当完全分开。 现在的程序中,运动和显示是完全同步的。即:在定时器中调用所有 敌人的运动函数,然后将主角的动画向前推一帧,接着绘制地图,调 用所有敌人的显示函数、重绘主角。这样的好处是不会掉帧,但带来 的问题是,如果要提高敌人的运动速度,那么帧数也跟着上去了。所 以当DEMO版反馈说速度太慢的时候,我修改起来非常困难。而这个问 题到最后也仅仅是将4步一格该成了2步一格。 第三,VC中数组存在上限。如果用“int aaa[1000000000]”定义一个 数组,编译器肯定不会给分配那么大的内存空间。而在这个程序中, 地图矩阵、NPC矩阵都超过了VC中数组的上限。但这一点知道的太晚了。 在1.0版本中已经发现地图最右端缺少了几行,但不知道是什么原因 造成的。(地图编辑器中未出现此问题,因为地图编辑器是用“序列 化”的方式存盘读盘的。)解决这个问题的方法是用“new”来分配 内存空间。 第四,由于不知道应该如何使用“new”和“delete”,几乎所有的DC 都使用了全局变量。这是完全没有必要的。程序运行期大约会耗用20 多M的内存空间,相当于一个大型游戏所使用的内存空间了。 另外,在游戏的剧情、美工方面也有许多问题,总之一个词“业余”。 我就不总结了。下一部作品,我将争取在程序上有一个质的飞跃。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值