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 – coding Update() for SquareChase


  1. Add the following to Update() method right before MyBase.Update(gameTime):

    If (timeRemaining = 0.0) Then
      currentSquare = New Rectangle(rand.Next(0, Me.Window.ClientBounds.Width - 25),rand.Next(0, Me.Window.ClientBounds.Height - 25),25, 25)
      timeRemaining = TimePerSquare
    End If
    Dim mouseInfo As MouseState = Mouse.GetState()
    If (mouseInfo.LeftButton = ButtonState.Pressed) And(currentSquare.Contains(mouseInfo.X, mouseInfo.Y)) Then
      playerScore += 1
      timeRemaining = 0.0
    End If
    timeRemaining = MathHelper.Max(0, timeRemaining -CType(gameTime.ElapsedGameTime.TotalSeconds, Single))
    Me.Window.Title = "Score : " & playerScore.ToString()

What just happened?

The first thing the Update() routine does is check to see if the current square has expired, by checking to see if timeRemaining has been reduced to zero. If it has, a new square is generated using the Next() method of the rand object. In this form, Next() takes two parameters: an (inclusive) minimum value and a (non-inclusive) maximum value. In this case, the minimum is set to 0, while the maximum is set to the size of the Me.Window.ClientBounds property minus 25 pixels. This ensures that the square will always be fully within the game window.

The square that is generated represents both the location and size of the square that we will draw shortly. Because we are drawing a solid color, the texture and the square do not need to be the same size. When we draw the square, XNA will stretch the 16x16 texture to fill the 25x25 square. If we were drawing a detailed image instead of a colored square, we would need to take this scaling into account to avoid visual artifacts or odd stretching when drawing the texture.

Next, the current position and button state of the mouse is captured into the mouseInfo variable through Mouse.GetState(). Both the Keyboard and the GamePad classes also use a GetState() method that captures all of the data about that input device when the method is executed.

Tip

Visual Basic versus C# - variable naming

C# is a case-sensitive language, so if we were to declare variables named "mouse", "Mouse" and "MOUSE", they would all represent different objects in memory. Visual Basic is case-insensitive, meaning that all three of these identifiers refer to the same variable. In fact, Visual Studio will change them all to match the way you type it when it is first declared in your code.

It is common in C# to use a lower-case variable of the same name as the class or structure you are creating an instance of, so in C# "mouse=Mouse.GetState();" would be perfectly acceptable, but it would be problematic in Visual Basic. You might notice that the XNA template itself does this in a few cases (spriteBatch, gameTime), which are carried over from their C# counterparts. This ends up working out, because the SpriteBatch and GameTime classes do not have any methods or properties that can be accessed outside of an instance of the classes, while Mouse does.

If the mouse reports that the left button is pressed, the code checks with the currentSquare object by calling its Contains() method to determine if the mouse's coordinates fall within its area. If they do, then the player has "caught" the square and scores a point. The timeRemaining counter is set to 0, indicating that the next time Update() is called, it should create a new square.

After dealing with the user input, the MathHelper.Max() method is used to decrease timeRemaining by an amount equal to the elapsed game time, since the last call to Update(). Max() is used to ensure that the value does not go below zero.

Finally, the game window title bar is updated to display the player's score.

Tip

MathHelper

The Microsoft.Xna.Framework namespace provides a class called MathHelper that contains lots of goodies to make your life easier when dealing with numeric data, including converting degrees to and from radians, clamping values between a certain range, and generating smooth arcs between a starting and ending value.

The Draw() method

The final method in the default Game1.vb file is responsible, not surprisingly, for drawing the current game state to the display. Draw() is normally called once after each call to Update(), unless something is happening to slow down the execution of your game. In that case, Draw() calls may be skipped in order to call Update() more frequently. There will always be at least one call to Update() between calls to Draw(), however, as sequential Draw() calls would provide no benefit—nothing in the game state will have changed.

The default Draw() method simply clears the display window to the Cornflower Blue color.

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