Déc 022013
 

In Article 16, we had animated a sprite.
Now lets enhance the demo with a 2d camera following our character creating a simple scrolling effect.
Note : a next article will cover parallax scrolling.

We will simply had a camera class.
Lets declare our camera,
then instantiate it in loadcontent passing the width and height,

cam = New camera(graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height)

update it,

cam.setfocalpoint(New Vector2(dest_box.X, dest_box.Y), New Vector2(graphics.PreferredBackBufferWidth * 1.5 + 32, graphics.PreferredBackBufferHeight * 1.5 + 32))
cam.update()

and finally use it in our draw method thru a parameter in the spritebatch.begin method

spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, Nothing, Nothing, Nothing, Nothing, cam.viewmatrix)

The camera class is itself extremely simple (note that we will manage zoom and rotation also later)

Imports Microsoft.Xna.Framework
Public Class camera
Public position As Vector2
Public viewmatrix As Matrix
Dim _screenx As Integer
Dim _screeny As Integer
Public Sub New(SizeX As Integer, sizeY As Integer)
_screenx = SizeX
_screeny = sizeY
End Sub

Public Sub setfocalpoint(focalposition As Vector2)
position = New Vector2(focalposition.X - _screenx / 2, focalposition.Y - _screeny / 2)
If position.X < 0 Then position.X = 0 If position.Y < 0 Then position.Y = 0 End Sub Public Sub setfocalpoint(ByVal focalposition As Vector2, ByVal lock As Vector2) If focalposition.X > lock.X Then focalposition.X = lock.X
If focalposition.Y > lock.Y Then focalposition.Y = lock.Y

position = New Vector2(focalposition.X - _screenx / 2, focalposition.Y - _screeny / 2)

If position.X < 0 Then position.X = 0 If position.Y < 0 Then position.Y = 0 End Sub Public Sub update() viewmatrix = Matrix.CreateTranslation(New Vector3(-position, 0)) End Sub End Class

The video illustrating this.

The source code.
xna_demo_23.2

 Posted by at 21 h 10 min
Déc 012013
 

XNA and FPE is nice to play with.

This time lets start a platform game : an animated character jumping from one platform to another, collecting gems, in a wold of physics.

Some interesting point to note :

1-when my character is walking on tiles, the engine detects a collision at each edge, blocking my character 🙁
mad googling around, it seems i should :
-use joints : does not work
-use shapes : did not test it yet
-use multiple fixture for one bigger body : did not test it yet

For now, i create a circle for each tile instead of rectangles, it does mitigate (a lot) that bug : it is mostly invisible to the human eye (we see only the texture remember, not a body).

2-We want the character to jump only when touching the top of the tile, not the sides (or bottom).
Also, we dont want the character to jump while in the air.
There I have applied what seems the most common trick : a feet sensor, an extra body (not drawn) joined to my character (below) thru JointFactory.CreateWeldJoint.
When « feet » will collide with « floor » then isjumping=false else isjumping=true

3-The scenery is automatically generated from a text file (= a level) : level .
A level editor could come later.

As a whole, this is far from being perfect yet : the user/gamer experience is not optimal yet but still, I believe it is a good start 🙂

The video.

The source code.

XNA_DEMO_22

 Posted by at 18 h 41 min
Déc 012013
 

Here how far I got so far with monogame and ubuntu 13.04.
Remember I am not a linux boy. See previous article about monogame and windows.

1-install mono complete 2.1 (from ubuntu.org)
2-install mono develop 3.04 (from ubuntu store)
3-donwload and compile opentk (not sure this part is needed)
4-install monogame mpack (templates) for mono develop
5-sudo apt-get install libopenal1
6-sudo apt-get install libsdl1.2debian
7-sudo apt-get install libsdl-mixer1.2
8-test a monogame c# project

At this point you can build a c# project with monogame in ubuntu 🙂

Now for vb.net projects, I managed to create a new project but could not compile it (missing vb compiler?)
I guess I might have to look for more recent monodevelop/mono source (maybe from opensuse?)
Tip : for vb.net projects, change the file format to use when creating new projects to MSBuild (Visual Studio 2008) instead of the default MSBuild (Visual Studio 2010) From Preferences > Load/Save

 Posted by at 14 h 17 min
Nov 302013
 

Although I am having lots of fun with XNA, I decided I would give a first quick look to monogame (the opensource alternative to XNA).

I used a fresh install (3.2) which adds template to vs2010, 2012 & 2013 : here.
I then picked up a previous XNA project (Pong) and changed the project referench from XNA assemblies to Monogame assemblies.
And voila, the build executed just fine and I could run my demo again with monogame 🙂

Next attempt will be to port a project written on windows for Ubuntu.

Also need to test what the dependencies are when distributing such an app compiled with monogame (not more xna runtime dependency I guess?).

 Posted by at 13 h 26 min
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