Air Hockey Tutorial 4

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.

Adding A Player
First we need to create a player. A Player needs a bat ,score and a goal. So we need a "CreatePlayer" method that will create our players. This will return a Player.

As you can see we have used the number of the player to set things like the position of the bat and the bitmap. Using this can allow you to use the same code more than once if you code it right. For this to work you will need to load some images, these images are [[media:Air Hockey Tutorial 4 pics.zip| here]]. In GameResources you will need to load the images like this:

Now we need to add 2 Players to the AirHockeyGame. This will be done in "CreateGame". Add this to "CreateGame":

Moving The Bat
Now that the we have added the Bats we need to draw it to the screen. This will be done in our "Draw" method. Next we will create a "MoveBat" method. This will move both of the bats. It will add friction just like we did with the ball. Before we create "MoveBat" we need to create the Matrix2D. So add this underneath our other Matrix2D dim statement.

Then we need to set it, it will be set in the same place as "scaleBall" was set. Now we can create "MoveBat". Here the Bats can be moved... but the bats can move off the table and out of there own half, so we need to fix this. This is easy for the sides as they can be the same for both bats. This will also go in "MoveBat" at the end of the for. This causes the bat to bounce off the wall when it hits it. Now for the bat the is in the bottom half. For the bottom edge it will work just like the side but for the top one it will stop dead as it can't go over half way. This will have its own method that will be called from "MoveBat". For the bat in the top half, it will be almost the same but bit different. Both "TopHalf" and "BottomHalf" will be called from "MoveBat" like this:

Hitting The Ball
At the moment when the ball hits a bat nothing happens. So in the our "MoveBall" method we are going to check if the ball has hit the bat in there.

When the bat and the ball hit we will be doing a vector collision which will both objects to bounce off each other. The game is a bit boring with out being able to doing anything in so now well will add human interaction. This will be done by using the mouse input.

Human Interaction
To work out how fast the user is trying to bat we will use the "GetMouseMovement" function and also we will centre the mouse so it wont go off the screen. The mouse will also be hidden. For this we will create a method called "MovePlayer".

"ProcessEvents" was called in there so the mouse position will get reset and will not stuff up our mouse movement. This will be then called from "MoveBat" like: This must be called before the for loop.

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

List Of Tutorials

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