日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

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

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

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


??? 對(duì)應(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)來(lái),甚至CompassDirections枚舉還可以進(jìn)一步細(xì)分,但是ISprite已經(jīng)有最基本“精靈”功能了。對(duì)于大多數(shù)簡(jiǎn)單的任務(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í)際上就是用背景圖對(duì)應(yīng)的區(qū)域重新繪制精靈所在的區(qū)域表面--這種技巧在游戲編程中是最基本的技巧之一。

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

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



總結(jié)

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

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