Contact

 
Google
Web www.alanphipps.com

 
   
 
   
www.alanphipps.com

.: Code Cleanup - Round 2

 
 

In this tutorial we will make a few changes to the existing code so that our game runs faster and uses less resources. So, using the source code from the last tutorial as usual, copy the code folder and rename it to CodeCleanup2, open the project and as always we will start with the Graphics Module.

Rename the "Non-Ball Object and Variable Declarations" region to "Object and Variable Declarations" and move all the Declarations from the "Ball Object and variable Declarations" region into the "Object and Variable Declarations" region. Once the "Ball Object and variable Declarations" region is empty it can be deleted. Now, open the "Object and Variable Declarations" region and locate the NonBallSB SpriteBatch, once found right-click on the word NonBallSB and rename it to MainSB.

The Function DefineBallSB can be deleted and the function DefineNonBallDetails() has been renamed to DefineObjectDetails()

 

In the CheckForCollision Sub, there are a couple of changes, basically the temporary variables VectorMovingBall and VectorOtherBall have been removed and the line BallArray(IndexOfMovingBall).IsPositionedCorrectly = False 'Ball will now be positioned correctly has also been deleted. This Sub should now look like this:

'Check for Ball Collision
Public Sub CheckForCollision()
If IndexOfMovingBallFound = False Then ' If the moving ball is unknown
For m As Integer = 0 To UBound(BallArray) ' Search array for the ball that is moving
If BallArray(m).IsMoving = True Then ' When the moving ball is found
IndexOfMovingBall = m ' store the ball index in a variable and exit the for loop
IndexOfMovingBallFound = True
Exit For
End If
Next
End If

'Each ball will be enclosed in a bounding sphere. The BoundingSphere.Intersects method allows us to tell when
' two balls collide. The following objects are part of this process. The objects are declared here so that they are
' not declared in every loop, which would cause more work for the program
'One BoundingSphere for the Moving Ball

Dim TempBoundingSphereMovingBall As BoundingSphere = New BoundingSphere(New Vector3(BallArray(IndexOfMovingBall).BallRect.X, _
BallArray(IndexOfMovingBall).BallRect.Y, 0), 20)
Dim TempBoundingSphereOtherBall As BoundingSphere ' One BoundingSphere for the Other ball in the collision.

For Each Singleball As Ball In BallArray ' check each ball for a collision
If Singleball.IsMoving = True Then ' If the current ball is the ball that has just been launched then
GoTo NextBall ' goto to the next ball
End If
If Singleball.BallRect.Bottom >= BallLauncherRect7.Y Then ' If the current ball is lower than the ball launcher
GoTo NextBall ' goto the next ball
End If

'Set the boundingsphere to the boundaries of the current ball
TempBoundingSphereOtherBall = New BoundingSphere(New Vector3(Singleball.BallRect.X, Singleball.BallRect.Y, 0), 20)

If TempBoundingSphereMovingBall.Intersects(TempBoundingSphereOtherBall) Then 'Check for a collision

'Set Public variable to be used in PositionBallCorrectly()
BoundingSphereOtherBallPub = TempBoundingSphereOtherBall

BallArray(IndexOfMovingBall).BallXVel = 0 ' Stop the Ball in the X direction
BallArray(IndexOfMovingBall).BallYVel = 0 ' Stop the ball in the Y direction
BallArray(IndexOfMovingBall).IsMoving = False ' Set the ball's IsMoving variable to False
IndexOfMovingBallFound = False ' Tell the program to find the next moving ball
ABallHasBeenLaunched = False ' Set the Ball Moving Global variable to false
TempBoundingSphereMovingBall = Nothing ' Empty the variables
TempBoundingSphereOtherBall = Nothing
ABAllNeedsPositionedCorrectly = True 'Ball will now be positioned correctly
Exit Sub

End If
NextBall:
Next

' The loop is finished and are disposed
TempBoundingSphereMovingBall = Nothing
TempBoundingSphereOtherBall = Nothing

End Sub

The only other change in the graphics module is that i went through all the declarations and changed any UShort to Integer after reading on the net that Integers are faster. If we move onto the Ball class, there are 2 changes, one was the deletion of the line of code from the Object Declarations:

Public IsPositionedCorrectly As Boolean = True ' Used to move a ball into a rectangle so that it is positioned correctly

and the MoveBallLauncher Sub has had it's first If statement removed:

If BallLauncherRect7IsEmpty = True Then ' BallLauncherRect7IsEmpty is Set to True when a ball is launched

If we move onto the Form1 code and start in the InitializeGraphics function, the following line has been deleted

DefineBallSB()

The only other change in form1 is to the Render function, which now looks like:

Private Sub Render()

'Clear the backbuffer to a blue color
Device.Clear(ClearOptions.Target, Microsoft.Xna.Framework.Graphics.Color.Blue, 1.0F, 0)

'Draw all Non Ball Textures
MainSB.Begin(SpriteBlendMode.AlphaBlend)
'Draw Ball
If UBound(BallArray) > 0 Then
For Each SingleBall As Ball In BallArray ' Check every ball in array
MainSB.Draw(SingleBall.BallTex, SingleBall.BallRect, Color.White) 'Draw Each Ball
If SingleBall.IsMoving = True Then 'If a ball is set to move
SingleBall.MoveBall() 'Move the ball
End If
If BallLauncherRect7IsEmpty = True Then ' BallLauncherRect7IsEmpty is Set to True when a ball is launched
SingleBall.MoveBallLauncherBalls() ' Move the balls in the ball launcher
End If
Next
If ABallHasBeenLaunched = True Then ' A global variable used to tell the program when to start checking for
If UBound(BallArray) > 1 Then ' collisions
CheckForCollision() ' Check for a collision
End If
End If

'If a Ball needs to be positioned correctly
If ABAllNeedsPositionedCorrectly = True Then
If UBound(BallArray) > 1 Then
'Correctly Position Ball
PositionBallCorrectly()
End If
End If
End If

MainSB.Draw(BackgroundTex, BackgroundRect, Color.White)
MainSB.Draw(PlayAreaTex, PlayAreaRect, Color.White)
MainSB.Draw(BallLauncherTex, BallLauncherRect, Color.White)

'Draw Arrow - Arrow is drawn after ball so that it appears on top of ball.
'Anything drawn last will appear above anything drawn before

MainSB.Draw(ArrowTex, New Vector2(405, 485), Nothing, Color.White, ArrowSBRotate, _
New Vector2((ArrowTex.Width / 2), (ArrowTex.Height / 2)), 1.0F, SpriteEffects.None, 0.0F)
MainSB.End()

'Display the Scene to the Screen
Device.Present()

End Sub

 

All objects are now drawn with one spritebatch and the check to see if the graphics device is ready has been removed as it is automatically checked by the XNA framework as of Beta 2. In the next tutorial we will develop the Sub that decides how balls are removed when 3 balls of the smae colour touch, once complete you will find it on the tutorials page.

 

CodeCleanup2 Source Code - 187Kb
Next Tutorial - Deleting Balls After a Collision

 

 

     
 
 
     

 

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