Page 1 of 1

Using XNA in VB.NET Part II

#1 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 463
  • View blog
  • Posts: 4,255
  • Joined: 25-March 09

Post icon  Posted 15 July 2011 - 07:24 AM

Using XNA in VB.NET Part II
Drawing Sprite

Using XNA in VB.NET Part I

Hello everyone :)

In this tutorial i will show you how to draw a sprite in XNA using VB.NET and we will make it bounce back from the edges. My goal here is this tutorials to look as much as possible as if your going to write it in C# so if you understand them correct it will be easier for you to take any C# example and kinda use them in VB.NET. Now lets start with our tutorial. So to draw a sprite we need 2 thing Texture and Vector. Texture will represent the image that will be displayed and the Vector is used for the position of the Sprite. Now since we will be showing 2D sprite we will use Texture2D and Vector2. To make the sprite to move we will use Vector2 as the speed for the sprite. So in our class scope we need to add this variables:
Public Class Game
    Inherits Microsoft.Xna.Framework.Game
    'Fields in our game graphic manager etc'
    Private graphics As GraphicsDeviceManager
    Private spriteBatch As SpriteBatch
    'Texture that we will render'
    Private mTexture As Texture2D
    'Set the coordinates to draw the sprite at'
    Private spritePos As Vector2 = Vector2.Zero
    'X and Y speed of the sprite'
    Private XSpeed As Single = 80
    Private YSpeed As Single = 120
    'Vector2 used for the speed of the sprite'
    Private spriteSpeed As New Vector2(XSpeed, YSpeed)
...



So now before we load the contents i have to say that i did not find a way to use contents in VB.NET so instead i will be using Texture2.FromStream() method to load the texture for our sprite. I`m using VS2010 in 2008 i think its Texture2.FromFile() instead FromStream() but i`m not really sure. However here is how to load the texture. In the LoadContent() method we load the picture like so:

    Protected Overrides Sub LoadContent()
        ' TODO: use this.Content to load your game content here
        MyBase.LoadContent()
        ' Create a new SpriteBatch, which can be used to draw textures.
        spriteBatch = New SpriteBatch(GraphicsDevice)
        'Load the texture'
        'We are using Stream since i couldn't find how to make content in VB'
        Dim textureStream As System.IO.Stream = New System.IO.StreamReader(Application.StartupPath & "\Textures\VBNETGame.png").BaseStream
        'Loading the texture'
        mTexture = Texture2D.FromStream(GraphicsDevice, textureStream)

    End Sub



Now i`m using Application.StartupPath but you can just use "\Textures\VBNETGame.png" instead but i guess its a habit of mine to use it. Now i`m using this blob looking thing i will attach it at the end so you can download it too. I made the background transparent using GIMP and save it as png and put it in "\XNAGame\bin\Debug\Textures" where XNAGame is my project. So now that we have loaded the texture we will draw it. Here is how we draw the texture in our Draw() method:

    Protected Overrides Sub Draw(ByVal gameTime As Microsoft.Xna.Framework.GameTime)
        GraphicsDevice.Clear(Color.CornflowerBlue)
        'TODO: Add your drawing code here'
        'Draw the sprite'
        spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend)
        spriteBatch.Draw(mTexture, spritePos, Color.White)
        spriteBatch.End()
        MyBase.Draw(gameTime)
    End Sub



Now if you just run your program as it is you will have to see the blob thing in the upper left corner like so:
Attached Image

