Air Hockey Tutorial 8

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

Adding A Splash Screen

Now we need to a screen that will displayed when the game starts up and when the game is paused. First we will need to load the image.

Call NewImage("Menu", "air_hockey_logo.png")

Now that the image is loaded we can set up our menu screen. To do this we need to create a method called "SplashScreen". This will display how to play the game and also introduce our game.

Private Sub SplashScreen()
    Dim i As Long
    i = 0
    Dim sec As Long
    sec = 5
    Do Until MouseAndKey.IsKeyPressed(Keys_VK_RETURN) Or MouseAndKey.IsKeyPressed(Keys_VK_ESCAPE) Or Core.WindowCloseRequested()
        Call Graphics.DrawBitmap(GameImage("Menu"), 0, 0)
        If i < 60 * sec Then
            Call Text.DrawText("To control Air Hockey", white, GameFont("Comic"), 260, 460)
            Call Text.DrawText("you move the mouse to control the bat", white, GameFont("Comic"), 150, 490)
        Else
            If i < 120 * sec Then
                Call Text.DrawText("To pause the game press 'P'", white, GameFont("Comic"), 225, 460)
            Else
                If i < 180 * sec Then
                    Call Text.DrawText("To reset the ball if it gets stuck press 'R'", white, GameFont("Comic"), 125, 460)
                Else
                    If i < 240 * sec Then
                        Call Text.DrawText("To exit the game press 'Esc'", white, GameFont("Comic"), 215, 460)
                    Else
                        Call Text.DrawText("Press Enter", white, GameFont("Comic"), 330, 460)
                    End If
                End If
            End If
        End If
        Call Core.RefreshScreen_WithFrame(60)
        Call Core.ProcessEvents
        i = i + 1
    Loop
End Sub

This code looks very similar to the "Reset" method in how the text gets displayed. This method will get displayed until user presses enter or tries to exit the program. This will be called just after we call "StartUp".

Adding a Pause Menu

Now we want the game to be paused when the user presses a key. To do this we will need to add this code to "Run".

If MouseAndKey.IsKeyPressed(Keys_VK_P) Then
    Call MouseAndKey.ShowHideMouse(True)
    Call Pause(game)
    Call MouseAndKey.ShowHideMouse(False)
End If

This will allow the user to use there mouse and also display our pause screen. In "Pause" we will need to display our screen and also allow the user to start a new game.

Private Sub Pause(game As AirHockeyGame)
    Do Until MouseAndKey.IsKeyPressed(Keys_VK_RETURN) Or MouseAndKey.IsKeyPressed(Keys_VK_ESCAPE) Or Core.WindowCloseRequested() Or MouseAndKey.IsKeyPressed(Keys_VK_N)
        Call Core.ProcessEvents
        Call Graphics.DrawBitmap(GameImage("Menu"), 0, 0)
        Call Text.DrawText("Paused, press 'Enter'", white, GameFont("Comic"), 265, 460)
        Call Text.DrawText("For a new game press 'N'", white, GameFont("Comic"), 240, 490)
        Call Core.RefreshScreen_WithFrame(60)
        
        If MouseAndKey.IsKeyPressed(Keys_VK_N) Then
            game.Reset = 200
            game.Players(0).Score = 0
            game.Players(1).Score = 0
            game.Resetting = True
            Call Reset(game)
        End If
    Loop
End Sub

If the user presses 'N' "Reset" will get called and as we don't want the 3-2-1 to be shown we will need to set the value of Reset to a number larger than 180.

Download

Here is a copy of everything that is covered in this tutorial Air Hockey Tutorial 8.zip

List Of Tutorials

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