Programmerare, skeptiker, sekulärhumanist, antirasist.
Författare till bok om C64 och senbliven lantis.
Röstar pirat.
2010-01-02
There are some features of the XNA Framework that is unavailable from Visual Basic, but this should not stop you from writing descent games in Visual Basic. On my machine, I have installed XNA Game Studio 3.1 (a game developing environment from Microsoft) and I also have a beta of Visual Studio 2010 that I am going to use. This example will just contain the code necessary to get something on the screen, a sprite floating across.
From VS2010, I am using a regular console application and the target platform for the project is .NET Framework 3.5.
Now I must add two references: Microsoft.Xna.Framework and Microsoft.Xna.Framework.Game. I use version 3.1, the version that got installed when I installed XNA Game Studio 3.1.
The next step is to create the game class. I call my class TestGame. TestGame should inherit from the Microsoft.Xna.Framework.Game class. In here I create a Main method to get the program started, and I select that method to be the starting point for the program in the Project Settings window. This is the code so far:
Public Class TestGame Inherits Microsoft.Xna.Framework.Game Public Shared Sub Main() End Sub End Class
In the Main method, I create my game (the TestGame class) and from the constructor, a graphics device manager for the game. I use the graphics device manager to set my preferred resolution (800×600) and to switch to fullscreen mode. Note that I want to keep the reference to the graphics device manager as a member of my game class.
Public Class TestGame Inherits Microsoft.Xna.Framework.Game Private Gfx As Microsoft.Xna.Framework.GraphicsDeviceManager Public Shared Sub Main() Dim Game As New TestGame() Game.Run() End Sub Public Sub New() Me.Gfx = New Microsoft.Xna.Framework.GraphicsDeviceManager(Me) Me.Gfx.PreferredBackBufferWidth = 800 Me.Gfx.PreferredBackBufferHeight = 600 If Not Me.Gfx.IsFullScreen Then Me.Gfx.ToggleFullScreen() End If End Sub End Class
The next thing to do is some overrides from the base class. These methods will be overloaded:
Protected Overrides Sub Initialize() MyBase.Initialize() End Sub Protected Overrides Sub LoadContent() MyBase.LoadContent() End Sub Protected Overrides Sub UnloadContent() MyBase.UnloadContent() End Sub Protected Overrides Sub Update(ByVal gameTime As Microsoft.Xna.Framework.GameTime) MyBase.Update(gameTime) End Sub Protected Overrides Sub Draw(ByVal gameTime As Microsoft.Xna.Framework.GameTime) MyBase.Draw(gameTime) End Sub
Just to make something happen on the screen, I am adding these members:
Private Sb As Microsoft.Xna.Framework.Graphics.SpriteBatch Private SpriteTexture As Microsoft.Xna.Framework.Graphics.Texture2D Private SpriteX As Integer = 0 Private SpriteY As Integer = 0
The SpriteBatch will manage my sprites and the Texture2D is the sprite graphics. In the LoadContent function, I will load a sprite from my hard drive.
Protected Overrides Sub LoadContent() Me.Sb = New Microsoft.Xna.Framework.Graphics.SpriteBatch(Me.Gfx.GraphicsDevice) Me.SpriteTexture = Microsoft.Xna.Framework.Graphics.Texture2D.FromFile(Me.Gfx.GraphicsDevice, _ "mysprite.png") MyBase.LoadContent() End Sub
The Update function is for changing the game scenery.
Protected Overrides Sub Update(ByVal gameTime As Microsoft.Xna.Framework.GameTime) SpriteX += 1 SpriteY += 1 MyBase.Update(gameTime) End Sub
And the Draw function is for screen rendering.
Protected Overrides Sub Draw(ByVal gameTime As Microsoft.Xna.Framework.GameTime) Me.Gfx.GraphicsDevice.Clear(Microsoft.Xna.Framework.Graphics.Color.Black) Me.Sb.Begin(Microsoft.Xna.Framework.Graphics.SpriteBlendMode.AlphaBlend) Me.Sb.Draw(Me.SpriteTexture, New Microsoft.Xna.Framework.Rectangle(Me.SpriteX, Me.SpriteY, 32, 32), _ Microsoft.Xna.Framework.Graphics.Color.Red) Me.Sb.End() MyBase.Draw(gameTime) End Sub
This is the complete code that produces a sprite that floats over the screen in Visual Basic using XNA:
Public Class TestGame Inherits Microsoft.Xna.Framework.Game Private Gfx As Microsoft.Xna.Framework.GraphicsDeviceManager Private Sb As Microsoft.Xna.Framework.Graphics.SpriteBatch Private SpriteTexture As Microsoft.Xna.Framework.Graphics.Texture2D Private SpriteX As Integer = 0 Private SpriteY As Integer = 0 Public Shared Sub Main() Dim Game As New TestGame() Game.Run() End Sub Public Sub New() Me.Gfx = New Microsoft.Xna.Framework.GraphicsDeviceManager(Me) Me.Gfx.PreferredBackBufferWidth = 800 Me.Gfx.PreferredBackBufferHeight = 600 If Not Me.Gfx.IsFullScreen Then Me.Gfx.ToggleFullScreen() End If End Sub Protected Overrides Sub Initialize() MyBase.Initialize() End Sub Protected Overrides Sub LoadContent() Me.Sb = New Microsoft.Xna.Framework.Graphics.SpriteBatch(Me.Gfx.GraphicsDevice) Me.SpriteTexture = Microsoft.Xna.Framework.Graphics.Texture2D.FromFile(Me.Gfx.GraphicsDevice, _ "mysprite.png") MyBase.LoadContent() End Sub Protected Overrides Sub UnloadContent() MyBase.UnloadContent() End Sub Protected Overrides Sub Update(ByVal gameTime As Microsoft.Xna.Framework.GameTime) SpriteX += 1 SpriteY += 1 MyBase.Update(gameTime) End Sub Protected Overrides Sub Draw(ByVal gameTime As Microsoft.Xna.Framework.GameTime) Me.Gfx.GraphicsDevice.Clear(Microsoft.Xna.Framework.Graphics.Color.Black) Me.Sb.Begin(Microsoft.Xna.Framework.Graphics.SpriteBlendMode.AlphaBlend) Me.Sb.Draw(Me.SpriteTexture, New Microsoft.Xna.Framework.Rectangle(Me.SpriteX, Me.SpriteY, 32, 32), _ Microsoft.Xna.Framework.Graphics.Color.Red) Me.Sb.End() MyBase.Draw(gameTime) End Sub End Class
Categories: VB.NET, Visual Studio 10
Tags: Game development
Bjud mig på en kopp kaffe (20:-) som tack för bra innehåll!
Leave a Reply