// Game.cpp: implementation of the CGame class.
//
//////////////////////////////////////////////////////////////////////
#include "Game.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CGame::CGame(CDirectWnd* win)
{
m_win = win;
m_menu = new CMenu(m_win);
m_bkground = new CBkGround(m_win);
m_about = new CAbout(m_win);
gamestate = game_menu;
}
CGame::~CGame()
{
ReleaseAll();
}
void CGame::Run()
{
switch(gamestate)
{
case game_menu: //游戏菜单
{
ShowMenu();
}
break;
case game_main: //游戏主循环
{
GameLoop(); //游戏主循环 if Game_main
}
break;
case game_quit:
{
PostMessage(m_win->GetHwnd(),WM_QUIT,0,0);
}
break;
case game_over:
{
ShowGameOver();
}
break;
case game_about:
{
ShowAbout();
}
break;
}
if(DDERR_SURFACELOST == m_win->FlipScreen())
{
m_win->LoadBitmapResource();
}
}
void CGame::ShowMenu()
{
int choseItem = m_menu->Run();
if (KEYDOWN(VK_RETURN))
{
switch(choseItem)
{
case 1:
{
InitGame();
m_win->m_music->Stop();
m_win->m_music->Play(1);
gamestate = game_main;
}
break;
case 2:
{
gamestate = game_about;
}
break;
case 3:
gamestate = game_quit;
break;
}
}
}
void CGame::GameLoop()
{
if (KEYDOWN(VK_ESCAPE))
{
m_win->m_music->Stop();
m_win->m_music->Play(0);
ReleaseObList();
gamestate = game_menu;
return;
}
// 显示背景
m_bkground->Run();
// 显示关卡
m_win->ShowText(SrcWidth/2-50,20,"第");
m_win->ShowText(SrcWidth/2-30,20,missionNum);
m_win->ShowText(SrcWidth/2-20,20,"关");
// 显示分数
m_win->ShowText(SrcWidth/2+70,20,"分数:");
m_win->ShowText(SrcWidth/2+110,20,score);
if(missionFinish)
{
if( missionNum < 5 )
{
m_win->ShowText(SrcWidth/2-100,SrcHeight/2-20,"第");
m_win->ShowText(SrcWidth/2-80,SrcHeight/2-20,missionNum);
m_win->ShowText(SrcWidth/2-70,SrcHeight/2-20,"关结束,请按空格键继续!");
if( KEYDOWN(VK_SPACE) )
{
missionNum++;
enemyNum = 0;
missionFinish = FALSE;
}
}
else if( 5 == missionNum )
{
m_win->ShowText(SrcWidth/2-100,SrcHeight/2-20,"恭喜你,你已经通关了,请按空格键退出!");
if( KEYDOWN(VK_SPACE) )
{
m_win->m_music->Stop();
m_win->m_music->Play(0);
ReleaseObList();
gamestate = game_menu;
return;
}
}
}
// 创建敌人
if(!missionFinish)
{
CreateEnemy();
}
// 遍历链表
CObNode *p = m_ObList->Head->Next;
while(p != m_ObList->Tail)
{
CObNode *q = p->Next;
while(q != m_ObList->Tail)
{
if(NULL != q)
{
if(q->BaseObj->getExist())
CheckHit(p,q);
q = q->Next;
}
}
if(NULL != p->BaseObj)
{
p->BaseObj->Run();
if ( 0 == p->BaseObj->getId() )
{
CPlayerPlane *pPlane = (CPlayerPlane *)(p->BaseObj);
// 奖励生命
if(score - oldscore >= 5000)
{
oldscore = score;
pPlane->setLifeNum(pPlane->getLifeNum() + 1);
m_win->m_snd->PlaySound(7,0);
}
// 检测我机状态
if( 0 == pPlane->getLifeNum() && 0 == pPlane->getExist() )
{
ReleaseObList();
gamestate = game_over;
return;
}
}
// 检测敌机状态
if ( 2 == p->BaseObj->getId() )
{
CEnemyPlane *ePlane = (CEnemyPlane *)(p->BaseObj);
if( 1 == ePlane->getDead() )
{
score = score + ePlane->getScore();
}
if( 3 == ePlane->getEnemyType() )
{
if ( 1 == ePlane->getDead() )
missionFinish = TRUE ;
}
}
// 检测炸弹状态
if ( 5 == p->BaseObj->getId() )
{
CBomb *bom = (CBomb *)(p->BaseObj);
if( 0 == bom->getExist() )
KillAllEnemy();
}
// 若对象存在标志为0,则删除该对象
if(p->BaseObj->getExist() == 0)
{
p = p->Last;
delete p->Next->BaseObj;
m_ObList->Delete(p->Next);
}
p = p->Next;
}
}
}
void CGame::InitGame()
{
srand( (unsigned)time( NULL ) );
enemyNum = 0;
score = 0;
oldscore = score;
missionNum = 1;
missionFinish = FALSE;
m_ObList = new CObList();
new CPlayerPlane(m_win,m_ObList,3);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,1,2,1,3,1,
1,2,3,1,3,2,1,3,3,2, 1,2,2,1,3,1,2,1,3,4
}
};
for(int i=0;i<5;i++)
for(int j=0;j<100;j++)
mission[i][j]=temp[i][j];
}
void CGame::CheckHit(CObNode *Obj1,CObNode *Obj2)
{
RECT r1,r2;
m_win->GetRect(&r1,
Obj1->BaseObj->getPosX(),
Obj1->BaseObj->getPosY(),
Obj1->BaseObj->getWidth(),
Obj1->BaseObj->getHeight()
);
m_win->GetRect(&r2,
Obj2->BaseObj->getPosX(),
Obj2->BaseObj->getPosY(),
Obj2->BaseObj->getWidth(),
Obj2->BaseObj->getHeight()
);
// 0:我机 1:我机子弹 2: 敌机 3:敌机子弹 4:奖励物品
switch(Obj1->BaseObj->getId())
{
case 0:
{
if(2 == Obj2->BaseObj->getId())
{
CPlayerPlane *pPlane = (CPlayerPlane *)(Obj1->BaseObj);
CEnemyPlane *ePlane = (CEnemyPlane *)(Obj2->BaseObj);
if( !pPlane->getSuper() )
{
if( IsCollide(&r1,&r2) )
{
pPlane->setLife(pPlane->getLife() - ePlane->getLife());
}
}
}
else if(3 == Obj2->BaseObj->getId())
{
CPlayerPlane *pPlane = (CPlayerPlane *)(Obj1->BaseObj);
CEnemyBullet *eBullet = (CEnemyBullet *)(Obj2->BaseObj);
if( !pPlane->getSuper() )
{
if( IsCollide(&r1,&r2) )
{
pPlane->setLife(pPlane->getLife() - eBullet->getPower());
}
}
}
else if(4 == Obj2->BaseObj->getId())
{
if( IsCollide(&r1,&r2) )
{
Obj2->BaseObj->setExist(0);
CPlayerPlane *pPlane = (CPlayerPlane *)(Obj1->BaseObj);
CBonus *bonus = (CBonus *)(Obj2->BaseObj);
pPlane->getBonus(bonus->getBonusType());
m_win->m_snd->PlaySound(4,0);
score = score + 100;
}
}
}
break;
case 1:
{
if(2 == Obj2->BaseObj->getId())
{
if( IsCollide(&r1,&r2) )
{
Obj1->BaseObj->setExist(0);
CPlayerBullet *pBullet = (CPlayerBullet *)(Obj1->BaseObj);
CEnemyPlane *ePlane = (CEnemyPlane *)(Obj2->BaseObj);
new CExplode(m_win,
m_ObList,
ePlane->getPosX(),
ePlane->getPosY(),
2);
m_win->m_snd->PlaySound(5,0);
ePlane->setLife(ePlane->getLife() - pBullet->getPower());
}
}
}
break;
case 2:
{
if(1 == Obj2->BaseObj->getId())
{
if( IsCollide(&r1,&r2) )
{
Obj2->BaseObj->setExist(0);
CEnemyPlane *ePlane = (C