Nov 292013
 

There are times where you need to get text input from the player like his name, etc.

Although this is possible to do it with XNA only, it is a bit of a hassle : you need to put a timer between each keystroke, etc and result is not perfect.

Lets use Nuclex framework.
Yes the same Nuclex we used in a previous article for gamepad inputs.

Code is rather simpler : we add a reference to nuclex framework, add an event handler and finally draw our string on screen. As simple as that !

Source is there : .XNA_DEMO_24
See vide lower in this article.


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
Imports Nuclex.Input

Public Class Game
Inherits Microsoft.Xna.Framework.Game
Dim graphics As GraphicsDeviceManager
Dim SpriteBatch As SpriteBatch
Dim input As InputManager
Dim myfont As SpriteFont
Dim msg As String
Dim dummyTexture As Texture2D
Public Sub New()
graphics = New GraphicsDeviceManager(Me)
input = New InputManager(Services, Window.Handle)
Components.Add(input)
Window.Title = "Test"
End Sub
Protected Overrides Sub Initialize()
MyBase.Initialize()
AddHandler input.GetKeyboard.CharacterEntered, AddressOf keyboardCharacterEntered
End Sub
Protected Overrides Sub LoadContent()
SpriteBatch = New SpriteBatch(GraphicsDevice)
myfont = Content.Load(Of SpriteFont)("xnb\myfont")
MyBase.LoadContent()
End Sub
Private Sub keyboardCharacterEntered(character As Char)
If Asc(character) = 8 Then
If msg.Length > 0 Then msg = msg.Remove(msg.Length - 1)
Else
If Asc(character) >= 32 And Asc(character) <= 126 Then msg += character End If 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) GraphicsDevice.Clear(Color.CornflowerBlue) SpriteBatch.Begin() If msg <> "" Then SpriteBatch.DrawString(myfont, msg, New Vector2(10, 10), Color.White)
SpriteBatch.End()
MyBase.Draw(gameTime)
End Sub
End Class

 Posted by at 15 h 00 min
Nov 282013
 

In article 13, we made our first steps with Farseer Physics Engine (FPE).
We had create a world and 2 objects (a floor and boxes going thru gravity).

This time, lets create a body from a texture (no more a simple shape like a rectanle).
Lets also add he ability to move that body around with our mouse by using a mouse joint.

Have a look at the method CreateFromTexture in the DrawablePhysicObject class : in short, it creates a polygon from a texture.
Have a look at the update method where we use a FixedMouseJoint.
Also, see how easy it is to add extra objects like 2 extra floors.

Side note, finding documentation on FPE can sometimes be tedious, especially on latest 3.5 version where significant changes were introduced.
Still, here is a good start (although meant for 3.3).
Also, the box2d manual is usefull to understand concepts.
And to close this parenthesis around documentation, the farseer samples are very instructive as well.

Look at the video below to illustrate all this : body from texture, mouse joint.

 Posted by at 22 h 34 min
Nov 252013
 

One thing that gives makes a game more dynamic is animated sprite.
Indeed, moving a fix shape all over the screen is nice. Moving an animated shape is better 🙂

Lets start with the below sprite sheet (yes it is link – hope no one will sue me for copyright infringement, this is only for educating purpose).

spritesheet

What we have here is 8 28*32 frames.
First frame is at position x=0, second frame at position x=28, third frame at position x=56, etc… you get the idea.
Which means that it we number our frame, we could easily get the position x=frame*28.

Second trick is one overloaded draw method which enables us to draw in a destination rectanble (on screen) from source rectangle (in a texture, our sprite sheet !).

Add to that some simple trick in the keyboard input where we would alternate two frames for one specific movement, and you obtain an animated sprite !

Source code : xna_demo_23.

A video (or screenshot) as always.

And the text source code if you want to have a quick glimpse at it.


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
Dim graphics As GraphicsDeviceManager
Dim spriteBatch As SpriteBatch
Private mTexture As Texture2D
Dim dest_box As Rectangle

Dim timer As Integer
Dim switchFrame As Boolean
Dim direction_ As Integer

Public Enum direction
Up = 0
Down = 1
Left = 2
Right = 3
End Enum

Dim frame As Integer = 0

Public Sub New()
graphics = New GraphicsDeviceManager(Me)
End Sub

Protected Overrides Sub Initialize()
MyBase.Initialize()
End Sub
Protected Overrides Sub LoadContent()
MyBase.LoadContent()
spriteBatch = New SpriteBatch(GraphicsDevice)
'Loading the texture'
Dim textureStream As System.IO.Stream = New System.IO.StreamReader(Application.StartupPath + "\spritesheet.png").BaseStream
mTexture = Texture2D.FromStream(GraphicsDevice, textureStream)
dest_box = New Rectangle(0, 0, 28, 32)

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

End Sub
Protected Overrides Sub Update(ByVal gameTime As Microsoft.Xna.Framework.GameTime)

Dim newState As KeyboardState
newState = Keyboard.GetState

If (newState.IsKeyDown(Keys.Right)) Then
dest_box.X += 2
direction_ = direction.Right
End If

