1. 程式人生 > >OpenGL實現3D魔方遊戲原始碼

OpenGL實現3D魔方遊戲原始碼

        首先這個程式是建立的是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 0

class 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 3

class 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);
	
private:
	int 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 amounts
static 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 height
GLfloat windowWidth;
GLfloat windowHeight;


static LPCTSTR lpszAppName = "WcgCube";

void exitGame(HWND hWnd,HDC hDC,HGLRC hRC);
// Declaration for Window procedure
LRESULT CALLBACK WndProc(	HWND 	hWnd,
							UINT	message,
							WPARAM	wParam,
							LPARAM	lParam);

// Set Pixel Format function - forward declaration
void 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 scene
void 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;

	// Loop through all the palette entries
	for(i = 0; i < nColors; i++)
		{
		// Fill in the 8-bit equivalents for each component
		pPal->palPalEntry[i].peRed = (i >> pfd.cRedShift) & RedRange;
		pPal->palPalEntry[i].peRed = (unsigned char)(
			(double) pPal->palPalEntry[i].peRed * 255.0 / RedRange);

		pPal->palPalEntry[i].peGreen = (i >> pfd.cGreenShift) & GreenRange;
		pPal->palPalEntry[i].peGreen = (unsigned char)(
			(double)pPal->palPalEntry[i].peGreen * 255.0 / GreenRange);

		pPal->palPalEntry[i].peBlue = (i >> pfd.cBlueShift) & BlueRange;
		pPal->palPalEntry[i].peBlue = (unsigned char)(
			(double)pPal->palPalEntry[i].peBlue * 255.0 / BlueRange);

		pPal->palPalEntry[i].peFlags = (unsigned char) NULL;
		}
		

	// Create the palette
	hRetPal = CreatePalette(pPal);

	// Go ahead and select and realize the palette for this device context
	SelectPalette(hDC,hRetPal,FALSE);
	RealizePalette(hDC);

	// Free the memory used for the logical palette structure
	free(pPal);

	// Return the handle to the new palette
	return hRetPal;
	}


// Select the pixel format for a given device context
void SetDCPixelFormat(HDC hDC)
	{
	int nPixelFormat;

	static PIXELFORMATDESCRIPTOR pfd = {
		sizeof(PIXELFORMATDESCRIPTOR),	// Size of this structure
		1,								// Version of this structure	
		PFD_DRAW_TO_WINDOW |			// Draw to Window (not to bitmap)
		PFD_SUPPORT_OPENGL |			// Support OpenGL calls in window
		PFD_DOUBLEBUFFER,				// Double buffered mode
		PFD_TYPE_RGBA,					// RGBA Color mode
		32,								// Want 32 bit color 
		0,0,0,0,0,0,					// Not used to select mode
		0,0,							// Not used to select mode
		0,0,0,0,0,						// Not used to select mode
		16,								// Size of depth buffer
		0,								// Not used to select mode
		0,								// Not used to select mode
		0,	            				// Not used to select mode
		0,								// Not used to select mode
		0,0,0 };						// Not used to select mode

	// Choose a pixel format that best matches that described in pfd
	nPixelFormat = ChoosePixelFormat(hDC, &pfd);

	// Set the pixel format for the device context
	SetPixelFormat(hDC, nPixelFormat, &pfd);
	}

void dealKey(HWND hWnd,HDC hDC,HGLRC hRC,int wParam)
{
	switch (wParam)
	{
	case 27:
		exitGame(hWnd,hDC,hRC);
		break;
	case 113:                       //q
		if (rotateOK==1)
			return;
		rotateType=1;
		rotateOK=1;
		rotate=0.0f;
		break;
	case 119:						//w
		if (rotateOK==1)
			return;
		rotateType=2;
		rotateOK=1;
		rotate=0.0f;
		break;
	case 101:						//e
		if (rotateOK==1)
			return;
		rotateType=3;
		rotateOK=1;
		rotate=0.0f;
		break;
	case 114:						//r
		if (rotateOK==1)
			return;
		rotateType=4;
		rotateOK=1;
		rotate=0.0f;
		break;
	case 116:						//t
		if (rotateOK==1)
			return;
		rotateType=5;
		rotateOK=1;
		rotate=0.0f;
		break;
	case 121:						//y
		if (rotateOK==1)
			return;
		rotateType=6;
		rotateOK=1;
		rotate=0.0f;
		break;
	case 97:						//a
		if (rotateOK==1)
			return;
		rotateType=7;
		rotateOK=1;
		rotate=0.0f;
		break;
	case 115:						//s
		if (rotateOK==1)
			return;
		rotateType=8;
		rotateOK=1;
		rotate=0.0f;
		break;
	case 100:						//d
		if (rotateOK==1)
			return;
		rotateType=9;
		rotateOK=1;
		rotate=0.0f;
		break;
	case 102:						//f
		if (rotateOK==1)
			return;
		rotateType=10;
		rotateOK=1;
		rotate=0.0f;
		break;
	case 103:						//g
		if (rotateOK==1)
			return;
		rotateType=11;
		rotateOK=1;
		rotate=0.0f;
		break;
	case 104:						//h
		if (rotateOK==1)
			return;
		rotateType=12;
		rotateOK=1;
		rotate=0.0f;
		break;
	case VK_UP:	
		xRot-= 5.0f;
		break;
	case VK_DOWN:
		xRot += 5.0f;
		break;
	case VK_LEFT:
		yRot -= 5.0f;
		break;
	case VK_RIGHT:
		yRot += 5.0f;
		break;
	}
	if(xRot > 356.0f)
		xRot = 0.0f;

	if(xRot < -1.0f)
		xRot = 355.0f;

	if(yRot > 356.0f)
		yRot = 0.0f;

	if(yRot < -1.0f)
		yRot = 355.0f;
}

void exitGame(HWND hWnd,HDC hDC,HGLRC hRC)
{
			// Kill the timer that we created
			KillTimer(hWnd,101);

			// Deselect the current rendering context and delete it
			wglMakeCurrent(hDC,NULL);
			wglDeleteContext(hRC);

			// Delete the palette
			if(hPalette != NULL)
				DeleteObject(hPalette);

			// Tell the application to terminate after the window
			// is gone.
			PostQuitMessage(0);
}

