⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gameengine.cpp

📁 本游戏是一款单机版的坦克大战游戏
💻 CPP
📖 第 1 页 / 共 5 页
字号:
//////////////////////////////////////////////////////////////////////////////////////////////////
//      #                                                                               #       //
//		#		* File Name:	GameImageEngine.h										#	    //
//		#																				#	    //
//		#		* Function:		the engine of the game									#	    //
//		#																				#	    //
//		#		* Instruction:	Created by JIAN NAN FEI 2008 8 29						#	    //
//      #                                                                               #       //
///////////////////////////////////////////////////////////////////////////////////////////////////

//----------------------------------------------------------------------------------------------//
//										DEFINES REFERENCES HERE									//
//----------------------------------------------------------------------------------------------//

	// Define Weapon kinds
	#define	GAME_MODEL_1P      0
	#define	GAME_MODEL_2P      1


	// Define the application states
	#define APP_MAINMENU     0
	#define APP_GAMESCREEN   1
	#define APP_CREDITS      2
	#define APP_HELPSCREEN   3
	#define APP_LEVELSCREEN  4

	#define MAP_WALL_TOTAL			8
	#define MAP_WALL_COL_MAX	    36
	#define MAP_WALL_ROW_MAX	    21

	#define MAP_WALL_COL_VIEW	    13
	#define MAP_WALL_ROW_VIEW	    9

	#define WALL_WIDTH				80
	#define WALL_HEIGHT				80

	#define TANK_TOTAL_NUMBER		8
	#define TANK_WAR_SIDE			2

	#define SCREEN_X_MIN			100
	#define SCREEN_X_MAX			850
	#define SCREEN_Y_MIN			100
	#define SCREEN_Y_MAX			550

//----------------------------------------------------------------------------------------------//
//										HEADFILES REFERENCES HERE								//
//----------------------------------------------------------------------------------------------//

	#include <windows.h>
	#include <windowsx.h>
	#include <mmsystem.h>
	#include <stdio.h>
	#include <math.h>
	#include <ddraw.h>

	#include "GameImageEngine.h"
	#include "GameSoundEngine.hpp"

	#include "GameTypes.h"
	#include "CLevel.hpp"
	#include "CWall.hpp"
	#include "CTank.hpp"
	#include "TankWarr.h"
	#include "CBullet.hpp"


