Nov 052013
 

One article for today : lets build on what have seen so far and lets have a bouncing ball with a background and some sound when the ball hit the walls.

As always we load contents (textures and sound), we update our variables (position and direction), and we finally draw.
By now, you probably got the idea 🙂

The project files : xna_demo_4


Imports Microsoft.Xna.Framework
Imports Microsoft.Xna.Framework.Graphics
Imports Microsoft.Xna.Framework.Audio
Imports Microsoft.Xna.Framework.Content
Imports Microsoft.Xna.Framework.Media
Imports Microsoft.Xna.Framework.Input

Public Class Game
Inherits Microsoft.Xna.Framework.Game
'Fields in our game graphic manager etc'
Dim graphics As GraphicsDeviceManager
Dim 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)
'sound
Private mysound As SoundEffect
'background
Private backgroundTexture As Texture2D

Public Sub New()
graphics = New GraphicsDeviceManager(Me)
'graphics.ToggleFullScreen()
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'
mTexture = Content.Load(Of Texture2D)("bille")
'load sound
mysound = Content.Load(Of SoundEffect)("sleep")
'load background
backgroundTexture = Content.Load(Of Texture2D)("background2")
End Sub

Protected Overrides Sub UnloadContent()
MyBase.UnloadContent()
'TODO: Unload any non ContentManager content here'
End Sub

Protected Overrides Sub Update(ByVal gameTime As Microsoft.Xna.Framework.GameTime)
'TODO: Add your update logic here'
'The method that will update our sprite position'
UpdateSprite(gameTime)
'
MyBase.Update(gameTime)
End Sub

Protected Overrides Sub Draw(ByVal gameTime As Microsoft.Xna.Framework.GameTime)
'GraphicsDevice.Clear(Color.CornflowerBlue)
'TODO: Add your drawing code here'
Dim mainFrame As New Rectangle(0, 0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height)
'Draw the sprite'
spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend)
spriteBatch.Draw(backgroundTexture, mainFrame, Color.White)
spriteBatch.Draw(mTexture, spritePos, Color.White)
spriteBatch.End()
'TODO: Add your drawing code here'
MyBase.Draw(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 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
mysound.Play()
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 mysound.Play() 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
mysound.Play()

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 mysound.Play() End If End Sub End Class

 Posted by at 22 h 24 min
Nov 042013
 

A quick one : lets adapt the last example (VB.Net and XNA : Article 4) and lets illustrate how to use the mouse input.
Here our texture will move around as we move the mouse.

Here are the project files : xna_demo_3.1

And below the code to review. Look at the updatemouse method.

Imports Microsoft.Xna.Framework
Imports Microsoft.Xna.Framework.Graphics
Imports Microsoft.Xna.Framework.Audio
Imports Microsoft.Xna.Framework.Content
Imports Microsoft.Xna.Framework.Media
Imports Microsoft.Xna.Framework.Input

Public Class Game
Inherits Microsoft.Xna.Framework.Game
'Fields in our game graphic manager etc'
Dim graphics As GraphicsDeviceManager
'
Dim font1 As SpriteFont
Dim msg As String
'
Dim 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

Public Sub New()
graphics = New GraphicsDeviceManager(Me)
'graphics.ToggleFullScreen()
End Sub

Protected Overrides Sub Initialize()
'TODO: Add your initialization logic here'
MyBase.Initialize()
msg = "hello world"
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)
'Loading the texture'
mTexture = Content.Load(Of Texture2D)("bille")
'lets create a font
font1 = Content.Load(Of SpriteFont)("myfont")
End Sub
Protected Overrides Sub UnloadContent()
MyBase.UnloadContent()
'TODO: Unload any non ContentManager content here'
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'
updatemouse()
'
MyBase.Update(gameTime)
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.DrawString(font1, msg, New Vector2(20.0F, 20.0F), Color.White)
spriteBatch.Draw(mTexture, spritePos, Color.White)
spriteBatch.End()
MyBase.Draw(gameTime)
End Sub

Private Sub updatemouse()
Dim newState As MouseState
newState = Mouse.GetState
spritePos.X = newState.X
spritePos.Y = newState.Y
'
msg = spritePos.X & "," & spritePos.Y
End Sub
End Class

 Posted by at 22 h 57 min
Nov 032013
 

This time, lets see how to use the keyboard input to move a texture around since the update and draw event have no secrets for us anymore 🙂
And from now on, we’ll use XNA content files (see previous article).

Lets use the KeyboardState class and its GetState method.
And upon specific keypress (if newState.IsKeyDown(Keys.Right) …) lets update the X and Y of our texture.

As always, the full project : xna_demo_3 .


Imports Microsoft.Xna.Framework
Imports Microsoft.Xna.Framework.Graphics
Imports Microsoft.Xna.Framework.Audio
Imports Microsoft.Xna.Framework.Content
Imports Microsoft.Xna.Framework.Media
Imports Microsoft.Xna.Framework.Input

Public Class Game
Inherits Microsoft.Xna.Framework.Game
'Fields in our game graphic manager etc'
Dim graphics As GraphicsDeviceManager
'
Dim font1 As SpriteFont
Dim msg As String
'
Dim 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

Public Sub New()
graphics = New GraphicsDeviceManager(Me)
'graphics.ToggleFullScreen()

End Sub

Protected Overrides Sub Initialize()
'TODO: Add your initialization logic here'
MyBase.Initialize()
msg = "hello world"
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)
'Loading the texture'
mTexture = Content.Load(Of Texture2D)("bille")
'lets create a font
font1 = Content.Load(Of SpriteFont)("myfont")