// Entry point of all Windows programs
int APIENTRY WinMain(	HINSTANCE 	hInstance,
						HINSTANCE 	hPrevInstance,
						LPSTR 		lpCmdLine,
						int			nCmdShow)
	{
	MSG			msg;		// Windows message structure
	WNDCLASS	wc;			// Windows class structure
	HWND		hWnd;		// Storeage for window handle
	HWND		hDesktopWnd;// Storeage for desktop window handle
	HDC			hDesktopDC; // Storeage for desktop window device context
	int			nScreenX, nScreenY; // Screen Dimensions

	// Register Window style
	wc.style			= CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
	wc.lpfnWndProc		= (WNDPROC) WndProc;
	wc.cbClsExtra		= 0;
	wc.cbWndExtra		= 0;
	wc.hInstance 		= hInstance;
	wc.hIcon			= NULL;
	wc.hCursor			= LoadCursor(NULL, IDC_ARROW);
	
	// No need for background brush for OpenGL window
	wc.hbrBackground	= NULL;		
	
	wc.lpszMenuName		= NULL;
	wc.lpszClassName	= lpszAppName;

	// Register the window class
	if(RegisterClass(&wc) == 0)
		return FALSE;

    // Get he Window handle and Device context to the desktop
	hDesktopWnd = GetDesktopWindow();
	hDesktopDC = GetDC(hDesktopWnd);

    // Get the screen size
	nScreenX = GetDeviceCaps(hDesktopDC, HORZRES);
	nScreenY = GetDeviceCaps(hDesktopDC, VERTRES);

    // Release the desktop device context
    ReleaseDC(hDesktopWnd, hDesktopDC);

	// Create the main application window
	hWnd = CreateWindow(
				lpszAppName,
				lpszAppName,
				
				// OpenGL requires WS_CLIPCHILDREN and WS_CLIPSIBLINGS
				WS_POPUP | WS_CLIPCHILDREN | WS_CLIPSIBLINGS,
	
				// Window position and size
				0, 0,
				nScreenX, nScreenY,
				NULL,
				NULL,
				hInstance,
				NULL);


	// If window was not created, quit
	if(hWnd == NULL)
		return FALSE;


	// Display the window
	ShowWindow(hWnd,SW_SHOW);
	UpdateWindow(hWnd);

	// Process application messages until the application closes
	while( GetMessage(&msg, NULL, 0, 0))
		{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
		}

	return msg.wParam;
	}



// Window procedure, handles all messages for this program
LRESULT CALLBACK WndProc(	HWND 	hWnd,
							UINT	message,
							WPARAM	wParam,
							LPARAM	lParam)
	{
	static HGLRC hRC;		// Permenant Rendering context
	static HDC hDC;			// Private GDI Device context

	switch (message)
	   	{
		// Window creation, setup for OpenGL
		case WM_CREATE:
			// Store the device context
			hDC = GetDC(hWnd);		

			// Select the pixel format
			SetDCPixelFormat(hDC);		

			// Create the rendering context and make it current
			hRC = wglCreateContext(hDC);
			wglMakeCurrent(hDC, hRC);

			// Create the palette
			hPalette = GetOpenGLPalette(hDC);
	// Black background
	glClearColor(0.0f, 0.0f, 0.0f, 1.0f );

	glEnable(GL_DEPTH_TEST);	
//	glEnable(GL_DITHER);
	glShadeModel(GL_SMOOTH);

			// Create a timer that fires 30 times a second
			SetTimer(hWnd,33,1,NULL);
			break;

		// Window is being destroyed, cleanup
		case WM_DESTROY:
			exitGame(hWnd,hDC,hRC);
			break;
        
		case WM_KEYDOWN:
			dealKey(hWnd,hDC,hRC,wParam);
			InvalidateRect(hWnd,NULL,FALSE);
			break;

		case WM_CHAR:
			dealKey(hWnd,hDC,hRC,wParam);
			InvalidateRect(hWnd,NULL,FALSE);
			break;
		
		// Window is resized.
		case WM_SIZE:
			// Call our function which modifies the clipping
			// volume and viewport
			ChangeSize(LOWORD(lParam), HIWORD(lParam));
			break;

		// Timer, moves and bounces the rectangle, simply calls
		// our previous OnIdle function, then invalidates the 
		// window so it will be redrawn.
		case WM_TIMER:
			{
			IdleFunction();
		
			InvalidateRect(hWnd,NULL,FALSE);
			}
			break;

		// The painting function.  This message sent by Windows 
		// whenever the screen needs updating.
		case WM_PAINT:
			{
			// Call OpenGL drawing code
			RenderScene();

			// Call function to swap the buffers
			SwapBuffers(hDC);

			// Validate the newly painted client area
			ValidateRect(hWnd,NULL);
			}
			break;


		// Windows is telling the application that it may modify
		// the system palette.  This message in essance asks the 
		// application for a new palette.
		case WM_QUERYNEWPALETTE:
			// If the palette was created.
			if(hPalette)
				{
				int nRet;

				// Selects the palette into the current device context
				SelectPalette(hDC, hPalette, FALSE);

				// Map entries from the currently selected palette to
				// the system palette.  The return value is the number 
				// of palette entries modified.
				nRet = RealizePalette(hDC);

				// Repaint, forces remap of palette in current window
				InvalidateRect(hWnd,NULL,FALSE);

				return nRet;
				}
			break;

	
		// This window may set the palette, even though it is not the 
		// currently active window.
		case WM_PALETTECHANGED:
			// Don't do anything if the palette does not exist, or if
			// this is the window that changed the palette.
			if((hPalette != NULL) && ((HWND)wParam != hWnd))
				{
				// Select the palette into the device context
				SelectPalette(hDC,hPalette,FALSE);

				// Map entries to system palette
				RealizePalette(hDC);
				
				// Remap the current colors to the newly realized palette
				UpdateColors(hDC);
				return 0;
				}
			break;


        default:   // Passes it on if unproccessed
            return (DefWindowProc(hWnd, message, wParam, lParam));

        }

    return (0L);
	}

cubemanage.cpp檔案為:

