Air Hockey Tutorial 5

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.

Setting Up The AI
The AI that will be in this game not be very smart, but if you wish to try and improve it you are welcome to. The AI will have two main mode, defence and offence. Which mode the AI is in will depend on which half of the field the ball is in. When the ball is in its own half it will be in offence mode and when the ball is in the players half it will be in defence mode. The AI will be controlled from a method called "MoveAI". This will be called from "MoveBall" just after "MovePlayer" was called, it will look like this:

Defence Of The AI
The defence of the AI will be done from a method called "AIDefence". The side to side moment of the AI will be explained with this image below. When the AI is in the yellow section it will try and move to the pink section, This is the same with the green section. When the AI is in the pink section it will try and have the same x position as the ball has. The code for this will look like this.

Now we will make the AI go back to its goal line. When the AI in the yellow section it will try to move into the green section and when its in the green section it will try and move to the yellow section. This will cause the AI to get stuck on the line between the two sections. The code for this is. This will go at the end of "AIDefence".

When these two rules for the AI are put together the AI will do an average job of defending its goal.

Offence Of The AI
The main goal of the AI on offence is to hit the ball towards the other goal. When the AI is behind the ball it will try and do this, but when the ball is behind the AI, the AI will go back into defence mode. Also the if the ball has almost stopped and the ball is behind the AI, the AI will attack the ball. The code for this is. Using this method the AI will not try and score, it will just hit the ball and hope.

Move AI Movement
You may of noticed that the AI will sometimes get stuck when it tries to hit the ball and the ball is on the edge, so to fix this we will keep the AI off the edge unless the ball gets to slow. If the ball gets too slow the AI will go into Offence mode. This is what the changed "MoveAI" will now look like. There is a lot of 'IF' statements in it, but it would be too hard trying to write it with a select statement.

List Of Tutorials

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