目录1. 概述..................................................1. 概述这次实训,目的以VS 为环境,利用MFC,使用C++语言编写一个简单的飞机大战游戏。
实现的主要功能:1.有开始游戏界面,上下左右键控制飞机自由移动,空格键发射炮弹。
2.设有关卡,每打完一个boss就会过关,每过一关战机炮弹会升级,敌机随机发射子弹数量增加。
3.战机起始十条命,可以通过过关和吃到道具增加血量。
4.战机子弹打中敌机,敌机就会爆炸,boss血量随关卡升高而增多,打败敌机或者boss会有分数加成。
5.设有无敌模式和普通模式,无敌模式大招无限,生命无限,战机导弹直接升至最高等级。
2. 相关技术碰撞检测用CRect类的GetRect()函数可以获得当前对象的矩形区域, IntersectRect()并用此函数判断二者是否碰撞。
CObList链表这个程序使用CObList链表来存储各效果。
CobList类似于一个双向链表,POSITION 类型的变量为链表的键。
使用POSITION变量,既可以作为链表循环中的循环变量,也可以作为标记某个位置的标签。
我们可以通过获得某元素的POSITION来访问它。
本程序中主要用到的函数有:GetHeadPosition():获取链表首元素的POSITION;AddTail():将新的元素添加到链表最后;GetNext( POSITION& rPosition ):使rPosition指向下一个元素。
设置定时器SetTimer()函数来设置定时器。
背景滚动为了让游戏场景更加逼真,游戏节奏更加合理,可以通过让背景滚动来提高游戏的场景效应,实现这一技术时,可以将一张背景图片复制为2张,从上到下连续的拼接在一起,使其不会出现图片的卡顿和瞬移现象。
内存释放技术这个程序中有大量的地方需要使用内存,如果不及时删除会出现闪图等情况,容易导致程序崩溃,要及时释放内存资源。
3. 总体设计与详细设计系统模块划分主要功能模块4. 编码实现{{}}}}ddTail(new CEnemy);}nCreator--;ddTail(new Cda);count++;}ddTail(new Cshengming);count++;}ddTail(new Cboss);bxt = 1;bn++;}if (level == 2&&bn==0&&count>=50&&count<=52) {m_ObjList[enboss].AddTail(new Cboss);bxt = 1;bn++;}if (level == 3&&bn==0&&count>=75&&count<=77) {m_ObjList[enboss].AddTail(new Cboss);bxt = 1;bn++;}if (level == 4&&bn==0&&count>=100&&count<=102) {m_ObjList[enboss].AddTail(new Cboss);bxt = 1;bn++;}if (level == 5&&bn==0&&count>=150&&count<=152) {m_ObjList[enboss].AddTail(new Cboss);bxt = 1;bn++;}if (level>5&&count%200==0&&bn==0){m_ObjList[enboss].AddTail(new Cboss);bxt = 1;bn++;}if (m_pMe == NULL)return;ddTail(new CBomb + 20, + 10));}if (level == 2 && wu == 0){m_ObjList[enBomb].AddTail(new CBomb + 10, + 30));m_ObjList[enBomb].AddTail(new CBomb + 30, + 30));}if (level == 3 && wu == 0){m_ObjList[enBomb].AddTail(new CBomb + 10, + 30));m_ObjList[enBomb].AddTail(new CBomb + 30, + 30));m_ObjList[enBomb].AddTail(new CBomb + 20, + 30)); }if (level >= 4 && wu == 0){m_ObjList[enBomb].AddTail(new CBomb + 10, + 30));m_ObjList[enBomb].AddTail(new CBomb + 30, + 30));m_ObjList[enBomb].AddTail(new CBomb + 20, + 30));m_ObjList[enBomb].AddTail(new CBomb , + 30));m_ObjList[enBomb].AddTail(new CBomb + 40, + 30));}if (wu == 1){m_ObjList[enBomb].AddTail(new CBomb + 10, + 30));m_ObjList[enBomb].AddTail(new CBomb + 30, + 30));m_ObjList[enBomb].AddTail(new CBomb + 20, + 30));m_ObjList[enBomb].AddTail(new CBomb, + 30));m_ObjList[enBomb].AddTail(new CBomb + 40, + 30));}}}if (GetKey(VK_F1) == 1)etHeadPosition(); ePos != NULL;) {Cboss* pEnemy = (Cboss*)m_ObjList[enboss].GetNext(ePos);CPoint ePt = pEnemy->GetPoint();CBossball * t;for (int i = -level + 1; i <= level - 1; i++){t = new CBossball + 20, + 30);t->zx = int*sin(* double(i)) / 9));t->zy = int*cos(* double(i)) / 9));m_ObjList[enBossball].AddTail(t);}}num = 100;}num--;etHeadPosition(); ePos != NULL;){CEnemy* pEnemy = (CEnemy*)m_ObjList[enEnemy].GetNext(ePos);if (!pEnemy->Fired())continue;CPoint ePt = pEnemy->GetPoint();BOOL by = FALSE;ddTail(t);}}}}etHeadPosition(); (nPos4 = nPos3) != NULL;){CBall* pBall = (CBall*)m_ObjList[enBossball].GetNext(nPos3);CRect bRect = pBall->GetRect();CRect tmpRect;if (&bRect, mRect)){ddTail(new CExplosion,);emoveAt(nPos4);delete pBall;if (shengming <= 0){etHeadPosition(); (nPos2 = nPos1) != NULL;){CBall* pBall = (CBall*)m_ObjList[enEnemy].GetNext(nPos1);CRect bRect = pBall->GetRect();CRect tmpRect;if (&bRect, mnRect)){ddTail(new CExplosion,);emoveAt(nPos2);delete pBall;if (shengming <= 0){etHeadPosition(); (bPos2 = bPos1) != NULL;){CBall* pBall = (CBall*)m_ObjList[enBall].GetNext(bPos1);CRect bRect = pBall->GetRect();CRect tmpRect;if (&bRect, mRect)){ddTail(new CExplosion,);emoveAt(bPos2);delete pBall;if (shengming <= 0){etHeadPosition(); (bPos2 = bPos1) != NULL;){CBall* pBall = (CBall*)m_ObjList[endazhao].GetNext(bPos1);CRect mRect = pBall->GetRect();CRect tmpRect;if (&mRect, bRect)){dazhao++;emoveAt(bPos2);delete pBall;}}}etHeadPosition(); (bPos2 = bPos1) != NULL;){CBall* pBall = (CBall*)m_ObjList[enming].GetNext(bPos1);CRect bRect = pBall->GetRect();CRect tmpRect;if (&bRect, mRect)){shengming++;emoveAt(bPos2);delete pBall;}}}etHeadPosition();(mPos2 = mPos1) != NULL;){CBomb* pBomb = (CBomb*)m_ObjList[enBomb].GetNext(mPos1);CRect bRect = pBomb->GetRect();POSITION ePos1 = NULL, ePos2 = NULL;for (ePos1 = m_ObjList[enEnemy].GetHeadPosition();(ePos2 = ePos1) != NULL;) {CEnemy* pEnemy = (CEnemy*)m_ObjList[enEnemy].GetNext(ePos1);CRect mRect = pEnemy->GetRect();CRect tmpRect;if (&bRect, mRect)){count++;number += 5;ddTail(new CExplosion,);emoveAt(mPos2);delete pBomb;}emoveAt(ePos2);delete pEnemy;break;}}}etHeadPosition(); (mPos4 = mPos3) != NULL;) {CBomb* pBomb = (CBomb*)m_ObjList[enBomb].GetNext(mPos3);CRect bRect = pBomb->GetRect();POSITION ePos3 = NULL, ePos4 = NULL;for (ePos3 = m_ObjList[enBall].GetHeadPosition(); (ePos4 = ePos3) != NULL;) {CBall* pBall = (CBall*)m_ObjList[enBall].GetNext(ePos3);CRect mRect = pBall->GetRect();CRect tmpRect;if (&bRect, mRect)){emoveAt(mPos4);delete pBomb;emoveAt(ePos4);delete pBall;break;}}}etHeadPosition(); (mPos6 = mPos5) != NULL;){CBomb* pBomb = (CBomb*)m_ObjList[enBomb].GetNext(mPos5);CRect bRect = pBomb->GetRect();POSITION ePos5 = NULL, ePos6 = NULL;for (ePos5 = m_ObjList[enboss].GetHeadPosition(); (ePos6 = ePos5) != NULL;) {Cboss* pBall = (Cboss*)m_ObjList[enboss].GetNext(ePos5);CRect mRect = pBall->GetRect();CRect tmpRect;if (&bRect, mRect)){boss--;ddTail(new CExplosion,);emoveAt(mPos6);delete pBomb;if (boss <= 0){number = number + 100 * level;bxt = 0;emoveAt(ePos6);delete pBall;dazhao++;bn = 0;count = 0;shengming += 2;l=0;boss=0;break;}}}}}void CPlaneGameView::OnTimer(UINT_PTR nIDEvent){ddTail(new CBomb - 50, + 80));m_ObjList[enBomb].AddTail(new CBomb - 20, + 80));m_ObjList[enBomb].AddTail(new CBomb - 70, + 80));m_ObjList[enBomb].AddTail(new CBomb - 100, + 80));m_ObjList[enBomb].AddTail(new CBomb - 130, + 80));m_ObjList[enBomb].AddTail(new CBomb - 550, + 80));m_ObjList[enBomb].AddTail(new CBomb - 520, + 80));m_ObjList[enBomb].AddTail(new CBomb - 160, + 80));m_ObjList[enBomb].AddTail(new CBomb - 490, + 80));m_ObjList[enBomb].AddTail(new CBomb - 190, + 80)); m_ObjList[enBomb].AddTail(new CBomb - 430, + 80)); m_ObjList[enBomb].AddTail(new CBomb - 400, + 80)); m_ObjList[enBomb].AddTail(new CBomb - 220, + 80)); m_ObjList[enBomb].AddTail(new CBomb - 370, + 80)); m_ObjList[enBomb].AddTail(new CBomb - 340, + 80)); m_ObjList[enBomb].AddTail(new CBomb - 250, + 80)); m_ObjList[enBomb].AddTail(new CBomb - 310, + 80)); m_ObjList[enBomb].AddTail(new CBomb - 280, + 80)); m_ObjList[enBomb].AddTail(new CBomb, + 80));m_ObjList[enBomb].AddTail(new CBomb + 30, + 80)); m_ObjList[enBomb].AddTail(new CBomb + 60, + 80)); m_ObjList[enBomb].AddTail(new CBomb + 90, + 80)); m_ObjList[enBomb].AddTail(new CBomb + 120, + 80));m_ObjList[enBomb].AddTail(new CBomb + 570, + 80)); m_ObjList[enBomb].AddTail(new CBomb + 180, + 80)); m_ObjList[enBomb].AddTail(new CBomb + 540, + 80)); m_ObjList[enBomb].AddTail(new CBomb + 510, + 80)); m_ObjList[enBomb].AddTail(new CBomb + 210, + 80)); m_ObjList[enBomb].AddTail(new CBomb + 480, + 80)); m_ObjList[enBomb].AddTail(new CBomb + 450, + 80)); m_ObjList[enBomb].AddTail(new CBomb + 240, + 80)); m_ObjList[enBomb].AddTail(new CBomb + 420, + 80)); m_ObjList[enBomb].AddTail(new CBomb + 390, + 80)); m_ObjList[enBomb].AddTail(new CBomb + 270, + 80)); m_ObjList[enBomb].AddTail(new CBomb + 360, + 80)); m_ObjList[enBomb].AddTail(new CBomb + 330, + 80)); m_ObjList[enBomb].AddTail(new CBomb + 300, + 80));m_ObjList[enBomb].AddTail(new CBomb - 20, + 60)); m_ObjList[enBomb].AddTail(new CBomb - 70, + 60)); m_ObjList[enBomb].AddTail(new CBomb - 100, + 60)); m_ObjList[enBomb].AddTail(new CBomb - 130, + 60)); m_ObjList[enBomb].AddTail(new CBomb - 550, + 60)); m_ObjList[enBomb].AddTail(new CBomb - 520, + 60)); m_ObjList[enBomb].AddTail(new CBomb - 160, + 60)); m_ObjList[enBomb].AddTail(new CBomb - 490, + 60)); m_ObjList[enBomb].AddTail(new CBomb - 460, + 60)); m_ObjList[enBomb].AddTail(new CBomb - 190, + 60)); m_ObjList[enBomb].AddTail(new CBomb - 430, + 60)); m_ObjList[enBomb].AddTail(new CBomb - 400, + 60)); m_ObjList[enBomb].AddTail(new CBomb - 220, + 60)); m_ObjList[enBomb].AddTail(new CBomb - 370, + 60));m_ObjList[enBomb].AddTail(new CBomb - 250, + 60)); m_ObjList[enBomb].AddTail(new CBomb - 310, + 60)); m_ObjList[enBomb].AddTail(new CBomb - 280, + 60)); m_ObjList[enBomb].AddTail(new CBomb, + 60));m_ObjList[enBomb].AddTail(new CBomb + 30, + 60)); m_ObjList[enBomb].AddTail(new CBomb + 60, + 60)); m_ObjList[enBomb].AddTail(new CBomb + 90, + 60)); m_ObjList[enBomb].AddTail(new CBomb + 120, + 60)); m_ObjList[enBomb].AddTail(new CBomb + 150, + 60)); m_ObjList[enBomb].AddTail(new CBomb + 570, + 60)); m_ObjList[enBomb].AddTail(new CBomb + 180, + 60)); m_ObjList[enBomb].AddTail(new CBomb + 540, + 60)); m_ObjList[enBomb].AddTail(new CBomb + 510, + 60)); m_ObjList[enBomb].AddTail(new CBomb + 210, + 60));m_ObjList[enBomb].AddTail(new CBomb + 450, + 60)); m_ObjList[enBomb].AddTail(new CBomb + 240, + 60)); m_ObjList[enBomb].AddTail(new CBomb + 420, + 60)); m_ObjList[enBomb].AddTail(new CBomb + 390, + 60)); m_ObjList[enBomb].AddTail(new CBomb + 270, + 60)); m_ObjList[enBomb].AddTail(new CBomb + 360, + 60)); m_ObjList[enBomb].AddTail(new CBomb + 330, + 60)); m_ObjList[enBomb].AddTail(new CBomb + 300, + 60));} else if(nChar==90&&wu==0&&dazhao>=1){dazhao = dazhao - 1;CPoint pt = m_pMe->GetPoint();m_ObjList[enBomb].AddTail(new CBomb - 50, + 80));m_ObjList[enBomb].AddTail(new CBomb - 20, + 80));m_ObjList[enBomb].AddTail(new CBomb - 100, + 80)); m_ObjList[enBomb].AddTail(new CBomb - 130, + 80)); m_ObjList[enBomb].AddTail(new CBomb - 550, + 80)); m_ObjList[enBomb].AddTail(new CBomb - 520, + 80)); m_ObjList[enBomb].AddTail(new CBomb - 160, + 80)); m_ObjList[enBomb].AddTail(new CBomb - 490, + 80)); m_ObjList[enBomb].AddTail(new CBomb - 460, + 80)); m_ObjList[enBomb].AddTail(new CBomb - 190, + 80)); m_ObjList[enBomb].AddTail(new CBomb - 430, + 80)); m_ObjList[enBomb].AddTail(new CBomb - 400, + 80)); m_ObjList[enBomb].AddTail(new CBomb - 220, + 80)); m_ObjList[enBomb].AddTail(new CBomb - 370, + 80)); m_ObjList[enBomb].AddTail(new CBomb - 340, + 80)); m_ObjList[enBomb].AddTail(new CBomb - 250, + 80));m_ObjList[enBomb].AddTail(new CBomb - 280, + 80)); m_ObjList[enBomb].AddTail(new CBomb, + 80));m_ObjList[enBomb].AddTail(new CBomb + 30, + 80)); m_ObjList[enBomb].AddTail(new CBomb + 60, + 80)); m_ObjList[enBomb].AddTail(new CBomb + 90, + 80)); m_ObjList[enBomb].AddTail(new CBomb + 120, + 80)); m_ObjList[enBomb].AddTail(new CBomb + 150, + 80)); m_ObjList[enBomb].AddTail(new CBomb + 570, + 80)); m_ObjList[enBomb].AddTail(new CBomb + 180, + 80)); m_ObjList[enBomb].AddTail(new CBomb + 540, + 80)); m_ObjList[enBomb].AddTail(new CBomb + 510, + 80)); m_ObjList[enBomb].AddTail(new CBomb + 210, + 80)); m_ObjList[enBomb].AddTail(new CBomb + 480, + 80)); m_ObjList[enBomb].AddTail(new CBomb + 450, + 80));m_ObjList[enBomb].AddTail(new CBomb + 420, + 80)); m_ObjList[enBomb].AddTail(new CBomb + 390, + 80)); m_ObjList[enBomb].AddTail(new CBomb + 270, + 80)); m_ObjList[enBomb].AddTail(new CBomb + 360, + 80)); m_ObjList[enBomb].AddTail(new CBomb + 330, + 80)); m_ObjList[enBomb].AddTail(new CBomb + 300, + 80)); m_ObjList[enBomb].AddTail(new CBomb - 50, + 60)); m_ObjList[enBomb].AddTail(new CBomb - 20, + 60)); m_ObjList[enBomb].AddTail(new CBomb - 70, + 60)); m_ObjList[enBomb].AddTail(new CBomb - 100, + 60)); m_ObjList[enBomb].AddTail(new CBomb - 130, + 60)); m_ObjList[enBomb].AddTail(new CBomb - 550, + 60)); m_ObjList[enBomb].AddTail(new CBomb - 520, + 60)); m_ObjList[enBomb].AddTail(new CBomb - 160, + 60));m_ObjList[enBomb].AddTail(new CBomb - 460, + 60)); m_ObjList[enBomb].AddTail(new CBomb - 190, + 60)); m_ObjList[enBomb].AddTail(new CBomb - 430, + 60)); m_ObjList[enBomb].AddTail(new CBomb - 400, + 60)); m_ObjList[enBomb].AddTail(new CBomb - 220, + 60)); m_ObjList[enBomb].AddTail(new CBomb - 370, + 60)); m_ObjList[enBomb].AddTail(new CBomb - 340, + 60)); m_ObjList[enBomb].AddTail(new CBomb - 250, + 60)); m_ObjList[enBomb].AddTail(new CBomb - 310, + 60)); m_ObjList[enBomb].AddTail(new CBomb - 280, + 60)); m_ObjList[enBomb].AddTail(new CBomb, + 60));m_ObjList[enBomb].AddTail(new CBomb + 30, + 60)); m_ObjList[enBomb].AddTail(new CBomb + 60, + 60)); m_ObjList[enBomb].AddTail(new CBomb + 90, + 60));m_ObjList[enBomb].AddTail(new CBomb + 150, + 60)); m_ObjList[enBomb].AddTail(new CBomb + 570, + 60)); m_ObjList[enBomb].AddTail(new CBomb + 180, + 60)); m_ObjList[enBomb].AddTail(new CBomb + 540, + 60)); m_ObjList[enBomb].AddTail(new CBomb + 510, + 60)); m_ObjList[enBomb].AddTail(new CBomb + 210, + 60)); m_ObjList[enBomb].AddTail(new CBomb + 480, + 60)); m_ObjList[enBomb].AddTail(new CBomb + 450, + 60)); m_ObjList[enBomb].AddTail(new CBomb + 240, + 60)); m_ObjList[enBomb].AddTail(new CBomb + 420, + 60)); m_ObjList[enBomb].AddTail(new CBomb + 390, + 60)); m_ObjList[enBomb].AddTail(new CBomb + 270, + 60)); m_ObjList[enBomb].AddTail(new CBomb + 360, + 60)); m_ObjList[enBomb].AddTail(new CBomb + 330, + 60));}}void CPlaneGameView::OnLButtonDown(UINT nFlags, CPoint point){实训中遇到的主要问题及解决方法1.背景滚动的问题,开始时不明白背景该如何滚动,可以把一张图复制为两个,首尾相接,从而实现滚动。