Contact

 
Google
Web www.alanphipps.com

 
   
 
   
www.alanphipps.com

.: Tutorial 4 - Create Game Area

 
 

 

Following on from tutorial 3 we will now define the part of the game window that will be used for the game itself. If we had the balls in the game just now they would travel from one side of the application window to the other, completely ignoring the background texture we added in the last tutorial. We want to keep the balls inside the transparent section of the background texture, in order to do this we will simply create a rectangle that has the same boundaries as the transparent part of the texture. We will write the ball code in a later tutorial.

As before we will declare the variables, define the shape and size and then render the object. So, locate the tutorial 3 project folder and copy it then rename the copy to CreatePlayArea. Open the project and add the following code to the variables section after the line "Public Class Form1"

 

'Define Game Boundaries
Private PlayAreaRect As Rectangle = Nothing
Private PlayAreaSB As SpriteBatch = Nothing
Private PlayAreaTex As Texture2D = Nothing
Private PlayAreaTexCreationParams As TextureCreationParameters = TextureCreationParameters.Default

As before this creates holders for the various components of the game boundaries. Add the following function after the variables:

'Define PlayArea Details
Private Sub DefinePlayArea()

PlayAreaRect.Height = 482
PlayAreaRect.Width = 382
PlayAreaRect.X = 211
PlayAreaRect.Y = 62
PlayAreaSB = New SpriteBatch(Device)
PlayAreaTex = Texture2D.FromFile(Device, "../../images/380x480AlphaChannelOnly.png", BackgoundTexCreationParams)

End Sub

You can find the 380x480AlphaChannelOnly.png in the source code at the bottom of the page, this image is 380x 480 pixels in size and contains only alpha channel data, meaning it will appear totally invisible. The X and Y coordinates line up with top-left corner of the transparent part of the background texture.

Next we add the DefinePlayArea() function call just under the DefineBackground() call in the initializegraphics() function as shown below:

Private Function IntializeGraphics()

Try
Dim presentParams As New PresentationParameters
presentParams.SwapEffect = SwapEffect.Discard
Dim XNAGraphicsAdapater As Microsoft.Xna.Framework.Graphics.GraphicsAdapter = Graphics.GraphicsAdapter.Adapters.Item(0)
Device = New Graphics.GraphicsDevice(XNAGraphicsAdapater, DeviceType.Hardware, Me.Handle, CreateOptions.SoftwareVertexProcessing, presentParams)
DefineBackground()
DefinePlayArea()

Return True

Catch ex As Exception
MsgBox(ex.Message)
Return False
End Try

End Function

Now, we add the Render components in the Render function:

'Draw Game Area Boundary
PlayAreaSB.Begin(SpriteBlendMode.AlphaBlend)
PlayAreaSB.Draw(PlayAreaTex, PlayAreaRect, Color.White)
PlayAreaSB.End()

Now if you run the program it should look like this:

XNA with VB.NET - Game Area Defined

I know, it looks exactly the same as it did in the last tutorial, but now we have a way of keeping the balls within the transparent section and in future tutorials it will begin to look more like a game. Now, head back to the tutorials page for the next tutorial.

 

CreateGameArea Source Code - 48Kb
Next Tutorial - Create a Rotating 2D Texture

 

     
 
 
     

 

Web site contents © Copyright Alan Phipps 2006, All rights reserved.
Website templates
   
 
 

 

__PayPal

PayPal - Any Amount is Welcome
 
Please Donate to the Nvidia Geforce Go 7950 GTX Fund, All donations welcome. Thanks.

 

XNA in C#

 
 

 

Games at Amazon