#include "iostream"
using namespace std;

#include "cubemanage.h"

CubeManage::CubeManage(){
	for (int i=0;i<CUBE_SIZE;i++) {
		for (int j=0;j<CUBE_SIZE;j++) {
			for (int k=0;k<CUBE_SIZE;k++) {
				cubes[i][j][k]=new WcgCube();
			}
		}
	}
}

CubeManage::~CubeManage(){
	for (int i=0;i<CUBE_SIZE;i++) {
		for (int j=0;j<CUBE_SIZE;j++) {
			for (int k=0;k<CUBE_SIZE;k++) {
				delete cubes[i][j][k];
			}
		}
	}
}

void CubeManage::turn(int rotateType) {	
	if (rotateType==1) {
		turnByZShun(2);
	}
	else
	if (rotateType==2) {
		turnByXShun(2);
	}
	else
	if (rotateType==3) {
		turnByZNi(0);
	}
	else
	if (rotateType==4) {
		turnByXNi(0);
	}
	else
	if (rotateType==5) {
		turnByYShun(2);
	}
	else
	if (rotateType==6) {
		turnByYNi(0);
	}
	else
	if (rotateType==7) {
		turnByZNi(2);
	}
	else
	if (rotateType==8) {
		turnByXNi(2);
	}
	else
	if (rotateType==9) {
		turnByZShun(0);
	}
	else
	if (rotateType==10) {
		turnByXShun(0);
	}
	else
	if (rotateType==11) {
		turnByYNi(2);
	}
	else
	if (rotateType==12) {
		turnByYShun(0);
	}
}

