Air Hockey Tutorial 2
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 |
Creating an AirHockeyGame Type
In GameLogic we are going to create a function called "CreateGame" which will return a AirHockeyGame. This will start to set up the data structure for the our game. The data that needs to be in the an AirHockeyGame data type is Players(), Ball and TablePics. First before we can create the data types we need to load the pictures so we can use them. The pictures are here for you to download. To do this go into "GameResources" and in the "LoadImages" method put this code:
Call NewImage("ball", "ball1.png") Call NewImage("Table", "air_hocky_table.png") Call NewImage("TableHorizontal", "air_hocky_table_collision_horizontal.png") Call NewImage("TableVertical", "air_hocky_table_collision_vertical.png")
At the moment we will create the Ball and TablePics.
Private Function CreateGame() As AirHockeyGame CreateGame.TablePics = CreateTable Set CreateGame.Ball = New Sprite Set CreateGame.Ball = Graphics.CreateSprite(GameImage("ball")) Call CreateGame.Ball.SetUsePixelCollision(True) End Function
In here we have created a game with a ball and a table, but the table is empty. Now we need to create a table using a function called "CreateTable" which is called in "CreateGame". This will return all of our pictures for our table.
Private Function CreateTable() As Table Set CreateTable.BackGround = New Bitmap Set CreateTable.BackGround = GameImage("Table") Set CreateTable.TableHorizontal = New Sprite Set CreateTable.TableHorizontal = Graphics.CreateSprite(GameImage("TableHorizontal")) Set CreateTable.TableVertical = New Sprite Set CreateTable.TableVertical = Graphics.CreateSprite(GameImage("TableVertical")) Call CreateTable.TableVertical.SetUsePixelCollision(True) Call CreateTable.TableHorizontal.SetUsePixelCollision(True) End Function
Drawing The Table
Now that we have loaded the pictures for the table we need to display them. For this we are going to make a Draw method that will draw everything we want to the screen. At the moment all we want to do is draw the table and change the background colour. So Draw will 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) End Sub
Now you need to call Draw in the loop that is in Run, Run should now be:
Sub Run() Call StartUp Dim game As AirHockeyGame Do Until MouseAndKey.IsKeyPressed(Keys_VK_ESCAPE) Or Core.WindowCloseRequested() Call Draw(game) Call Core.ProcessEvents Call Core.RefreshScreen_WithFrame(60) Loop Call ShutDown End Sub
Setting The Constants Of The Table
To make some of the code easer to read we will add some constants. These will be things like the position of the sides and so on.
Public Const TopOfTable As Long = 70 Public Const BottomOfTable As Long = 530 Public Const RightOfTable As Long = 584 Public Const LeftOfTable As Long = 216 Public Const MiddleOfTable As Long = 300 Public Const CenterOfTable As Long = 400
Download
Here is a copy of everything that is covered in this tutorial Air Hockey Tutorial 2.zip

