VERSION 5.00
Begin VB.Form Form1 
   BackColor       =   &H00000000&
   Caption         =   "Pong"
   ClientHeight    =   6660
   ClientLeft      =   750
   ClientTop       =   1590
   ClientWidth     =   10215
   LinkTopic       =   "Form1"
   MaxButton       =   0   'False
   MinButton       =   0   'False
   ScaleHeight     =   6675
   ScaleMode       =   0  'User
   ScaleWidth      =   10215
   Begin VB.CommandButton cmdExit 
      Caption         =   "Exit"
      Height          =   255
      Left            =   7800
      TabIndex        =   2
      TabStop         =   0   'False
      Top             =   6120
      Visible         =   0   'False
      Width           =   615
   End
   Begin VB.CommandButton cmdNewGame 
      Caption         =   "Play"
      Height          =   255
      Left            =   7080
      TabIndex        =   3
      TabStop         =   0   'False
      Top             =   6120
      Visible         =   0   'False
      Width           =   615
   End
   Begin VB.Timer Timer1 
      Interval        =   1
      Left            =   8760
      Top             =   6000
   End
   Begin VB.Label Label2 
      Appearance      =   0  'Flat
      BackColor       =   &H00000000&
      BackStyle       =   0  'Transparent
      Caption         =   "Game Over!"
      ForeColor       =   &H00FFFFFF&
      Height          =   249
      Left            =   5400
      TabIndex        =   1
      Top             =   6120
      Width           =   1095
   End
   Begin VB.Label Label1 
      Appearance      =   0  'Flat
      BackColor       =   &H00000000&
      BackStyle       =   0  'Transparent
      ForeColor       =   &H00FFFFFF&
      Height          =   255
      Left            =   4320
      TabIndex        =   0
      Top             =   6120
      Width           =   855
   End
   Begin VB.Shape shpPlayer1 
      BorderColor     =   &H00FFFFFF&
      FillColor       =   &H00FFFFFF&
      FillStyle       =   0  'Solid
      Height          =   973
      Left            =   480
      Top             =   2760
      Width           =   255
   End
   Begin VB.Shape shpPlayer2 
      BorderColor     =   &H00FFFFFF&
      FillColor       =   &H00FFFFFF&
      FillStyle       =   0  'Solid
      Height          =   973
      Left            =   9480
      Top             =   2760
      Width           =   255
   End
   Begin VB.Shape shpBall 
      BackColor       =   &H00FFFFFF&
      BorderColor     =   &H0000FFFF&
      FillColor       =   &H0000FFFF&
      FillStyle       =   0  'Solid
      Height          =   255
      Left            =   4920
      Shape           =   3  'Circle
      Top             =   3120
      Width           =   255
   End
   Begin VB.Shape shpWallBottom 
      BackColor       =   &H00FFFFFF&
      BorderColor     =   &H00000000&
      FillColor       =   &H00FFFFFF&
      FillStyle       =   0  'Solid
      Height          =   25
      Left            =   0
      Top             =   6625
      Width           =   10215
   End
   Begin VB.Shape shpWallTop 
      BorderColor     =   &H00000000&
      FillColor       =   &H00FFFFFF&
      FillStyle       =   0  'Solid
      Height          =   25
      Left            =   0
      Top             =   0
      Width           =   10215
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
'*****************************************
'Pong - Tutorial 2
'*****************************************
Dim speedY As Integer 'vertical speed of ball
Dim speedX As Integer 'horizontal speed of ball
Dim padSpeed As Integer 'the speed of the players paddle
Dim origPaddleLoc As Integer
Dim origBallLocY As Integer
Dim origBallLocX As Integer
Dim NewBall As Integer
Dim btop, bleft, bwidth, bheight As Integer
Dim p1top, p1left, p1width, p1height As Integer
Dim p2top, p2left, p2width, p2height As Integer

Private Sub cmdExit_Click()
    'unload me
    End
End Sub

Private Sub cmdNewGame_Click()
    'reset game
    cmdNewGame.Visible = False
    cmdExit.Visible = False
    Timer1.Interval = 1
    shpBall.Visible = True
    Label2.Visible = False
    speedX = -150
    speedY = Int((50 * Rnd) + 1) 'generate random value between 1 and 50
    NewBall = 1
End Sub

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)

If KeyCode = vbKeyUp Then    'the up key
    padSpeed = -150
ElseIf KeyCode = vbKeyDown Then    'the down key
    padSpeed = 150
End If

End Sub

Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
    padSpeed = 0    'stop the paddle from moving
End Sub

Private Sub Form_Load()
    'center form
    Move (Screen.Width - Width) \ 2, (Screen.Height - Height) \ 2
    Label2.Visible = False   '"Game Over!"
    speedX = -150
    speedY = Int((50 * Rnd) + 1) 'generate random value between 1 and 50

    'record the origional starting locations for everything
    origPaddleLoc = shpPlayer1.Top
    origBallLocX = shpBall.Left
    origBallLocY = shpBall.Top
    NewBall = 1
    Timer1.Interval = 0
    shpBall.Visible = False
    cmdExit.Visible = True
    cmdNewGame.Visible = True