void CubeManage::draw(int rotateType,GLfloat rotate) {
	GLfloat PI=3.1415f;
	GLfloat cubeRadium=10.0f;
	GLfloat cubeSpace=2.0f;
	GLfloat x,y,z;
	int i,j,k;

	x=ORIENTX-(CUBE_SIZE/2)*(cubeRadium*2+cubeSpace);
	y=ORIENTZ-(CUBE_SIZE/2)*(cubeRadium*2+cubeSpace);
	z=ORIENTZ-(CUBE_SIZE/2)*(cubeRadium*2+cubeSpace);
	
	if (rotateType==0) {
		for (i=0;i<CUBE_SIZE;i++) {
			for (j=0;j<CUBE_SIZE;j++) {
				for (k=0;k<CUBE_SIZE;k++) {
					(cubes[i][j][k])->draw(x+(cubeRadium*2+cubeSpace)*i,y+(cubeRadium*2+cubeSpace)*j,z+(cubeRadium*2+cubeSpace)*k);
				}
			}
		}
	}
	else
	if (rotateType==1) {
  	glPushMatrix();
  	glRotatef(360-180*rotate/PI,0.0f,0.0f,1.0f);
		for (i=0;i<CUBE_SIZE;i++) {
			for (j=0;j<CUBE_SIZE;j++) {
				(cubes[i][j][CUBE_SIZE-1])->draw(x+(cubeRadium*2+cubeSpace)*i,y+(cubeRadium*2+cubeSpace)*j,z+(cubeRadium*2+cubeSpace)*(CUBE_SIZE-1));
			}
		}
  	glPopMatrix();
		for (i=0;i<CUBE_SIZE;i++) {
			for (j=0;j<CUBE_SIZE;j++) {
				for (k=0;k<CUBE_SIZE-1;k++) {
					(cubes[i][j][k])->draw(x+(cubeRadium*2+cubeSpace)*i,y+(cubeRadium*2+cubeSpace)*j,z+(cubeRadium*2+cubeSpace)*k);
				}
			}
		}
	}
	else
	if (rotateType==2) {
  	glPushMatrix();
  	glRotatef(360-180*rotate/PI,1.0f,0.0f,0.0f);
		for (i=0;i<CUBE_SIZE;i++) {
			for (j=0;j<CUBE_SIZE;j++) {
				(cubes[CUBE_SIZE-1][i][j])->draw(x+(cubeRadium*2+cubeSpace)*(CUBE_SIZE-1),y+(cubeRadium*2+cubeSpace)*i,z+(cubeRadium*2+cubeSpace)*j);
			}
		}
  	glPopMatrix();
		for (i=0;i<CUBE_SIZE-1;i++) {
			for (j=0;j<CUBE_SIZE;j++) {
				for (k=0;k<CUBE_SIZE;k++) {
					(cubes[i][j][k])->draw(x+(cubeRadium*2+cubeSpace)*i,y+(cubeRadium*2+cubeSpace)*j,z+(cubeRadium*2+cubeSpace)*k);
				}
			}
		}
	}
	else
	if (rotateType==3) {
  	glPushMatrix();
  	glRotatef(360-180*rotate/PI,0.0f,0.0f,-1.0f);
		for (i=0;i<CUBE_SIZE;i++) {
			for (j=0;j<CUBE_SIZE;j++) {
				(cubes[i][j][0])->draw(x+(cubeRadium*2+cubeSpace)*i,y+(cubeRadium*2+cubeSpace)*j,z);
			}
		}
  	glPopMatrix();
		for (i=0;i<CUBE_SIZE;i++) {
			for (j=0;j<CUBE_SIZE;j++) {
				for (k=1;k<CUBE_SIZE;k++) {
					(cubes[i][j][k])->draw(x+(cubeRadium*2+cubeSpace)*i,y+(cubeRadium*2+cubeSpace)*j,z+(cubeRadium*2+cubeSpace)*k);
				}
			}
		}
	}
	else
	if (rotateType==4) {
  	glPushMatrix();
  	glRotatef(360-180*rotate/PI,-1.0f,0.0f,0.0f);
		for (i=0;i<CUBE_SIZE;i++) {
			for (j=0;j<CUBE_SIZE;j++) {
				(cubes[0][i][j])->draw(x,y+(cubeRadium*2+cubeSpace)*i,z+(cubeRadium*2+cubeSpace)*j);
			}
		}
  	glPopMatrix();
		for (i=1;i<CUBE_SIZE;i++) {
			for (j=0;j<CUBE_SIZE;j++) {
				for (k=0;k<CUBE_SIZE;k++) {
					(cubes[i][j][k])->draw(x+(cubeRadium*2+cubeSpace)*i,y+(cubeRadium*2+cubeSpace)*j,z+(cubeRadium*2+cubeSpace)*k);
				}
			}
		}
	}
	else
	if (rotateType==5) {
  	glPushMatrix();
  	glRotatef(360-180*rotate/PI,0.0f,1.0f,0.0f);
		for (i=0;i<CUBE_SIZE;i++) {
			for (j=0;j<CUBE_SIZE;j++) {
				(cubes[i][CUBE_SIZE-1][j])->draw(x+(cubeRadium*2+cubeSpace)*i,y+(cubeRadium*2+cubeSpace)*(CUBE_SIZE-1),z+(cubeRadium*2+cubeSpace)*j);
			}
		}
  	glPopMatrix();
		for (i=0;i<CUBE_SIZE;i++) {
			for (j=0;j<CUBE_SIZE-1;j++) {
				for (k=0;k<CUBE_SIZE;k++) {
					(cubes[i][j][k])->draw(x+(cubeRadium*2+cubeSpace)*i,y+(cubeRadium*2+cubeSpace)*j,z+(cubeRadium*2+cubeSpace)*k);
				}
			}
		}
	}
	else
	if (rotateType==6) {
  	glPushMatrix();
  	glRotatef(360-180*rotate/PI,0.0f,-1.0f,0.0f);
		for (i=0;i<CUBE_SIZE;i++) {
			for (j=0;j<CUBE_SIZE;j++) {
				(cubes[i][0][j])->draw(x+(cubeRadium*2+cubeSpace)*i,y,z+(cubeRadium*2+cubeSpace)*j);
			}
		}
  	glPopMatrix();
		for (i=0;i<CUBE_SIZE;i++) {
			for (j=1;j<CUBE_SIZE;j++) {
				for (k=0;k<CUBE_SIZE;k++) {
					(cubes[i][j][k])->draw(x+(cubeRadium*2+cubeSpace)*i,y+(cubeRadium*2+cubeSpace)*j,z+(cubeRadium*2+cubeSpace)*k);
				}
			}
		}
	}
	else
	if (rotateType==7) {
  	glPushMatrix();
  	glRotatef(180*rotate/PI,0.0f,0.0f,1.0f);
		for (i=0;i<CUBE_SIZE;i++) {
			for (j=0;j<CUBE_SIZE;j++) {
				(cubes[i][j][CUBE_SIZE-1])->draw(x+(cubeRadium*2+cubeSpace)*i,y+(cubeRadium*2+cubeSpace)*j,z+(cubeRadium*2+cubeSpace)*(CUBE_SIZE-1));
			}
		}
  	glPopMatrix();
		for (i=0;i<CUBE_SIZE;i++) {
			for (j=0;j<CUBE_SIZE;j++) {
				for (k=0;k<CUBE_SIZE-1;k++) {
					(cubes[i][j][k])->draw(x+(cubeRadium*2+cubeSpace)*i,y+(cubeRadium*2+cubeSpace)*j,z+(cubeRadium*2+cubeSpace)*k);
				}
			}
		}
	}
	else
	if (rotateType==8) {
  	glPushMatrix();
  	glRotatef(180*rotate/PI,1.0f,0.0f,0.0f);
		for (i=0;i<CUBE_SIZE;i++) {
			for (j=0;j<CUBE_SIZE;j++) {
				(cubes[CUBE_SIZE-1][i][j])->draw(x+(cubeRadium*2+cubeSpace)*(CUBE_SIZE-1),y+(cubeRadium*2+cubeSpace)*i,z+(cubeRadium*2+cubeSpace)*j);
			}
		}
  	glPopMatrix();
		for (i=0;i<CUBE_SIZE-1;i++) {
			for (j=0;j<CUBE_SIZE;j++) {
				for (k=0;k<CUBE_SIZE;k++) {
					(cubes[i][j][k])->draw(x+(cubeRadium*2+cubeSpace)*i,y+(cubeRadium*2+cubeSpace)*j,z+(cubeRadium*2+cubeSpace)*k);
				}
			}
		}
	}
	else
	if (rotateType==9) {
  	glPushMatrix();
  	glRotatef(180*rotate/PI,0.0f,0.0f,-1.0f);
		for (i=0;i<CUBE_SIZE;i++) {
			for (j=0;j<CUBE_SIZE;j++) {
				(cubes[i][j][0])->draw(x+(cubeRadium*2+cubeSpace)*i,y+(cubeRadium*2+cubeSpace)*j,z);
			}
		}
  	glPopMatrix();
		for (i=0;i<CUBE_SIZE;i++) {
			for (j=0;j<CUBE_SIZE;j++) {
				for (k=1;k<CUBE_SIZE;k++) {
					(cubes[i][j][k])->draw(x+(cubeRadium*2+cubeSpace)*i,y+(cubeRadium*2+cubeSpace)*j,z+(cubeRadium*2+cubeSpace)*k);
				}
			}
		}
	}
	else
	if (rotateType==10) {
  	glPushMatrix();
  	glRotatef(180*rotate/PI,-1.0f,0.0f,0.0f);
		for (i=0;i<CUBE_SIZE;i++) {
			for (j=0;j<CUBE_SIZE;j++) {
				(cubes[0][i][j])->draw(x,y+(cubeRadium*2+cubeSpace)*i,z+(cubeRadium*2+cubeSpace)*j);
			}
		}
  	glPopMatrix();
		for (i=1;i<CUBE_SIZE;i++) {
			for (j=0;j<CUBE_SIZE;j++) {
				for (k=0;k<CUBE_SIZE;k++) {
					(cubes[i][j][k])->draw(x+(cubeRadium*2+cubeSpace)*i,y+(cubeRadium*2+cubeSpace)*j,z+(cubeRadium*2+cubeSpace)*k);
				}
			}
		}
	}
	else
	if (rotateType==11) {
  	glPushMatrix();
  	glRotatef(180*rotate/PI,0.0f,1.0f,0.0f);
		for (i=0;i<CUBE_SIZE;i++) {
			for (j=0;j<CUBE_SIZE;j++) {
				(cubes[i][CUBE_SIZE-1][j])->draw(x+(cubeRadium*2+cubeSpace)*i,y+(cubeRadium*2+cubeSpace)*(CUBE_SIZE-1),z+(cubeRadium*2+cubeSpace)*j);
			}
		}
  	glPopMatrix();
		for (i=0;i<CUBE_SIZE;i++) {
			for (j=0;j<CUBE_SIZE-1;j++) {
				for (k=0;k<CUBE_SIZE;k++) {
					(cubes[i][j][k])->draw(x+(cubeRadium*2+cubeSpace)*i,y+(cubeRadium*2+cubeSpace)*j,z+(cubeRadium*2+cubeSpace)*k);
				}
			}
		}
	}
	else
	if (rotateType==12) {
  	glPushMatrix();
  	glRotatef(180*rotate/PI,0.0f,-1.0f,0.0f);
		for (i=0;i<CUBE_SIZE;i++) {
			for (j=0;j<CUBE_SIZE;j++) {
				(cubes[i][0][j])->draw(x+(cubeRadium*2+cubeSpace)*i,y,z+(cubeRadium*2+cubeSpace)*j);
			}
		}
  	glPopMatrix();
		for (i=0;i<CUBE_SIZE;i++) {
			for (j=1;j<CUBE_SIZE;j++) {
				for (k=0;k<CUBE_SIZE;k++) {
					(cubes[i][j][k])->draw(x+(cubeRadium*2+cubeSpace)*i,y+(cubeRadium*2+cubeSpace)*j,z+(cubeRadium*2+cubeSpace)*k);
				}
			}
		}
	}
}

