Contact

 
Google
Web www.alanphipps.com

 
   
 
   
www.alanphipps.com

.: Drawing and Launching Multiple Balls

 
 

In the last tutorial , we draw the first ball and rendered it's movement. in this tutorial we will continue this idea by drawing many balls that will be launched into the game, by pressing the spacebar. Once a ball is launched a new ball will be created to take it's place, this new ball can also be launched, repeating the process. So, using the source code from the last tutorial, copy the code folder and rename it to multiple balls. Open the project and in the Graphics Module make the following changes:

I have created regions to hold similar code, so that it can be minimized when i don't need to look at it. Creating a region is easily done, simply type

#Region "Enter a description that best describes the data inside the region "

Enter you data here

End Region

 

Regions do not affect the execution of the code they just help to make it more readable. Now the new code in the graphics module is as follows:

'Define Ball SpriteBatch to draw the balls
Public BallSB As SpriteBatch = Nothing

'Define the ball Array to hold each ball in the game
Public BallArray() As Ball = New Ball() {}

'If any of the ball launcher Rectangles are empty then the ball from the previous Rectangle will move into the
'Next one

Public BallLauncherRect7IsEmpty As Boolean = False

'Variables to hold the index of which balls are in the ball launcher
Public IndexOfBallInBallLauncherRect1 As UShort = 0
Public IndexOfBallInBallLauncherRect2 As UShort = 0
Public IndexOfBallInBallLauncherRect3 As UShort = 0
Public IndexOfBallInBallLauncherRect4 As UShort = 0
Public IndexOfBallInBallLauncherRect5 As UShort = 0
Public IndexOfBallInBallLauncherRect6 As UShort = 0
Public IndexOfBallInBallLauncherRect7 As UShort = 0
Public IndexOfBallsNotFound As Boolean = False
Public ABallHasBeenLaunched As Boolean = False

'Define Ball Details
Public Sub DefineBallSB()
BallSB = New SpriteBatch(Device)
End Sub

'Get Random Colour
Public Function GetRandomColour()
Dim Randomize As Random = New Random
Dim RndNum As Byte = Randomize.Next(0, 10)
Dim RndColour As String = "Blue"

Select Case RndNum
Case Is = 0
RndColour = "Blue"
Case Is = 1
RndColour = "Red"
Case Is = 2
RndColour = "Green"
Case Is = 3
RndColour = "Yellow"
Case Is = 4
RndColour = "Black"
End Select

Return RndColour

Randomize = Nothing
RndNum = Nothing
RndColour = Nothing
End Function

'Sub to create ball, set BallColour to GetRandomColour() for a random colour
Public Sub CreateBall(ByVal BallColour As String, ByVal xpos As Integer, ByVal ypos As Integer)

Dim TempBall As Ball = New Ball
ReDim Preserve BallArray(UBound(BallArray) + 1)
TempBall.BallRect = New Rectangle(xpos, ypos, 38, 38)
TempBall.BallTex = Texture2D.FromFile(Device, "../../images/Ball" & BallColour & ".png", TempBall.BallTexCreationParams)
TempBall.IsMoving = False
BallArray(UBound(BallArray)) = TempBall

TempBall = Nothing

End Sub

'Create Balls in the ball launcher
Public Sub SetBallLauncherBalls()

CreateBall(GetRandomColour(), BallLauncherRect1.X, BallLauncherRect1.Y)
CreateBall(GetRandomColour(), BallLauncherRect2.X, BallLauncherRect2.Y)
CreateBall(GetRandomColour(), BallLauncherRect3.X, BallLauncherRect3.Y)
CreateBall(GetRandomColour(), BallLauncherRect4.X, BallLauncherRect4.Y)
CreateBall(GetRandomColour(), BallLauncherRect5.X, BallLauncherRect5.Y)
CreateBall(GetRandomColour(), BallLauncherRect6.X, BallLauncherRect6.Y)
CreateBall(GetRandomColour(), BallLauncherRect7.X, BallLauncherRect7.Y)

End Sub

First there is an Array called BallArray that will hold all the balls in the game. There are a number of variables that will hold data on where a particular ball is in that array and some variables that will be used in conditional statements. The new functions speak for themselves, GetRandomColour which will decide what colour a new ball is, CreateBall which as it says creates a new ball and SetBallLauncherBalls which creates 7 new balls and places them in the ball Launcher Rectangles.

I have also changed the Ball Launcher Rectangles as below:

'Define Ball Launcher Rectangles
Public BallLauncherRect1 As Rectangle = New Rectangle(192, 495, 38, 38)
Public BallLauncherRect2 As Rectangle = New Rectangle(230, 495, 38, 38)
Public BallLauncherRect3 As Rectangle = New Rectangle(265, 495, 38, 38)
Public BallLauncherRect4 As Rectangle = New Rectangle(295, 495, 38, 38)
Public BallLauncherRect5 As Rectangle = New Rectangle(325, 495, 38, 38)
Public BallLauncherRect6 As Rectangle = New Rectangle(355, 495, 38, 38)
Public BallLauncherRect7 As Rectangle = New Rectangle(387, 468, 38, 38)

Please note that i have deleted the following code from the previous tutorial:


'Define Ball Details
Public Sub DefineBall()
Ball1.BallSB = New SpriteBatch(Device)
Ball1.BallRect = BallLauncherRect6
Ball1.BallTex = Texture2D.FromFile(Device, "../../images/BallRed.png", Ball1.BallTexCreationParams)
End Sub

'Define New ball
Public Ball1 As Ball = New Ball