If (newState.IsKeyDown(Keys.Left)) Then
dest_box.X -= 2
direction_ = direction.Left
End If

If (newState.IsKeyDown(Keys.Up)) Then
dest_box.Y -= 2
direction_ = direction.Up
End If

If (newState.IsKeyDown(Keys.Down)) Then
dest_box.Y += 2
direction_ = direction.Down
End If

animation()

'if all keys up, then freeze
If newState.IsKeyUp(Keys.Up) AndAlso newState.IsKeyUp(Keys.Down) AndAlso newState.IsKeyUp(Keys.Left) AndAlso newState.IsKeyUp(Keys.Right) Then
timer = 0
switchFrame = False
End If

MyBase.Update(gameTime)

End Sub
Protected Overrides Sub Draw(ByVal gameTime As Microsoft.Xna.Framework.GameTime)
GraphicsDevice.Clear(Color.CornflowerBlue)

spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend)

spriteBatch.Draw(mTexture, dest_box, New Rectangle(frame * 28, 0, 28, 32), Color.White)

spriteBatch.End()
MyBase.Draw(gameTime)
End Sub

Sub animation()

timer += 1

'every 8 keypress, we switch between frame A and B for one specific move
If (timer = 8) Then
switchFrame = True
ElseIf (timer = 16) Then
timer = 0
switchFrame = False
End If

If (switchFrame = False) Then
Select Case direction_
Case direction.Up
frame = 5
Case direction.Down
frame = 2
Case direction.Left
frame = 0
Case direction.Right
frame = 7
End Select
Else
Select Case direction_
Case direction.Up
frame = 4
Case direction.Down
frame = 3
Case direction.Left
frame = 1
Case direction.Right
frame = 6
End Select
End If
End Sub
End Class

 Posted by at 20 h 56 min
Nov 242013
 

In the coming weeksn hopefully : monogame, xna on different platforms, farseer and polygon shapes, 3d in xna, some more games …

Recap #2 of recent articles around VB.Net and XNA :

VB.Net and XNA : Vrooom V3 (car racing game)
VB.Net and XNA : PixDead by my 12 years old son 🙂
VB.Net and XNA : Article 15 – Screen Manager (i.e multi screens in XNA)
VB.Net and XNA : Article 14 – simple buttons
VB.Net and XNA : Article 13 – First steps with Farseer Physics Engine
VB.Net and XNA : Article 12 – Pixel collision on rotated shapes
VB.Net and XNA : Article 11 – Use a gamepad
VB.Net and XNA : Article 10 – A simple progressbar
XNA Games : how to distribute with Inno Setup

previous recap of recent articles around VB.Net and XNA :

VB.Net and XNA : Article 9 – A Pong Game
VB.Net and XNA : Vroom V1
VB.Net and XNA : Article 8 (pixel collision)
VB.Net and XNA : Article 7 (Scrolling)
VB.Net and XNA : Article 6 (move a texture, add a background, play some sound)
VB.Net and XNA : Article 5 (mouse input)
VB.NET and XNA : Article 4 (keyboard input)
VB.NET and XNA : Article 3 (draw text)
VB.Net and XNA : Article 2 (moving texture)
VB.Net and XNA : Article 1 (skeleton class)
VB.Net and XNA : Article 0 (introduction)

 Posted by at 21 h 20 min
Nov 242013
 

A week ago, I posted an article about a basic car racing game I have written.

This time comes version 3.

-it uses a screenmanager (to manage several screens)
-it supports gamepad (any)
-it supports 2 players
-the car is now a class

A future version should support network gaming over tcpip.

Binary here : carv3 .
Remember that you have to install xna runtime.

If you are interested by the source code, contact me.
As always, any feedback, suggestions, ideas, remarks welcome!

 Posted by at 21 h 14 min
Nov 242013
 

featuring PixDead, a vb.net xna game made by son.
The objective is to shoot as many zombies as possible.

Scrolling, pixel collision, music, keyboard/mouse input, difficulty levels, an installer, etc .

Play controls :
-cursor keys (or zqsd) to move the player
-mouse to point your gunn left click to shoot
-increase your gun every 200 dollars (press numpad 1)
-increase life every 400 dollars (press numpad 2)
-press P to pause game

Download it here.

 Posted by at 20 h 20 min
Nov 242013
 

Another needed feature in games is the ability to display multiple screens : intro, help, main game screen, etc …
For sure we could use one unique screen and redraw the whole thing each time but that would quickly become messy with lots of cases.

Here attached a demo that can be used as a basis.
As always our main module will call the first xna class (clsmain) : standard stuff.

From there we will call the menu screen.
In that screen, you can either call the help screen the game screen.
In these 2 screens, you can always go back to the menu screenpushing F10.

xna_screens

The code to go from one screen to the other is always :

my_screen_name= New class_name(Me.game)
ScreenManager.ShowScreen(my_screen_name)
Me.Close()

The source code : xna_demo_01 .
A project template to help starting new projects : XNA_SCREENMANAGER.

A video to illustrate.

 Posted by at 18 h 48 min