//----------------------------------------------------------------------------------------------//
//										GLOBAL MEMBER VARIAYS REFERENCES HERE					//
//----------------------------------------------------------------------------------------------//

	struct MarkNode
	{
		int i,j;
	};

	struct Alpha
	{
		unsigned int start;
		unsigned int end;
	};
	int						 iAppState = APP_MAINMENU;

	LPDIRECTDRAW            lpDD;
	LPDIRECTDRAWSURFACE     lpFrontBuffer;
	LPDIRECTDRAWSURFACE     lpBackBuffer;
	LPDIRECTDRAWSURFACE     lpTank;
	LPDIRECTDRAWSURFACE     lpBkGround;
	LPDIRECTDRAWSURFACE     lpMainMenuFrame;
	LPDIRECTDRAWPALETTE     lpArtPalette;
	LPDIRECTDRAWSURFACE     lpAlpha;
	LPDIRECTDRAWSURFACE     lpStatusBar;
	LPDIRECTDRAWSURFACE     lpBoom;
	LPDIRECTDRAWSURFACE     lpETank;
	LPDIRECTDRAWSURFACE     lpSelect;
	LPDIRECTDRAWSURFACE     lpTankSelect;
	LPDIRECTDRAWSURFACE     lpWall[MAP_WALL_TOTAL];
	LPDIRECTDRAWSURFACE     lpPowerBar;
	LPDIRECTDRAWSURFACE     lpTankShoot;
	LPDIRECTDRAWSURFACE     lpHelicopter;

	//** INITIAL POINTERS
	
	CLevel					 Level;
	CWall					 Wall[MAP_WALL_ROW_MAX][MAP_WALL_COL_MAX];
	CTank					 Tank[TANK_WAR_SIDE][TANK_TOTAL_NUMBER];
	CBullet					 Bullet[TANK_WAR_SIDE][TANK_TOTAL_NUMBER];


	unsigned int			score;
	unsigned int			Lastscore;


	DWORD					dwShipExplode;
	int						iOption;

	LPDIRECTSOUND           lpDS;
	HSNDOBJ                 hsoEnter          = NULL;
	HSNDOBJ                 hsoShoot          = NULL;
	HSNDOBJ                 hsoStop           = NULL;
	HSNDOBJ                 hsoBoom           = NULL;
	HSNDOBJ                 hsoUfoShoot       = NULL;
	HSNDOBJ                 hsoSelect         = NULL;
	HSNDOBJ                 hsoGetExtra       = NULL;
	HSNDOBJ                 hsoGameOver       = NULL;

	BOOL                    bSoundEnabled = FALSE;

	BOOL					bShipStop = FALSE;
	BOOL					bShootEnable = TRUE;
	BOOL					bShoot = FALSE;
	BOOL					bBoom = FALSE;
	BOOL					bKeyState = FALSE;
	BOOL					bpromotion = FALSE;
	BOOL					bTask=FALSE;
	BOOL					bSelect=TRUE;

	HWND                    hWndMain;
	HINSTANCE               hInst;
	BOOL                    bIsActive;
	DWORD                   dwTransType;
	DWORD                   lastTickCount = 0;

	DWORD                   dwFillColor;

	//** SHIP VARIABLES
	int						iPlayerModel=GAME_MODEL_1P;
	int						iScreenMoveX=0;
	int						iScreenMoveY=0;
	int						iTankMovState;
	int						iTankMovX=500;
	int						iTankMovY=550;
	int						iBulletMovX=0;
	int						iBulletMovY=0;
	int						iWeapon = 0;
	int						iShipState=0;
	int						iLastWeapon = -1;
	int						iMaxWeapon = 0;
	int						iShield = 0;
	int						iLastShield = -1;
	int						i=0;
	int						j=0;
	int						k=0;
	int						frame=0;
	int						framep = 0;
	int						MovDirection=0;
	int						Boomx=0,Boomy=0;
	int						bframe=0,bframep=0;
	int						tick=0;
	int						screenmovex=0;
	int						screenmovey=0;
	
	//** LEVEL VARIABLES
	int	  				    iLevel=1;
	int	  				    iTankNum=TANK_TOTAL_NUMBER;
	int						SHOOTCHANCE  = 1000;

	char szscore[]="S-\0";
	char szlifes[]="P-\0";
	char szlevel[]="L-\0";


	const Alpha			AA[40] = 
		{
		{0,14},{19,27},{31,46},{47,61},{65,78},{80,94},
		{96,110},{114,125},{128,143},{145,159},{160,175},
		{176,191},{193,206},{207,222},{224,237},{238,252},{253,267},
		{269,284},{285,291},{292,307},{308,323},{324,338},{340,364},
		{365,380},{382,397},{398,413},{414,429},{430,444},{447,460},
		{464,475},{477,492},{494,508},{509,533},{533,548},{550,564},
		{564,579},{579,599},{600,610},{611,615},{617,620}
		};


//----------------------------------------------------------------------------------------------//
//										FUNCTION MEMBERS REFERENCES HERE						//
//----------------------------------------------------------------------------------------------//

	int PASCAL WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow );
	BOOL RestoreSurfaces( void );
	void DestroyGame(void);
	BOOL RestoreSurfaces( void );
	void bltText(char*  buffer, int  x, int y);
	void ShowGameOver();
	void ShowLevel();
	void DrawBackGround(LPDIRECTDRAWSURFACE lpDest, int x, int y, int wx, int wy);
	BOOL LoadUfoSurface(int iFlags);
	BOOL CleanupAndExit( char *err);


	MarkNode GetFrontScene(int x,int y);
	BOOL SearchFlyingPath(int m,int n);
	BOOL SearchMovePath(int direction,CTank &tank);

	void InitWall();
	void GetViewWall();

	void InitTank();
	void GetTank(int t=0);
	void GetNowPos(CTank &tank);
	BOOL DrawBoom(int x,int y);
	void DrawWall(int x,int y,int type);
	void UpdateFrame();

	void InitBullet();
	void InstallBullet(int m,int n);

	BOOL CheckViewBoundry(int x,int y);
	BOOL IsOverBoundry(int x,int y);
	void TankViewMove();

	void KeyDown(HWND hWnd,WPARAM wParam);
	void KeyUp(HWND hWnd,WPARAM wParam);

//----------------------------------------------------------------------------------------------//
//										FUNCTION COMPLISHMENT REFERENCES HERE					//
//----------------------------------------------------------------------------------------------//

//-----------------------------------------------------------------
//
// Name: InitLevel
// Instruction: member fuctions on level
//
//-----------------------------------------------------------------
	void InitLevel(BOOL bStart)
	{
		screenmovex=0;
		screenmovey=0;
		Level.SetTankNum(20);
		Level.SetGameLevel(1);
		InitWall();
		InitTank();
		InitBullet();
	}