void CubeManage::output() {
	for (int i=0;i<CUBE_SIZE;i++) {
		for (int j=0;j<CUBE_SIZE;j++) {
			cubes[0][i][j]->output();
		}
	}
}

void CubeManage::output(int scr,int site){
	int sign;
	int i,j;

	if (site==1) {
		cout << "site=1,nonsense!" << endl;
		return;
	}
	
	switch (scr) {
		case 1:
			if (site==0)
				sign=-X;
			else
				sign=X;
			cout << "scr=" << scr << " sign=" << sign << endl;
			for (i=0;i<CUBE_SIZE;i++) {
				for (j=0;j<CUBE_SIZE;j++) {
					cout << i << "," << j << "=";
					cubes[site][i][j]->output(sign);
					cout << endl;
				}
			}
			break;
		case 2:
			if (site==0)
				sign=-Y;
			else
				sign=Y;
			for (i=0;i<CUBE_SIZE;i++) {
				for (j=0;j<CUBE_SIZE;j++) {
					cout << i << "," << j << "=";
					cubes[i][site][j]->output(sign);
					cout << endl;
				}
			}
			break;
		case 3:
			if (site==0)
				sign=-Z;
			else
				sign=Z;
			for (i=0;i<CUBE_SIZE;i++) {
				for (j=0;j<CUBE_SIZE;j++) {
					cout << i << "," << j << "=";
					cubes[i][j][site]->output(sign);
					cout << endl;
				}
			}
			break;
	}
}

void CubeManage::goStep(int *leftLeg,int *rightLeg,int *goDirection,int step,int leftEdge,int rightEdge) {
	for (int i=0;i<step;i++) {
		switch (*goDirection) {
			case 0:
				*leftLeg=*leftLeg-1;
				if (*leftLeg<leftEdge) {
					*leftLeg=*leftLeg+1;
					*goDirection=3;
					*rightLeg=*rightLeg+1;
				}
				break;
			case 1:
				*rightLeg=*rightLeg-1;
				if (*rightLeg<leftEdge) {
					*rightLeg=*rightLeg+1;
					*goDirection=0;
					*leftLeg=*leftLeg-1;
				}
				break;
			case 2:
				*leftLeg=*leftLeg+1;
				if (*leftLeg>=rightEdge) {
					*leftLeg=*leftLeg-1;
					*goDirection=1;
					*rightLeg=*rightLeg-1;
				}
				break;
			case 3:
				*rightLeg=*rightLeg+1;
				if (*rightLeg>=rightEdge) {
					*rightLeg=*rightLeg-1;
					*goDirection=2;
					*leftLeg=*leftLeg+1;
				}
				break;
		}
	}
}

void CubeManage::turnByXShun(int x) {
	int step=CUBE_SIZE-1;
	int leftEdge=0;
	int rightEdge=CUBE_SIZE;
	int goDirection0=3;
	int goDirection1=3;
	int y0=0;
	int z0=0;
	int y1=0;
	int z1=0;
	WcgCube *tempcubes[CUBE_SIZE][CUBE_SIZE];
	
	tempcubes[CUBE_SIZE/2][CUBE_SIZE/2]=cubes[x][CUBE_SIZE/2][CUBE_SIZE/2];
	cubes[x][CUBE_SIZE/2][CUBE_SIZE/2]->turnByXShun(x);
	for (int i=0;i<CUBE_SIZE/2;i++) {
		step=CUBE_SIZE-i*2-1;
		goDirection0=3;
		goDirection1=3;
		leftEdge=i;
		rightEdge=CUBE_SIZE-i;
		y0=leftEdge;
		z0=leftEdge;
		y1=leftEdge;
		z1=leftEdge;
		goStep(&y1,&z1,&goDirection1,step,leftEdge,rightEdge);
		for (int j=0;j<step*4;j++) {
			tempcubes[y1][z1]=cubes[x][y0][z0];
			cubes[x][y0][z0]->turnByXShun(x);
			goStep(&y0,&z0,&goDirection0,1,leftEdge,rightEdge);
			goStep(&y1,&z1,&goDirection1,1,leftEdge,rightEdge);
		}
		for (int m=0;m<CUBE_SIZE;m++) {
			for (int n=0;n<CUBE_SIZE;n++) {
				cubes[x][m][n]=tempcubes[m][n];
			}
		}
	}
}

void CubeManage::turnByXNi(int x) {
	turnByXShun(x);
	turnByXShun(x);
	turnByXShun(x);
}

