飛機大戰(zhàn)實訓(xùn)_第1頁
飛機大戰(zhàn)實訓(xùn)_第2頁
飛機大戰(zhàn)實訓(xùn)_第3頁
飛機大戰(zhàn)實訓(xùn)_第4頁
飛機大戰(zhàn)實訓(xùn)_第5頁
已閱讀5頁,還剩16頁未讀 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、信息科學(xué)與工程學(xué)院軟件實踐實訓(xùn)報告實 訓(xùn) 報 告飛機大戰(zhàn)游戲設(shè)計與開發(fā)王上前專業(yè)名稱:物聯(lián)網(wǎng)工程班 級:物聯(lián)網(wǎng)11級1班學(xué) 號:20111052131信息科學(xué)與工程學(xué)院二零一二年十二月目 錄1. 概述031.1 實訓(xùn)項目簡介031.2 實訓(xùn)功能說明031.2.1 基本功能032. 相關(guān)技術(shù)032.1 基類的使用032.2 windows定時器技術(shù)032.3獲取矩形區(qū)域032.4內(nèi)存釋放043. 總體設(shè)計與詳細設(shè)計043.1 系統(tǒng)模塊劃分043.2 主要功能模塊054. 編碼實現(xiàn)205. 實訓(xùn)中遇到的主要問題及解決方法216. 實訓(xùn)體會21211. 概述1.1 實訓(xùn)項目簡介本次實訓(xùn)項目是做一個飛

2、機大戰(zhàn)的游戲,完成一個界面簡潔流暢、游戲方式簡單,玩起來易于上手的桌面游戲。該飛機大戰(zhàn)項目運用的主要技術(shù)即是mfc編程中的一些函數(shù)、鏈表思想以及貼圖技術(shù)。1.2 實訓(xùn)功能說明1.2.1 基本功能(1)設(shè)置一個戰(zhàn)機具有一定的速度,通過鍵盤,方向鍵可控制戰(zhàn)機的位置,空格鍵發(fā)射子彈。(2)界面中敵機出現(xiàn)的位置為隨機的。(3)對于隨機產(chǎn)生的敵機和敵機炸彈,若超過矩形區(qū)域,則釋放該對象。(4)添加爆炸效果,包括戰(zhàn)機子彈打中敵機爆炸、敵機炸彈打中戰(zhàn)機爆炸、戰(zhàn)機與敵機相撞爆炸以及戰(zhàn)機子彈與敵機炸彈相撞爆炸四種爆炸效果。且爆炸發(fā)生后敵機、子彈、炸彈均消失。2. 相關(guān)技術(shù)2.1基類的使用 在飛機大戰(zhàn)的游戲中用到

3、了很多基類,例如myplane、bomb等等,這樣使得游戲的實現(xiàn)更加規(guī)范,有效性。通過主函數(shù)的調(diào)用,實現(xiàn)了很多功能。2.2 windows定時器技術(shù)windows定時器是一種輸入設(shè)備,它周期性地在每經(jīng)過一個指定的時間間隔后就通知應(yīng)用程序一次。程序?qū)r間間隔告訴windows,然后windows給您的程序發(fā)送周期性發(fā)生的wm_timer消息以表示時間到了。本程序中使用多個定時器,分別控制不同的功能。在mfc的api函數(shù)中使用settimer()函數(shù)設(shè)置定時器,設(shè)置系統(tǒng)間隔時間,在ontimer()函數(shù)中實現(xiàn)響應(yīng)定時器的程序。2.3獲取矩形區(qū)域首先,使用crect定義一個對象,然后使用getcli

4、entrect(&對象名)函數(shù),獲取界面的矩形區(qū)域rect.width() 為矩形區(qū)域的寬度,rect.height()為矩形區(qū)域的高度。使用intersectrect(&,&)函數(shù)來判斷兩個源矩形是否有重合的部分。如果有不為空,則返回非零值;否則,返回0。2.4內(nèi)存釋放在vc/mfc用cdc繪圖時,頻繁的刷新,屏幕會出現(xiàn)閃爍的現(xiàn)象,cpu時間占用率相當高,繪圖效率極低,很容易出現(xiàn)程序崩潰。及時的釋放程序所占用的內(nèi)存資源是非常重要的。在程序中使用到的刷子等占用內(nèi)存資源的對象都要及時的刪除。delete brush等。3. 總體設(shè)計與詳細設(shè)計3.1 系統(tǒng)模塊劃分飛機大戰(zhàn)游戲主要模塊可以說是有幾個

