用C++多态写了个飞机游戏的架子,但主机重生的问题很显眼,有兴趣的朋友来看看
飞机[重生].rar
提取码:73663310
提取链接:http://www.mikebox.com/index.php?code=73663310
我的q是:380122248(真心希望有人指点啊)
源码全在上面,只是个关于主飞机析构的问题搞不清白。。
我把相关的代码先贴一下:(源码还有运行测试都在“飞机[重生].rar ”里面了)
//=======WinMain.cpp 这是Win32的部分
程序代码:
程序代码:
程序代码:
程序代码:
飞机[重生].rar
提取码:73663310
提取链接:http://www.mikebox.com/index.php?code=73663310
我的q是:380122248(真心希望有人指点啊)
源码全在上面,只是个关于主飞机析构的问题搞不清白。。
我把相关的代码先贴一下:(源码还有运行测试都在“飞机[重生].rar ”里面了)
//=======WinMain.cpp 这是Win32的部分
程序代码:
#include "WinMain.h"
#pragma comment(lib,"Debug/lib/Logo.lib")
HWND hWnd;
HINSTANCE main_hInstance;
HWND main_hWnd;
CGame* pGame=NULL; //...............
CMyInput* pMyInput=NULL;
LRESULT CALLBACK WinProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam);
bool InitWindow( HINSTANCE hInstance, int nCmdShow);
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPre,LPSTR lpCmdline,int cmdShow)
{
MSG msg;
if(!InitWindow(hInstance,cmdShow)) //执行窗口初始化
{
return false;
}
main_hInstance=hInstance;
main_hWnd=hWnd;
{
pMyInput=new CMyInput(hInstance,hWnd); //................①
CMyPlane::LoadMyPlaneImage(); //................②
CMyBullet::LoadMyBulletImage(); //................②
CMyEnemyPlane::LoadMyEnemyPlaneImage(); //................②
CMyExplode::LoadMyExplodeImage(); //................②
pGame=new CGame(hWnd); //................③
}
while(true) //进入消息循环:
{
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) //寻找消息不同于GetMessage()
{
if ( msg.message==WM_QUIT)
{
break;
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
pGame->Update(); //................④
pGame->FrameFun(); //................⑤
pGame->RenderFun(); //................⑥
Sleep(pGame->m_sleepValue);
}
delete pGame;
return (int)msg.wParam;
}
bool InitWindow( HINSTANCE hInstance, int nCmdShow)
{
//定义窗口风格:
WNDCLASS wc;
wc.style = CS_HREDRAW | CS_VREDRAW;;
wc.lpfnWndProc = (WNDPROC)WinProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(hInstance,(LPCTSTR)IDI_ICON1);
wc.hCursor = NULL;
wc.hbrBackground = CreateSolidBrush(0x0);
wc.lpszMenuName = NULL;
wc.lpszClassName = WND_CLASS_NAME;
if(!RegisterClass(&wc)) //注册窗口
{
return false;
}
if(!(hWnd=CreateWindowEx(0,
WND_CLASS_NAME,
"Just So So",
WS_SYSMENU|WS_MINIMIZEBOX|WS_CAPTION,
WINDOW_START_X,
WINDOW_START_Y,
WINDOW_WIDTH,
WINDOW_HEIGHT,
0,
0,
hInstance,
0
)))
{
return false;
}
if(!hWnd)
{
return FALSE;
}
ShowWindow(hWnd,nCmdShow);//显示窗口
UpdateWindow(hWnd);//刷新窗口
MyLogo(hWnd,WINDOW_WIDTH,WINDOW_HEIGHT); //显示"吸血鬼"logo
return TRUE;
}
LRESULT CALLBACK WinProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
//RECT rect;
switch(message)
{
case WM_CREATE:
{
return 0;
}
break;
case WM_SETFOCUS:
{
if(pMyInput->GetKeyboardDevice()!=NULL)
pMyInput->GetKeyboardDevice()->Acquire(); //搞得心里烦
}
break;
case WM_SETCURSOR:
{
SetCursor(0); //隐藏鼠标
}
break;
case WM_PAINT:
{
hdc=BeginPaint(hWnd,&ps);
//InvalidateRect(hWnd,&rect,true);
EndPaint(hWnd,&ps);
return 0;
}
break;
case WM_GETMINMAXINFO:
{
// Don't allow resizing in windowed mode.
// Fix the size of the window to 640x480 (client size)
MINMAXINFO* pMinMax = (MINMAXINFO*) lParam;
DWORD dwFrameWidth = GetSystemMetrics( SM_CXSIZEFRAME );
DWORD dwFrameHeight = GetSystemMetrics( SM_CYSIZEFRAME );
DWORD dwMenuHeight = GetSystemMetrics( SM_CYMENU );
DWORD dwCaptionHeight = GetSystemMetrics( SM_CYCAPTION );
pMinMax->ptMinTrackSize.x = WINDOW_WIDTH + dwFrameWidth * 2;
pMinMax->ptMinTrackSize.y = WINDOW_HEIGHT-20 + dwFrameHeight * 2
+dwMenuHeight + dwCaptionHeight;
pMinMax->ptMaxTrackSize.x = pMinMax->ptMinTrackSize.x;
pMinMax->ptMaxTrackSize.y = pMinMax->ptMinTrackSize.y;
return 0;
}
break;
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
}
break;
default:
break;
}
return DefWindowProc(hWnd,message,wParam,lParam);
}
//====Game.cpp 这个是游戏框架#pragma comment(lib,"Debug/lib/Logo.lib")
HWND hWnd;
HINSTANCE main_hInstance;
HWND main_hWnd;
CGame* pGame=NULL; //...............
CMyInput* pMyInput=NULL;
LRESULT CALLBACK WinProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam);
bool InitWindow( HINSTANCE hInstance, int nCmdShow);
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPre,LPSTR lpCmdline,int cmdShow)
{
MSG msg;
if(!InitWindow(hInstance,cmdShow)) //执行窗口初始化
{
return false;
}
main_hInstance=hInstance;
main_hWnd=hWnd;
{
pMyInput=new CMyInput(hInstance,hWnd); //................①
CMyPlane::LoadMyPlaneImage(); //................②
CMyBullet::LoadMyBulletImage(); //................②
CMyEnemyPlane::LoadMyEnemyPlaneImage(); //................②
CMyExplode::LoadMyExplodeImage(); //................②
pGame=new CGame(hWnd); //................③
}
while(true) //进入消息循环:
{
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) //寻找消息不同于GetMessage()
{
if ( msg.message==WM_QUIT)
{
break;
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
pGame->Update(); //................④
pGame->FrameFun(); //................⑤
pGame->RenderFun(); //................⑥
Sleep(pGame->m_sleepValue);
}
delete pGame;
return (int)msg.wParam;
}
bool InitWindow( HINSTANCE hInstance, int nCmdShow)
{
//定义窗口风格:
WNDCLASS wc;
wc.style = CS_HREDRAW | CS_VREDRAW;;
wc.lpfnWndProc = (WNDPROC)WinProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(hInstance,(LPCTSTR)IDI_ICON1);
wc.hCursor = NULL;
wc.hbrBackground = CreateSolidBrush(0x0);
wc.lpszMenuName = NULL;
wc.lpszClassName = WND_CLASS_NAME;
if(!RegisterClass(&wc)) //注册窗口
{
return false;
}
if(!(hWnd=CreateWindowEx(0,
WND_CLASS_NAME,
"Just So So",
WS_SYSMENU|WS_MINIMIZEBOX|WS_CAPTION,
WINDOW_START_X,
WINDOW_START_Y,
WINDOW_WIDTH,
WINDOW_HEIGHT,
0,
0,
hInstance,
0
)))
{
return false;
}
if(!hWnd)
{
return FALSE;
}
ShowWindow(hWnd,nCmdShow);//显示窗口
UpdateWindow(hWnd);//刷新窗口
MyLogo(hWnd,WINDOW_WIDTH,WINDOW_HEIGHT); //显示"吸血鬼"logo
return TRUE;
}
LRESULT CALLBACK WinProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
//RECT rect;
switch(message)
{
case WM_CREATE:
{
return 0;
}
break;
case WM_SETFOCUS:
{
if(pMyInput->GetKeyboardDevice()!=NULL)
pMyInput->GetKeyboardDevice()->Acquire(); //搞得心里烦
}
break;
case WM_SETCURSOR:
{
SetCursor(0); //隐藏鼠标
}
break;
case WM_PAINT:
{
hdc=BeginPaint(hWnd,&ps);
//InvalidateRect(hWnd,&rect,true);
EndPaint(hWnd,&ps);
return 0;
}
break;
case WM_GETMINMAXINFO:
{
// Don't allow resizing in windowed mode.
// Fix the size of the window to 640x480 (client size)
MINMAXINFO* pMinMax = (MINMAXINFO*) lParam;
DWORD dwFrameWidth = GetSystemMetrics( SM_CXSIZEFRAME );
DWORD dwFrameHeight = GetSystemMetrics( SM_CYSIZEFRAME );
DWORD dwMenuHeight = GetSystemMetrics( SM_CYMENU );
DWORD dwCaptionHeight = GetSystemMetrics( SM_CYCAPTION );
pMinMax->ptMinTrackSize.x = WINDOW_WIDTH + dwFrameWidth * 2;
pMinMax->ptMinTrackSize.y = WINDOW_HEIGHT-20 + dwFrameHeight * 2
+dwMenuHeight + dwCaptionHeight;
pMinMax->ptMaxTrackSize.x = pMinMax->ptMinTrackSize.x;
pMinMax->ptMaxTrackSize.y = pMinMax->ptMinTrackSize.y;
return 0;
}
break;
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
}
break;
default:
break;
}
return DefWindowProc(hWnd,message,wParam,lParam);
}
程序代码:
#include "WinMain.h"
#pragma comment(lib,"Msimg32.lib")
/*
class CGame
{
protected:
class CMyTimer* m_pMyTimer;
class CMyBackground* m_pMyBackground;
class CMyPlane* m_pMyPlane;
HWND m_hWnd;
HDC m_hdc,m_mdc,m_bdc;
HBITMAP m_fullBmp;
public:
static int m_sleepValue;
friend class CMySprite;
friend class CMyAnimation;
CGame(HWND _hWnd);
virtual ~CGame();
void Update();
void FrameFun();
void RenderFun();
void RenderClear();
void RenderWords();
void RenderScreen();
void DeleteAll();
void MyPlaneUpdate();
};
*/
int CGame::m_sleepValue=0;
CGame::CGame(HWND _hWnd)
{
m_hWnd=_hWnd;
m_hdc=GetDC(m_hWnd);
m_mdc=CreateCompatibleDC(m_hdc);
m_bdc=CreateCompatibleDC(m_hdc);
m_fullBmp=CreateCompatibleBitmap(m_hdc,WINDOW_WIDTH,WINDOW_HEIGHT);
SelectObject(m_mdc,m_fullBmp);
m_pMyTimer=new CMyTimer();
m_pMyBackground=new CMyBackground(200,0);
m_pMyPlane=CMyPlane::InitMyPlane();
}
CGame::~CGame()
{
DeleteAll();
}
void CGame::Update()
{
m_pMyTimer->UpdateFPS();
if(m_pMyTimer->GetFPS()>150)
{
m_sleepValue++;
}
MyPlaneUpdate();
}
void CGame::FrameFun()
{
float dt=m_pMyTimer->GetDelta();
CMyEnemyPlane::CreateEnemyPlane(dt);
CMyBase::GameLogic(dt);
}
void CGame::RenderFun()
{
RenderClear();
CMyBase::GameRender(m_mdc);
RenderWords();
RenderScreen();
}
void CGame::RenderClear()
{
SelectObject(m_mdc,GetStockBrush(BLACK_BRUSH));
Rectangle(m_mdc,0,0,WINDOW_WIDTH,WINDOW_HEIGHT);
}
void CGame::RenderWords()
{
m_pMyTimer->ShowFPS(m_mdc);
m_pMyBackground->RenderStars(m_mdc);
m_pMyPlane->RenderHP(m_mdc);
}
void CGame::RenderScreen()
{
BitBlt(m_hdc,0,0,WINDOW_WIDTH,WINDOW_HEIGHT,m_mdc,0,0,SRCCOPY);
//TransparentBlt
(m_hdc,0,0,WINDOW_WIDTH,WINDOW_HEIGHT,m_mdc,0,0,WINDOW_WIDTH,WINDOW_HEIGHT,RGB(0,0,0));
}
void CGame::DeleteAll()
{
delete m_pMyTimer;
delete m_pMyBackground;
delete m_pMyPlane;
ReleaseDC(m_hWnd,m_hdc);
DeleteDC(m_mdc);
DeleteDC(m_bdc);
}
void CGame::MyPlaneUpdate()
{
if(m_pMyPlane->m_balive==false && CMyPlane::s_life>0)
{
m_pMyPlane=NULL;
m_pMyPlane=new CMyPlane(WINDOW_WIDTH/2-50,WINDOW_HEIGHT-
48);//CMyPlane::InitMyPlane();
CMyPlane::s_life--;
}
}
//=====MyPlane.cpp 这个是主飞机(就是玩家)其中的静态部分惹出了问题#pragma comment(lib,"Msimg32.lib")
/*
class CGame
{
protected:
class CMyTimer* m_pMyTimer;
class CMyBackground* m_pMyBackground;
class CMyPlane* m_pMyPlane;
HWND m_hWnd;
HDC m_hdc,m_mdc,m_bdc;
HBITMAP m_fullBmp;
public:
static int m_sleepValue;
friend class CMySprite;
friend class CMyAnimation;
CGame(HWND _hWnd);
virtual ~CGame();
void Update();
void FrameFun();
void RenderFun();
void RenderClear();
void RenderWords();
void RenderScreen();
void DeleteAll();
void MyPlaneUpdate();
};
*/
int CGame::m_sleepValue=0;
CGame::CGame(HWND _hWnd)
{
m_hWnd=_hWnd;
m_hdc=GetDC(m_hWnd);
m_mdc=CreateCompatibleDC(m_hdc);
m_bdc=CreateCompatibleDC(m_hdc);
m_fullBmp=CreateCompatibleBitmap(m_hdc,WINDOW_WIDTH,WINDOW_HEIGHT);
SelectObject(m_mdc,m_fullBmp);
m_pMyTimer=new CMyTimer();
m_pMyBackground=new CMyBackground(200,0);
m_pMyPlane=CMyPlane::InitMyPlane();
}
CGame::~CGame()
{
DeleteAll();
}
void CGame::Update()
{
m_pMyTimer->UpdateFPS();
if(m_pMyTimer->GetFPS()>150)
{
m_sleepValue++;
}
MyPlaneUpdate();
}
void CGame::FrameFun()
{
float dt=m_pMyTimer->GetDelta();
CMyEnemyPlane::CreateEnemyPlane(dt);
CMyBase::GameLogic(dt);
}
void CGame::RenderFun()
{
RenderClear();
CMyBase::GameRender(m_mdc);
RenderWords();
RenderScreen();
}
void CGame::RenderClear()
{
SelectObject(m_mdc,GetStockBrush(BLACK_BRUSH));
Rectangle(m_mdc,0,0,WINDOW_WIDTH,WINDOW_HEIGHT);
}
void CGame::RenderWords()
{
m_pMyTimer->ShowFPS(m_mdc);
m_pMyBackground->RenderStars(m_mdc);
m_pMyPlane->RenderHP(m_mdc);
}
void CGame::RenderScreen()
{
BitBlt(m_hdc,0,0,WINDOW_WIDTH,WINDOW_HEIGHT,m_mdc,0,0,SRCCOPY);
//TransparentBlt
(m_hdc,0,0,WINDOW_WIDTH,WINDOW_HEIGHT,m_mdc,0,0,WINDOW_WIDTH,WINDOW_HEIGHT,RGB(0,0,0));
}
void CGame::DeleteAll()
{
delete m_pMyTimer;
delete m_pMyBackground;
delete m_pMyPlane;
ReleaseDC(m_hWnd,m_hdc);
DeleteDC(m_mdc);
DeleteDC(m_bdc);
}
void CGame::MyPlaneUpdate()
{
if(m_pMyPlane->m_balive==false && CMyPlane::s_life>0)
{
m_pMyPlane=NULL;
m_pMyPlane=new CMyPlane(WINDOW_WIDTH/2-50,WINDOW_HEIGHT-
48);//CMyPlane::InitMyPlane();
CMyPlane::s_life--;
}
}
程序代码:
#include "WinMain.h"
/*#define HP 100
class CMyPlane : public CMyBase
{
protected:
static HBITMAP s_MyPlaneImage;
static HBITMAP s_BarImage;
static HBITMAP s_BarSideImage;
static HBITMAP s_PlayerImage;
CMySprite* m_pMyPlaneSprite;
CMySprite* m_pMyBarSprite;
CMySprite* m_pMyBarSideSprite;
CMySprite* m_pPlayerSprite;
static CMyPlane* s_pMyPlane;
int m_speed;
int m_HP;
char strLIFE[20];
static int s_life;
int m_bullettype;
float m_frequency0;
float m_frequency1;
float m_frequency2;
float m_frequency3;
bool m_isW;
bool m_isA;
bool m_isD;
public:
friend class CMyEnemyBullet;
friend class CMyEnemy;
friend class CGame;
static CMyPlane* InitMyPlane();
CMyPlane(int _x,int _y);
virtual ~CMyPlane();
virtual void Logic(float dt);
virtual void Render(HDC mdc);
static void LoadMyPlaneImage();
void ChangeBullet();
void GetPosition(int& _x,int& _y);
void RenderHP(HDC mdc);
void RenderBar(HDC mdc);
void RenderBarSide(HDC mdc);
void RenderPlayer(HDC mdc);
};
*/
HBITMAP CMyPlane::s_MyPlaneImage=NULL;
HBITMAP CMyPlane::s_BarImage=NULL;
HBITMAP CMyPlane::s_BarSideImage=NULL;
HBITMAP CMyPlane::s_PlayerImage=NULL;
CMyPlane* CMyPlane::s_pMyPlane=NULL;
int CMyPlane::s_life=3;
CMyPlane* CMyPlane::InitMyPlane()
{
if(s_pMyPlane==NULL)
{
s_pMyPlane=new CMyPlane(WINDOW_WIDTH/2-50,WINDOW_HEIGHT-48);
}
return s_pMyPlane;
}
CMyPlane::CMyPlane(int _x,int _y):CMyBase(_x,_y)
{
m_frequency0 = 0.6f;
m_frequency1 = 0.6f;
m_frequency2 = 0.6f;
m_frequency3 = 1.0f;
m_width = 50;
m_height = 48;
m_speed = 100;
m_HP=HP;
m_isW=false;
m_isA=false;
m_isD=false;
m_bullettype = 0;
assert(s_MyPlaneImage!=NULL);
assert(s_BarImage!=NULL);
assert(s_BarSideImage!=NULL);
assert(s_PlayerImage!=NULL);
m_pMyPlaneSprite=new CMySprite(s_MyPlaneImage);
m_pMyBarSideSprite=new CMySprite(s_BarSideImage);
m_pMyBarSprite=new CMySprite(s_BarImage);
m_pPlayerSprite=new CMySprite(s_PlayerImage);
}
CMyPlane::~CMyPlane()
{
delete m_pMyPlaneSprite;
delete m_pMyBarSideSprite;
delete m_pMyBarSprite;
delete m_pPlayerSprite;
}
void CMyPlane::LoadMyPlaneImage()
{
s_MyPlaneImage=(HBITMAP)LoadImage(0,"image/FC-
1.bmp",IMAGE_BITMAP,150,65,LR_LOADFROMFILE);
s_BarImage=(HBITMAP)LoadImage
(0,"image/Bar.bmp",IMAGE_BITMAP,144,13,LR_LOADFROMFILE);
s_BarSideImage=(HBITMAP)LoadImage
(0,"image/BarSide.bmp",IMAGE_BITMAP,150,17,LR_LOADFROMFILE);
s_PlayerImage=(HBITMAP)LoadImage
(0,"image/Player.bmp",IMAGE_BITMAP,47,58,LR_LOADFROMFILE);
}
void CMyPlane::Logic(float dt)
{
float ds = dt*m_speed;
if(CMyInput::hh->ReadKeyboard())
{
if(CMyInput::hh->IsKeyPressed(DIK_W))
{
m_posy-=ds; m_isW=true;
}
if(CMyInput::hh->IsKeyPressed(DIK_S))
{
m_posy+=ds;
}
if(CMyInput::hh->IsKeyPressed(DIK_A))
{
m_posx-=ds; m_isA=true;
}
if(CMyInput::hh->IsKeyPressed(DIK_D))
{
m_posx+=ds; m_isD=true;
}
if(m_posx<=0)
{
m_posx=0;
}
if(m_posx>=WINDOW_WIDTH-m_width)
{
m_posx=WINDOW_WIDTH-m_width;
}
if(m_posy<=0)
{
m_posy=0;
}
if(m_posy>=WINDOW_HEIGHT-m_height)
{
m_posy=WINDOW_HEIGHT-m_height;
}
//========================================================================================
==
if(CMyInput::hh->IsKeyPressed(DIK_L))
//换子弹
{
ChangeBullet();
}
if(CMyInput::hh->IsKeyPressed(DIK_I))
//放满屏导弹
{
m_frequency3+=dt;
if(m_frequency3>1.0f)
{
for(int j=0;j<3;j++)
{
for(int i=0;i<WINDOW_WIDTH;i+=23)
{
new CMyBullet
(i,WINDOW_HEIGHT,6,PI*90.0f/180.0f);
}
}
m_frequency3-=1.0f;
}
}
if(CMyInput::hh->IsKeyPressed(DIK_K))
//放导弹
{
m_frequency1+=dt;
if(m_frequency1>0.6f)
{
new CMyBullet(m_posx+m_width/2-40,
m_posy,5,PI*90.0f/180.0f);
new CMyBullet(m_posx+m_width/2+40-
23,m_posy,5,PI*90.0f/180.0f);
m_frequency1-=0.6f;
}
}
if(CMyInput::hh->IsKeyPressed(DIK_SPACE))
//大杂烩
{
m_frequency2+=dt;
if(m_frequency2>0.6f)
{
new CMyBullet(m_posx+m_width/2-
11/2,m_posy,0,PI*30.0f/180.0f);
new CMyBullet(m_posx+m_width/2-
11/2,m_posy,0,PI*60.0f/180.0f);
new CMyBullet(m_posx+m_width/2-
11/2,m_posy,0,PI*120.0f/180.0f);
new CMyBullet(m_posx+m_width/2-
11/2,m_posy,0,PI*150.0f/180.0f);
new CMyBullet(m_posx+m_width/2-
11/2,m_posy,4,PI*90.0f/180.0f);//终于加上了火焰,欧耶
new CMyBullet(m_posx+m_width/2-
11/2,m_posy,4,PI*270.0f/180.0f);
new CMyBullet(m_posx+m_width/2-
11/2,m_posy,2,PI*210.0f/180.0f);
new CMyBullet(m_posx+m_width/2-
11/2,m_posy,2,PI*330.0f/180.0f);
new CMyBullet(m_posx+m_width/2-
11/2,m_posy,1,PI*240.0f/180.0f);
new CMyBullet(m_posx+m_width/2-
11/2,m_posy,1,PI*300.0f/180.0f);
new CMyBullet(m_posx+m_width/2-
11/2,m_posy,3,PI*0.0f/180.0f);
new CMyBullet(m_posx+m_width/2-
11/2,m_posy,3,PI*180.0f/180.0f);
m_frequency2-=0.6f;
}
}
if(CMyInput::hh->IsKeyPressed(DIK_J))
//5种增强子弹
{
m_frequency0+=dt;
if(m_frequency0>0.6f)
{
switch(m_bullettype)
{
case 0:
new CMyBullet(m_posx+m_width/2-
11/2,m_posy,m_bullettype,PI*60.0f/180.0f);
new CMyBullet(m_posx+m_width/2-
11/2,m_posy,m_bullettype,PI*120.0f/180.0f);
new CMyBullet(m_posx+m_width/2-
11/2,m_posy,m_bullettype,PI*90.0f/180.0f);
break;
case 1:
new CMyBullet(m_posx+m_width/2-
16/2,m_posy,m_bullettype,PI*60.0f/180.0f);
new CMyBullet(m_posx+m_width/2-
16/2,m_posy,m_bullettype,PI*90.0f/180.0f);
new CMyBullet(m_posx+m_width/2-
16/2,m_posy,m_bullettype,PI*120.0f/180.0f);
break;
case 2:
new CMyBullet(m_posx+m_width/2-16/2-
15,m_posy,m_bullettype,PI*90.0f/180.0f);
new CMyBullet(m_posx+m_width/2-16/2
,m_posy,m_bullettype,PI*90.0f/180.0f);
new CMyBullet(m_posx+m_width/2-
16/2+15,m_posy,m_bullettype,PI*90.0f/180.0f);
break;
case 3:
new CMyBullet(m_posx+m_width/2-16/2
,m_posy,m_bullettype,PI*90.0f/180.0f);
new CMyBullet(m_posx+m_width/2-16/2
,m_posy,m_bullettype,PI*30.0f/180.0f);
new CMyBullet(m_posx+m_width/2-16/2
,m_posy,m_bullettype,PI*60.0f/180.0f);
new CMyBullet(m_posx+m_width/2-16/2
,m_posy,m_bullettype,PI*150.0f/180.0f);
new CMyBullet(m_posx+m_width/2-16/2
,m_posy,m_bullettype,PI*120.0f/180.0f);
break;
case 4:
new CMyBullet(m_posx+m_width/2-16/2
,m_posy,m_bullettype,PI*90.0f/180.0f);
new CMyBullet(m_posx+m_width/2-16/2
,m_posy,m_bullettype,PI*30.0f/180.0f);
new CMyBullet(m_posx+m_width/2-16/2
,m_posy,m_bullettype,PI*60.0f/180.0f);
new CMyBullet(m_posx+m_width/2-16/2
,m_posy,m_bullettype,PI*150.0f/180.0f);
new CMyBullet(m_posx+m_width/2-16/2
,m_posy,m_bullettype,PI*120.0f/180.0f);
break;
}
m_frequency0-=0.6f;
}
}
}
}
void CMyPlane::Render(HDC mdc)
{
if(m_isW==false && m_isA==false && m_isD==false)
//...
{
m_pMyPlaneSprite->Render(mdc,m_posx,m_posy,m_width,m_height,0,0);
}
if(m_isW==true && m_isA==false && m_isD==false)
//...W
{
m_pMyPlaneSprite->Render(mdc,m_posx,m_posy,m_width,m_height+17,0,0);
m_isW=false;
}
if(m_isW==false && m_isA==true && m_isD==false)
//...A
{
m_pMyPlaneSprite->Render(mdc,m_posx,m_posy,m_width,m_height,0+m_width,0);
m_isA=false;
}
if(m_isW==false && m_isA==false && m_isD==true)
//...D
{
m_pMyPlaneSprite->Render
(mdc,m_posx,m_posy,m_width,m_height,0+2*m_width,0);
m_isD=false;
}
if(m_isW==true && m_isA==true && m_isD==false)
//...W+A
{
m_pMyPlaneSprite->Render
(mdc,m_posx,m_posy,m_width,m_height+17,0+m_width,0);
m_isA=false;
m_isW=false;
}
if(m_isW==true && m_isA==false && m_isD==true)
//...W+D
{
m_pMyPlaneSprite->Render
(mdc,m_posx,m_posy,m_width,m_height+17,0+2*m_width,0);
m_isD=false;
m_isW=false;
}
if(m_isW==false && m_isA==true && m_isD==true)
//...A+D
{
m_pMyPlaneSprite->Render(mdc,m_posx,m_posy,m_width,m_height,0,0);
m_isA=false;
m_isD=false;
}
if(m_isW==true && m_isA==true && m_isD==true)
//...A+D+W
{
m_pMyPlaneSprite->Render(mdc,m_posx,m_posy,m_width,m_height+17,0,0);
m_isA=false;
m_isD=false;
m_isW=false;
}
}
void CMyPlane::ChangeBullet()
{
m_bullettype=(m_bullettype+1)%5;
}
void CMyPlane::GetPosition(int& _x,int& _y)
{
_x=m_posx;
_y=m_posy;
}
void CMyPlane::RenderHP(HDC mdc)
{
sprintf(strLIFE," = %d",s_life);
SetBkMode(mdc,TRANSPARENT); //透明处理
SetTextColor(mdc,RGB(0,255,255));
TextOut(mdc,47,0,strLIFE,(int)strlen(strLIFE));
TextOut(mdc,47,18,"GL615 1ST",(int)strlen("GL615 1ST"));
RenderBarSide(mdc);
RenderBar(mdc);
RenderPlayer(mdc);
}
void CMyPlane::RenderBarSide(HDC mdc)
{
m_pMyBarSideSprite->Render(mdc,47,58-17,150,17,0,0);
}
void CMyPlane::RenderBar(HDC mdc)
{
m_pMyBarSprite->Render(mdc,47+3,58-17+2,144*m_HP/HP,13,0,0);
}
void CMyPlane::RenderPlayer(HDC mdc)
{
m_pPlayerSprite->Render(mdc,0,0,47,58,0,0);
}
//=======MyBase.cpp 这个就是基类(多态就靠它了)/*#define HP 100
class CMyPlane : public CMyBase
{
protected:
static HBITMAP s_MyPlaneImage;
static HBITMAP s_BarImage;
static HBITMAP s_BarSideImage;
static HBITMAP s_PlayerImage;
CMySprite* m_pMyPlaneSprite;
CMySprite* m_pMyBarSprite;
CMySprite* m_pMyBarSideSprite;
CMySprite* m_pPlayerSprite;
static CMyPlane* s_pMyPlane;
int m_speed;
int m_HP;
char strLIFE[20];
static int s_life;
int m_bullettype;
float m_frequency0;
float m_frequency1;
float m_frequency2;
float m_frequency3;
bool m_isW;
bool m_isA;
bool m_isD;
public:
friend class CMyEnemyBullet;
friend class CMyEnemy;
friend class CGame;
static CMyPlane* InitMyPlane();
CMyPlane(int _x,int _y);
virtual ~CMyPlane();
virtual void Logic(float dt);
virtual void Render(HDC mdc);
static void LoadMyPlaneImage();
void ChangeBullet();
void GetPosition(int& _x,int& _y);
void RenderHP(HDC mdc);
void RenderBar(HDC mdc);
void RenderBarSide(HDC mdc);
void RenderPlayer(HDC mdc);
};
*/
HBITMAP CMyPlane::s_MyPlaneImage=NULL;
HBITMAP CMyPlane::s_BarImage=NULL;
HBITMAP CMyPlane::s_BarSideImage=NULL;
HBITMAP CMyPlane::s_PlayerImage=NULL;
CMyPlane* CMyPlane::s_pMyPlane=NULL;
int CMyPlane::s_life=3;
CMyPlane* CMyPlane::InitMyPlane()
{
if(s_pMyPlane==NULL)
{
s_pMyPlane=new CMyPlane(WINDOW_WIDTH/2-50,WINDOW_HEIGHT-48);
}
return s_pMyPlane;
}
CMyPlane::CMyPlane(int _x,int _y):CMyBase(_x,_y)
{
m_frequency0 = 0.6f;
m_frequency1 = 0.6f;
m_frequency2 = 0.6f;
m_frequency3 = 1.0f;
m_width = 50;
m_height = 48;
m_speed = 100;
m_HP=HP;
m_isW=false;
m_isA=false;
m_isD=false;
m_bullettype = 0;
assert(s_MyPlaneImage!=NULL);
assert(s_BarImage!=NULL);
assert(s_BarSideImage!=NULL);
assert(s_PlayerImage!=NULL);
m_pMyPlaneSprite=new CMySprite(s_MyPlaneImage);
m_pMyBarSideSprite=new CMySprite(s_BarSideImage);
m_pMyBarSprite=new CMySprite(s_BarImage);
m_pPlayerSprite=new CMySprite(s_PlayerImage);
}
CMyPlane::~CMyPlane()
{
delete m_pMyPlaneSprite;
delete m_pMyBarSideSprite;
delete m_pMyBarSprite;
delete m_pPlayerSprite;
}
void CMyPlane::LoadMyPlaneImage()
{
s_MyPlaneImage=(HBITMAP)LoadImage(0,"image/FC-
1.bmp",IMAGE_BITMAP,150,65,LR_LOADFROMFILE);
s_BarImage=(HBITMAP)LoadImage
(0,"image/Bar.bmp",IMAGE_BITMAP,144,13,LR_LOADFROMFILE);
s_BarSideImage=(HBITMAP)LoadImage
(0,"image/BarSide.bmp",IMAGE_BITMAP,150,17,LR_LOADFROMFILE);
s_PlayerImage=(HBITMAP)LoadImage
(0,"image/Player.bmp",IMAGE_BITMAP,47,58,LR_LOADFROMFILE);
}
void CMyPlane::Logic(float dt)
{
float ds = dt*m_speed;
if(CMyInput::hh->ReadKeyboard())
{
if(CMyInput::hh->IsKeyPressed(DIK_W))
{
m_posy-=ds; m_isW=true;
}
if(CMyInput::hh->IsKeyPressed(DIK_S))
{
m_posy+=ds;
}
if(CMyInput::hh->IsKeyPressed(DIK_A))
{
m_posx-=ds; m_isA=true;
}
if(CMyInput::hh->IsKeyPressed(DIK_D))
{
m_posx+=ds; m_isD=true;
}
if(m_posx<=0)
{
m_posx=0;
}
if(m_posx>=WINDOW_WIDTH-m_width)
{
m_posx=WINDOW_WIDTH-m_width;
}
if(m_posy<=0)
{
m_posy=0;
}
if(m_posy>=WINDOW_HEIGHT-m_height)
{
m_posy=WINDOW_HEIGHT-m_height;
}
//========================================================================================
==
if(CMyInput::hh->IsKeyPressed(DIK_L))
//换子弹
{
ChangeBullet();
}
if(CMyInput::hh->IsKeyPressed(DIK_I))
//放满屏导弹
{
m_frequency3+=dt;
if(m_frequency3>1.0f)
{
for(int j=0;j<3;j++)
{
for(int i=0;i<WINDOW_WIDTH;i+=23)
{
new CMyBullet
(i,WINDOW_HEIGHT,6,PI*90.0f/180.0f);
}
}
m_frequency3-=1.0f;
}
}
if(CMyInput::hh->IsKeyPressed(DIK_K))
//放导弹
{
m_frequency1+=dt;
if(m_frequency1>0.6f)
{
new CMyBullet(m_posx+m_width/2-40,
m_posy,5,PI*90.0f/180.0f);
new CMyBullet(m_posx+m_width/2+40-
23,m_posy,5,PI*90.0f/180.0f);
m_frequency1-=0.6f;
}
}
if(CMyInput::hh->IsKeyPressed(DIK_SPACE))
//大杂烩
{
m_frequency2+=dt;
if(m_frequency2>0.6f)
{
new CMyBullet(m_posx+m_width/2-
11/2,m_posy,0,PI*30.0f/180.0f);
new CMyBullet(m_posx+m_width/2-
11/2,m_posy,0,PI*60.0f/180.0f);
new CMyBullet(m_posx+m_width/2-
11/2,m_posy,0,PI*120.0f/180.0f);
new CMyBullet(m_posx+m_width/2-
11/2,m_posy,0,PI*150.0f/180.0f);
new CMyBullet(m_posx+m_width/2-
11/2,m_posy,4,PI*90.0f/180.0f);//终于加上了火焰,欧耶
new CMyBullet(m_posx+m_width/2-
11/2,m_posy,4,PI*270.0f/180.0f);
new CMyBullet(m_posx+m_width/2-
11/2,m_posy,2,PI*210.0f/180.0f);
new CMyBullet(m_posx+m_width/2-
11/2,m_posy,2,PI*330.0f/180.0f);
new CMyBullet(m_posx+m_width/2-
11/2,m_posy,1,PI*240.0f/180.0f);
new CMyBullet(m_posx+m_width/2-
11/2,m_posy,1,PI*300.0f/180.0f);
new CMyBullet(m_posx+m_width/2-
11/2,m_posy,3,PI*0.0f/180.0f);
new CMyBullet(m_posx+m_width/2-
11/2,m_posy,3,PI*180.0f/180.0f);
m_frequency2-=0.6f;
}
}
if(CMyInput::hh->IsKeyPressed(DIK_J))
//5种增强子弹
{
m_frequency0+=dt;
if(m_frequency0>0.6f)
{
switch(m_bullettype)
{
case 0:
new CMyBullet(m_posx+m_width/2-
11/2,m_posy,m_bullettype,PI*60.0f/180.0f);
new CMyBullet(m_posx+m_width/2-
11/2,m_posy,m_bullettype,PI*120.0f/180.0f);
new CMyBullet(m_posx+m_width/2-
11/2,m_posy,m_bullettype,PI*90.0f/180.0f);
break;
case 1:
new CMyBullet(m_posx+m_width/2-
16/2,m_posy,m_bullettype,PI*60.0f/180.0f);
new CMyBullet(m_posx+m_width/2-
16/2,m_posy,m_bullettype,PI*90.0f/180.0f);
new CMyBullet(m_posx+m_width/2-
16/2,m_posy,m_bullettype,PI*120.0f/180.0f);
break;
case 2:
new CMyBullet(m_posx+m_width/2-16/2-
15,m_posy,m_bullettype,PI*90.0f/180.0f);
new CMyBullet(m_posx+m_width/2-16/2
,m_posy,m_bullettype,PI*90.0f/180.0f);
new CMyBullet(m_posx+m_width/2-
16/2+15,m_posy,m_bullettype,PI*90.0f/180.0f);
break;
case 3:
new CMyBullet(m_posx+m_width/2-16/2
,m_posy,m_bullettype,PI*90.0f/180.0f);
new CMyBullet(m_posx+m_width/2-16/2
,m_posy,m_bullettype,PI*30.0f/180.0f);
new CMyBullet(m_posx+m_width/2-16/2
,m_posy,m_bullettype,PI*60.0f/180.0f);
new CMyBullet(m_posx+m_width/2-16/2
,m_posy,m_bullettype,PI*150.0f/180.0f);
new CMyBullet(m_posx+m_width/2-16/2
,m_posy,m_bullettype,PI*120.0f/180.0f);
break;
case 4:
new CMyBullet(m_posx+m_width/2-16/2
,m_posy,m_bullettype,PI*90.0f/180.0f);
new CMyBullet(m_posx+m_width/2-16/2
,m_posy,m_bullettype,PI*30.0f/180.0f);
new CMyBullet(m_posx+m_width/2-16/2
,m_posy,m_bullettype,PI*60.0f/180.0f);
new CMyBullet(m_posx+m_width/2-16/2
,m_posy,m_bullettype,PI*150.0f/180.0f);
new CMyBullet(m_posx+m_width/2-16/2
,m_posy,m_bullettype,PI*120.0f/180.0f);
break;
}
m_frequency0-=0.6f;
}
}
}
}
void CMyPlane::Render(HDC mdc)
{
if(m_isW==false && m_isA==false && m_isD==false)
//...
{
m_pMyPlaneSprite->Render(mdc,m_posx,m_posy,m_width,m_height,0,0);
}
if(m_isW==true && m_isA==false && m_isD==false)
//...W
{
m_pMyPlaneSprite->Render(mdc,m_posx,m_posy,m_width,m_height+17,0,0);
m_isW=false;
}
if(m_isW==false && m_isA==true && m_isD==false)
//...A
{
m_pMyPlaneSprite->Render(mdc,m_posx,m_posy,m_width,m_height,0+m_width,0);
m_isA=false;
}
if(m_isW==false && m_isA==false && m_isD==true)
//...D
{
m_pMyPlaneSprite->Render
(mdc,m_posx,m_posy,m_width,m_height,0+2*m_width,0);
m_isD=false;
}
if(m_isW==true && m_isA==true && m_isD==false)
//...W+A
{
m_pMyPlaneSprite->Render
(mdc,m_posx,m_posy,m_width,m_height+17,0+m_width,0);
m_isA=false;
m_isW=false;
}
if(m_isW==true && m_isA==false && m_isD==true)
//...W+D
{
m_pMyPlaneSprite->Render
(mdc,m_posx,m_posy,m_width,m_height+17,0+2*m_width,0);
m_isD=false;
m_isW=false;
}
if(m_isW==false && m_isA==true && m_isD==true)
//...A+D
{
m_pMyPlaneSprite->Render(mdc,m_posx,m_posy,m_width,m_height,0,0);
m_isA=false;
m_isD=false;
}
if(m_isW==true && m_isA==true && m_isD==true)
//...A+D+W
{
m_pMyPlaneSprite->Render(mdc,m_posx,m_posy,m_width,m_height+17,0,0);
m_isA=false;
m_isD=false;
m_isW=false;
}
}
void CMyPlane::ChangeBullet()
{
m_bullettype=(m_bullettype+1)%5;
}
void CMyPlane::GetPosition(int& _x,int& _y)
{
_x=m_posx;
_y=m_posy;
}
void CMyPlane::RenderHP(HDC mdc)
{
sprintf(strLIFE," = %d",s_life);
SetBkMode(mdc,TRANSPARENT); //透明处理
SetTextColor(mdc,RGB(0,255,255));
TextOut(mdc,47,0,strLIFE,(int)strlen(strLIFE));
TextOut(mdc,47,18,"GL615 1ST",(int)strlen("GL615 1ST"));
RenderBarSide(mdc);
RenderBar(mdc);
RenderPlayer(mdc);
}
void CMyPlane::RenderBarSide(HDC mdc)
{
m_pMyBarSideSprite->Render(mdc,47,58-17,150,17,0,0);
}
void CMyPlane::RenderBar(HDC mdc)
{
m_pMyBarSprite->Render(mdc,47+3,58-17+2,144*m_HP/HP,13,0,0);
}
void CMyPlane::RenderPlayer(HDC mdc)
{
m_pPlayerSprite->Render(mdc,0,0,47,58,0,0);
}
程序代码:
#include "WinMain.h"
/*
class CMyBase
{
protected:
static CMyBase* s_pHead;
bool CheckRect();
bool CheckCollision(int _x,int _y);
CMyBase* m_pNext;
int m_posx;
int m_posy;
int m_width;
int m_height;
bool m_balive;
public:
CMyBase(int _x,int _y);
virtual ~CMyBase();
virtual void Logic(float dt)=0;
virtual void Render(HDC mdc)=0;
static void GameLogic(float dt);
static void GameRender(HDC mdc);
};
*/
CMyBase* CMyBase::s_pHead = NULL;
CMyBase::CMyBase(int _x,int _y):m_posx(_x),m_posy(_y)
{
m_balive = true;
m_pNext = s_pHead;
s_pHead = this;
}
CMyBase::~CMyBase()
{
if(this==s_pHead)
{
s_pHead = m_pNext;
return;
}
CMyBase* temp = s_pHead;
for(;temp->m_pNext!=NULL;temp = temp->m_pNext)
{
if(temp->m_pNext==this)
{
temp->m_pNext = m_pNext;
return;
}
}
}
bool CMyBase::CheckRect()
{
if(m_posx<-m_width||m_posx>WINDOW_WIDTH||
m_posy<-m_height||m_posy>WINDOW_HEIGHT)
return true;
else
return false;
}
bool CMyBase::CheckCollision(int _x,int _y)
{
if((_x>=m_posx)&&(_x<=m_posx+m_width)&&
(_y>=m_posy)&&(_y<=m_posy+m_height))
return true;
else
return false;
}
void CMyBase::GameLogic(float dt)
{
CMyBase* temp = s_pHead;
for(;temp!=NULL;)
{
if(temp->m_balive==false||temp->CheckRect()==true)
{
CMyBase* p = temp->m_pNext;
delete temp;
temp = p;
}
else
{
temp->Logic(dt);
temp = temp->m_pNext;
}
}
}
void CMyBase::GameRender(HDC mdc)
{
CMyBase* temp = s_pHead;
for(;temp!=NULL;temp=temp->m_pNext)
{
temp->Render(mdc);
}
}
/*
class CMyBase
{
protected:
static CMyBase* s_pHead;
bool CheckRect();
bool CheckCollision(int _x,int _y);
CMyBase* m_pNext;
int m_posx;
int m_posy;
int m_width;
int m_height;
bool m_balive;
public:
CMyBase(int _x,int _y);
virtual ~CMyBase();
virtual void Logic(float dt)=0;
virtual void Render(HDC mdc)=0;
static void GameLogic(float dt);
static void GameRender(HDC mdc);
};
*/
CMyBase* CMyBase::s_pHead = NULL;
CMyBase::CMyBase(int _x,int _y):m_posx(_x),m_posy(_y)
{
m_balive = true;
m_pNext = s_pHead;
s_pHead = this;
}
CMyBase::~CMyBase()
{
if(this==s_pHead)
{
s_pHead = m_pNext;
return;
}
CMyBase* temp = s_pHead;
for(;temp->m_pNext!=NULL;temp = temp->m_pNext)
{
if(temp->m_pNext==this)
{
temp->m_pNext = m_pNext;
return;
}
}
}
bool CMyBase::CheckRect()
{
if(m_posx<-m_width||m_posx>WINDOW_WIDTH||
m_posy<-m_height||m_posy>WINDOW_HEIGHT)
return true;
else
return false;
}
bool CMyBase::CheckCollision(int _x,int _y)
{
if((_x>=m_posx)&&(_x<=m_posx+m_width)&&
(_y>=m_posy)&&(_y<=m_posy+m_height))
return true;
else
return false;
}
void CMyBase::GameLogic(float dt)
{
CMyBase* temp = s_pHead;
for(;temp!=NULL;)
{
if(temp->m_balive==false||temp->CheckRect()==true)
{
CMyBase* p = temp->m_pNext;
delete temp;
temp = p;
}
else
{
temp->Logic(dt);
temp = temp->m_pNext;
}
}
}
void CMyBase::GameRender(HDC mdc)
{
CMyBase* temp = s_pHead;
for(;temp!=NULL;temp=temp->m_pNext)
{
temp->Render(mdc);
}
}





2008-8-16 13:34