Next, if we move onto the Ball class that we created in the last tutorial, i have made a few changes here as well:

The following declaration has been deleted:

Public BallSB As SpriteBatch = Nothing

The following function has been add to the ball class:


Public Sub MoveBallLauncherBalls()
'Move Ball Launcher Balls
If BallLauncherRect7IsEmpty = True Then ' BallLauncherRect7IsEmpty is Set to True when a ball is launched
If IndexOfBallsNotFound = False Then ' this If statement stops the indexes being overwritten before they are used
For m As UShort = 0 To UBound(BallArray)
If BallArray(m).BallRect = BallLauncherRect1 Then
IndexOfBallInBallLauncherRect1 = m
End If
If BallArray(m).BallRect = BallLauncherRect2 Then
IndexOfBallInBallLauncherRect2 = m
End If
If BallArray(m).BallRect = BallLauncherRect3 Then
IndexOfBallInBallLauncherRect3 = m
End If
If BallArray(m).BallRect = BallLauncherRect4 Then
IndexOfBallInBallLauncherRect4 = m
End If
If BallArray(m).BallRect = BallLauncherRect5 Then
IndexOfBallInBallLauncherRect5 = m
End If
If BallArray(m).BallRect = BallLauncherRect6 Then
IndexOfBallInBallLauncherRect6 = m
End If
If BallArray(m).BallRect = BallLauncherRect7 Then
IndexOfBallInBallLauncherRect7 = m
End If
Next
IndexOfBallsNotFound = True
End If

'Move the balls in the previous rectangle forward to take position in the next one
If BallArray(IndexOfBallInBallLauncherRect6).BallRect.X < BallLauncherRect7.X Then
BallArray(IndexOfBallInBallLauncherRect6).BallRect.X += 1
End If
If BallArray(IndexOfBallInBallLauncherRect6).BallRect.X = BallLauncherRect7.X AndAlso _
BallArray(IndexOfBallInBallLauncherRect6).BallRect.Y > BallLauncherRect7.Y Then
BallArray(IndexOfBallInBallLauncherRect6).BallRect.Y -= 1
End If
If BallArray(IndexOfBallInBallLauncherRect5).BallRect.X < BallLauncherRect6.X Then
BallArray(IndexOfBallInBallLauncherRect5).BallRect.X += 1
End If
If BallArray(IndexOfBallInBallLauncherRect4).BallRect.X < BallLauncherRect5.X Then
BallArray(IndexOfBallInBallLauncherRect4).BallRect.X += 1
End If
If BallArray(IndexOfBallInBallLauncherRect3).BallRect.X < BallLauncherRect4.X Then
BallArray(IndexOfBallInBallLauncherRect3).BallRect.X += 1
End If
If BallArray(IndexOfBallInBallLauncherRect2).BallRect.X < BallLauncherRect3.X Then
BallArray(IndexOfBallInBallLauncherRect2).BallRect.X += 1
End If
If BallArray(IndexOfBallInBallLauncherRect1).BallRect.X < BallLauncherRect2.X Then
BallArray(IndexOfBallInBallLauncherRect1).BallRect.X += 1
End If
If BallArray(IndexOfBallInBallLauncherRect1).BallRect.X = BallLauncherRect2.X Then
CreateBall(GetRandomColour(), BallLauncherRect1.X, BallLauncherRect1.Y)
End If

'When the balls have moved forward set the conditional variables to false so that the
'operation does not run again

If BallArray(IndexOfBallInBallLauncherRect6).BallRect.Y = BallLauncherRect7.Y Then
IndexOfBallsNotFound = False
BallLauncherRect7IsEmpty = False
End If

End If
End Sub

This code basically runs when a ball has been launched, it checks for which balls are in the ball launcher and when found it moves those balls into the next ball launcher position.

There are a couple of changes in Form1 too, the part of the Form1_KeyDown function that is used for the spacebar has been changed to :

Case Is = Windows.Forms.Keys.Space
If ABallHasBeenLaunched = False Then ' If a ball is not already moving
For Each SingleBall As Ball In BallArray ' Check every ball in the array
If SingleBall.BallRect = BallLauncherRect7 Then ' If ball is in the launch position
If SingleBall.IsMoving = False Then
GetBallVectorFromArrow() ' Get direction of arrow
End If
BallLauncherRect7IsEmpty = True
SingleBall.IsMoving = True
ABallHasBeenLaunched = True
Exit For ' because only one ball will be in the launch position we can safely exit the
'for loop after we have found it

End If
Next
End If

This change basically means that once a ball has been launched the next one must wait until the first one has stopped before it can be launched.

The InitializeGraphics function has been changed to:

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)
DefineNonBallDetails()
DefineBallSB()
SetBallLauncherBalls()
Return True

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

End Function

The only other change is in the Render function where the BallSB code has changed to:

'Draw Ball
If UBound(BallArray) > 0 Then
BallSB.Begin(SpriteBlendMode.AlphaBlend)
For Each SingleBall As Ball In BallArray ' Check every ball in array
BallSB.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
SingleBall.MoveBallLauncherBalls() ' Move the balls in the ball launcher
Next
BallSB.End()
End If

This change merely updates the balls in the ball launcher as well as the balls in the game. Now if you run the game it should now look like this:

XNA in VB.NET - Multiple Balls

In the next tutorial we will deal with ball collision, once i have completed it i will upload it to the tutorials page.

 

Multiple Balls Source Code - 180Kb
Next Tutorial - Installing XNA GSE Beta 2

 

     
 
 
     

 

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