void CubeManage::turnByYShun(int y) {
	int step=CUBE_SIZE-1;
	int leftEdge=0;
	int rightEdge=CUBE_SIZE;
	int goDirection0=3;
	int goDirection1=3;
	int x0=0;
	int z0=0;
	int x1=0;
	int z1=0;
	WcgCube *tempcubes[CUBE_SIZE][CUBE_SIZE];
	
	tempcubes[CUBE_SIZE/2][CUBE_SIZE/2]=cubes[CUBE_SIZE/2][y][CUBE_SIZE/2];
	cubes[CUBE_SIZE/2][y][CUBE_SIZE/2]->turnByYShun(y);
	for (int i=0;i<CUBE_SIZE/2;i++) {
		step=CUBE_SIZE-i*2-1;
		goDirection0=3;
		goDirection1=3;
		leftEdge=i;
		rightEdge=CUBE_SIZE-i;
		x0=leftEdge;
		z0=leftEdge;
		x1=leftEdge;
		z1=leftEdge;
		goStep(&z1,&x1,&goDirection1,step,leftEdge,rightEdge);
		for (int j=0;j<step*4;j++) {
			tempcubes[x1][z1]=cubes[x0][y][z0];
			cubes[x0][y][z0]->turnByYShun(y);
			goStep(&z0,&x0,&goDirection0,1,leftEdge,rightEdge);
			goStep(&z1,&x1,&goDirection1,1,leftEdge,rightEdge);
		}
		for (int m=0;m<CUBE_SIZE;m++) {
			for (int n=0;n<CUBE_SIZE;n++) {
				cubes[m][y][n]=tempcubes[m][n];
			}
		}
	}
}

void CubeManage::turnByYNi(int y) {
	turnByYShun(y);
	turnByYShun(y);
	turnByYShun(y);
}

void CubeManage::turnByZShun(int z) {
	int step=CUBE_SIZE-1;
	int leftEdge=0;
	int rightEdge=CUBE_SIZE;
	int goDirection0=3;
	int goDirection1=3;
	int x0=0;
	int y0=0;
	int x1=0;
	int y1=0;
	WcgCube *tempcubes[CUBE_SIZE][CUBE_SIZE];
	
	tempcubes[CUBE_SIZE/2][CUBE_SIZE/2]=cubes[CUBE_SIZE/2][CUBE_SIZE/2][z];
	cubes[CUBE_SIZE/2][CUBE_SIZE/2][z]->turnByZShun(z);
	for (int i=0;i<CUBE_SIZE/2;i++) {
		step=CUBE_SIZE-i*2-1;
		goDirection0=3;
		goDirection1=3;
		leftEdge=i;
		rightEdge=CUBE_SIZE-i;
		x0=leftEdge;
		y0=leftEdge;
		x1=leftEdge;
		y1=leftEdge;
		goStep(&x1,&y1,&goDirection1,step,leftEdge,rightEdge);
		for (int j=0;j<step*4;j++) {
			tempcubes[x1][y1]=cubes[x0][y0][z];
			cubes[x0][y0][z]->turnByZShun(z);
			goStep(&x0,&y0,&goDirection0,1,leftEdge,rightEdge);
			goStep(&x1,&y1,&goDirection1,1,leftEdge,rightEdge);
		}
		for (int m=0;m<CUBE_SIZE;m++) {
			for (int n=0;n<CUBE_SIZE;n++) {
				cubes[m][n][z]=tempcubes[m][n];
			}
		}
	}
}

void CubeManage::turnByZNi(int z) {
	turnByZShun(z);
	turnByZShun(z);
	turnByZShun(z);
}

wcgcube.cpp檔案為:

#include "iostream"
using namespace std;

#include "wcgcube.h"

WcgCube::WcgCube(){
	direct[0]=Z;
	direct[1]=X;
	direct[2]=-Z;
	direct[3]=-X;
	direct[4]=Y;
	direct[5]=-Y;
	
	sideColor[0][0]=1.0f;
	sideColor[0][1]=1.0f;
	sideColor[0][2]=1.0f;
	
	sideColor[1][0]=1.0f;
	sideColor[1][1]=1.0f;
	sideColor[1][2]=0.0f;
	
	sideColor[2][0]=1.0f;
	sideColor[2][1]=0.0f;
	sideColor[2][2]=0.0f;
	
	sideColor[3][0]=1.0f;
	sideColor[3][1]=0.0f;
	sideColor[3][2]=1.0f;
	
	sideColor[4][0]=0.0f;
	sideColor[4][1]=1.0f;
	sideColor[4][2]=1.0f;
	
	sideColor[5][0]=0.0f;
	sideColor[5][1]=1.0f;
	sideColor[5][2]=0.0f;
}

WcgCube::~WcgCube(){
}