So now we have drawn our first sprite using XNA in VB.NET now to make it move and bounce of the edges. For this we will make a method that i called UpdateSprite:

    Private Sub UpdateSprite(ByVal gameTime As GameTime)
        'The function move the sprite and check if its on the limits of the screen so it can bounce back'
        Dim intMaxX As Integer
        Dim intMinX As Integer = 0
        Dim intMaxY As Integer
        Dim intMinY As Integer = 0

        'move the sprite by speed scaled by elapsed time'
        spritePos += spriteSpeed * CSng(gameTime.ElapsedGameTime.TotalSeconds)
        'Get the max. X and Y coordinates'
        intMaxX = graphics.GraphicsDevice.Viewport.Width - mTexture.Width
        intMaxY = graphics.GraphicsDevice.Viewport.Height - mTexture.Height
        'Check the sprite if its at some of the edges of the screen and then reverce the speed of the sprite'
        If spritePos.X > intMaxX Then
            'Check if the sprite is at the maximum X coordinates of the screen if so reverse the speed'
            spriteSpeed.X *= -1
            spritePos.X = intMaxX

        ElseIf spritePos.X < intMinX Then
            'Check if the sprite is at the minimum X coordinates of the scree and reverse the speed'
            spriteSpeed.X *= -1
            spritePos.X = intMinX
        End If

        If spritePos.Y > intMaxY Then
            'Check if the sprite is at the maximum Y coordinates of the screen if so reverse the speed'
            spriteSpeed.Y *= -1
            spritePos.Y = intMaxY

        ElseIf spritePos.Y < intMinY Then
            'Check if the sprite is at the minimum Y coordinates of the scree and reverse the speed'
            spriteSpeed.Y *= -1
            spritePos.Y = intMinY
        End If

    End Sub



Now as you can see what we do here is first move the sprite on the screen scaled by the elapsed time and then check where our sprite is at the screen. if he is at any of the edges we reverse the speed (according to at what edge is X or Y). Now that we create this method we need to add it in the Update() method like so:

    Protected Overrides Sub Update(ByVal gameTime As Microsoft.Xna.Framework.GameTime)
        'Allows the game to exit'
        If GamePad.GetState(PlayerIndex.One).Buttons.Back = ButtonState.Pressed Then
            Me.Exit()
        End If
        'TODO: Add your update logic here'
        'The method that will update our sprite position'
        UpdateSprite(gameTime)
        MyBase.Update(gameTime)
    End Sub



Now if you run your program you will see the sprite moving across the screen and if it is near some of the edges it will bounce back and move the other direction. Here is the whole code in the game class:

Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports Microsoft.Xna.Framework
Imports Microsoft.Xna.Framework.Audio
Imports Microsoft.Xna.Framework.Content
Imports Microsoft.Xna.Framework.GamerServices
Imports Microsoft.Xna.Framework.Graphics
Imports Microsoft.Xna.Framework.Input
Imports Microsoft.Xna.Framework.Media

