Air Hockey Tutorial 3
From SwinGame
This tutorial will teach you how to use the SwinGameSDK to develop a simple air hockey game. At the end of this tutorial you should be able to use Visual Basic 6 to implement your game which makes use of Vectors, Sprites and SoundEffects.
Contents |
Putting The Ball On The Table
To Put the ball on the table we have to draw it to the screen and also give it a x and y value. We will add the ball to the centre of the table. Also the ball will have a random vector. To do this, create a new method called StartBall with the first parameter as an AirHockeyGame. Then set the x and y of the ball to the middle of the screen. Next create 2 random numbers between -3 and +3. Then set the vector of the ball to have those two numbers. "StartBall" should look like this:
Private Sub StartBall(game As AirHockeyGame) Dim x As Long Dim y As Long Call Game.Ball.setX(CenterOfTable - (Graphics.CurrentWidth(Game.Ball) / 2)) Call Game.Ball.setY(MiddleOfTable - (Graphics.CurrentHeight(Game.Ball) / 2)) Call Randomize x = Int(Rnd * 7) - 3 y = Int(Rnd * 7) - 3 Call game.Ball.SetMovementVector(Physics.CreateVector_NoInvert(x, y)) End Sub
This method can be called every time something happens to the ball, like someone scoring a goal.
Moving The Ball
Now that the ball is on the table and has a vector we need move the ball. To do this we will create a method called "MoveBall". This will move the ball and do other stuff later on. As we will want to move the ball every frame we will call "MoveBall" in side the loop in "Run". "MoveBall" will look like this:
Private Sub MoveBall(Game As AirHockeyGame) Call Graphics.MoveSprite(Game.Ball, Game.Ball.GetMovementVector) End Sub
Keeping The Ball Inside The Table
As you may of noticed that the ball doesn't stay on the table. To fix this is very easy. All we need to use is the images of the sides that are part of the table. So when the ball hits "Game.TablePics.TableHorizontal" we need to invert the y moment of the ball and when the ball hits "Game.TablePics.TableVertical" we neeed to invert the x moment of the ball. This will get done in a method call "KeepBallOnTable".
Private Sub KeepBallOnTable(Game As AirHockeyGame) If Physics.HaveSpritesCollided(Game.Ball, Game.TablePics.TableHorizontal) Then Call Game.Ball.SetMovementVector(Physics.CreateVector_NoInvert(Game.Ball.GetMovementX, Game.Ball.GetMovementY * -1)) End If If Physics.HaveSpritesCollided(Game.Ball, Game.TablePics.TableVertical) Then Call Game.Ball.SetMovementVector(Physics.CreateVector_NoInvert(Game.Ball.GetMovementX * -1, Game.Ball.GetMovementY)) End If End Sub
The "KeepBallOnTable" on table method will be called from the "MoveBall" method. With this method the ball can sometimes get stuck on the edge of the table. So to fix we will place the ball at the edge of the table when it hits the side of the table. The code for this looks like this:
Private Sub KeepBallOnTable(Game As AirHockeyGame) If Physics.HaveSpritesCollided(Game.Ball, Game.TablePics.TableHorizontal) Then Call RescueTheBall(Game) Call Game.Ball.SetMovementVector(Physics.CreateVector_NoInvert(Game.Ball.GetMovementX, Game.Ball.GetMovementY * -1)) End If If Physics.HaveSpritesCollided(Game.Ball, Game.TablePics.TableVertical) Then Call RescueTheBall(Game) Call Game.Ball.SetMovementVector(Physics.CreateVector_NoInvert(Game.Ball.GetMovementX * -1, Game.Ball.GetMovementY)) End If End Sub
This will be called every time the ball hits the edge. "KeepBallOnTable" should now look like:
Private Sub KeepBallOnTable(Game As AirHockeyGame) If Physics.HaveSpritesCollided(Game.Ball, Game.TablePics.TableHorizontal) Then Call RescueTheBall(Game) Call Game.Ball.SetMovementVector(Physics.CreateVector_NoInvert(Game.Ball.GetMovementX, Game.Ball.GetMovementY * -1)) End If If Physics.HaveSpritesCollided(Game.Ball, Game.TablePics.TableVertical) Then Call RescueTheBall(Game) Call Game.Ball.SetMovementVector(Physics.CreateVector_NoInvert(Game.Ball.GetMovementX * -1, Game.Ball.GetMovementY)) End If End Sub
The ball will some times still go off the table when it goes in the goal, but we will get to that later.
Adding Friction
To make the game more realistic we need to add friction. This can be done by multiplying a "ScaleMatrix" and the vector. We are going to make the vector 98% of what it was. This will be done in the "MoveBall" method. To do this we need to make a global variable so that we don't create too many Matrix2D. This will go at the top of the page.
Dim scaleBall As Matrix2D
Then this will go at the top of run and it will give a value to our Matrix2D
Set scaleBall = New Matrix2D Set scaleBall = Physics.ScaleMatrix(0.98)
So "MoveBall" will look like this:
Private Sub MoveBall(Game As AirHockeyGame) Call KeepBallOnTable(Game) Call Game.Ball.SetMovementVector(Physics.Multiply_Vector(scaleBall , Game.Ball.GetMovementVector)) Call Graphics.MoveSprite(Game.Ball, Game.Ball.GetMovementVector) End Sub
Limiting The Speed Of The Ball
The ball can some times get very fast and not hit the side at all as it has jumped over it. So to fix this we will limit the speed of the vector to 25. We will limit the vector in the "MoveBall" method. "MoveBall" will look like this:
Private Sub MoveBall(Game As AirHockeyGame) Call KeepBallOnTable(Game) Call Game.Ball.SetMovementVector(Physics.LimitVector(Game.Ball.GetMovementVector, 25)) Call Game.Ball.SetMovementVector(Physics.Multiply_Vector(scaleBall , Game.Ball.GetMovementVector)) Call Graphics.MoveSprite(Game.Ball, Game.Ball.GetMovementVector) End Sub
Download
Here is a copy of everything that is covered in this tutorial Air Hockey Tutorial 3.zip