void WcgCube::draw(GLfloat orientX,GLfloat orientY,GLfloat orientZ) {
	GLfloat cubeRadium=10.0f;
	GLfloat cubeSpace=2.0f;
	for (int i=0;i<6;i++) {
		glColor3f(sideColor[i][0],sideColor[i][1],sideColor[i][2]);
		if (direct[i]==Z) {
			// Front face
			glBegin(GL_POLYGON);
				glVertex3f(orientX+cubeRadium,orientY+cubeRadium,orientZ+cubeRadium);

				glVertex3f(orientX+cubeRadium,orientY-cubeRadium,orientZ+cubeRadium);

				glVertex3f(orientX-cubeRadium,orientY-cubeRadium,orientZ+cubeRadium);

				glVertex3f(orientX-cubeRadium,orientY+cubeRadium,orientZ+cubeRadium);
			glEnd();
		}
		else
		if (direct[i]==-Z) {
			// Back Face
			glBegin(GL_POLYGON);
				glVertex3f(orientX+cubeRadium,orientY+cubeRadium,orientZ-cubeRadium);

				glVertex3f(orientX+cubeRadium,orientY-cubeRadium,orientZ-cubeRadium);

				glVertex3f(orientX-cubeRadium,orientY-cubeRadium,orientZ-cubeRadium);

				glVertex3f(orientX-cubeRadium,orientY+cubeRadium,orientZ-cubeRadium);
			glEnd();
		}
		else
		if (direct[i]==Y) {
			// Top Face
			glBegin(GL_POLYGON);
				glVertex3f(orientX+cubeRadium,orientY+cubeRadium,orientZ-cubeRadium);

				glVertex3f(orientX+cubeRadium,orientY+cubeRadium,orientZ+cubeRadium);

				glVertex3f(orientX-cubeRadium,orientY+cubeRadium,orientZ+cubeRadium);

				glVertex3f(orientX-cubeRadium,orientY+cubeRadium,orientZ-cubeRadium);
			glEnd();
		}
		else
		if (direct[i]==-Y) {
			// Bottom Face
			glBegin(GL_POLYGON);
				glVertex3f(orientX+cubeRadium,orientY-cubeRadium,orientZ-cubeRadium);

				glVertex3f(orientX+cubeRadium,orientY-cubeRadium,orientZ+cubeRadium);

				glVertex3f(orientX-cubeRadium,orientY-cubeRadium,orientZ+cubeRadium);

				glVertex3f(orientX-cubeRadium,orientY-cubeRadium,orientZ-cubeRadium);
			glEnd();
		}
		else
		if (direct[i]==X) {
			// Left face
			glBegin(GL_POLYGON);
				glVertex3f(orientX+cubeRadium,orientY+cubeRadium,orientZ+cubeRadium);

				glVertex3f(orientX+cubeRadium,orientY+cubeRadium,orientZ-cubeRadium);

				glVertex3f(orientX+cubeRadium,orientY-cubeRadium,orientZ-cubeRadium);

				glVertex3f(orientX+cubeRadium,orientY-cubeRadium,orientZ+cubeRadium);
			glEnd();
		}
		else
		if (direct[i]==-X) {
			// Right face
			glBegin(GL_POLYGON);
				glVertex3f(orientX-cubeRadium,orientY+cubeRadium,orientZ+cubeRadium);

				glVertex3f(orientX-cubeRadium,orientY+cubeRadium,orientZ-cubeRadium);

				glVertex3f(orientX-cubeRadium,orientY-cubeRadium,orientZ-cubeRadium);

				glVertex3f(orientX-cubeRadium,orientY-cubeRadium,orientZ+cubeRadium);
			glEnd();
		}
	}
}

void WcgCube::output() {
	for (int i=0;i<6;i++) {
		cout << "direct[" << i << "]=" << direct[i] << endl;
	}
}

void WcgCube::output(int sign) {
	for (int i=0;i<6;i++) {
		if (direct[i]==sign)
			cout <<  i;
	}
}

void WcgCube::turnByXShun(int x) {
	turnByX(x,-1);
}

void WcgCube::turnByXNi(int x) {
	turnByX(x,1);
}

void WcgCube::turnByX(int x,int sign) {
	for (int i=0;i<6;i++) {
		switch (direct[i]) {
			case Z:
				direct[i]=(-1)*sign*Y;
				break;
			case -Z:
				direct[i]=sign*Y;
				break;
			case Y:
				direct[i]=sign*Z;
				break;
			case -Y:
				direct[i]=(-1)*sign*Z;
				break;
		}
	}
}

void WcgCube::turnByYShun(int y) {
	turnByY(y,-1);
}

void WcgCube::turnByYNi(int y) {
	turnByY(y,1);
}

void WcgCube::turnByY(int y,int sign) {
	for (int i=0;i<6;i++) {
		switch (direct[i]) {
			case Z:
				direct[i]=sign*X;
				break;
			case -Z:
				direct[i]=(-1)*sign*X;
				break;
			case X:
				direct[i]=(-1)*sign*Z;
				break;
			case -X:
				direct[i]=sign*Z;
				break;
		}
	}
}

void WcgCube::turnByZShun(int z) {
	turnByZ(z,-1);
}

void WcgCube::turnByZNi(int z) {
	turnByZ(z,1);
}

void WcgCube::turnByZ(int z,int sign) {
	for (int i=0;i<6;i++) {
		switch (direct[i]) {
			case Y:
				direct[i]=(-1)*sign*X;
				break;
			case -Y:
				direct[i]=sign*X;
				break;
			case X:
				direct[i]=sign*Y;
				break;
			case -X:
				direct[i]=(-1)*sign*Y;
				break;
		}
	}
}

        通過鍵盤上的按鍵q、w、e、r、t、a、s、d、f、g、h來旋轉改變魔方的各種組合。

        最終效果圖如下所示:





相關推薦

OpenGL實現3D魔方遊戲原始碼

        首先這個程式是建立的是Windows應用程式,建立控制檯程式是不能執行的,另外,專案——專案屬性——配置屬性——常規-----使用多位元組字符集,這樣編譯才能夠通過的,否則如果選擇使用 Unicode 字符集,編譯會有錯誤提示:error C2440: “初

canvas實現3D魔方

TP earch 哪些 類圖 點距 圖片 sea 渲染 繪制圖像 摘要:使用canvas實現可交互的3D魔方 一、簡單分析 魔方物理性質: 1.中心塊(6個):中心塊與中心軸連接在一起,但可以順著軸的方向自由的轉動。 2.棱塊(12個):棱塊的表面是兩個正方形,結構類似一個

使用C語言實現隨機小遊戲原始碼

#include"stdio.h" #include"stdlib.h" #include"windows.h" int print() {     printf("\n\n+++++++++++++你會看見的數字和運算子+++++++++++++\n");   &

CSS3 3D環境實現立體 魔方效果代碼

3d 魔方 css 環境實現 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>魔方</title>

[微信小遊戲+Three.JS]給場景添加反射材質,實現3D水珠移動效果

rac webgl round 圖片 nmap 微信小遊戲 ops In 繪制 前幾篇博客,我分別加好了3D移動盒子,也給場景加好了天空盒 這篇博客,就給場景再加一些效果 繪制的水珠的源代碼來自Three.JS在GitHub上的demo 小遊戲所用到的,修改過的JS庫

python打造特別火的一個小遊戲,16行程式碼實現3D撞球小遊戲

以下是製作上面炫酷動畫所需的全部程式碼:         我們需要三組剛體(當您在Blender的物件上開啟一個剛體的屬性時,Blender將模擬與其它剛體的碰撞): 1.平面 第2行程式碼建立了一個簡單的平面,立方體將放置在

【181117】VC++ 基於Opengl技術3D戰機飛行測試原始碼原始碼

