Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
XNA 4.0 Game Development by Example: Beginner's Guide - Visual Basic Edition

You're reading from   XNA 4.0 Game Development by Example: Beginner's Guide - Visual Basic Edition Create your own exciting games with Visual Basic and Microsoft XNA 4.0

Arrow left icon
Product type Paperback
Published in Dec 2011
Publisher Packt
ISBN-13 9781849692403
Length 424 pages
Edition 1st Edition
Arrow right icon
Author (1):
Arrow left icon
Kurt Jaegers Kurt Jaegers
Author Profile Icon Kurt Jaegers
Kurt Jaegers
Arrow right icon
View More author details
Toc

Table of Contents (16) Chapters Close

XNA 4.0 Game Development by Example – Visual Basic Edition Beginner's Guide
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
1. Introducing XNA Game Studio FREE CHAPTER 2. Flood Control – Underwater Puzzling 3. Flood Control – Smoothing Out the Rough Edges 4. Asteroid Belt Assault – Lost in Space 5. Asteroid Belt Assault – Special Effects 6. Robot Rampage – Multi-Axis Mayhem 7. Robot Rampage – Lots and Lots of Bullets 8. Gemstone Hunter - Put on your Platform Shoes 9. Gemstone Hunter—Standing on your Own Two Pixels Index

Time for action – creating the squareTexture


  1. Open Microsoft Paint or your favorite image editor, create a new 16 x16 pixel image, and fill it with white.

  2. Save the image as "SQUARE.BMP" in a temporary location.

  3. Back in Visual Studio, right-click on SquareChaseContent (Content) in Solution Explorer (you may need to scroll down to see it) and select Add | Existing Item. Browse to the image you created and click on Ok.

  4. Add the following code to the LoadContent() method after the spriteBatch initialization:

    squareTexture = Content.Load(Of Texture2D)("SQUARE")

What just happened?

To load content, it must first exist. In steps 1 and 2 mentioned previously, you created a bitmap image for the square texture. In step 3, you added the bitmap image as a piece of content to your project.

Tip

Powers of two

Very old graphics cards required that all texture images be sized to "powers of two" (2, 4, 8, 16, 32, 64, 128, 256, and so on). This limitation is largely non-existent with modern video hardware, especially for 2D graphics. In fact, the sample code in the XNA Platform Starter Kit uses textures that do not conform to the "powers of two" limitation. In our case, the size of the image we create is not critical, as we will be scaling the output when we draw squares to the screen.

Finally, in step 4, you used the Content instance of the ContentManager class to load the image from disk and into memory when your game runs. The Content object is established automatically by XNA for you when you create a new project. When we add content items, such as images and sound effects to our game project, the XNA Content Pipeline converts our content files into an intermediate format that we can read through the Content object. These XNB files get deployed alongside the executable for our game to provide their content data at runtime.

The Content object's Load() method requires us to specify what kind of data we are loading by including Of along with the data type in parenthesis before the parameters that are passed to the method. In this case, the content we are loading is a Texture2D, which represents a 2D image. Finally, we specify the asset name of the item we wish to load from the content project. The default asset name is the same as the filename, without the file extension.

Tip

Asset names

If you create subdirectories inside your Content project (as we will do in the other games in this book), the asset name will include the path to the file. If our square.bmp file were in a directory called Textures, the asset name would be "Textures\Square".

The Update() method

Once LoadContent() has finished doing its job, an XNA game enters an endless loop in which it attempts to call the Update() method 60 times per second. This default update rate can be changed by setting the TargetElapsedTime property of the Game1 object, but for our purposes, the default time step will be fine. If your Update() logic begins taking too long to run, your game will begin skipping calls to the Draw() method in favor of multiple calls to Update(), to attempt to catch up to the current game time.

All of your game logic gets built into the Update() method. It is here that you check for player input, move sprites, spawn enemies, track scores, and everything else except draw to the display. Update() receives a single parameter called gameTime, which can be used to determine how much time has elapsed since the previous call to Update() or to determine if your game is skipping Draw() calls by checking its IsRunningSlowly property.

The default Update() method contains code to exit the game if the player presses the Back button on the first gamepad controller.

Tip

Exiting a game under Windows

The default Update() code provides anyone with a gamepad a way to end the game, but what if you do not have a gamepad? Press Alt + F4 on the keyboard or click on the standard Windows close button to exit your game when running in Windows.

You have been reading a chapter from
XNA 4.0 Game Development by Example: Beginner's Guide - Visual Basic Edition
Published in: Dec 2011
Publisher: Packt
ISBN-13: 9781849692403
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image