Air Hockey Tutorial 3

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.

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: 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:

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". 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:

This will be called every time the ball hits the edge. "KeepBallOnTable" should now look like: 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. Then this will go at the top of run and it will give a value to our Matrix2D

So "MoveBall" will look like this:

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:

Download
Here is a copy of everything that is covered in this tutorial [[media:Air Hockey Tutorial 3.zip|Air Hockey Tutorial 3.zip]]

List Of Tutorials

 * 1) Introduction
 * 2) Table
 * 3) Ball
 * 4) Player
 * 5) AI
 * 6) Scoring
 * 7) Sound
 * 8) Menu
 * 9) Finishing