




已阅读5页,还剩16页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
信息科学与工程学院软件实践实训报告实 训 报 告飞机大战游戏设计与开发王上前专业名称:物联网工程班 级:物联网11级1班学 号:20111052131信息科学与工程学院二零一二年十二月目 录1. 概述031.1 实训项目简介031.2 实训功能说明031.2.1 基本功能032. 相关技术032.1 基类的使用032.2 windows定时器技术032.3获取矩形区域032.4内存释放043. 总体设计与详细设计043.1 系统模块划分043.2 主要功能模块054. 编码实现205. 实训中遇到的主要问题及解决方法216. 实训体会21211. 概述1.1 实训项目简介本次实训项目是做一个飞机大战的游戏,完成一个界面简洁流畅、游戏方式简单,玩起来易于上手的桌面游戏。该飞机大战项目运用的主要技术即是mfc编程中的一些函数、链表思想以及贴图技术。1.2 实训功能说明1.2.1 基本功能(1)设置一个战机具有一定的速度,通过键盘,方向键可控制战机的位置,空格键发射子弹。(2)界面中敌机出现的位置为随机的。(3)对于随机产生的敌机和敌机炸弹,若超过矩形区域,则释放该对象。(4)添加爆炸效果,包括战机子弹打中敌机爆炸、敌机炸弹打中战机爆炸、战机与敌机相撞爆炸以及战机子弹与敌机炸弹相撞爆炸四种爆炸效果。且爆炸发生后敌机、子弹、炸弹均消失。2. 相关技术2.1基类的使用 在飞机大战的游戏中用到了很多基类,例如myplane、bomb等等,这样使得游戏的实现更加规范,有效性。通过主函数的调用,实现了很多功能。2.2 windows定时器技术windows定时器是一种输入设备,它周期性地在每经过一个指定的时间间隔后就通知应用程序一次。程序将时间间隔告诉windows,然后windows给您的程序发送周期性发生的wm_timer消息以表示时间到了。本程序中使用多个定时器,分别控制不同的功能。在mfc的api函数中使用settimer()函数设置定时器,设置系统间隔时间,在ontimer()函数中实现响应定时器的程序。2.3获取矩形区域首先,使用crect定义一个对象,然后使用getclientrect(&对象名)函数,获取界面的矩形区域rect.width() 为矩形区域的宽度,rect.height()为矩形区域的高度。使用intersectrect(&,&)函数来判断两个源矩形是否有重合的部分。如果有不为空,则返回非零值;否则,返回0。2.4内存释放在vc/mfc用cdc绘图时,频繁的刷新,屏幕会出现闪烁的现象,cpu时间占用率相当高,绘图效率极低,很容易出现程序崩溃。及时的释放程序所占用的内存资源是非常重要的。在程序中使用到的刷子等占用内存资源的对象都要及时的删除。delete brush等。3. 总体设计与详细设计3.1 系统模块划分飞机大战游戏主要模块可以说是有几个重要的类组成的。首先是老师给出的cgameobject类,以及在其派生下的bomb,ball,enemy,myplane等等。如图:3.2 主要功能模块主要功能是在飞机大战view类中,ontimer函数中实现,将各个类调用,随机产生敌机,子弹的输出,爆炸的实现等等。它的实现图可以如下实现:4. 编码实现4.1飞机大战view.cpp#include stdafx.h#include 飞机大战.h#include 飞机大战doc.h#include 飞机大战view.h#ifdef _debug#define new debug_new#endif/ c飞机大战viewimplement_dyncreate(c飞机大战view, cview)begin_message_map(c飞机大战view, cview)/ 标准打印命令on_command(id_file_print, &cview:onfileprint)on_command(id_file_print_direct, &cview:onfileprint)on_command(id_file_print_preview, &cview:onfileprintpreview)on_wm_create()on_wm_timer()on_wm_keydown()on_wm_keyup()end_message_map()/ c飞机大战view 构造/析构c飞机大战view:c飞机大战view()/ todo: 在此处添加构造代码c飞机大战view:c飞机大战view()bool c飞机大战view:precreatewindow(createstruct& cs)/ todo: 在此处通过修改/ createstruct cs 来修改窗口类或样式return cview:precreatewindow(cs);/ c飞机大战view 绘制void c飞机大战view:ondraw(cdc* /*pdc*/)c飞机大战doc* pdoc = getdocument();assert_valid(pdoc);if (!pdoc)return;/ todo: 在此处为本机数据添加绘制代码/ c飞机大战view 打印bool c飞机大战view:onprepareprinting(cprintinfo* pinfo)/ 默认准备return doprepareprinting(pinfo);void c飞机大战view:onbeginprinting(cdc* /*pdc*/, cprintinfo* /*pinfo*/)/ todo: 添加额外的打印前进行的初始化过程void c飞机大战view:onendprinting(cdc* /*pdc*/, cprintinfo* /*pinfo*/)/ todo: 添加打印后进行的清理过程/ c飞机大战view 诊断#ifdef _debugvoid c飞机大战view:assertvalid() constcview:assertvalid();void c飞机大战view:dump(cdumpcontext& dc) constcview:dump(dc);c飞机大战doc* c飞机大战view:getdocument() const / 非调试版本是内联的assert(m_pdocument-iskindof(runtime_class(c飞机大战doc);return (c飞机大战doc*)m_pdocument;#endif /_debug/ c飞机大战view 消息处理程序int c飞机大战view:oncreate(lpcreatestruct lpcreatestruct)if (cview:oncreate(lpcreatestruct) = -1)return -1;/ todo: 在此添加您专用的创建代码settimer(1, 20, 0);cmyplane:loadimagew();/-为什么不能放在ontimer里面? cenemy:loadimage(); cexplosion:loadimage(); cbomb:loadimage(); cball:loadimage();return 0;void c飞机大战view:ontimer(uint_ptr nidevent)/ todo: 在此添加消息处理程序代码和/或调用默认值 short key;key=getkeystate(vk_down); if(key&0x80)plane.setvermotion(1);key=getkeystate(vk_right);if(key&0x80)plane.sethormotion(1);key=getkeystate(vk_left);if(key&0x80)plane.sethormotion(-1);key=getkeystate(vk_up);if(key&0x80)plane.setvermotion(-1); /*cdc memdc;*/定义一个显示设备对象 static int life=50;static int g=1; static int score=0; /cbitmap membmap;/定义一个位图对象 cdc *pdc=getdc();crect rect;getclientrect(&rect); cbrush m_brushbackground,m_brushbackground1,m_brushbackground2,brush,brush1;cbitmap bmp,bnp,bnp1;bmp.loadbitmap(idb_beijing); /加载位图 m_brushbackground.createpatternbrush(&bmp);/创建位图画刷 pdc-fillrect(rect,&m_brushbackground); /用背景画刷填充区域 /添加文字pdc-setbkmode(transparent);/设置文字背景透明pdc-settextcolor(rgb(255,0,0);/设置文字颜色为黄色cfont font;font.createfont( 0, / nheight 0, / nwidth 0, / nescapement 0, / norientation fw_normal, / nweight false, / bitalic false, / bunderline 0, / cstrikeout ansi_charset, / ncharset out_default_precis, / noutprecision clip_default_precis, / nclipprecision default_quality, / nquality default_pitch | ff_swiss, / npitchandfamily _t(宋体) ); / lpszfacenamecfont *oldfont=pdc-selectobject(&font);/pdc-selectobject(oldfont);cstring life;/显示生命值life.format(l生命:%d,life);pdc-textoutw(rect.width()/2-200,10,life);cstring guan;/显示生命值guan.format(l关数:%d,g);pdc-textoutw(rect.width()/2-300,10,guan);cstring kan;/显示生命值kan.format(l注意:上,下,左,右,开火space,暂停ctrl);pdc-textoutw(rect.width()/2-8,10,kan);cstring grade;/显示分数grade.format(l分数:%d,score);pdc-selectobject(oldfont);pdc-textoutw(rect.width()/2-100,10,grade); plane.draw(pdc, true);/画出战机/添加爆炸效果position posexplosion=null;for(posexplosion=listexplosion.getheadposition();posexplosion!=null; )cexplosion *pexplosion=(cexplosion*)listexplosion.getnext(posexplosion);pexplosion-draw(pdc,false);if(score=0&score=300)g=1; if (listenemy.getcount()=110&score=600)g=2; if (listenemy.getcount()610&score=1200)g=3; if (listenemy.getcount()=1210&score=1800)g=4; if (listenemy.getcount()=1810&score=3000)g=5; if (listenemy.getcount()getrect(),plane.getrect()/敌机子弹速度 int x=rand()%50; if(x=0) listball.addtail(new cball(penemy-getpoint().x+6,penemy-getpoint().y+25,penemy-getmontion(); listball.addtail(new cball(penemy-getpoint().x+20,penemy-getpoint().y+25,penemy-getmontion(); penemy-draw(pdc,true); else listenemy.removeat(oldpos); listexplosion.addtail(new cexplosion(rect.left,rect.top);/敌机爆 life-=1; break; position posbomb,oldposbomb;/导弹设置cbomb* pbomb;posbomb=listbomb.getheadposition();while(posbomb!=null)/getheadposition 返回列表中首元素的位置crect enrect;oldposbomb=posbomb;pbomb= (cbomb *)listbomb.getnext(posbomb);/getnext(pos)获取列表元素标识由rposition,然后设置rposition 到下一项的position 值列表中的。 /crect enemyrect = penemy-getrect();/获得敌机机区域/if(!enrect.intersectrect(penemy-getrect(),pbomb-getrect()/矩形框没有交集 pbomb-draw(pdc,true); /战机子弹炸掉敌机if(life!=0)position b1=null,b2=null;b1=listbomb.getheadposition();while(b2=b1)!=null)/getheadposition 返回列表中首元素的位置 pbomb= (cbomb*)listbomb.getnext(b1);/getnext获取循环遍历中的下一个元素 crect brect = pbomb-getrect();/获得导弹区域position e1=null,e2=null;e1=listenemy.getheadposition();while(e2=e1)!=null)/getheadposition 返回列表中首元素的位置penemy=(cenemy*)listenemy.getnext(e1);crect erect=penemy-getrect();/获得敌机区域crect enrect;/定义一个区域,该区域作用为获得导弹和敌机区域的交集。if(enrect.intersectrect(&brect,erect)/若导弹和敌机有交集,删除导弹和敌机,添加爆炸效果score+=20;/添加炸弹效果listexplosion.addtail(new cexplosion(erect.left, erect.top);/删除导弹listbomb.removeat(b2);/removeat 从列表中删除指定位置的元素 delete pbomb;/删除敌机listenemy.removeat(e2);delete penemy;break;/敌机子弹炸掉战机if(life!=0) position c1=null,c2=null;crect mrect = plane.getrect();/获得战机区域c1=listball.getheadposition();while( c2 = c1 ) != null)/getheadposition 返回列表中首元素的位置pball = (cball*)listball.getnext(c1);crect brect = pball-getrect();/获得子弹区域crect tmprect;if(tmprect.intersectrect(&brect,mrect)/子弹区域与战机区域相交life-=1;/添加炸弹效果listexplosion.addtail(new cexplosion(mrect.left,mrect.top);/删除子弹listball.removeat(c2);delete pball;break; /添加敌机子弹position posball=null;/*cball* pball;*/posball=listball.getheadposition();/crect enemyrect = penemy-getrect();/获得敌机机区域while(posball!=null)/crect enemyrect = penemy-getrect();/获得敌机机区域cball* pball=(cball *)listball.getnext(posball);pball-draw(pdc, false); /listball.addtail(new cball(penemy-getpoint().x+12,penemy-getpoint().y+25,penemy-getmontion(); if(score%200=0&score!=0)life+=1;score+=20;if(lifefillrect(rect,&m_brushbackground1); /用背景画刷填充区域killtimer(1); releasedc(pdc);cview:ontimer(nidevent);void c飞机大战view:onkeydown(uint nchar, uint nrepcnt, uint nflags)/ todo: 在此添加消息处理程序代码和/或调用默认值 if (nchar=vk_left)plane.sethormotion(-1);else if(nchar=vk_right)plane.sethormotion(1);else if(nchar=vk_up)plane.setvermotion(-1);else if(nchar=vk_down)plane.setvermotion(1);if(nchar=vk_control)/ctrl键表示暂停sleep(2000);/暂停5秒 else if(nchar=vk_space)/空格键/战机添加导弹 listbomb.addtail(new cbomb(plane.getpoint().x+10,plane.getpoint().y+20); listbomb.addtail(new cbomb(plane.getpoint().x+30,plane.getpoint().y+20); cview:onkeydown(nchar, nrepcnt, nflags);void c飞机大战view:onkeyup(uint nchar, uint nrepcnt, uint nflags)/ todo: 在此添加消息处理程序代码和/或调用默认值 if(pmyplane!=null)if(nchar=vk_right)plane.sethormotion(0);if(nchar=vk_left)plane.sethormotion(0);if(nchar=vk_up)plane.setvermotion(0);if(nchar=vk_down)plane.setvermotion(0);cview:onkeyup(nchar, nrepcnt, nflags);4.2myplane.cpp#include stdafx.h#include myplane.h#include resource.hcimagelist cmyplane:m_images;cmyplane:cmyplane(void)m_ptpos.x=400; m_ptpos.y=400; m_nhormotion=0; m_nvermotion=0;cmyplane:cmyplane(void)bool cmyplane:draw(cdc* pdc,bool bpause) m_ptpos.x=m_ptpos.x+m_nhormotion*5;m_ptpos.y=m_ptpos.y+m_nvermotion*5;/使飞机在屏幕内if(m_ptpos.x=1000)m_ptpos.x=1000;if(m_ptpos.x=0)m_ptpos.x=0;if(m_ptpos.y=600)m_ptpos.y=580; return m_images.draw(pdc,0,m_ptpos, ild_transparent);bool cmyplane:loadimage() return cgameobject:loadimage(m_images,idb_myplane,rgb(0,0,0),50,60,1);4.3 enemy.cpp#include stdafx.h#include enemy.h#include resource.hcimagelist cenemy:m_images;cenemy:cenemy(void)srand(gettickcount();m_ptpos.x=rand()%990;/rand产生随机数,初始敌机位置m_ptpos.y=0;m_v=rand()%5+1;m_nimgindex=rand()%2;/上下飞机if(m_nimgindex=0) m_nmotion=1; m_ptpos.y=0;if(m_nimgindex=1) m_nmotion=-1; m_ptpos.y=700;cenemy:cenemy(void)bool cenemy:draw(cdc *pdc, bool bpause) if(m_ptpos.y=700)m_ptpos.y=0;if(m_ptpos.y=1000)return false;if(m_ptpos.y=800)return false;bool cbomb:loadimagew()return cgameobject:loadimagew(m_images,idb_bomb,rgb(0,0,0),10,20,1);4.5ball.cpp#include stdafx.h#include ball.h#include resource.hcimagelist cball:m_images;cball:cball(int x,int y,int nmotion) m_ptpos.x=x;m_ptpos.y=y;m_nmotion=nmotion;cball:cball(void)bool cball:draw(cdc* pdc,bool bpause)m_ptpos.y= m_ptpos.y+m_nmotion*6;/固定子弹速度为10return m_images.draw(pdc,0,m_ptpos,ild_transparent);/使子弹在屏幕内if(m_ptpos.x=1000)return false;if(m_ptpos.y=700)return false;bool cball:loadimage() return cgameobject:loadimage(m_images,idb_ball,rgb(0,0,0),8,8,1);4.6explosion.cpp#include stdafx.h#include explosion.h#include resource.hcimagelist cexplosion:m_images;cexplosion:cexplosion(int x,int y) m_ptpos.x=x;m_ptpos.y=y;m_nprocess=0;cexplosion:cexplosion(voi
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 劳务派遣公司合同书
- 农业生产化提升协议
- 农业科技项目研发资助协议
- 高质量学术成果责任承诺书(8篇)
- 曹操观沧海诗词对比分析与解读:高中语文古诗文教学教案
- 农产品网络销售合作协议
- 工地员工安全培训测试题及答案解析
- 教育机构学生成绩管理与分析工具
- 农田种植及灌溉管理协议
- 数据统计表-各行业(编号1)
- 光伏电站智能监控系统建设方案
- 儿童户外安全培训课件
- 供水工程成本预算与动态控制方案
- 护栏供应及安装合同范本
- 2025年反假货币试题题库及答案
- 现房与期房培训课件
- 2024年仙桃市高新技术产业投资有限公司招聘笔试真题
- 汽车知识培训讲师简介课件
- 2025年教师职称-浙江-浙江教师职称(基础知识、综合素质、初中信息技术)历年参考题库典型考点含答案解析
- 北京高校标准化食堂标准(2026版)讲解
- 专题1.4数学归纳法(高效培优讲义)
评论
0/150
提交评论