5、重要的類組成的。首先是老師給出的cgameobject類,以及在其派生下的bomb,ball,enemy,myplane等等。如圖:3.2 主要功能模塊主要功能是在飛機大戰(zhàn)view類中,ontimer函數(shù)中實現(xiàn),將各個類調(diào)用,隨機產(chǎn)生敵機,子彈的輸出,爆炸的實現(xiàn)等等。它的實現(xiàn)圖可以如下實現(xiàn):4. 編碼實現(xiàn)4.1飛機大戰(zhàn)view.cpp#include stdafx.h#include 飛機大戰(zhàn).h#include 飛機大戰(zhàn)doc.h#include 飛機大戰(zhàn)view.h#ifdef _debug#define new debug_new#endif/ c飛機大戰(zhàn)viewimplement_dy

6、ncreate(c飛機大戰(zhàn)view, cview)begin_message_map(c飛機大戰(zhàn)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

7、飛機大戰(zhàn)view 構(gòu)造/析構(gòu)c飛機大戰(zhàn)view:c飛機大戰(zhàn)view()/ todo: 在此處添加構(gòu)造代碼c飛機大戰(zhàn)view:c飛機大戰(zhàn)view()bool c飛機大戰(zhàn)view:precreatewindow(createstruct& cs)/ todo: 在此處通過修改/ createstruct cs 來修改窗口類或樣式return cview:precreatewindow(cs);/ c飛機大戰(zhàn)view 繪制void c飛機大戰(zhàn)view:ondraw(cdc* /*pdc*/)c飛機大戰(zhàn)doc* pdoc = getdocument();assert_valid(pdoc);if (!

8、pdoc)return;/ todo: 在此處為本機數(shù)據(jù)添加繪制代碼/ c飛機大戰(zhàn)view 打印bool c飛機大戰(zhàn)view:onprepareprinting(cprintinfo* pinfo)/ 默認準備return doprepareprinting(pinfo);void c飛機大戰(zhàn)view:onbeginprinting(cdc* /*pdc*/, cprintinfo* /*pinfo*/)/ todo: 添加額外的打印前進行的初始化過程void c飛機大戰(zhàn)view:onendprinting(cdc* /*pdc*/, cprintinfo* /*pinfo*/)/ todo:

9、 添加打印后進行的清理過程/ c飛機大戰(zhàn)view 診斷#ifdef _debugvoid c飛機大戰(zhàn)view:assertvalid() constcview:assertvalid();void c飛機大戰(zhàn)view:dump(cdumpcontext& dc) constcview:dump(dc);c飛機大戰(zhàn)doc* c飛機大戰(zhàn)view:getdocument() const / 非調(diào)試版本是內(nèi)聯(lián)的assert(m_pdocument-iskindof(runtime_class(c飛機大戰(zhàn)doc);return (c飛機大戰(zhàn)doc*)m_pdocument;#endif /_debug/

10、 c飛機大戰(zhàn)view 消息處理程序int c飛機大戰(zhàn)view:oncreate(lpcreatestruct lpcreatestruct)if (cview:oncreate(lpcreatestruct) = -1)return -1;/ todo: 在此添加您專用的創(chuàng)建代碼settimer(1, 20, 0);cmyplane:loadimagew();/-為什么不能放在ontimer里面? cenemy:loadimage(); cexplosion:loadimage(); cbomb:loadimage(); cball:loadimage();return 0;void c飛機大

11、戰(zhàn)view:ontimer(uint_ptr nidevent)/ todo: 在此添加消息處理程序代碼和/或調(diào)用默認值 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.setvermo

12、tion(-1); /*cdc memdc;*/定義一個顯示設(shè)備對象 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

13、_brushbackground.createpatternbrush(&bmp);/創(chuàng)建位圖畫刷 pdc-fillrect(rect,&m_brushbackground); /用背景畫刷填充區(qū)域 /添加文字pdc-setbkmode(transparent);/設(shè)置文字背景透明pdc-settextcolor(rgb(255,0,0);/設(shè)置文字顏色為黃色cfont font;font.createfont( 0, / nheight 0, / nwidth 0, / nescapement 0, / norientation fw_normal, / nweight false, / b

14、italic 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(oldfo

15、nt);cstring life;/顯示生命值life.format(l生命:%d,life);pdc-textoutw(rect.width()/2-200,10,life);cstring guan;/顯示生命值guan.format(l關(guān)數(shù):%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;/顯示分數(shù)grade.format(l分

16、數(shù):%d,score);pdc-selectobject(oldfont);pdc-textoutw(rect.width()/2-100,10,grade); plane.draw(pdc, true);/畫出戰(zhàn)機/添加爆炸效果position posexplosion=null;for(posexplosion=listexplosion.getheadposition();posexplosion!=null; )cexplosion *pexplosion=(cexplosion*)listexplosion.getnext(posexplosion);pexplosion-draw(

17、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) l

18、istball.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);/

19、敵機爆 life-=1; break; position posbomb,oldposbomb;/導(dǎo)彈設(shè)置cbomb* pbomb;posbomb=listbomb.getheadposition();while(posbomb!=null)/getheadposition 返回列表中首元素的位置crect enrect;oldposbomb=posbomb;pbomb= (cbomb *)listbomb.getnext(posbomb);/getnext(pos)獲取列表元素標識由rposition,然后設(shè)置rposition 到下一項的position 值列表中的。 /crect ene

20、myrect = penemy-getrect();/獲得敵機機區(qū)域/if(!enrect.intersectrect(penemy-getrect(),pbomb-getrect()/矩形框沒有交集 pbomb-draw(pdc,true); /戰(zhàn)機子彈炸掉敵機if(life!=0)position b1=null,b2=null;b1=listbomb.getheadposition();while(b2=b1)!=null)/getheadposition 返回列表中首元素的位置 pbomb= (cbomb*)listbomb.getnext(b1);/getnext獲取循環(huán)遍歷中的下一

21、個元素 crect brect = pbomb-getrect();/獲得導(dǎo)彈區(qū)域position e1=null,e2=null;e1=listenemy.getheadposition();while(e2=e1)!=null)/getheadposition 返回列表中首元素的位置penemy=(cenemy*)listenemy.getnext(e1);crect erect=penemy-getrect();/獲得敵機區(qū)域crect enrect;/定義一個區(qū)域,該區(qū)域作用為獲得導(dǎo)彈和敵機區(qū)域的交集。if(enrect.intersectrect(&brect,erect)/若導(dǎo)彈和

22、敵機有交集,刪除導(dǎo)彈和敵機,添加爆炸效果score+=20;/添加炸彈效果listexplosion.addtail(new cexplosion(erect.left, erect.top);/刪除導(dǎo)彈listbomb.removeat(b2);/removeat 從列表中刪除指定位置的元素 delete pbomb;/刪除敵機listenemy.removeat(e2);delete penemy;break;/敵機子彈炸掉戰(zhàn)機if(life!=0) position c1=null,c2=null;crect mrect = plane.getrect();/獲得戰(zhàn)機區(qū)域c1=listb

23、all.getheadposition();while( c2 = c1 ) != null)/getheadposition 返回列表中首元素的位置pball = (cball*)listball.getnext(c1);crect brect = pball-getrect();/獲得子彈區(qū)域crect tmprect;if(tmprect.intersectrect(&brect,mrect)/子彈區(qū)域與戰(zhàn)機區(qū)域相交life-=1;/添加炸彈效果listexplosion.addtail(new cexplosion(mrect.left,mrect.top);/刪除子彈listball

24、.removeat(c2);delete pball;break; /添加敵機子彈position posball=null;/*cball* pball;*/posball=listball.getheadposition();/crect enemyrect = penemy-getrect();/獲得敵機機區(qū)域while(posball!=null)/crect enemyrect = penemy-getrect();/獲得敵機機區(qū)域cball* pball=(cball *)listball.getnext(posball);pball-draw(pdc, false); /list

25、ball.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); /用背景畫刷填充區(qū)域killtimer(1); releasedc(pdc);cview:ontimer(nidevent);void c飛機大戰(zhàn)view:onkeydown(uint nchar, uint nrepcnt, uint nflags

26、)/ todo: 在此添加消息處理程序代碼和/或調(diào)用默認值 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)/空格鍵/戰(zhàn)機添加導(dǎo)彈 listbomb.addta

27、il(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飛機大戰(zhàn)view:onkeyup(uint nchar, uint nrepcnt, uint nflags)/ todo: 在此添加消息處理程序代碼和/或調(diào)用默認值 if(pmyplane!=null)if(nchar=vk_right)pl

28、ane.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

29、.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;/使飛機在屏幕內(nèi)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_ima

30、ges.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產(chǎn)生隨機數(shù)

31、,初始敵機位置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;bo

32、ol 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,bo

33、ol bpause)m_ptpos.y= m_ptpos.y+m_nmotion*6;/固定子彈速度為10return m_images.draw(pdc,0,m_ptpos,ild_transparent);/使子彈在屏幕內(nèi)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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論