飞机大战游戏设计与实现精选Word格式文档下载.docx
《飞机大战游戏设计与实现精选Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《飞机大战游戏设计与实现精选Word格式文档下载.docx(36页珍藏版)》请在冰豆网上搜索。
大boss则在屏幕中随机走动不出屏幕,直至血量为0消失。
1.3战机设置
战机初始有五条生命,每条命的血量为50,所获得的积分以及其生命值、血条都在窗口的左上角显示。
在第一、二关时,按空格键战机会出现两个向上发射的炮弹,到第三、四关的时候,按空格键战机会发射三个不同方向的炮弹。
战机吃到药瓶道具时血量会增加20,但不可以超过50;
吃到闪电道具时,便可放大招,放大招的次数为15。
当战机被敌方的炮弹击中或与敌方相撞时,血量减少,减少为0时生命值减1,直至生命值为0。
1.4游戏设置
进入游戏时,会有背景音乐,窗口左上角显示战机所得积分及其生命值。
根据积分情况进入不同的关卡。
游戏通关时屏幕清空,并显示通关成功,按Y可重新开始游戏。
当战机的血量以及生命值为0时,游戏失败,屏幕清空并显示游戏结束,按Y即可重新开始游戏。
2.相关技术
2.1Windows定时器技术
Windows定时器是一种输入设备,它周期性地在每经过一个指定的时间间隔后就通知应用程序一次。
程序将时间间隔告诉windows,然后windows给你的程序发送周期性发生的WM_YIMER消息以表示时间到了。
本程序在OnCreate()函数中使用了编号为1的定时器,控制了每30毫秒刷新一次。
2.2双缓冲技术
在内存中创建一个与屏幕绘图区域一致的对象,先将图形绘制到内存这个对象上,再一次性将这个对象上的图形拷贝到屏幕上,这样即可解决图片闪烁问题。
本程序在绘制滚动背景以及飞机,敌机等中用到了该技术。
2.3获取矩形区域
用CRect定义一个对象,利用这个对象得到要用到的矩形区域,用IntersectRect函数来判断两个源矩形是不是有重合的部分。
3.总体设计与详细设计
3.1系统模块划分
飞机大战游戏主要模块可以说是有几个重要的类组成的。
首先是老师给出的CGameObject类,以及在其派生下的Bomb,Ball,Enemy,MyPlane,Explosion,AddBlood等等此处不一一列举。
另外还有CObject派生的BackGround类。
如下图:
3.2主要功能模块
主要功能是在飞机大战View类中,OnTimer函数中实现,将各个类调用,随机产生敌机,子弹的输出,爆炸的实现等等。
它的实现图可以如下实现:
本程序所包含的功能和类模块:
游戏子系统
模块名称
功能简述
应用程序对象
游戏程序的加载、游戏对象的绘制、玩家的键盘事件获取
游戏对象
各个游戏对象的抽象父类(CGameObject)
战机对象
战机类(MyPlane)
敌机对象
敌机类(Enemy,LittleBoss,Boss)
导弹对象
导弹类(Bomb,Bomb2)
炸弹对象
炸弹类(Ball,BossBall)
爆炸对象
爆炸类(Explosion)
道具对象
道具类(AddBlood,AddBomb)
4.编码实现
4.1初始准备
功能描述
数据结构
与算法
CflyfightView:
:
CflyfightView()
{
//TODO:
在此处添加构造代码
flag=0;
flag2=0;
level=1;
bossblood=100;
}
~CflyfightView()
BOOLCflyfightView:
PreCreateWindow(CREATESTRUCT&
cs)
在此处通过修改
//CREATESTRUCTcs来修改窗口类或样式
returnCView:
PreCreateWindow(cs);
//CflyfightView绘制
voidCflyfightView:
OnDraw(CDC*pDC)
CflyfightDoc*pDoc=GetDocument();
ASSERT_VALID(pDoc);
if(!
pDoc)
return;
在此处为本机数据添加代码
//CflyfightView打印
OnPreparePrinting(CPrintInfo*pInfo)
//默认准备
returnDoPreparePrinting(pInfo);
OnBeginPrinting(CDC*/*pDC*/,CPrintInfo*/*pInfo*/)
添加额外的打印前进行的初始化过程
OnEndPrinting(CDC*/*pDC*/,CPrintInfo*/*pInfo*/)
添加打印后进行的清理过程
//CflyfightView诊断
#ifdef_DEBUG
AssertValid()const
CView:
AssertValid();
Dump(CDumpContext&
dc)const
Dump(dc);
CflyfightDoc*CflyfightView:
GetDocument()const//非调试版本是内联的
ASSERT(m_pDocument->
IsKindOf(RUNTIME_CLASS(CflyfightDoc)));
return(CflyfightDoc*)m_pDocument;
#endif//_DEBUG
//CflyfightView消息处理程序
intCflyfightView:
OnCreate(LPCREATESTRUCTlpCreateStruct)
if(CView:
OnCreate(lpCreateStruct)==-1)
return-1;
在此添加专用代码和/或调用基类
PlaySound((LPCTSTR)IDR_WAVE1,AfxGetInstanceHandle(),SND_RESOURCE|SND_ASYNC|SND_LOOP);
SetTimer(1,30,0);
CMyPlane:
LoadImage();
CEnemy:
CExplosion:
CBomb:
CBall:
CAddBlood:
CAddBomb:
CBomb2:
CLittleBoss:
CBoss:
CBossBall:
return0;
OnKeyUp(UINTnChar,UINTnRepCnt,UINTnFlags)
在此添加消息处理程序代码和/或调用默认值
if(nChar==VK_DOWN||VK_UP)
myPlane.SetVerMotion(0);
if(nChar==VK_RIGHT||VK_LEFT)
myPlane.SetHorMotion(0);
OnKeyUp(nChar,nRepCnt,nFlags);
补充说明
4.2战机对象
战机移动、战机导弹的发射
shortkey=GetKeyState(VK_RIGHT);
shortkey1=GetKeyState(VK_LEFT);
shortkey2=GetKeyState(VK_UP);
shortkey3=GetKeyState(VK_DOWN);
if((key&
0x80)!
=0)
myPlane.SetHorMotion
(1);
elseif((key1&
myPlane.SetHorMotion(-1);
elseif((key2&
myPlane.SetVerMotion(-1);
elseif((key3&
myPlane.SetVerMotion
(1);
OnKeyDown(UINTnChar,UINTnRepCnt,UINTnFlags)
if(nChar==VK_SPACE)
{
if(myPlane.Fired())
if(flag==0&
&
(level==1||level==2))
{
listBomb.AddTail(newCBomb(myPlane.GetPoint().x+10,myPlane.GetPoint().y+20,0,12));
listBomb.AddTail(newCBomb(myPlane.GetPoint().x+30,myPlane.GetPoint().y+20,0,12));
}
elseif(flag==0&
(level==3||level==4))
listBomb.AddTail(newCBomb(myPlane.GetPoint().x+10,myPlane.GetPoint().y+20,5,12));
listBomb.AddTail(newCBomb(myPlane.GetPoint().x+30,myPlane.GetPoint().y+20,-5,12));
if(flag2!
listBomb2.AddTail(newCBomb2(myPlane.GetPoint().x+10,myPlane.GetPoint().y+20,10,10));
listBomb2.AddTail(newCBomb2(myPlane.GetPoint().x+40,myPlane.GetPoint().y+20,5,10));
listBomb2.AddTail(newCBomb2(myPlane.GetPoint().x+20,myPlane.GetPoint().y+20,0,10));
listBomb2.AddTail(newCBomb2(myPlane.GetPoint().x+40,myPlane.GetPoint().y+20,-5,10));
listBomb2.AddTail(newCBomb2(myPlane.GetPoint().x+40,myPlane.GetPoint().y+20,-10,10));
flag2--;
if(flag2<
1)
flag=0;
}
4.3敌机对象
敌机、大小boss随机产生,随机方向发射子弹,大boss随机发射子弹,大小boss的血条显示
//敌机的随机产生
if(level==1)
{if(rand()%30==10)
listEnemy.AddTail(newCEnemy());
if(level==2||level==3)
{if(rand()%25==15)
if(level==4)
{if(rand()%20==15)
//大小Boss的随机产生
if(myPlane.score%600==0&
level!
=1&
=2)
listlboss.AddHead(newCLittleBoss());
if(myPlane.score==3630)
listboss.AddHead(newCBoss());
//敌机、Boss产生子弹
posEnemy=listEnemy.GetHeadPosition();
while(posEnemy!
=NULL)
{
CEnemy*pEnemy=(CEnemy*)listEnemy.GetNext(posEnemy);
CPointpoint2=pEnemy->
GetPoint();
if(rand()%60==1)
listBall.AddTail(newCBall(point2.x+10,point2.y+10,0,-12));
poslboss=listlboss.GetHeadPosition();
while(poslboss!
CLittleBoss*plboss=(CLittleBoss*)listlboss.GetNext(poslboss);
CPointpoint3=plboss->
if(rand()%50==1)
listBall.AddTail(newCBall(point3.x+10,point3.y+10,-5,-12));
listBall.AddTail(newCBall(point3.x+10,point3.y+10,0,-12));
listBall.AddTail(newCBall(point3.x+10,point3.y+10,5,-12));
posboss=listboss.GetHeadPosition();
while(posboss!
CBoss*pboss=(CBoss*)listboss.GetNext(posboss);
CPointpoint4=pboss->
if(rand()%40==1)
listbossball.AddTail(newCBossBall(point4.x+45,point4.y+90,-5,-12));
listbossball.AddTail(newCBossBall(point4.x+45,point4.y+90,0,-12));
listbossball.AddTail(newCBossBall(point4.x+45,point4.y+10,5,-12));
//大Boss血条显示
CBrushbrush;
brush.CreateSolidBrush(RGB(255,0,0));
pDC->
SelectObject(brush);
Rectangle(m_ptPos.x,m_ptPos.y,m_ptPos.x+bblood*0.9,m_ptPos.y-5);
//小Boss血条显示
Rectangle(m_ptPos.x,m_ptPos.y,m_ptPos.x+lblood,m_ptPos.y-5);
4.4道具对象
积分达到一定的时候出现道具,战机吃掉道具,道具消失,战机补血或获得技能
if(myPlane.score%510==0)
listAddblood.AddHead(newCAddBlood());
if(myPlane.score%900==0&
=1)
listaddbomb.AddHead(newCAddBomb());
/********加血药瓶********/
POSITIONposaddblood=listAddblood.GetHeadPosition();
POSITIONpos4;
for(posaddblood;
(pos4=posaddblood)!
=NULL;
)
CAddBlood*paddblood=(CAddBlood*)listAddblood.GetNext(posaddblood);
paddblood->
Draw(&
m_mDC,FALSE);
CRectrect;
intsection=rect.IntersectRect(paddblood->
GetRect(),myPlane.GetRect());
if(section!
if(myPlane.blood<
50)
myPlane.blood+=20;
listAddblood.RemoveAt(pos4);
deletepaddblood;
break;
/********技能道具*******/
POSITIONposaddbomb=listaddbomb.GetHeadPosition();
POSITIONpos5;
for(posaddbomb;
(pos5=posaddbomb)!
CAddBomb*paddbomb=(CAddBomb*)listaddbomb.GetNext(posaddbomb);
paddbomb->
intsection=rect.IntersectRect(paddbomb->
flag=1;
flag2=15;
listaddbomb.RemoveAt(pos5);
deletepaddbomb;
break;
4.5主要内容
主要内容
战机导弹击中敌机和大小Boss,战机与敌机和大小Boss相撞,敌机和大小Boss发射的炮弹击中战机
//战机导弹击中敌机
POSITIONpos1=NULL;
//删除敌机
POSITIONpos2=NULL,pos3=NULL;
//删除导弹
for(posBomb=listBomb.GetHeadPosition();
(pos2=posBomb)!
=NULL;
CBomb*pBomb=(CBomb*)listBomb.GetNext(posBomb);
for(posEnemy=listEnemy.GetHeadPosition();
(pos1=posEnemy)!
CRectintersectRect;
CEnemy*pEnemy=(CEnemy*)listEnemy.GetNext(posEnemy);
intintersectSection=intersectRect.IntersectRect(pEnemy->
GetRect(),pBomb->
GetRect());
if(intersectSection!
=0)
CRecteRect;
eRect=pEnemy->
GetRect();
listExplosion.AddTail(newCExplosion(eRect.left,eRect.top));
myPlane.score+=30;
if(myPlane.score%510==0)
listAddblood.AddHead(newCAddBlood());
if(myPlane.score%900==0&
listaddbomb.AddHead(newCAddBomb());
if(myPlane.score%600==0&
listlboss.AddHead(newCLittleBoss());
if(myPlane.score==3630)
listboss.AddHead(newCBoss());
listEnemy.RemoveAt