基本信息
源码名称:c++ 飞机大战 小游戏源码
源码大小:9.77M
文件格式:.zip
开发语言:C/C++
更新时间:2020-06-17
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 4 元×
微信扫码支付:4 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
void CPlaneApp::OnCreateGame() // WM_CREATE //创建消息
{
//1.背景初始化
back.InitBack(m_hIns);
//2.玩家飞机初始化
plane.InitPlayer(m_hIns);
//3.启动所有的定时器
::SetTimer(m_hMainWnd, BACK_MOVE_TIMER_ID, 100, 0); //背景定时器
::SetTimer(m_hMainWnd, PLAYER_MOVE_TIMER_ID, 1, 0); //玩家飞机定时器
::SetTimer(m_hMainWnd, PLAYER_SEND_TIMER_ID, 350, 0); //玩家飞机发炮弹
::SetTimer(m_hMainWnd, GUNNER_MOVE_TIMER_ID, 1, 0); //所有炮弹移动
::SetTimer(m_hMainWnd, CREATE_FOEPLANE_TIMER_ID, 500, 0); //创建敌人飞机
::SetTimer(m_hMainWnd, FOEPLANE_MOVE_TIMER_ID, 30, 0); //敌人飞机移动
::SetTimer(m_hMainWnd, CHANGE_SHOWID_TIMER_ID, 150, 0); //改变ShowID
}
void CPlaneApp::OnGameDraw() // WM_PAINT 重绘
{
PAINTSTRUCT ps = { 0 };
HDC hdc = ::BeginPaint(m_hMainWnd, &ps);
HDC hMemDC = ::CreateCompatibleDC(hdc);
HBITMAP hBitmap = ::CreateCompatibleBitmap(hdc, 380, 550);
::SelectObject(hMemDC, hBitmap);
//--------------------------------
back.ShowBack(hMemDC); //显示背景
plane.ShowPlayer(hMemDC); //显示玩家飞机
gunBox.AllGunnerShow(hMemDC); //显示所有炮弹
foeBox.AllFoePlaneShow(hMemDC); //显示所有敌人飞机
blastBox.AllBlastFoePlaneShow(hMemDC); //显示所有爆照敌人飞机
//--------------------------------
::BitBlt(hdc, 0, 0, 380, 550, hMemDC, 0, 0, SRCCOPY);
::DeleteObject(hBitmap);
::DeleteDC(hMemDC);
::EndPaint(m_hMainWnd, &ps);
}
void CPlaneApp::OnGameRun(WPARAM nTimerID) // WM_TIMER 定时器
{
if (nTimerID == CHANGE_SHOWID_TIMER_ID)
{
blastBox.ChangeShowID();
}
if (nTimerID == FOEPLANE_MOVE_TIMER_ID) //敌人飞机移动
{
foeBox.AllFoePlaneMove();
if (this->IsGameOver() == true)
{
::KillTimer(m_hMainWnd, BACK_MOVE_TIMER_ID); //背景定时器
::KillTimer(m_hMainWnd, PLAYER_MOVE_TIMER_ID); //玩家飞机定时器
::KillTimer(m_hMainWnd, PLAYER_SEND_TIMER_ID); //玩家飞机发炮弹
::KillTimer(m_hMainWnd, GUNNER_MOVE_TIMER_ID); //所有炮弹移动
::KillTimer(m_hMainWnd, CREATE_FOEPLANE_TIMER_ID); //创建敌人飞机
::KillTimer(m_hMainWnd, FOEPLANE_MOVE_TIMER_ID); //敌人飞机移动
::KillTimer(m_hMainWnd, CHANGE_SHOWID_TIMER_ID); //改变ShowID
MessageBox(0, "Game Over!", "tip", MB_OK);
}
}
if (nTimerID == CREATE_FOEPLANE_TIMER_ID) //创建敌人飞机
{
foeBox.CreateFoePlane(m_hIns);
}
if (nTimerID == GUNNER_MOVE_TIMER_ID) //炮弹移动
{
gunBox.AllGunnerMove();
this->GunnerHitFoePlane();
}
if (nTimerID == PLAYER_SEND_TIMER_ID) //玩家发射炮弹
{
plane.SendGunner(m_hIns,gunBox);
}
if (nTimerID == BACK_MOVE_TIMER_ID) //背景移动
{
back.MoveBack();
}
if (nTimerID == PLAYER_MOVE_TIMER_ID) //玩家飞机移动
{
if (GetAsyncKeyState(VK_LEFT))
plane.MovePlayer(VK_LEFT);
if (GetAsyncKeyState(VK_RIGHT))
plane.MovePlayer(VK_RIGHT);
if (GetAsyncKeyState(VK_UP))
plane.MovePlayer(VK_UP);
if (GetAsyncKeyState(VK_DOWN))
plane.MovePlayer(VK_DOWN);
}
//重绘
RECT rect = { 0,0,380,550 };
::InvalidateRect(m_hMainWnd, &rect,FALSE);
}
void CPlaneApp::OnKeyDown(WPARAM nKey) // WM_KEYDOWN 键盘消息
{
//plane.MovePlayer(nKey);
//RECT rect = { 0,0,380,550 };
//::InvalidateRect(m_hMainWnd, &rect, FALSE);
}
void CPlaneApp::GunnerHitFoePlane() //炮弹击中敌人飞机
{
bool bFlag = false;
//遍历所有炮弹
list<CGunner*>::iterator iteGun = gunBox.m_lstGunner.begin();
while (iteGun != gunBox.m_lstGunner.end())
{
//遍历所有敌人飞机
list<CFoePlane*>::iterator iteFoe = foeBox.m_lstFoePlane.begin();
while (iteFoe != foeBox.m_lstFoePlane.end())
{
if ((*iteFoe)->IsGunnerHitFoePlane(*iteGun) == true)
{
bFlag = true;
//删除炮弹
delete(*iteGun);
iteGun = gunBox.m_lstGunner.erase(iteGun);
//计算飞机血量
(*iteFoe)->DownBlood();
//判断是否爆炸
if ((*iteFoe)->IsBoom() == true)
{
//将爆炸敌人飞机添加到爆炸飞机盒子
blastBox.m_lstBlastPlane.push_back(*iteFoe);
foeBox.m_lstFoePlane.erase(iteFoe); //删除敌人飞机节点
}
break;
}
iteFoe ;
}
if (bFlag == false)
iteGun ;
else
bFlag = false;
}
}
bool CPlaneApp::IsGameOver()
{
list<CFoePlane*>::iterator ite = foeBox.m_lstFoePlane.begin();
while (ite != foeBox.m_lstFoePlane.end())
{
if ((*ite)->IsHitPlayer(plane) == true)
{
return true;
}
ite;
}
return false;
经典飞机大战硬核还原,注释详细,适合新手快速升级
注释详细,新手进步快速的参考实例,有问题联系QQ:2085728093
void CPlaneApp::OnCreateGame() // WM_CREATE //创建消息
{
//1.背景初始化
back.InitBack(m_hIns);
//2.玩家飞机初始化
plane.InitPlayer(m_hIns);
//3.启动所有的定时器
::SetTimer(m_hMainWnd, BACK_MOVE_TIMER_ID, 100, 0); //背景定时器
::SetTimer(m_hMainWnd, PLAYER_MOVE_TIMER_ID, 1, 0); //玩家飞机定时器
::SetTimer(m_hMainWnd, PLAYER_SEND_TIMER_ID, 350, 0); //玩家飞机发炮弹
::SetTimer(m_hMainWnd, GUNNER_MOVE_TIMER_ID, 1, 0); //所有炮弹移动
::SetTimer(m_hMainWnd, CREATE_FOEPLANE_TIMER_ID, 500, 0); //创建敌人飞机
::SetTimer(m_hMainWnd, FOEPLANE_MOVE_TIMER_ID, 30, 0); //敌人飞机移动
::SetTimer(m_hMainWnd, CHANGE_SHOWID_TIMER_ID, 150, 0); //改变ShowID
}
void CPlaneApp::OnGameDraw() // WM_PAINT 重绘
{
PAINTSTRUCT ps = { 0 };
HDC hdc = ::BeginPaint(m_hMainWnd, &ps);
HDC hMemDC = ::CreateCompatibleDC(hdc);
HBITMAP hBitmap = ::CreateCompatibleBitmap(hdc, 380, 550);
::SelectObject(hMemDC, hBitmap);
//--------------------------------
back.ShowBack(hMemDC); //显示背景
plane.ShowPlayer(hMemDC); //显示玩家飞机
gunBox.AllGunnerShow(hMemDC); //显示所有炮弹
foeBox.AllFoePlaneShow(hMemDC); //显示所有敌人飞机
blastBox.AllBlastFoePlaneShow(hMemDC); //显示所有爆照敌人飞机
//--------------------------------
::BitBlt(hdc, 0, 0, 380, 550, hMemDC, 0, 0, SRCCOPY);
::DeleteObject(hBitmap);
::DeleteDC(hMemDC);
::EndPaint(m_hMainWnd, &ps);
}
void CPlaneApp::OnGameRun(WPARAM nTimerID) // WM_TIMER 定时器
{
if (nTimerID == CHANGE_SHOWID_TIMER_ID)
{
blastBox.ChangeShowID();
}
if (nTimerID == FOEPLANE_MOVE_TIMER_ID) //敌人飞机移动
{
foeBox.AllFoePlaneMove();
if (this->IsGameOver() == true)
{
::KillTimer(m_hMainWnd, BACK_MOVE_TIMER_ID); //背景定时器
::KillTimer(m_hMainWnd, PLAYER_MOVE_TIMER_ID); //玩家飞机定时器
::KillTimer(m_hMainWnd, PLAYER_SEND_TIMER_ID); //玩家飞机发炮弹
::KillTimer(m_hMainWnd, GUNNER_MOVE_TIMER_ID); //所有炮弹移动
::KillTimer(m_hMainWnd, CREATE_FOEPLANE_TIMER_ID); //创建敌人飞机
::KillTimer(m_hMainWnd, FOEPLANE_MOVE_TIMER_ID); //敌人飞机移动
::KillTimer(m_hMainWnd, CHANGE_SHOWID_TIMER_ID); //改变ShowID
MessageBox(0, "Game Over!", "tip", MB_OK);
}
}
if (nTimerID == CREATE_FOEPLANE_TIMER_ID) //创建敌人飞机
{
foeBox.CreateFoePlane(m_hIns);
}
if (nTimerID == GUNNER_MOVE_TIMER_ID) //炮弹移动
{
gunBox.AllGunnerMove();
this->GunnerHitFoePlane();
}
if (nTimerID == PLAYER_SEND_TIMER_ID) //玩家发射炮弹
{
plane.SendGunner(m_hIns,gunBox);
}
if (nTimerID == BACK_MOVE_TIMER_ID) //背景移动
{
back.MoveBack();
}
if (nTimerID == PLAYER_MOVE_TIMER_ID) //玩家飞机移动
{
if (GetAsyncKeyState(VK_LEFT))
plane.MovePlayer(VK_LEFT);
if (GetAsyncKeyState(VK_RIGHT))
plane.MovePlayer(VK_RIGHT);
if (GetAsyncKeyState(VK_UP))
plane.MovePlayer(VK_UP);
if (GetAsyncKeyState(VK_DOWN))
plane.MovePlayer(VK_DOWN);
}
//重绘
RECT rect = { 0,0,380,550 };
::InvalidateRect(m_hMainWnd, &rect,FALSE);
}
void CPlaneApp::OnKeyDown(WPARAM nKey) // WM_KEYDOWN 键盘消息
{
//plane.MovePlayer(nKey);
//RECT rect = { 0,0,380,550 };
//::InvalidateRect(m_hMainWnd, &rect, FALSE);
}
void CPlaneApp::GunnerHitFoePlane() //炮弹击中敌人飞机
{
bool bFlag = false;
//遍历所有炮弹
list<CGunner*>::iterator iteGun = gunBox.m_lstGunner.begin();
while (iteGun != gunBox.m_lstGunner.end())
{
//遍历所有敌人飞机
list<CFoePlane*>::iterator iteFoe = foeBox.m_lstFoePlane.begin();
while (iteFoe != foeBox.m_lstFoePlane.end())
{
if ((*iteFoe)->IsGunnerHitFoePlane(*iteGun) == true)
{
bFlag = true;
//删除炮弹
delete(*iteGun);
iteGun = gunBox.m_lstGunner.erase(iteGun);
//计算飞机血量
(*iteFoe)->DownBlood();
//判断是否爆炸
if ((*iteFoe)->IsBoom() == true)
{
//将爆炸敌人飞机添加到爆炸飞机盒子
blastBox.m_lstBlastPlane.push_back(*iteFoe);
foeBox.m_lstFoePlane.erase(iteFoe); //删除敌人飞机节点
}
break;
}
iteFoe ;
}
if (bFlag == false)
iteGun ;
else
bFlag = false;
}
}
bool CPlaneApp::IsGameOver()
{
list<CFoePlane*>::iterator ite = foeBox.m_lstFoePlane.begin();
while (ite != foeBox.m_lstFoePlane.end())
{
if ((*ite)->IsHitPlayer(plane) == true)
{
return true;
}
ite;
}
return false;
}