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
Oct 302013
 

First of all, and this is where beginners could get confused, there is no form in an XNA VB.Net app.
You actually need a module with a main sub calling the run method of your XNA class.

You need to make a class that will inherit from Inherits Microsoft.Xna.Framework.Game and in there you will have to override a couple of methods.

The basic steps/methods are
1-initialise variables in Initialize
2-load your content (audio, fonts, textures) in loadcontent
3-put your update logic in update (check inputs, set new coordinates for a sprite, etc)
4-(re)draw your window in draw

And thats basically it, you are ready to take advantage of all the classes and methods of the XNA framework 🙂

Next article will show a working XNA app.

To make it easy on you, at the end of this article is a template project for vs2013 : copy the zip file in C:\Users\username\Documents\Visual Studio 2013\Templates\ProjectTemplates and VS2013 will propose you to create a XNA project from scratch.

See below for a skeleton class.


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

Public Sub New()
graphics = New GraphicsDeviceManager(Me)
Window.Title = "Test"
graphics.PreferredBackBufferHeight = 300
graphics.PreferredBackBufferWidth = 300
End Sub

Protected Overrides Sub Initialize()
'TODO: Add your initialization logic here'
'...
MyBase.Initialize()
End Sub

Protected Overrides Sub LoadContent()
MyBase.LoadContent()
' TODO: use this.Content to load your game content here'
'...
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'
'...
MyBase.Draw(gameTime)
End Sub
End Class

VS2013 template

xna0

 Posted by at 21 h 36 min
Oct 302013
 

I am usually not a pc gaming person nor I am into vb.net langage (thus I used to teach it years ago).
Still, lately I could see my 2 kids writing games in VB.net.
And of course, as « Daddy » is a dev guy, he sure knows how to handle scrolling, sprites, pixel collision, etc.

Well it happens not (at all!) 🙂

Hence me looking closer at this platform, reading some nice things around XNA (a framework for dotnet) and deciding to give it a go.

I will therefore post in the coming days a serie of article illustrating some very basics that should beginners to start writing some tiny games.

First things first, you will need :
Visual Studio Express 2013 windows desktop edition (free)
the XNA assemblies (Game Studio 4)
Dotnet 4.x (will be installed with VS2013)
XNA runtime (needed also when youo distribute your executable).

Note that XNA is end of life (MS will drop support on April 2014).
However this is a good framework to practice and learn gaming tips and tricks.
Plus, there is a future is MonoGame : an open source implementation of the Microsoft XNA 4.x Framework .

Regards,
Erwan