Air Hockey Tutorial 5

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.

This page contains a Tutorial. Tutorials are designed to walk you through the development of a small game.
Warning: You must have completed the previous tutorial(s) to go through this tutorial

Contents

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

Private Sub MoveAI(Bat As Sprite, Ball As Sprite)
    If Ball.getY > 300 Then
        'defence mode
    Else
        'offence mode
    End If
End Sub

This will be called from "MoveBall" just after "MovePlayer" was called, it will look like this:

Call MoveAI(Game.Players(1).Bat, Game.Ball)

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.Image:Air Hockey AI defence side to side.png
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.

Private Sub AIDefence(Bat As Sprite, Ball As Sprite)
    If Bat.getX > (470 - Graphics.CurrentWidth(Bat)) Then
        Call Bat.SetMovementVector(Physics.AddVectors(Bat.GetMovementVector, Physics.CreateVector_NoInvert(-0.5, 0)))
    Else
        If Bat.getX < 330 Then
            Call Bat.SetMovementVector(Physics.AddVectors(Bat.GetMovementVector, Physics.CreateVector_NoInvert(0.5, 0)))
        Else
            If (Bat.getX + Graphics.CurrentWidth(Bat)) > (Ball.getX + Graphics.CurrentWidth(Ball)) Then
                Call Bat.SetMovementVector(Physics.AddVectors(Bat.GetMovementVector, Physics.CreateVector_NoInvert(-0.5, 0)))
            End If
            If (Bat.getX + Graphics.CurrentWidth(Bat)) < (Ball.getX + Graphics.CurrentWidth(Ball)) Then
                Call Bat.SetMovementVector(Physics.AddVectors(Bat.GetMovementVector, Physics.CreateVector_NoInvert(0.5, 0)))
            End If
        End If
    End If
End Sub

Now we will make the AI go back to its goal line. Image:Air Hockey AI defence top to bottom.png
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.

If Bat.getY > 90 Then
    Call Bat.SetMovementVector(Physics.AddVectors(Bat.GetMovementVector, Physics.CreateVector_NoInvert(0, -0.5)))
Else
    Call Bat.SetMovementVector(Physics.AddVectors(Bat.GetMovementVector, Physics.CreateVector_NoInvert(0, 0)))
End If

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.

Private Sub AIOffence(Bat As Sprite, Ball As Sprite)
    If (Bat.getY + (Graphics.CurrentHeight(Bat))) < Ball.getY Then
        Call Bat.SetMovementVector(Physics.AddVectors(Bat.GetMovementVector, Physics.GetVectorFromAngle(Physics.CalculateAngle_Number(Bat.getX + Graphics.CurrentWidth(Bat) / 2, Bat.getY, Ball.getX + Graphics.CurrentWidth(Ball) / 2, Ball.getY + Graphics.CurrentHeight(Ball) / 2), 1.5)))
    Else
        If Physics.Magnitude(Ball.GetMovementVector) < 1 Then
            Call Bat.SetMovementVector(Physics.AddVectors(Bat.GetMovementVector, Physics.GetVectorFromAngle(Physics.CalculateAngle_Number(Bat.getX + Graphics.CurrentWidth(Bat) / 2, Bat.getY, Ball.getX + Graphics.CurrentWidth(Ball) / 2, Ball.getY + Graphics.CurrentHeight(Ball) / 2), 1.5)))
        Else
            Call AIDefence(Bat, Ball)
        End If
    End If
End Sub

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.

Private Sub MoveAI(Bat As Sprite, Ball As Sprite)
    If Physics.Magnitude(Ball.GetMovementVector) < 1 And Ball.getY < MiddleOfTable Then
        Call AIOffence(Bat, Ball)
    Else
        If Bat.getX < (LeftOfTable + Graphics.CurrentWidth(Bat)) Then
            Call Bat.SetMovementVector(Physics.AddVectors(Bat.GetMovementVector, Physics.CreateVector_NoInvert(1, 0)))
        Else
            If Bat.getX > (RightOfTable - (Graphics.CurrentWidth(Bat) * 2)) Then
                Call Bat.SetMovementVector(Physics.AddVectors(Bat.GetMovementVector, Physics.CreateVector_NoInvert(-1, 0)))
            Else
                If Bat.getY < (TopOfTable + Graphics.CurrentHeight(Bat)) Then
                    Call Bat.SetMovementVector(Physics.AddVectors(Bat.GetMovementVector, Physics.CreateVector_NoInvert(0, 1)))
                Else
                    If Ball.getY > MiddleOfTable Then
                        'defence mode
                        Call AIDefence(Bat, Ball)
                    Else
                        'offence mode
                        Call AIOffence(Bat, Ball)
                    End If
                End If
            End If
        End If
    End If
End Sub


List Of Tutorials

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