日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

游戏编程入门(1) -- 精灵 ISprite

發(fā)布時(shí)間:2025/3/19 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 游戏编程入门(1) -- 精灵 ISprite 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
??? 對于游戲編程而言,我也是個(gè)初學(xué)者,這個(gè)游戲編程入門系列的文章,就當(dāng)作是我在學(xué)習(xí)游戲編程的筆記和階段小結(jié)吧。我們先從最簡單的“精靈”開始,暫時(shí)我們不需要考慮DirectX或是OpenGL,不需要考慮3維等等這些復(fù)雜情形,直接使用GDI+繪圖功能就可以了。

??? 精靈,是構(gòu)成游戲中活動(dòng)體(比如,飛機(jī)、野獸等游戲人物)的最基本單元,任何一個(gè)活動(dòng)體都可以由一個(gè)或多個(gè)精靈組合而成,每個(gè)精靈都是一個(gè)對象實(shí)例,它能夠繪制自己、移動(dòng)(更復(fù)雜的還可以旋轉(zhuǎn))等等基本動(dòng)作。
??? 我讓所有的精靈都實(shí)現(xiàn)ISprite接口,該接口如下:


??? 對應(yīng)的代碼如下:
??? public?interface?ISprite
????{
????????Graphics?Graphics{
set?;}?//繪制的設(shè)備
????????Bitmap???Source{set?;}???//精靈表面位圖
????????Image????BackgroundImage{set?;}?//游戲背景

????????Point?Location{
get?;}

????????
void?Erase()?;
????????
void?Draw()?;

????????
void?SetDirection(CompassDirections?dir)?;
????????
void?Move()?;
????}

????
public?enum?CompassDirections?
????{
????????NotSet?
=?0?,
????????North?
=?1,
????????NorthEast?
=?2,
????????East?
=?3,
????????SouthEast?
=?4,
????????South?
=?5,
????????SouthWest?
=?6,
????????West?
=?7,
????????NorthWest?
=?8
????}

??? 這個(gè)接口也許并不是完整的,隨了實(shí)際的深入,可能還會(huì)有很多的元素添加進(jìn)來,甚至CompassDirections枚舉還可以進(jìn)一步細(xì)分,但是ISprite已經(jīng)有最基本“精靈”功能了。對于大多數(shù)簡單的任務(wù),我們已經(jīng)可以給出ISprite的一個(gè)實(shí)現(xiàn):
??? public?class?Sprite?:ISprite
????{
????????
private?CompassDirections?direction?=?CompassDirections.South?;

????????
#region?ISprite?成員
????????
private?Graphics?graphics?=?null?;
????????
public??Graphics?Graphics
????????{
????????????
set
????????????{
????????????????
this.graphics?=?value?;
????????????}
????????}

????????
private?Bitmap?source?=?null?;
????????
public??Bitmap?Source
????????{
????????????
set
????????????{
????????????????
this.source?=?value?;
????????????}
????????}

????????
private?Point?location?=?new?Point(0?,0)?;
????????
public??Point?Location
????????{
????????????
get
????????????{
????????????????
return?this.location?;
????????????}
????????}

????????
#region?BackgroundImage
????????
private?Image?backgroundImage?=?null?;?
????????
public??Image?BackgroundImage
????????{
????????????
set
????????????{
????????????????
this.backgroundImage?=?value?;
????????????}
????????}
????????
#endregion
????????
????????
public?void?Erase()
????????{
????????????Rectangle?ret?
=?new?Rectangle(this.location.X??,?Location.Y,?this.source.Width?,this.source.Height?);
????????????
this.graphics.DrawImage(this.backgroundImage,?ret,?ret,?GraphicsUnit.Pixel);????????????
????????}

????????
public?void?Draw()
????????{
????????????
this.graphics.DrawImage(this.source,this.location.X?,this.location.Y);
????????}


????????
public?void?SetDirection(CompassDirections?dir)
????????{
????????????
this.direction?=?dir?;
????????}

????????
public?void?Move()
????????{
????????????
int?stepSize?=?5?;
????????????
if(this.direction?==?CompassDirections.South)
????????????{
????????????????
this.location.Y?+=?stepSize?;
????????????????
return?;
????????????}

????????????
if(this.direction?==?CompassDirections.East)
????????????{
????????????????
this.location.X?+=?stepSize?;
????????????????
return?;
????????????}

????????????
if(this.direction?==?CompassDirections.West)
????????????{
????????????????
this.location.X?-=?stepSize?;
????????????????
return?;
????????????}

????????????
if(this.direction?==?CompassDirections.North)
????????????{
????????????????
this.location.Y?-=?stepSize?;
????????????????
return?;
????????????}
????????}

????????
#endregion
????}

??? 我們要特別注意Erase方法的實(shí)現(xiàn),所謂Erase實(shí)際上就是用背景圖對應(yīng)的區(qū)域重新繪制精靈所在的區(qū)域表面--這種技巧在游戲編程中是最基本的技巧之一。

??? 當(dāng)精靈接收到游戲環(huán)境的時(shí)鐘脈沖通知時(shí),最常見的方法調(diào)用組合是:
??????????????? theSprite.Erase()?;
????????????????theSprite.Move()?;
????????????????theSprite.Draw()?;
??? 首先,精靈擦除自己,然后依據(jù)方向CompassDirections移動(dòng)自己到新的位置,最后在新的位置繪制自己。
??? 所以,當(dāng)時(shí)鐘脈沖連續(xù)不斷的到來時(shí),我們就可以看到精靈在移動(dòng)了,通過我們的游戲操縱桿或鍵盤我們可以調(diào)用目標(biāo)精靈的SetDirection方法來指定其要移動(dòng)的方向。

??? 下篇文章,我們將基于ISprite構(gòu)建一個(gè)最簡單的游戲示例,在這個(gè)示例中,我們可以通過鍵盤的方向鍵來控制游戲中主角的移動(dòng)。



總結(jié)

以上是生活随笔為你收集整理的游戏编程入门(1) -- 精灵 ISprite的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。