End Sub
Protected Overrides Sub UnloadContent()
MyBase.UnloadContent()
'TODO: Unload any non ContentManager content here'
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'

updateinput(gameTime)

MyBase.Update(gameTime)

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.DrawString(font1, msg, New Vector2(20.0F, 20.0F), Color.White)
spriteBatch.Draw(mTexture, spritePos, Color.White)
spriteBatch.End()
MyBase.Draw(gameTime)
End Sub

Private Sub updateinput(ByVal gameTime As GameTime)
Dim newState As KeyboardState
newState = Keyboard.GetState

If (newState.IsKeyDown(Keys.Right)) Then
spritePos.X = spritePos.X + 10
msg = "right " & spritePos.X & "," & spritePos.Y
End If

If (newState.IsKeyDown(Keys.Left)) Then
spritePos.X = spritePos.X - 10
msg = "left " & spritePos.X & "," & spritePos.Y
End If

If (newState.IsKeyDown(Keys.Up)) Then
spritePos.Y = spritePos.Y - 10
msg = "up " & spritePos.X & "," & spritePos.Y
End If

If (newState.IsKeyDown(Keys.Down)) Then
spritePos.Y = spritePos.Y + 10
msg = "down " & spritePos.X & "," & spritePos.Y
End If
End Sub
End Class

 Posted by at 23 h 09 min
Nov 032013
 

In previous article, we have seen a basic XNA application illustrating how to load a texture and how to move it around.

Lets see now how to write a text : load a spritefont as content, and use the drawstring method.

First lets look at a spritefont (in the zip file).
This is a simple XML file where you describe the font you want to use : font name, size, etc.

But you cannot use use as is in your vb.net XNA code.
You need to turn it into a XNA content (XNB file -> myfont.xnb next to exe file).
As my VS2013 does not manage XNA projects and contents, I use XNA Content Compiler.
Choose content type, select myfont.spritefont, input the destination folder and press compile !

Attached to this post the vb.net project files. xna_demo_2

Below the code for a quick review.


Imports Microsoft.Xna.Framework
Imports Microsoft.Xna.Framework.Graphics
Imports Microsoft.Xna.Framework.Audio
Imports Microsoft.Xna.Framework.Content
Imports Microsoft.Xna.Framework.Media
Imports Microsoft.Xna.Framework.Input

Public Class Game
Inherits Microsoft.Xna.Framework.Game
'Fields in our game graphic manager etc'
Dim graphics As GraphicsDeviceManager
'
Dim SpriteBatch As SpriteBatch
Dim font1 As SpriteFont
'
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)
'lets create a font
font1 = Content.Load(Of SpriteFont)("myfont")
End Sub
Protected Overrides Sub UnloadContent()
MyBase.UnloadContent()
'TODO: Unload any non ContentManager content here'
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'
MyBase.Update(gameTime)
End Sub
Protected Overrides Sub Draw(ByVal gameTime As Microsoft.Xna.Framework.GameTime)
GraphicsDevice.Clear(Color.CornflowerBlue)
'TODO: Add your drawing code here'
'write text
SpriteBatch.Begin()
SpriteBatch.DrawString(font1, "Ceci est un Test !!!", New Vector2(20.0F, 20.0F), Color.White)
SpriteBatch.End()
'
MyBase.Draw(gameTime)
End Sub
End Class

xna03

 Posted by at 18 h 12 min
Nov 022013
 

In previous articles, we have seen what is needed to code with XNA and also the basics of an XNA class.

Lets now have a look at the small XNA app below : a bouncing ball.

-We declare some global variables
-we load our content (here : a sprite = a bleu ball) in the loadcontent xna method
-we call our updatesprite method in the update xna method which will take care of the moving position
-we draw our sprite in the draw xna method using the update spritepos

Attached to this post is the complete source code and compiled executable : xna_demo_1

Enjoy !


Imports Microsoft.Xna.Framework
Imports Microsoft.Xna.Framework.Graphics
Imports Microsoft.Xna.Framework.Input

Public Class Game
Inherits Microsoft.Xna.Framework.Game
'Fields in our game graphic manager etc'
Dim graphics As GraphicsDeviceManager
Dim 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()
MyBase.Initialize()
End Sub

Protected Overrides Sub LoadContent()
MyBase.LoadContent()
' Create a new SpriteBatch, which can be used to draw textures.'
spriteBatch = New SpriteBatch(GraphicsDevice)
'
'Load the texture'
'We are using Stream , not a content (xnb format)
Dim textureStream As System.IO.Stream = New System.IO.StreamReader(Application.StartupPath + "\bille_bleu1.png").BaseStream
'Loading the texture'
mTexture = Texture2D.FromStream(GraphicsDevice, textureStream)
End Sub

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

Protected Overrides Sub Update(ByVal gameTime As Microsoft.Xna.Framework.GameTime)
'Allows the game to exit'
Dim newState As KeyboardState = Keyboard.GetState
If newState.IsKeyDown(Keys.Q) Then Application.Exit()
'The method that will update our sprite position'
UpdateSprite(gameTime)
MyBase.Update(gameTime)
End Sub

Protected Overrides Sub Draw(ByVal gameTime As Microsoft.Xna.Framework.GameTime)
GraphicsDevice.Clear(Color.CornflowerBlue)
'Draw the sprite'
spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend)
spriteBatch.Draw(mTexture, spritePos, Color.White)
spriteBatch.End()
MyBase.Draw(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 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 End Class

 Posted by at 12 h 53 min