#include "Game.h"
static int i = 11; //处理人跳的时候
static numweapon = 0; //处理发弹的时候
bool Bweapon = FALSE ;//人和武器发生碰撞
bool Bspace = FALSE;//空格被按下
bool Bsnail = FALSE;//武器 和蜗牛发生碰撞
bool BSNA = FALSE;//蜗牛是否死亡
bool BSP = FALSE;//蜗牛和人发生碰撞
//******************************************************************
//构造函数
//******************************************************************
Game::Game(HINSTANCE hinst)
{
m_pd3d = NULL;
m_pd3dDevice = NULL;
m_hwnd = NULL;
m_hinst = hinst;
m_pTextureBJ = NULL;
m_pTextureP = NULL;
m_pTextureFRUIT = NULL;
m_pTextureSNAIL = NULL;
m_pSprinte = NULL;
//背景图片的初试化
m_prectBj.left = 0;
m_prectBj.top = 0;
m_prectBj.right = 6998;
m_prectBj.bottom = 470;
m_pdestBj.x = 0;
m_pdestBj.y = 0;
m_pdestBj.z = 0;
//人图片的初试化
m_prectP.left = 0;
m_prectP.top = 0;
m_prectP.right = 175;
m_prectP.bottom = 150;
m_pdestP.x = 100;
m_pdestP.y = 285;
m_pdestP.z = 0;
//水果图片的初试化
m_prectFRUIT.left = 0;
m_prectFRUIT.top = 0;
m_prectFRUIT.right = 50;
m_prectFRUIT.bottom = 60;
m_pdestFRUIT.x = 800;
m_pdestFRUIT.y = 200;
m_pdestFRUIT.z = 0;
//Snail的初始化
m_prectSNAIL.left = 0;
m_prectSNAIL.top = 0;
m_prectSNAIL.right = 63;
m_prectSNAIL.bottom = 65;
m_pdestSNAIL.x = 600;
m_pdestSNAIL.y = 345;
m_pdestSNAIL.z = 0;
//weapon的初始化
m_prectWEA.left = 0;
m_prectWEA.top = 0;
m_prectWEA.right = 65;
m_prectWEA.bottom = 55;
m_pdestWEA.x = 300;
m_pdestWEA.y = 360;
m_pdestWEA.z = 0;
}
//******************************************************************
//******************************************************************
Game::~Game()
{
CleanUp();
UnregisterClass(WND_CLASS_NAME,m_hinst);
}
//*****************************************************************
//初始windows设备
//*****************************************************************
HRESULT Game::InitWindow(int nCmdShow)
{
WNDCLASSEX wc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.cbSize = sizeof(WNDCLASSEX);
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wc.hCursor = LoadCursor(NULL,IDC_ARROW);
wc.hIcon = LoadIcon(NULL,IDI_APPLICATION);
wc.hIconSm = LoadIcon(NULL,IDI_APPLICATION);
wc.hInstance = m_hinst;
wc.lpfnWndProc = MsgProc;
wc.lpszClassName = WND_CLASS_NAME;
wc.lpszMenuName = NULL;
wc.style = CS_VREDRAW | CS_HREDRAW;
GAME_ISNULL(RegisterClassEx(&wc));
m_hwnd = CreateWindow(WND_CLASS_NAME,
WIN_NAME,
WS_OVERLAPPEDWINDOW,
100,
200,
800,
500,
NULL,
NULL,
m_hinst,
NULL);
GAME_ISNULL(m_hwnd);
ShowWindow(m_hwnd, nCmdShow);
return S_OK;
}
//****************************************************************
//初始化D3D设备
//****************************************************************
HRESULT Game::InitD3DDevice()
{
GAME_ISNULL(m_pd3d = Direct3DCreate9(D3D_SDK_VERSION));
//设置显示参数
D3DPRESENT_PARAMETERS d3dParam;
ZeroMemory(&d3dParam,sizeof(d3dParam));
d3dParam.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dParam.Windowed = true;
d3dParam.BackBufferFormat = D3DFMT_UNKNOWN;
d3dParam.EnableAutoDepthStencil = true;
d3dParam.AutoDepthStencilFormat = D3DFMT_D16;
GAME_FAILED(m_pd3d->CreateDevice(D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
m_hwnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dParam,
&m_pd3dDevice));
//设置渲染管线默认状态
m_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );//关闭背面剔除
m_pd3dDevice->SetRenderState( D3DRS_LIGHTING, FALSE ); //关闭灯光,默认灯光是开启的
m_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
m_pd3dDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
m_pd3dDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
m_pd3dDevice->SetTextureStageState(0,D3DTSS_ALPHAOP,D3DTOP_MODULATE);
return S_OK;
}
//*********************************************************************
//初始化游戏场景
//*********************************************************************
HRESULT Game::InitScene()
{
D3DXCreateSprite(m_pd3dDevice,&m_pSprinte);
GAME_FAILED(D3DXCreateTextureFromFile(m_pd3dDevice,"back.png",&m_pTextureBJ));
GAME_FAILED(D3DXCreateTextureFromFile(m_pd3dDevice,"人.png",&m_pTextureP));
GAME_FAILED(D3DXCreateTextureFromFile(m_pd3dDevice,"水果.png",&m_pTextureFRUIT));
GAME_FAILED(D3DXCreateTextureFromFile(m_pd3dDevice,"snail.png",&m_pTextureSNAIL));
GAME_FAILED(D3DXCreateTextureFromFile(m_pd3dDevice,"weapon.png",&m_pTextureWEA));
return S_OK;
}
//*****************************************************************
//游戏初始化
//*****************************************************************
HRESULT Game::GameInit(int nCmdShow)
{
GAME_FAILED(InitWindow(nCmdShow));
GAME_FAILED(InitD3DDevice());
GAME_FAILED(InitScene());
return S_OK;
}
//***************************************************************
//处理人移动的问题
//***************************************************************
VOID Game::Move()
{
m_prectP.left = 0;
m_prectP.bottom = 150;
if(KEY(VK_UP))
{
m_prectP.left =327;
m_prectP.top = 0;
m_prectP.right = 457;
m_prectP.bottom = 150;
if(m_pdestP.y>219)
{
m_pdestP.y -=i;
i--;
}
}
else if(KEY(VK_RIGHT))
{
if(m_pdestBj.x >= -3300)
{
m_pdestBj.x-=4;
m_pdestFRUIT.x-=4;
m_pdestSNAIL.x-=4;
m_pdestWEA.x -=4;
}
else
{
m_pdestP.x +=5;
if(m_pdestP.x >= 820)
{
//处理游戏结束时候的问题
//SendMessage(m_hwnd,WM_CLOSE,0,0);
}
}
m_prectP.left +=170;
m_prectP.right +=170;
if(m_prectP.right>=350)
{
m_prectP.left = 0;
m_prectP.right = 175;
}
}
else if(KEY(VK_LEFT))
{
m_prectP.left +=170;
m_prectP.right +=170;
if(m_prectP.right>=350)
{
m_prectP.left = 0;
m_prectP.right = 175;
}
}
else if(KEY(VK_DOWN))
{
m_prectP.left = 510;
m_prectP.top = 0;
m_prectP.right = 685;
m_prectP.bottom = 130;
}
else
{
if(m_pdestP.y <285)
{
m_pdestP.y +=i;
i++;
}
m_prectP.left = 0;
m_prectP.top = 0;
m_prectP.right = 175;
m_prectP.bottom = 150;
}
}
//**************************************************************************
//处理碰撞问题
//**************************************************************************
VOID Game::Hit()
{
//人和地面上的石头发生碰撞
if(m_pdestBj.x <= -1860&&m_pdestBj.x >= -1900)
{
if(!KEY(VK_UP))
{
m_prectP.top = 0;
m_prectP.bottom = 150;
m_prectP.left = 875;
m_prectP.right = 1020;
}
}
//人和水果发生碰撞
if(m_pdestFRUIT.x < 180 &&m_pdestFRUIT.x > 80 )
{
if(KEY(VK_UP))
{
m_prectFRUIT.top = 0;
m_prectFRUIT.bottom = 0;
m_prectFRUIT.left = 60;
m_prectFRUIT.right = 110;
}
}
//人和weapon发生碰撞
if(m_pdestWEA.x < 180)
{
Bweapon = TRUE;
m_prectWEA.top = 0;
m_prectWEA.bottom = 0;
m_prectWEA.left = 0;
m_prectWEA.right = 65;
m_pdestWEA.x = m_pdestP.x + 100;
m_pdestWEA.y = m_pdestP.y + 25;
}
//发弹
if(Bweapon == TRUE && KEY(VK_SPACE))
Bspace = TRUE;
if( Bspace == TRUE)
{
numweapon++;
if (numweapon <= 3 || (numweapon > 6 && numweapon <=10))
{
m_prectWEA.top = 0;
m_prectWEA.bottom = 55;
m_prectWEA.left = 65;
m_prectWEA.right = 130;
m_pdestWEA.x +=10;
m_pdestWEA.y +=5;
}
if (numweapon > 3 && numweapon <= 6)
{
m_prectWEA.top = 0;
m_prectWEA.bottom = 55;
m_prectWEA.left = 0;
m_prectWEA.right = 65;
m_pdestWEA.x +=10;
m_pdestWEA.y +=5;
}
if(numweapon > 10)
{
numweapon = 0;
Bspace = FALSE;
m_prectWEA.top = 0;
m_prectWEA.bottom = 0;
m_prectWEA.left = 0;
m_prectWEA.right = 65;
m_pdestWEA.x = m_pdestP.x + 80;
m_pdestWEA.y = m_pdestP.y + 25;
}
}
//武器和snail发生碰撞
if (m_pdestSNAIL.x > m_pdestWEA.x- 10 &&
m_pdestSNAIL.x < m_pdestWEA.x +20 && Bspace == TRUE)
{
Bsnail = TRUE;
BSNA =TRUE;
m_prectSNAIL.top = 0;
m_prectSNAIL.bottom = 65;
m_prectSNAIL.left = 63;
m_prectSNAIL.right = 126;
}
if(Bsnail == TRUE )
{
static int k = 0;
k++;
if(k<100)
m_pdestSNAIL.y += k;
if(k >=50)
{
k = 0;
m_prectSNAIL.top = 0;
m_prectSNAIL.left = 0;
m_prectSNAIL.bottom = 0;
m_prectSNAIL.right= 63;
m_pdestSNAIL.y = 345;
Bsnail = FALSE;
}
}
//人和蜗牛发生碰撞
if (m_pdestSNAIL.x < m_pdestP.x + 100 &&/* m_pdestP.y > m_pdestSNAIL.y + 25 &&*/ BSNA == FALSE)
{
static int nump = 0;
nump ++;
if(nump < 10)
{
m_prectP.top = 0;
m_prectP.bottom = 150;
m_prectP.left = 685;
m_prectP.right = 875;
}
else if(nump>=10&&nump<65)
{
m_prectP.top = 0;
m_prectP.bottom = 150;
m_prectP.left = 685;
m_prectP.right = 875;
m_pdestP.y +=nump - 10;
BSP = TRUE;
}
}
}
//**********************************************************************
//选择道具该画的部分
//**********************************************************************
VOID Game::DrawProp()
{
//选择水果该画的部分
if(m_pdestFRUIT.x <= -50 )
{
m_prectFRUIT.top = 0;
m_prectFRUIT.bottom = 60;
m_prectFRUIT.left += 50;
m_prectFRUIT.right += 50;
m_pdestFRUIT.x = 800;
}
//在背景的不同地方画蜗牛
if(m_pdestSNAIL.x <= -10)
{
m_prectSNAIL.top = 0;
m_prectSNAIL.left = 0;
m_prectSNAIL.bottom = 65;
m_prectSNAIL.right = 63;
m_pdestSNAIL.x = 810;
BSNA = FALSE;
}
}
//*********************************************************************
//游戏场景渲染
//*********************************************************************
HRESULT Game::GameRender()
{
GAME_FAILED(m_pd3dDevice->Clear(0,0,D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,0xff000000,1.0f,0));
if(SUCCEEDED(m_pd3dDevice->BeginScene()))
{
if(SUCCEEDED(m_pSprinte->Begin(0)))
{
//m_prectP.left=110;
//m_prectP.right=110*2;
//m_prectP.top=0;
//m_prectP.bottom=100;
//m_pdestP.x+=2;
//m_pdestP.z-=0;
m_pSprinte->Draw(m_pTextureBJ,&m_prectBj,0,&m_pdestBj,0xFFffffff);
m_pSprinte->Draw(m_pTextureFRUIT,&m_prectFRUIT,0,&m_pdestFRUIT,0xFFffffff);
m_pSprinte->Draw(m_pTextureSNAIL,&m_prectSNAIL,0,&m_pdestSNAIL,0xFFffffff);
m_pSprinte->Draw(m_pTextureWEA,&m_prectWEA,0,&m_pdestWEA,0xFFffffff);
m_pSprinte->Draw(m_pTextureP,&m_prectP,0,&m_pdestP,0xFFffffff);
m_pSprinte->End();
}
m_pd3dDevice->EndScene();
}
else
{
return E_FAIL;
}
GAME_FAILED(m_pd3dDevice->Present(NULL,NULL,NULL,NULL));
return S_OK;
}
//********************************************************************
//释放D3D相关接口
//********************************************************************
void Game::CleanUp()
{
SAFE_RELEASE(m_pTextureWEA);
SAFE_RELEASE(m_pTextureSNAIL); //释放画蜗牛的纹理接口指针
SAFE_RELEASE(m_pTextureFRUIT); //释放画水果的纹理接口指针
SAFE_RELEASE(m_pTextureP); //释放画人的纹理接口指针
SAFE_RELEASE(m_pTextureBJ); //释放画人的纹理接口指针
SAFE_RELEASE(m_pSprinte); //释放精灵接口指针
SAFE_RELEASE(m_pd3dDevice); //释放设备指针
SAFE_RELEASE(m_pd3d); //释放D3D接口指针
}
//********************************************************************
//处理游戏逻辑
//********************************************************************
HRESULT Game::GameLogic()
{
if(BSP == FALSE)
{
Move();
}
Hit();
DrawProp();
return S_OK;
}
//*******************************************************************
//游戏循环
//*******************************************************************
HRESULT Game::GameRun()
{
MSG msg;
while(true)
{
//消息处理
if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
if(msg.message == WM_QUIT)
{
break;
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
GAME_FAILED(GameLogic()); //处理游戏逻辑
GAME_FAILED(GameRender()); //绘制图象
}
//响应退出
if(KEY(VK_ESCAPE))
{
SendMessage(m_hwnd,WM_CLOSE,0,0);
}
}
return S_OK;
}
#ifndef GAME_H
#define GAME_H
#include <stdio.h>
#include <d3d9.h>
#include <d3dx9.h>
#include "Macro.h"
#define WND_CLASS_NAME "冒险岛"
#define WIN_NAME "冒险岛"
LRESULT CALLBACK MsgProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam);
class Game
{
public:
Game(HINSTANCE hinst);
~Game();
HRESULT GameInit(int nCmdShow); //游戏初试化
HRESULT GameRun(); //游戏循环
protected:
virtual HRESULT InitWindow(int nCmdShow); //windows初始化
virtual HRESULT InitD3DDevice(); //设备初始化
virtual HRESULT InitScene(); //场景初始化
virtual VOID Move(); //人物移动
virtual VOID Hit(); //碰撞
virtual VOID DrawProp(); //选择道具该画的部分
virtual HRESULT GameRender(); //渲染
virtual HRESULT GameLogic(); //游戏逻辑
virtual void CleanUp(); //游戏的释放
private:
LPDIRECT3D9 m_pd3d; //d3d接口指针
LPDIRECT3DDEVICE9 m_pd3dDevice; //d3d设备接口指针
HWND m_hwnd;
HINSTANCE m_hinst;
LPDIRECT3DTEXTURE9 m_pTextureBJ,m_pTextureP,m_pTextureFRUIT,m_pTextureSNAIL,m_pTextureWEA;
LPD3DXSPRITE m_pSprinte;
RECT m_prectBj,m_prectP,m_prectFRUIT,m_prectSNAIL,m_prectWEA;
D3DXVECTOR3 m_pdestBj,m_pdestP,m_pdestFRUIT,m_pdestSNAIL,m_pdestWEA;
};
#endif
//通用宏定义
#ifndef GAMEMACRO_H
#define GAMEMACRO_H
#define SAFE_RELEASE(p) if( (p) != NULL ){ (p)->Release(); (p) = NULL; }
#define SAFE_DEL(p) if( (p) != NULL ){ delete (p); (p) = NULL; }
#define SAFE_DEL_ARRAY(p) if( (p) != NULL ){ delete[] (p); (p) = NULL; }
#define GAME_FAILED(hr) if( FAILED(hr) ){ return E_FAIL; }
#define GAME_ISNULL(hr) if( NULL == (hr) ){ return E_FAIL; }
#define KEY(key) (GetAsyncKeyState(key) & 0x8000 ? 1 : 0)
#endif
会做外挂的拿去、