Nov 242013
 

Quick and dirty : buttons for XNA with mouse over and mouse click.

3 rectangles, 3 textures, mouse inputs…

The source code : XNA_demo_13 .

The video to illustrate.


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
Dim graphics As GraphicsDeviceManager
Dim SpriteBatch As SpriteBatch

Private easy As Texture2D
Private medium As Texture2D
Private hard As Texture2D

Dim color_a As Color
Dim color_b As Color
Dim color_c As Color

Dim rec_a As Rectangle
Dim rec_b As Rectangle
Dim rec_c As Rectangle

Public Sub New()
graphics = New GraphicsDeviceManager(Me)
Window.Title = "Test"
Me.IsMouseVisible = True
End Sub

Protected Overrides Sub Initialize()
MyBase.Initialize()
End Sub
Protected Overrides Sub LoadContent()
MyBase.LoadContent()
SpriteBatch = New SpriteBatch(GraphicsDevice)
easy = Content.Load(Of Texture2D)("easy")
medium = Content.Load(Of Texture2D)("medium")
hard = Content.Load(Of Texture2D)("hard")

rec_a = New Rectangle(40, 40, easy.Width, easy.Height)
rec_b = New Rectangle(40, 140, medium.Width, medium.Height)
rec_c = New Rectangle(40, 240, hard.Width, hard.Height)

End Sub
Protected Overrides Sub UnloadContent()
MyBase.UnloadContent()
End Sub
Private Sub update_mouse()
Dim mouse_state As MouseState = Mouse.GetState()
Dim x As Integer = mouse_state.X
Dim y As Integer = mouse_state.Y

If rec_a.Contains(x, y) Then color_a = Color.Red Else color_a = Color.White
If rec_b.Contains(x, y) Then color_b = Color.Red Else color_b = Color.White
If rec_c.Contains(x, y) Then color_c = Color.Red Else color_c = Color.White

If rec_a.Contains(x, y) And mouse_state.LeftButton = ButtonState.Pressed Then color_a = Color.Yellow
If rec_b.Contains(x, y) And mouse_state.LeftButton = ButtonState.Pressed Then color_b = Color.Yellow
If rec_c.Contains(x, y) And mouse_state.LeftButton = ButtonState.Pressed Then color_c = Color.Yellow
End Sub

Protected Overrides Sub Update(ByVal gameTime As Microsoft.Xna.Framework.GameTime)
update_mouse()
MyBase.Update(gameTime)
End Sub

Protected Overrides Sub Draw(ByVal gameTime As Microsoft.Xna.Framework.GameTime)
GraphicsDevice.Clear(Color.CornflowerBlue)
SpriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend)
SpriteBatch.Draw(easy, rec_a, color_a)
SpriteBatch.Draw(medium, rec_b, color_b)
SpriteBatch.Draw(hard, rec_c, color_c)
SpriteBatch.End()
MyBase.Draw(gameTime)
End Sub

End Class

 Posted by at 14 h 39 min
Nov 242013
 

In previous articles, we have seen many basics which should help to make a game : scrolling, moving shapes, collision, inputs (mouse, keyboard, gamepad)…

However, you may sometimes need to implement some physic principles : gravity, friction, restitution, etc.

This is where Farseer Physics Engine comes to the rescue.
Farseer Physics Engine is a collision detection system with realistic physics responses.

2 importants concepts to start with :
World
The world object is the manager of it all. It iterates all the objects in the world each time you call the Step() function and makes sure everything is consistent and stable.
Body
The body keeps track of world position. It is basically a point is space that is affected by forces such as impulses from collisions and gravity.

Lets add a project reference to farseer (note : I used msbuild /property:configuration=release « Farseer Physics XNA.csproj » to build it).
Then we will create a world, add a body to it and draw a texture based on the body position.

I encourage you to play with the world properties such as restitution, friction etc to see how it affects your world.
Also, notice how easy it is to detect collision (although I manage only plain shape for now, no convex shapes).

The source code : XNA_DEMO_19

A video to illustrate it.

 Posted by at 13 h 53 min
Nov 232013
 

We have seen in previous article 8 how to perform pixel collision.
Altough it worked perfectly, it only worked on non rotated shapes.

But in some cases, your shape will rotate (like a car in a racing game – see article « A racing car game »).

To perform pixel collision for a rotated shape, here below the steps :

-you first need to draw a rotated rectangle around your shape
-you then need to draw a bounding rectanle around that rotated rectangle (also called AABB : Axis Aligned Bounding Box)
-finally you can check whether 2 AABB intersects, and if so, if in the intersected region, we have 2 non transparent pixels (colliding) or not

The source code : XNA_DEMO_18
Note : the project contains a class named primitives2d which is used only to draw lines/rectangles around my objects.

Lets illustrate this with some screenshots.

Lets rotate our rectangle along with my rocket
pixel2

Lets define our bounding box
pixel3

Despite bounding boxes intersecting, we dont detect collision yet
pixel4

Here we go ! In the intersection region, there are 2 non transparent pixels
pixel5

 Posted by at 17 h 41 min