Air Hockey Tutorial 1
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.
You can view the final product on the Air Hockey page. This includes a promo video, screen shots, and a download of the completed game as an executable and as source code.
Things You Need
- Motivation to create a game.
- Knowledge of procedural programming.
- Visual Basic 6.
- Image manipulation program (preferably Photoshop but Gimp is also good)
Starting The Project
You will need to download the latest version of SwinGameSDK for VB6. You can obtain a copy of SDK from the Download page. Once you have downloaded the SDK you need to register the Library. This can be done by running "SGSDKVB6.reg". Now that the Library is registered we add a reference to it in VB6. To add a reference to SwinGame follow these steps
- Open the project "VB6 Starter Pack.vbp"
- Go to the "Project" menu
- Select the "References..." option
- Find "SGSDKVB6" in the list of references
- Put a tick in in the box next to SGSDKVB6
- Now click OK
The reference should now be added.
Compiling The Project
To Compile the project follow these steps
- Have VB6 open with the "VB6 Starter Pack.vbp" loaded
- Go the "File" menu
- Select the "Make VB6 Starter Pack.exe"
- Now navigate to the folder that "VB6 Starter Pack.vbp" is in
- Click on OK
- Now go to My Computer and navigate to the folder that "VB6 Starter Pack.vbp" is in
- Open up "copyresourcesVB6.cmd".
- This has created a bin folder. In this folder is your "VB6 Starter Pack.exe"
It is also a good idea to create a short cut to "VB6 Starter Pack.exe" and place this in the main folder.
Starting To Make The Game
Creating And Closing The Window
Inside GameLogic there is the Sub called "Run", this is where we will start writing some code. We are going to create a Sub called "StartUp" that "Run" will call to create a graphics window. "StartUp" will look like this
Private Sub StartUp() Call Audio.OpenAudio Call Core.OpenGraphicsWindow("Air Hockey", 800, 600) Call LoadResources End Sub
This will allow the game to make sounds, open a graphics window and load all of the resources to be used in the game. Next we will create a loop that will exit when the user wants to close the window. This will go after "StartUp was called in "Run".
Do Until MouseAndKey.IsKeyPressed(Keys_VK_ESCAPE) Or Core.WindowCloseRequested() Call Core.ProcessEvents Call Core.RefreshScreen_WithFrame(60) Loop
When the user hits the X or the Esc button it will close. The ProcessEvents will allows SwinGame to process events that have happend like the user pressing a key. RefreshScreen_WithFrame(60) will refresh the screen 60 times a second. After this we need to close everything, so we will create a Sub called "ShutDown". "ShutDown" will look like this
Private Sub ShutDown() Call FreeResources Call Audio.CloseAudio End Sub
This will free the resources and close the audio.
GameLogic should look like this
Option Explicit Sub Run() Call StartUp Do Until MouseAndKey.IsKeyPressed(Keys_VK_ESCAPE) Or Core.WindowCloseRequested() Call Core.ProcessEvents Call Core.RefreshScreen_WithFrame(60) Loop Call ShutDown End Sub Private Sub StartUp() Call Audio.OpenAudio Call Core.OpenGraphicsWindow("Air Hockey", 800, 600) Call LoadResources End Sub Private Sub ShutDown() Call FreeResources Call Audio.CloseAudio End Sub
Setting Up Data Types
First we need to set up our data structure as this will make it a lot easer to manage the game when it gets complex.
For an Air hockey game you need:
- A game
A game needs:
- 2 Players
- A Ball
- A Table
A Player needs:
- A Bat
- A Score
- A Goal
A Table needs:
- A Background
- A Horizontal collision edge
- A Vertical collision edge
Now that we know what we need to make a Air Hockey game we can now create the data types. First we will create a new module called DataTypes. Now we will create the game type. It should look something like this
Public Type AirHockeyGame Players() As Player Ball As Sprite TablePics As Table End Type
Now that the game type is created we can now make the player and table types. The player type should look like this
Public Type Player Bat As Sprite Score As Long Goal As Sprite End Type
The table type should look something like this
Public Type Table BackGround As Bitmap TableHorizontal As Sprite TableVertical As Sprite End Type
Here is a copy of everything that is covered in this tutorial Air Hockey Tutorial 1.zip