楊衛(wèi)紅
摘要:就面向?qū)ο蟮某绦蛟O(shè)計(jì)而言,基類(lèi)的設(shè)計(jì)至關(guān)重要,在游戲軟件的開(kāi)發(fā)中,“精靈”的設(shè)計(jì)是關(guān)鍵的步驟。首先,可以創(chuàng)建精靈的基類(lèi),再通過(guò)繼承得到子類(lèi),如戰(zhàn)機(jī)類(lèi)、子彈類(lèi)、爆炸效果類(lèi)等。該文意在通過(guò)游戲的開(kāi)發(fā)過(guò)程,闡述面向?qū)ο蟮脑O(shè)計(jì)理念。
關(guān)鍵詞:面向?qū)ο螅换?lèi);派生
中圖分類(lèi)號(hào):TP311 文獻(xiàn)標(biāo)識(shí)碼:A 文章編號(hào):1009-3044(2016)09-0138-04
An Design Instance of Air Combat Game
YANG Wei-hong
(Dongguan Science and Technology School, Dongguan 523460, China)
Abstract: In the case of object-oriented programming, the design of the base class is very important. As we develop a game software,the design of the sprite is a key step. First, we may create a base class of sprite,then we get the derived class by inheritance, for example: fighter class, bullet class, explosion effect class etc. The intention of writing this article is to elaborate the design conception of object-oriented via the game development process.
Key words: game; class; inheritance
1 設(shè)計(jì)目標(biāo)
1)利用Visual Studio 2010創(chuàng)建一個(gè)基于VC++的MFC單文檔應(yīng)用程序SkyWar,程序運(yùn)行效果如圖1所示。
2)游戲背景及精靈的移動(dòng)平順無(wú)閃爍,爆炸的動(dòng)畫(huà)效果逼真。
3)游戲中的“我方戰(zhàn)機(jī)”可跟隨鼠標(biāo)移動(dòng),當(dāng)按下鼠標(biāo)左鍵時(shí),可向敵機(jī)射擊。
4)擊中目標(biāo)或雙方戰(zhàn)機(jī)發(fā)生碰撞時(shí),可顯示爆炸的動(dòng)畫(huà)效果。
5)游戲運(yùn)行環(huán)境:Windows XP/ Windows 2000 /Windows 7。
6)分辨率設(shè)置:1600×900像素。
2 設(shè)計(jì)思路
精靈是顯示在背景上層的圖像,也被稱為游戲?qū)ο?,通常,精靈表示游戲中所有運(yùn)動(dòng)的部分,精靈可以包括:自己人、敵人、子彈等。精靈的狀態(tài)是根據(jù)圖片、位置、色彩等變化的,因此,精靈需要保存各種信息,如:即時(shí)位置、即時(shí)速度、即時(shí)方向、可見(jiàn)性、幀、動(dòng)畫(huà)等。伴隨著精靈的出現(xiàn),將有一幀或多幀的動(dòng)畫(huà)被顯示出來(lái)。當(dāng)精靈需要出現(xiàn)時(shí),游戲?qū)ο髮⑿枰粋€(gè)視覺(jué)上真實(shí)的動(dòng)畫(huà)存在。
本文將呈現(xiàn)如何基于Visual Studio 2010,利用VC++編程開(kāi)發(fā)一個(gè)簡(jiǎn)單的空戰(zhàn)游戲的過(guò)程。要求游戲能顯示“我方戰(zhàn)機(jī)”、“敵機(jī)”、“子彈”、“爆炸效果”等精靈,并能進(jìn)行簡(jiǎn)單的空戰(zhàn)游戲。我們將通過(guò)設(shè)計(jì)一個(gè)精靈的基類(lèi)(原型類(lèi)),然后派生子類(lèi),創(chuàng)建相應(yīng)的對(duì)象,從而實(shí)現(xiàn)在背景上顯示游戲的精靈,該游戲中各種類(lèi)的關(guān)系如圖2所示。
其中,CFlyObj為基類(lèi),由CFlyObj類(lèi)派生出我方戰(zhàn)機(jī)類(lèi)CUpPlane,敵機(jī)類(lèi) CDnPlane,子彈類(lèi)CBullet,和爆炸類(lèi)CExplosion等。