.: Create Ball Class and Define Ball Movement
|
|
| |
In this project we will define the ball class and then create our first ball, the movement of the ball about the playarea will also be defined. So, download the Source Code from the previous tutorial, rename the folder to BallClass and open the project. Once open, right-click on the project name and select Add Class, name this class Ball and add the following code:
|
|
Imports Microsoft.Xna.Framework
Imports Microsoft.Xna.Framework.Graphics
Public Class Ball
'Define Ball SpriteBatch
Public BallSB As SpriteBatch = Nothing
Public BallTex As Texture2D = Nothing
Public BallTexCreationParams As TextureCreationParameters = TextureCreationParameters.Default
Public BallRect As Rectangle = Nothing
Public IsMoving As Boolean = False ' used to apply functions only to those balls that are moving
Public MarkedForDeletion As Boolean = False ' used to decide which balls are to be removed
Public BallXVel As Single = 0.02 'speed of the ball in the X direction
Public BallYVel As Single = 0.02 ' speed of the ball in the Y direction
' Location and Speed Variables
Public BallVector As Vector2 = New Vector2 ' Vector to store direction of ball
Public Sub MoveBall()
'move ball in direction of arrow
Dim Vector1 As Vector2 = New Vector2
'Holds the direction of the arrow and hence the trajectory of the ball
Vector1 = New Vector2(BallVectorXPub, BallVectorYPub)
'Selects whether the arrow is pointing left, right or straight up
Select Case MathHelper.ToDegrees(ArrowSBRotate)
Case Is < -0.1 ' Arrow points Left
' if ball is lower than the top of the Playarea
If BallRect.Y >= PlayAreaRect.Y Then
BallRect.Y -= (Vector1.X * (BallXVel + 0.005)) 'ball moves left along vector1 at speed BallXVel + 0.005
BallRect.X -= (Vector1.Y * BallYVel) ' ball moves up along vector1 at speed BallYVel
End If
Case -0.09 To 0.09 ' Arrow points Up
BallRect.Y -= (Vector1.Y * (BallXVel + 0.2)) ' ball moves straight up at speed BallYVel
Case Is > 0.1 ' Arrow points right
BallRect.Y -= (Vector1.Y * BallXVel) ' ball moves up along vector1 at speed BallXVel
BallRect.X -= -(Vector1.X * BallYVel) ' ball moves right along vector1 at speed BallYVel
End Select
' The following code keeps the ball inside the Playarea
If BallRect.Y < (PlayAreaRect.Y + 2) Then ' if ball moves above top of playarea then
BallXVel = 0 ' Stop ball in X direction
BallYVel = 0 ' Stop ball in Y direction
BallRect.Y = (PlayAreaRect.Y + 1) 'bring ball back into playarea if it had moved out of Playarea
IsMoving = False ' set Ismoving to false
End If
If BallRect.X < PlayAreaRect.X Then ' if ball moves passed left of playarea then
BallYVel = -(BallYVel) ' Change x direction of ball from left to right
End If
If BallRect.X > (PlayAreaRect.X + PlayAreaRect.Width) - 38 Then ' if ball moves passed right of playarea then
BallYVel = -(BallYVel) ' Change x direction of ball from right to left
End If
End Sub
End Class
The ball class defines the usual texture objects that will be used to draw each separate ball. A few other variables such as IsMoving and MarkedForDeletion are also declared, these as well as others that will be added later are used to control what actions are performed on which balls. The Sub MoveBall is used to determine where a ball is moved to, and hence when the Render function is called again, the ball is moved to those coordinates. The last part of the MoveBall Sub confines the ball to the playarea and in this game if a ball reaches the top of the playarea it is to stop, this has been configured also.
Next, go to the GraphicsModule defined in the last tutorial and add the following code:
'Define BallLauncher Rectangles
Public BallLauncherRect1 As Rectangle = New Rectangle(230, 495, 38, 38)
Public BallLauncherRect2 As Rectangle = New Rectangle(265, 495, 38, 38)
Public BallLauncherRect3 As Rectangle = New Rectangle(295, 495, 38, 38)
Public BallLauncherRect4 As Rectangle = New Rectangle(325, 495, 38, 38)
Public BallLauncherRect5 As Rectangle = New Rectangle(355, 495, 38, 38)
Public BallLauncherRect6 As Rectangle = New Rectangle(387, 468, 38, 38)
'Used to define Ball Direction when launched
Public BallVectorXPub As Single = BallLauncherRect6.X
Public BallVectorYPub As Single = BallLauncherRect6.Y
'Define New ball
Public Ball1 As Ball = New Ball
'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
The BallLauncher rectangles will be used to hold balls at different points in the game, BallLauncherRect6 is the point from which a ball will be launched into the playarea. BallVectorXPub and BallVectorYPub are used to define the trajectory of the ball. Ball1 is an instance of the ball class and the DefineBall sub configures the texture details of the ball. The DefineBall function is called from the InitializeGraphics function as before:
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()
DefineBall()
Return True
Catch ex As Exception
MsgBox(ex.Message)
Return False
End Try
End Function
The last thing to add to the module is the GetBallVectorFromArrow Sub:
'Get Ball trajectory fromm Arrow
Public Sub GetBallVectorFromArrow()
'Find the direction of the arrow
Select Case MathHelper.ToDegrees(ArrowSBRotate)
Case Is < -0.1 ' if arrow is pointing to the left
'Get arrow direction using the equations of a circle
' x = r * cos(angle) and
' y = r * sin(angle)
' where r is the radius and angle is the angle from 'right' t the direction of the arrow
BallVectorXPub = 494 * Math.Cos(ArrowSBRotate * MathHelper.ToRadians(70))
BallVectorYPub = 494 * (-Math.Sin(ArrowSBRotate * MathHelper.ToRadians(75)))
If BallVectorXPub < 50 Then ' If arrow goes further than 85 degrees to the left then
BallVectorXPub = 50 ' the arrow is set to 85 degrees to the left
End If
Case -0.09 To 0.09 ' if arrow is pointing straight up
BallVectorXPub = (PlayAreaRect.X + (PlayAreaRect.Width / 2))
BallVectorYPub = PlayAreaRect.Y
Case Is > 0.1 ' if arrow is pointing to the right
BallVectorYPub = 494 * Math.Cos(ArrowSBRotate)
BallVectorXPub = 494 * Math.Sin(ArrowSBRotate)
If BallVectorYPub < 45 Then ' If arrow goes further than 85 degrees to the right then
BallVectorYPub = 60 'the arrow is set to 85 degrees to the left
End If
End Select
End Sub
This function determines the vector along which the ball will travel when it is launched into the game. The x and y coordinates of the vector are stored in the BallVectorXPub and BallVectorYPub variables, and are passed to the ball using the Ball Class's MoveBall function. When the render function repeats, the ball will move along the vector. Next the Key_Down function in the main form must be modified to trigger the release of a ball as follows:
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
Select Case e.KeyCode
Case Is = Windows.Forms.Keys.Escape
Application.Exit()
Exit Sub
Case Is = Windows.Forms.Keys.Right
If ArrowSBRotate >= MathHelper.ToRadians(85) Then
ArrowSBRotate = MathHelper.ToRadians(85)
Exit Sub
End If
ArrowSBRotate = ArrowSBRotate + MathHelper.ToRadians(2)
Case Is = Windows.Forms.Keys.Left
If ArrowSBRotate <= MathHelper.ToRadians(-85) Then
ArrowSBRotate = MathHelper.ToRadians(-85)
Exit Sub
End If
ArrowSBRotate = ArrowSBRotate - MathHelper.ToRadians(2)
Case Is = Windows.Forms.Keys.Space
If Ball1.IsMoving = False Then
GetBallVectorFromArrow() ' Get direction of arrow
End If
Ball1.IsMoving = True
End Select
End Sub
This is basically the same as it was before except I have added another case to the select function, when the space bar is pressed the GetBallVectorFromArrow function uns and the ball's IsMoving variable is set to true. The ball is then launched into the playarea. Notice that the GetBallVectorFromArrow function does not run when the ball ismoving is set to True, so that any further movement of the arrow does not affect the the ball that has just been launched. I have also changed the values of the ArrowSBRotate to Degrees so that they are easier for me to understand as I look at them.
And Lastly is the Render function:
Private Sub Render()
If Device Is Nothing Then Return
'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
NonBallSB.Begin(SpriteBlendMode.AlphaBlend)
NonBallSB.Draw(BackgroundTex, BackgroundRect, Color.White)
NonBallSB.Draw(PlayAreaTex, PlayAreaRect, Color.White)
NonBallSB.Draw(BallLauncherTex, BallLauncherRect, Color.White)
NonBallSB.End()
'Draw Ball
Ball1.BallSB.Begin(SpriteBlendMode.AlphaBlend)
Ball1.BallSB.Draw(Ball1.BallTex, Ball1.BallRect, Color.White)
If Ball1.IsMoving = True Then
Ball1.MoveBall()
End If
Ball1.BallSB.End()
'Draw Arrow - Arrow is drawn after ball so that it appears on top of ball.
'Anything drawn last will appear above anything drawn before
NonBallSB.Begin(SpriteBlendMode.AlphaBlend)
NonBallSB.Draw(ArrowTex, New Vector2(405, 485), Nothing, Color.White, ArrowSBRotate, _
New Vector2((ArrowTex.Width / 2), (ArrowTex.Height / 2)), 1.0F, SpriteEffects.None, 0.0F)
NonBallSB.End()
Device.Present()
End Sub
As you can see i have moved the part that draws the arrow so that it occurs after the ball has been drawn, this ensures that the arrow appears above the ball. The condition in the draw ball statement says that the moveball function will only run if the ball's IsMoving variable is set to True.
Excellent, now if you run the project you should have a game that looks like this:

Now, you can return to the tutorials page for the next tutorial.
Web site contents © Copyright Alan Phipps 2006, All rights reserved.
Website templates |