End Sub

Private Sub Timer1_Timer()
    'move the ball
    shpBall.Top = shpBall.Top + speedY
    shpBall.Left = shpBall.Left + speedX

    'check to see if the ball's hit a wall
    If shpBall.Top + shpBall.Height >= shpWallBottom.Top Then
        shpBall.Top = shpWallBottom.Top - shpBall.Height
        speedY = -speedY    'use speedY = -(speedY / 2) to slow down ball
        Beep
    Else
        If shpBall.Top <= shpWallTop.Top + shpWallTop.Height Then
            shpBall.Top = shpWallTop.Top + shpWallTop.Height
            speedY = -speedY    'use speedY = -(speedY / 2) to slow down ball
            Beep
        End If
    End If

    'move the paddle
    If padSpeed <> 0 Then
        shpPlayer1.Top = shpPlayer1.Top + padSpeed
    End If

    'check to see if the paddle's hit a wall
    If shpPlayer1.Top <= shpWallTop.Top + shpWallTop.Height Then
        shpPlayer1.Top = shpWallTop.Top + shpWallTop.Height
    Else
        If shpPlayer1.Top + shpPlayer1.Height >= shpWallBottom.Top Then
            shpPlayer1.Top = shpWallBottom.Top - shpPlayer1.Height
        End If
    End If

    If shpPlayer2.Top <= shpWallTop.Top + shpWallTop.Height Then
        shpPlayer2.Top = shpWallTop.Top + shpWallTop.Height
    Else
        If shpPlayer2.Top + shpPlayer2.Height >= shpWallBottom.Top Then
            shpPlayer2.Top = shpWallBottom.Top - shpPlayer2.Height
        End If
    End If

    'move the computer player's paddle with ball
    If shpBall.Top < shpPlayer2.Top Then
        shpPlayer2.Top = shpPlayer2.Top - 250
    Else
        If shpBall.Top > shpPlayer2.Top + shpPlayer2.Height Then
            shpPlayer2.Top = shpPlayer2.Top + 250
        End If
    End If

    btop = shpBall.Top
    bleft = shpBall.Left
    bheight = shpBall.Height
    bwidth = shpBall.Width

    'parameters for paddle one
    p1top = shpPlayer1.Top
    p1left = shpPlayer1.Left
    p1width = shpPlayer1.Width
    p1height = shpPlayer1.Height

    'parameters for paddle two
    p2top = shpPlayer2.Top
    p2left = shpPlayer2.Left
    p2width = shpPlayer2.Width
    p2height = shpPlayer2.Height

    'if the ball has hit player 1's paddle
    If bleft <= p1left + p1width And bleft >= p1left - (p1width) Then
        If btop + bheight >= p1top And btop <= p1top + p1height Then
            'calculate the direction the ball will deflect off paddle
            tmp = ((p1top + (p1height / 2)) - (btop + (bheight / 2))) * 0.55
            speedY = speedY + -(tmp / 2)
            Beep
            'move ball to right edge of paddle
            shpBall.Left = p1left + p1width
            'deflect the ball
            speedX = -speedX
        End If
    End If

    'reset new value of ball
    bleft = shpBall.Left

    'if the ball has hit player 2's paddle
    If bleft + bwidth >= p2left And bleft <= p2left + p2width Then
        If btop + bheight >= p2top And btop <= p2top + p2height Then
            'calculate the direction the ball will deflect off paddle
            tmp = ((p2top + (p2height / 2)) - (btop + (bheight / 2))) * 0.55
            speedY = speedY + -(tmp / 2)
            Beep
            'move ball to left edge of paddle
            shpBall.Left = p2left - bwidth
            'deflect the ball
            speedX = -speedX
        End If
    End If

    'did ball go pass player one
    If shpBall.Left + shpBall.Width < 0 Then
        'reset the ball and paddles to their origional location
        shpBall.Left = origBallLocX
        shpBall.Top = origBallLocY
        shpPlayer1.Top = origPaddleLoc
        shpPlayer2.Top = origPaddleLoc
        speedX = -150
        speedY = Int((50 * Rnd) + 1) 'generate random value between 1 and 50
        NewBall = NewBall + 1
    Else
        'reset if the ball goes pass the computer's paddle
        If shpBall.Left > Form1.Width Then
            'reset the ball and paddles to their origional location
            shpBall.Left = origBallLocX
            shpBall.Top = origBallLocY
            shpPlayer1.Top = origPaddleLoc
            shpPlayer2.Top = origPaddleLoc
            speedX = 150
            speedY = Int((50 * Rnd) + 1) 'generate random value between 1 and 50
            NewBall = NewBall + 1
        End If
    End If
    If NewBall > 3 Then
        Timer1.Interval = 0
        shpBall.Visible = False
        Label2.Visible = True   '"Game Over!"
        cmdExit.Visible = True
        cmdNewGame.Visible = True
    Else
        Label1.Caption = "Ball " & Str(NewBall)
    End If
End Sub