原始碼下載簡介 VC++ 3D飛行測試原始碼,基於Opengl技術,編譯程式後執行EXE,注意要將DEBUG目錄中的EXE檔案拷貝至根目錄下,執行後即可看到在3D的地圖上有一架戰機在空中飛行,這在編寫3D飛行射擊遊戲時候非常有用,可將此作為一個飛行模組來用,請注意編譯時需要相關的Opengl

[原始碼和報告分享]基於WIN32 API介面程式設計實現的2048遊戲

 遊戲功能 l   倒計時 遊戲有一個 15分鐘 的倒計時,玩家需要在規定時間內完成遊戲(即合併出數字為2048的方塊) l   方塊出現規則 在遊戲中,方塊的出現完全隨機,僅出現在空方格中。當方塊出現時,方塊上的數字有 

[文件和原始碼分享] 基於JAVA實現的迷宮遊戲

程式開始執行時顯示一個迷宮地圖,迷宮中央有一隻老鼠,迷宮的右下方有一個糧倉。遊戲的任務是使用鍵盤上的方向鍵操縱老鼠在規定的時間內走到糧倉處。 老鼠形象可辨認,可用鍵盤操縱老鼠上下左右移動;迷宮的牆足夠結實,老鼠不能穿牆而過正確檢測結果,若老鼠在規定時間內走到糧倉處,提示成功,否則提示失敗;新增編輯迷宮功能,

[原始碼和文件分享]基於JAVA實現的紙牌遊戲

1 專案介紹 1.1 背景和目的 單人紙牌遊戲,牌桌上有7個堆共28張牌,第一堆1張牌,第二堆2張,。。。第7堆7張,每一堆的第一張牌朝上,其他朝下。牌桌上還有4個suitpiles,一個deck card堆和一個discard card堆(參考Windows的紙牌遊戲) 設計一個簡單的

java swing實現簡單的中國象棋小遊戲原始碼

大家好,今天給大家演示一下一款由Java swing實現的小遊戲中國象棋,這款中國象棋比較簡單,實現了主要的對弈功能,適合Java學習者和學生交作業,功能不是很複雜。下面來看看執行結果。 1. 將專案匯入到eclipse; 2. 正確匯入後即可執行,由於功能相對簡單,所以所

[原始碼和文件分享]基於WIN32 API實現黃金礦工遊戲單人版

三、遊戲功能設計 1.開屏 顯示內容: 程式啟動後,顯示初始化圖片,計時結束,進入選單介面。 邏輯處理: 控制圖片從左至右顯示。 2.選單 顯示內容: 顯示選單背景圖片,顯示“開始”按鈕。 邏輯處理: 檢測滑鼠移動。當滑鼠移動到按鈕上,更改按

北理工虛擬現實作業-OpenGL實現3D Max模型的匯入

兩個禮拜之前,李鳳霞老師又佈置了一次虛擬現實作業,這次的作業題目是自己從網上找一個汽車的3D模型(一般都是3D Max建的),然後把這個模型匯入到OpenGL中,在工程中顯示出來,同時能通過滑鼠的移動切換視角。由於時間太緊(矩陣分析要考試),這個作業就拜託了宿舍的一個同學,

[原始碼和文件分享]基於Java Swing實現的掃雷遊戲

1 引言 1.1 編寫目的 通過進行掃雷專案實戰演練,鞏固SE階段所學知識點。 1.2 背景 為了檢驗自己所學SE知識,熟悉鞏固基礎。為了鞏固javaSE的知識和技能,編寫益智遊戲掃雷專案,通過實戰發現自己在SE階段的不足。 2 總體設計 2.1 執行環境 jdk1

DirectX11--實現一個3D魔方(2)

前言 上一章我們主要講述了魔方的構造和初始化、紋理的準備工作。目前我還沒有打算講Direct3D 11關於底層繪圖的實現,因此接下來這一章的重點是魔方的旋轉。因為我們要的是能玩的魔方遊戲,而不是一個觀賞品。所以對旋轉這一步的處理就顯得尤其重要、精細,甚至可以展開很大的篇幅來講述。現在光是為了實現旋轉的這個動

[Unity3D]Unity3D遊戲開發之滑鼠旋轉、縮放實現3D物品展示

各位朋友,大家好,我是秦元培,歡迎大家關注我的博主,我的部落格地址是blog.csdn.net/qinyuanpei。最近博主重點研究了攝像機旋轉、縮放等問題,那麼今天為大家分享的是一個在3D展示中比較常用的功能,即通過滑鼠右鍵實現旋轉、滑鼠滾輪實現縮放、滑鼠中鍵實現平移

DirectX11--實現一個3D魔方(3)

前言 (2019/1/9 09:23)上一章我們主要講述了魔方的旋轉,這個旋轉真是有毒啊,搞完這個部分搭鍵鼠操作不到半天應該就可以搭完了吧... (2019/1/9 21:25)啊,真香 有人發這張圖片問我寫魔方的目的是不是這個。。。噗 現在光是鍵鼠相關的程式碼也搭了400行左右。。其中鍵盤相關的呼

Android Gallery實現3D相簿(附效果圖+Demo原始碼)

public boolean createReflectedForAdapter() { final int reflectionGap = 4; final int Height = 200; int index = 0;

VS下用C++實現的簡單3D射擊遊戲(附遊戲檔案與源工程程式碼)

記不得什麼時候開始感興趣寫遊戲程式碼了。開始的時候就只是寫一些簡單的小遊戲,打檯球,開小車,走迷宮,用一些2Dgraphics庫,完成每個小作品後都很開心。後來一想,在現在的計算機裝置條件下,2D太簡陋了,於是開始著手做一個3D賽車遊戲。說是賽車,實際上也就只有一輛小車。模

Java swing實現遊戲掃雷之掃雷遊戲原始碼

大家好,今天給大家演示一下由Java swing實現的小遊戲掃雷,該遊戲可在Java環境中執行,jdk版本不限,下面我們來看看如何執行。 1. 將專案原始碼匯入到eclipse; 2. 匯入原始碼後發現沒有錯誤,此時我們可以找到主類來執行,這裡的主類是SartFrame,我