Air Hockey Tutorial 6
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.
Contents |
GOAL!!
You many of noticed that the ball will sometimes go into the goal. We will add in a check to find out when the ball goes in a goal. In the "MoveBall" method we will add this code.
If Physics.HaveSpritesCollided(Game.Ball, Game.Players(0).Goal) Then 'Player scores End If If Physics.HaveSpritesCollided(Game.Ball, Game.Players(1).Goal) Then 'Computer scores End If
Scoring
We now need to keep track of the score. To do this we will add 1 to score that is with the player. So we will add this into the code above, like this.
If Physics.HaveSpritesCollided(Game.Ball, Game.Players(0).Goal) Then Game.Players(0).Score = Game.Players(0).Score + 1 End If If Physics.HaveSpritesCollided(Game.Ball, Game.Players(1).Goal) Then Game.Players(1).Score = Game.Players(1).Score + 1 End If
Resetting the Ball
Now that the ball is in the goal we will want to reset the ball, but we wont want it reset it straight away. So to do this we will edit the "AirHockeyGame" data type so it has a resetting and a reset. Resetting will be a Boolean and reset will be a Long. The type will now be like this.
Public Type AirHockeyGame Players() As Player Ball As Sprite TablePics As Table Reset As Long Resetting As Boolean End Type
Now in "CreateGame" we will need to set Resetting to false and Reset to 0. In "Run" we will check to see if Resetting it true and if so it will wait 3 seconds before re-spawning the ball. So when the ball goes in the goal we will change the value of Resetting to true and also set Reset to 0. Now we will add this code to "Run" after the "Draw" call.
If Game.Resetting Then Call Reset(Game) End If
Next we will create "Reset", but first we need to load a big font as we will need to use that in "Reset".
Call NewFont("ArialBig", "arial.ttf", 400)
Now for "Reset". To do this we will call the "StartBall" method that we created earlier. We will also reset the location of the bats and the vectors of the bats.
Private Sub Reset(Game As AirHockeyGame) Dim i As Long Game.Reset = Game.Reset + 1 If Game.Reset < 60 Then Call Text.DrawText("3", yellow, GameFont("ArialBig"), 290, 80) Else If Game.Reset < 120 Then Call Text.DrawText("2", yellow, GameFont("ArialBig"), 290, 80) Else If Game.Reset < 180 Then Call Text.DrawText("1", yellow, GameFont("ArialBig"), 290, 80) Else Game.Reset = 0 Game.Resetting = False Call StartBall(Game) For i = 0 To 1 Call Game.Players(i).Bat.setX(CenterOfTable - Graphics.CurrentWidth(Game.Players(i).Bat) / 2) Call Game.Players(i).Bat.setY(100 - 360 * (i - 1)) Call Game.Players(i).Bat.SetMovementVector(Physics.CreateVector_NoInvert(0, 0)) Next i End If End If End If End Sub
Now when the ball goes in the goal we can still see it, so to fix this we will change draw to look like this.
Private Sub Draw(Game As AirHockeyGame) Call Graphics.ClearScreen_ToColour(Core.GetColor(159, 207, 241)) Call Graphics.DrawBitmap(Game.TablePics.BackGround, 0, 0) If Game.Resetting = False Then Call Graphics.DrawSprite(Game.Ball) End If Call Graphics.DrawSprite(Game.Players(0).Bat) Call Graphics.DrawSprite(Game.Players(1).Bat) End Sub
Displaying The Score
To display the score we will draw it in the "Draw" method we have. to do this add this code to "Draw". You will also need to load the font "CourierBigger" which has the size 25 of the font "cour.ttf".
Call Text.DrawText("Red", white, GameFont("CourierBigger"), 75, 105) Call Text.DrawText("Blue", white, GameFont("CourierBigger"), 670, 105) Call Text.DrawText(Game.Players(1).Score, white, GameFont("CourierBigger"), 95, 160) Call Text.DrawText(Game.Players(0).Score, white, GameFont("CourierBigger"), 690, 160)