Public Class Game
    Inherits Microsoft.Xna.Framework.Game
    'Fields in our game graphic manager etc'
    Private graphics As GraphicsDeviceManager
    Private spriteBatch As SpriteBatch
    'Texture that we will render'
    Private mTexture As Texture2D
    'Set the coordinates to draw the sprite at'
    Private spritePos As Vector2 = Vector2.Zero
    'X and Y speed of the sprite'
    Private XSpeed As Single = 80
    Private YSpeed As Single = 120
    'Vector2 used for the speed of the sprite'
    Private spriteSpeed As New Vector2(XSpeed, YSpeed)
    Public Sub New()
        graphics = New GraphicsDeviceManager(Me)

    End Sub

    Protected Overrides Sub Initialize()
        'TODO: Add your initialization logic here'
        MyBase.Initialize()
    End Sub
    Protected Overrides Sub LoadContent()
        ' TODO: use this.Content to load your game content here
        MyBase.LoadContent()
        ' Create a new SpriteBatch, which can be used to draw textures.
        spriteBatch = New SpriteBatch(GraphicsDevice)
        'Load the texture'
        'We are using Stream since i couldn't find how to make content in VB'
        Dim textureStream As System.IO.Stream = New System.IO.StreamReader(Application.StartupPath & "\Textures\VBNETGame.png").BaseStream
        'Loading the texture'
        mTexture = Texture2D.FromStream(GraphicsDevice, textureStream)

    End Sub
    Protected Overrides Sub UnloadContent()
        MyBase.UnloadContent()
        'TODO: Unload any non ContentManager content here
        mTexture.Dispose()
        spritePos = Nothing
        spriteSpeed = Nothing
    End Sub
    Protected Overrides Sub Update(ByVal gameTime As Microsoft.Xna.Framework.GameTime)
        'Allows the game to exit'
        If GamePad.GetState(PlayerIndex.One).Buttons.Back = ButtonState.Pressed Then
            Me.Exit()
        End If
        'TODO: Add your update logic here'
        'The method that will update our sprite position'
        UpdateSprite(gameTime)
        MyBase.Update(gameTime)
    End Sub
    Private Sub UpdateSprite(ByVal gameTime As GameTime)
        'The function move the sprite and check if its on the limits of the screen so it can bounce back'
        Dim intMaxX As Integer
        Dim intMinX As Integer = 0
        Dim intMaxY As Integer
        Dim intMinY As Integer = 0

        'move the sprite by speed scaled by elepsed time'
        spritePos += spriteSpeed * CSng(gameTime.ElapsedGameTime.TotalSeconds)
        'Get the max. X and Y coordinates'
        intMaxX = graphics.GraphicsDevice.Viewport.Width - mTexture.Width
        intMaxY = graphics.GraphicsDevice.Viewport.Height - mTexture.Height
        'Check the sprite if its at some of the edges of the screen and then reverce the speed of the sprite'
        If spritePos.X > intMaxX Then
            'Check if the sprite is at the maximum X coordinates of the screen if so reverce the speed'
            spriteSpeed.X *= -1
            spritePos.X = intMaxX

        ElseIf spritePos.X < intMinX Then
            'Check if the sprite is at the minimum X coordinates of the scree and reverce the speed'
            spriteSpeed.X *= -1
            spritePos.X = intMinX
        End If

        If spritePos.Y > intMaxY Then
            'Check if the sprite is at the maximum Y coordinates of the screen if so reverce the speed'
            spriteSpeed.Y *= -1
            spritePos.Y = intMaxY

        ElseIf spritePos.Y < intMinY Then
            'Check if the sprite is at the minimum Y coordinates of the scree and reverce the speed'
            spriteSpeed.Y *= -1
            spritePos.Y = intMinY
        End If

    End Sub
    Protected Overrides Sub Draw(ByVal gameTime As Microsoft.Xna.Framework.GameTime)
        GraphicsDevice.Clear(Color.CornflowerBlue)
        'TODO: Add your drawing code here'
        'Draw the sprite'
        spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend)
        spriteBatch.Draw(mTexture, spritePos, Color.White)
        spriteBatch.End()
        MyBase.Draw(gameTime)
    End Sub
End Class


I hope you will find this tutorial helpful and you will start using XNA with your favorite language :)
Below is the texture i have used
Attached Image

Is This A Good Question/Topic? 0
  • +

Replies To: Using XNA in VB.NET Part II

#2 piman314  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 32
  • View blog
  • Posts: 169
  • Joined: 07-August 09

Posted 23 July 2011 - 06:29 PM

The code for loading the texture via Content is
Content.Load(Of Texture2D)("\Textures\VBNETGame.png")

Was This Post Helpful? 0
  • +
  • -

#3 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 463
  • View blog
  • Posts: 4,255
  • Joined: 25-March 09

Posted 24 July 2011 - 06:10 AM

well yes that is the VB.NET represent on how to load texture using contents. maybe i should elaborate why i didn't used contents. contents in XNA 4 is like separated project and i find it rather hard and not worth trying to make it work in VB.NET while you can just use Texture2D.FromStream() method. Now i am by far no expert in game programming i write several stuff. the main reason why i make them tutorials and not blog or something like that is because i wanted second pair of eyes to look at them before its posted. :)

This post has been edited by NoBrain: 24 July 2011 - 06:12 AM

Was This Post Helpful? 0
  • +
  • -

#4 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 463
  • View blog
  • Posts: 4,255
  • Joined: 25-March 09

Posted 29 December 2011 - 10:41 AM

part 3 is now approved you should check it out if you find this helpful.
Using XNA In VB.NET Part III
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1