Air Hockey Tutorial 4

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.
Note: This tutorial now uses SGSDK 1.1
Warning: You must have completed the previous tutorial(s) to go through this tutorial

Contents

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.

Private Function CreatePlayer(playernumber As Long) As Player
    Set CreatePlayer.Bat = New Sprite
    Set CreatePlayer.Bat = Graphics.CreateSprite(GameImage(playernumber & "bat"))  
    Call CreatePlayer.Bat.SetUsePixelCollision(True)
    Call CreatePlayer.Bat.setX(CenterOfTable - Graphics.CurrentWidth(CreatePlayer.Bat) / 2)
    Call CreatePlayer.Bat.setY(100 - 360 * (playernumber - 1))
    Set CreatePlayer.Goal = New Sprite
    Set CreatePlayer.Goal = Graphics.CreateSprite(GameImage(playernumber & "goal"))
    Call CreatePlayer.Goal.SetUsePixelCollision(True)
    CreatePlayer.Score = 0
    Call CreatePlayer.Bat.SetMass(5)
End Function

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 here. In GameResources you will need to load the images like this:

Call NewImage("0goal", "air_hocky_table_collision_AI_goal.png")
Call NewImage("1goal", "air_hocky_table_collision_player_goal.png")
Call NewImage("0bat", "player_bat.png")
Call NewImage("1bat", "AI_bat.png")

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

ReDim CreateGame.Players(1)
CreateGame.Players(0) = CreatePlayer(0)
CreateGame.Players(1) = CreatePlayer(1)

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.

Call Graphics.DrawSprite(Game.Players(0).Bat)
Call Graphics.DrawSprite(Game.Players(1).Bat)

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.

Dim scaleBat As Matrix2D

Then we need to set it, it will be set in the same place as "scaleBall" was set.

Set scaleBat = New Matrix2D
Set scaleBat = Physics.ScaleMatrix(0.9)

Now we can create "MoveBat".

Private Sub MoveBat(Game As AirHockeyGame)
    Dim i As Long
    For i = 0 To 1
        Call Game.Players(i).bat.SetMovementVector(Physics.Multiply_Vector(scaleBat, Game.Players(i).bat.GetMovementVector))
        Call Graphics.MoveSprite(Game.Players(i).bat, Game.Players(i).bat.GetMovementVector)
    Next i
    Call MovePlayer(Game.Players(0).bat)
    Call MoveAI(Game.Players(1).bat, Game.Ball)
End Sub

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.

If Game.Players(i).Bat.getX < LeftOfTable Then
    Call Game.Players(i).Bat.setX(LeftOfTable + 1)
    Call Game.Players(i).Bat.SetMovementX(Game.Players(i).Bat.GetMovementX * -1)
End If
If Game.Players(i).Bat.getX > RightOfTable - Graphics.CurrentWidth(Game.Players(i).Bat) Then
    Call Game.Players(i).Bat.setX(RightOfTable - Graphics.CurrentWidth(Game.Players(i).Bat) - 1)
    Call Game.Players(i).Bat.SetMovementX(Game.Players(i).Bat.GetMovementX * -1)
End If

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

Private Sub BottomHalf(Bat As Sprite)
    If Bat.getY < MiddleOfTable Then
        Call Bat.setY(MiddleOfTable)
        Call Bat.SetMovementY(0)
    End If
    If Bat.getY > BottomOfTable - Graphics.CurrentHeight(Bat) Then
        Call Bat.setY(BottomOfTable - Graphics.CurrentHeight(Bat) - 1)
        Call Bat.SetMovementY(Bat.GetMovementY * -1)
    End If
End Sub

For the bat in the top half, it will be almost the same but bit different.

  1. Private Sub TopHalf(Bat As Sprite)
  2. If Bat.getY < TopOfTable Then
  3. Call Bat.setY(TopOfTable - 1)
  4. Call Bat.SetMovementY(Bat.GetMovementY * -1)
  5. End If
  6. If Bat.getY > MiddleOfTable - Graphics.CurrentHeight(Bat) Then
  7. Call Bat.setY(MiddleOfTable - Graphics.CurrentHeight(Bat))
  8. Call Bat.SetMovementY(0)
  9. End If
  10. End Sub

Both "TopHalf" and "BottomHalf" will be called from "MoveBat" like this:

Call BottomHalf(Game.Players(0).bat)
Call TopHalf(Game.Players(1).bat)

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.

Dim i As Long
For i = 0 To 1
    If Physics.HaveSpritesCollided(Game.Ball, Game.Players(i).bat) Then
        Call Physics.VectorCollision(Game.Ball, Game.Players(i).bat)
    End If
Next i

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

Private Sub MovePlayer(bat As Sprite)
    Call bat.SetMovementVector(Physics.AddVectors(bat.GetMovementVector, Physics.MultiplyVector(MouseAndKey.GetMouseMovement, 0.015)))
    Call MouseAndKey.HideMouse
    Call MouseAndKey.MoveMouse(Core.ScreenWidth / 2, Core.ScreenHeight / 2)
    Call Core.ProcessEvents
End Sub

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

Call MovePlayer(Game.Players(0).bat)

This must be called before the for loop.

Download

Here is a copy of everything that is covered in this tutorial 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