//-----------------------------------------------------------------
//
// Name: Show
// Instruction: member fuctions on Showing information
//
//-----------------------------------------------------------------

	void ShowGameOver()
	{
		DWORD		tick;
		HRESULT		hRet;
		DDBLTFX     ddbltfx;
		char		buffer[10];

		ddbltfx.dwSize = sizeof( ddbltfx );
		ddbltfx.dwFillColor = dwFillColor;

		while (1)
		{
			hRet = lpFrontBuffer->Blt(NULL, NULL, NULL, DDBLT_COLORFILL, &ddbltfx);
			if (hRet == DD_OK)
			{
				break;
			}
			if (hRet == DDERR_SURFACELOST)
			{
				hRet = RestoreSurfaces();
				if (hRet != DD_OK)
					break;
			}
			if (hRet != DDERR_WASSTILLDRAWING)
				break;
		}
		strcpy(buffer,"GAME OVER");
		buffer[9] = '\0';
		bltText(buffer,500,400);

		tick = GetTickCount();
		sndPlaySound("res\\GameOver.wav",SND_ASYNC);
		while(GetTickCount() - tick < 3500);
	}

	void ShowCredits()
	{

		DWORD		tick;
		HRESULT		hRet;
		DDBLTFX     ddbltfx;
		char		buffer[24];

		ddbltfx.dwSize = sizeof( ddbltfx );
		ddbltfx.dwFillColor = dwFillColor;

		while (1)
		{
			hRet = lpFrontBuffer->Blt(NULL, NULL, NULL, DDBLT_COLORFILL, &ddbltfx);
			if (hRet == DD_OK)
			{
				break;
			}
			if (hRet == DDERR_SURFACELOST)
			{
				hRet = RestoreSurfaces();
				if (hRet != DD_OK)
					break;
			}
			if (hRet != DDERR_WASSTILLDRAWING)
				break;
		}
		bltText("-------------------------------------",300,235);
		bltText("-------------------------------------",300,240);

		strcpy(buffer,"THIS GAME IS FREEWARE\0");
		bltText(buffer,345,260);

		strcpy(buffer,"IF YOU WANT TO MAIL ME\0");
		bltText(buffer,330,330);

		strcpy(buffer,"OR YOU ARE FROM A GAME\0");
		bltText(buffer,320,400);

		strcpy(buffer,"COMPANY AND WANT TO\0");
		bltText(buffer,340,470);

		strcpy(buffer,"HIRE ME, MY MAIL IS\0");
		bltText(buffer,350,540);

		strcpy(buffer,"MAURICIORITTER@HOTMAIL.COM\0");
		bltText(buffer,320,610);	
		bltText("-----------------------------------------------",250,635);
		bltText("-----------------------------------------------",250,640);

		tick = GetTickCount();
		sndPlaySound("res\\Tap.wav",SND_ASYNC);
		while(GetTickCount() - tick < 6000);
	}

	void ShowLevel()
	{
		DWORD		tick;
		HRESULT		hRet;
		DDBLTFX     ddbltfx;
		char		buffer[24];

		ddbltfx.dwSize = sizeof( ddbltfx );
		ddbltfx.dwFillColor = dwFillColor;

		while (1)
		{
			hRet = lpFrontBuffer->Blt(NULL, NULL, NULL, DDBLT_COLORFILL, &ddbltfx);
			if (hRet == DD_OK)
			{
				break;
			}
			if (hRet == DDERR_SURFACELOST)
			{
				hRet = RestoreSurfaces();
				if (hRet != DD_OK)
					break;
			}
			if (hRet != DDERR_WASSTILLDRAWING)
				break;
		}

		strcpy(buffer,"LEVEL\0");
		bltText(buffer,500,400);
		bltText(NumToChar(iLevel),600,400);

		tick = GetTickCount();
		sndPlaySound("res\\Blub.wav",SND_ASYNC);
		while(GetTickCount() - tick < 2000);
	}

	void ShowHelp()
	{
 		int							ddrval;
		DWORD                       thisTickCount;
		RECT                        rcRect;
		DWORD                       delay = 18;
		static int					frame = 0;
		DDBLTFX				ddbltfx;
		HRESULT				hRet;

		static BOOL  bBlink;
		static DWORD dwTime;
		

		thisTickCount = GetTickCount();
			
		if ((thisTickCount - lastTickCount) <= delay+20)
				return;

		if (lastTickCount == 0)
		{
			ddbltfx.dwSize = sizeof( ddbltfx );
			ddbltfx.dwFillColor = RGB(0,0,0);
			rcRect.left = 0;
			rcRect.top = 0;
			rcRect.right = 1024;
			rcRect.bottom = 768;
				
			while (1)
			{
				hRet = lpFrontBuffer->Blt(&rcRect, NULL, NULL, DDBLT_COLORFILL, &ddbltfx);
				if (hRet == DD_OK)
					break;
				if (hRet == DDERR_SURFACELOST)
				{
					hRet = RestoreSurfaces();
					if (hRet != DD_OK)
						break;
				}
					if (hRet != DDERR_WASSTILLDRAWING)
						break;
			}

			while (1)
			{
				hRet = lpBackBuffer->Blt(&rcRect, NULL, NULL, DDBLT_COLORFILL, &ddbltfx);
				if (hRet == DD_OK)
					break;
				if (hRet == DDERR_SURFACELOST)
				{
					hRet = RestoreSurfaces();
					if (hRet != DD_OK)
						break;
				}
				if (hRet != DDERR_WASSTILLDRAWING)
					break;
			}

			bltText("------------------------------------------",300,165);
			bltText("------------------------------------------",300,140);
			bltText("PHOTON AMMO\0",400,200);
			bltText("WEAPON ADVANCE\0",400,250);
			bltText("100 POINTS BONUS\0",400,300);
			bltText("LASER AMMO\0",400,350);	
			bltText("SHIELD CHARGE\0",400,400);	
			bltText("LEFT, RIGHT - MOVE SHIP\0",350,450);	
			bltText("DOWN OR ENTER - STOP SHIP\0",350,480);
			bltText("CTRL - CHANGE WEAPON\0",350,515);
			bltText("------------------------------------------",300,550);
			bltText("PRESS ANY KEY TO CONTINUE\0",340,605);
			bBlink = FALSE;
			dwTime = 0;

		}
			
		ddbltfx.dwSize = sizeof( ddbltfx );
		ddbltfx.dwFillColor = dwFillColor;
		rcRect.left = 50;
		rcRect.top = 40;
		rcRect.right = 75;
		rcRect.bottom = 270;
			
		while (1)
		{
			hRet = lpBackBuffer->Blt(&rcRect, NULL, NULL, DDBLT_COLORFILL, &ddbltfx);
			if (hRet == DD_OK)
				break;
			if (hRet == DDERR_SURFACELOST)
			{
				hRet = RestoreSurfaces();
				if (hRet != DD_OK)
					break;
			}
			if (hRet != DDERR_WASSTILLDRAWING)
				break;
		}
			
		if (bBlink == TRUE)
		{
			ddbltfx.dwSize = sizeof( ddbltfx );
			ddbltfx.dwFillColor = 0;
			rcRect.left = 300;
			rcRect.top = 600;
			rcRect.right = 765;
			rcRect.bottom = 630;
			
			while (1)
			{
				hRet = lpBackBuffer->Blt(&rcRect, NULL, NULL, DDBLT_COLORFILL, &ddbltfx);
				if (hRet == DD_OK)
						break;
				if (hRet == DDERR_SURFACELOST)
				{
					hRet = RestoreSurfaces();
					if (hRet != DD_OK)
						break;
				}
				if (hRet != DDERR_WASSTILLDRAWING)
					break;
			}
			dwTime = dwTime + 1;
			if (dwTime == 20)
			{
				bltText("PRESS ANY KEY TO CONTINUE\0",340,605);
				bBlink = FALSE;
				dwTime = 0;
			}
		}
		else
		{
			dwTime = dwTime + 1;
			if (dwTime == 20)
			{
				bltText("PRESS ANY KEY TO CONTINUE\0",340,605);
				bBlink = TRUE;
				dwTime = 0;
			}
		}

		frame++;
		if (frame == 19)
			frame = 0;

		while( 1 )
		{
			ddrval = lpFrontBuffer->Flip(NULL, 0 );
			if( ddrval == DD_OK )
				break;
			if( ddrval == DDERR_SURFACELOST )
			{
				if( !RestoreSurfaces() )
					return;
			}
			if( ddrval != DDERR_WASSTILLDRAWING )
				break;
		}
		lastTickCount = thisTickCount;

		return;
	}
//-----------------------------------------------------------------
//
// Name: InitializeSound
// Instruction: member fuctions on sound
//
//-----------------------------------------------------------------

	void InitializeSound( void )
	{
		bSoundEnabled = FALSE;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -