2011年5月7日 星期六

用vb.net 開發 XNA 遊戲

一般常見的 C# + XNA ,現在介紹一下不同口味的東西,讓熟悉VB世界的人,也能盡情奔馳在XNA的世界裡

首先開一個很普通的VB.net windows From 的專案
接下來設定屬性,把『啟用應用程式架構』取消掉

移除掉不必要的參考『System.Drawing』

加入參考『Microsorft.XNA.Framework』、『Microsorft.XNA.Framework.Pipeline』、『Microsorft.XNA.Framework.Game』
接下來專案裡應該有這三樣參考

把以下這5樣東西匯入命名空間裡『Microsorft.XNA.Framework』、『Microsorft.XNA.Framework.Aduio』、『Microsorft.XNA.Framework.Content』、『Microsorft.XNA.Framework.Graphic』、『Microsorft.XNA.Framework.Input』

接下來加入一個模組,名字自取,這裡取名用預設值『Module1』
接下來加入一個類別,這裡取名為『MyGame』

在MyGame.vb裡,放入以下程式碼
Public Class MyGame
    Inherits Microsoft.Xna.Framework.Game

    Dim Graphics As GraphicsDeviceManager
    Protected sprite As SpriteBatch

    Sub New()
        Graphics = New GraphicsDeviceManager(Me)
        Content.RootDirectory = "Content"
    End Sub

    Protected Overrides Sub Initialize()
        MyBase.Initialize()
    End Sub

    Protected Overrides Sub LoadContent()
        sprite = New SpriteBatch(GraphicsDevice)
        'MyBase.LoadContent()
    End Sub
    Protected Overrides Sub UnloadContent()
        'MyBase.UnloadContent()
    End Sub

    Protected Overrides Sub Update(ByVal gameTime As Microsoft.Xna.Framework.GameTime)
        If GamePad.GetState(PlayerIndex.One).Buttons.Back = Input.ButtonState.Pressed Then
            End
        End If
        MyBase.Update(gameTime)
    End Sub

    Protected Overrides Sub Draw(ByVal gameTime As Microsoft.Xna.Framework.GameTime)
        MyBase.Draw(gameTime)
        Graphics.GraphicsDevice.Clear(Color.Green)
    End Sub
End Class

接下來回到Module1,放入以下程式碼

Module Module1
    Public Game1 As MyGame

    Public Sub Main()

        Game1 = New MyGame
        Game1.Run()
    End Sub
End Module
回到應用程式屬性頁面,把起始物件改成『Sub Main』

按下『F5』,執行看看

大功告成,讓遊戲真的能玩,你需要多下些工夫