游戏编程入门(1) -- 精灵 ISprite
生活随笔
收集整理的這篇文章主要介紹了
游戏编程入门(1) -- 精灵 ISprite
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
??? 對于游戲編程而言,我也是個初學者,這個游戲編程入門系列的文章,就當作是我在學習游戲編程的筆記和階段小結(jié)吧。我們先從最簡單的“精靈”開始,暫時我們不需要考慮DirectX或是OpenGL,不需要考慮3維等等這些復雜情形,直接使用GDI+繪圖功能就可以了。
??? 精靈,是構(gòu)成游戲中活動體(比如,飛機、野獸等游戲人物)的最基本單元,任何一個活動體都可以由一個或多個精靈組合而成,每個精靈都是一個對象實例,它能夠繪制自己、移動(更復雜的還可以旋轉(zhuǎn))等等基本動作。
??? 我讓所有的精靈都實現(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
????}
??? 這個接口也許并不是完整的,隨了實際的深入,可能還會有很多的元素添加進來,甚至CompassDirections枚舉還可以進一步細分,但是ISprite已經(jīng)有最基本“精靈”功能了。對于大多數(shù)簡單的任務(wù),我們已經(jīng)可以給出ISprite的一個實現(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方法的實現(xiàn),所謂Erase實際上就是用背景圖對應(yīng)的區(qū)域重新繪制精靈所在的區(qū)域表面--這種技巧在游戲編程中是最基本的技巧之一。
??? 當精靈接收到游戲環(huán)境的時鐘脈沖通知時,最常見的方法調(diào)用組合是:
??????????????? theSprite.Erase()?;
????????????????theSprite.Move()?;
????????????????theSprite.Draw()?; ??? 首先,精靈擦除自己,然后依據(jù)方向CompassDirections移動自己到新的位置,最后在新的位置繪制自己。
??? 所以,當時鐘脈沖連續(xù)不斷的到來時,我們就可以看到精靈在移動了,通過我們的游戲操縱桿或鍵盤我們可以調(diào)用目標精靈的SetDirection方法來指定其要移動的方向。
??? 下篇文章,我們將基于ISprite構(gòu)建一個最簡單的游戲示例,在這個示例中,我們可以通過鍵盤的方向鍵來控制游戲中主角的移動。
??? 精靈,是構(gòu)成游戲中活動體(比如,飛機、野獸等游戲人物)的最基本單元,任何一個活動體都可以由一個或多個精靈組合而成,每個精靈都是一個對象實例,它能夠繪制自己、移動(更復雜的還可以旋轉(zhuǎn))等等基本動作。
??? 我讓所有的精靈都實現(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
????}
??? 這個接口也許并不是完整的,隨了實際的深入,可能還會有很多的元素添加進來,甚至CompassDirections枚舉還可以進一步細分,但是ISprite已經(jīng)有最基本“精靈”功能了。對于大多數(shù)簡單的任務(wù),我們已經(jīng)可以給出ISprite的一個實現(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方法的實現(xiàn),所謂Erase實際上就是用背景圖對應(yīng)的區(qū)域重新繪制精靈所在的區(qū)域表面--這種技巧在游戲編程中是最基本的技巧之一。
??? 當精靈接收到游戲環(huán)境的時鐘脈沖通知時,最常見的方法調(diào)用組合是:
??????????????? theSprite.Erase()?;
????????????????theSprite.Move()?;
????????????????theSprite.Draw()?; ??? 首先,精靈擦除自己,然后依據(jù)方向CompassDirections移動自己到新的位置,最后在新的位置繪制自己。
??? 所以,當時鐘脈沖連續(xù)不斷的到來時,我們就可以看到精靈在移動了,通過我們的游戲操縱桿或鍵盤我們可以調(diào)用目標精靈的SetDirection方法來指定其要移動的方向。
??? 下篇文章,我們將基于ISprite構(gòu)建一個最簡單的游戲示例,在這個示例中,我們可以通過鍵盤的方向鍵來控制游戲中主角的移動。
總結(jié)
以上是生活随笔為你收集整理的游戏编程入门(1) -- 精灵 ISprite的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 关于membership的进一步理解
- 下一篇: Asp中一些FSO方面的函数