Air Hockey


Option Explicit
Dim puckx As Single, pucky As Single, vpuckx As Single, vpucky As Single
Dim padx1 As Single, padx2 As Single, pady1 As Single, pady2 As Single
Dim pad2x1 As Single, pad2x2 As Single, pad2y1 As Single, pad2y2 As Single
Dim touche As Boolean, touche1 As Boolean
Dim gagne As Boolean
Private Sub Form_Load()
Picture1.MousePointer = 2
puckx = puck.Left
pucky = puck.Top
vpuckx = 0
vpucky = 200
End Sub

Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
paddle1.Left = X – 120
paddle1.Top = max(Y – 120, Line1.Y1)
End Sub
Function max(ByVal a As Single, ByVal b As Single)
If a > b Then
    max = a
    Else
    max = b
End If
End Function
Function min(ByVal a As Single, ByVal b As Single)
If a > b Or a < 0 Then
    min = b
    Else
    min = a
End If
End Function

Private Sub Timer1_Timer()
Dim a As Single
If gagne = True Then
    vpuckx = 100
    vpucky = 0
    If pucky < 100 Then
        Scorered.Caption = Scorered.Caption + 1
        MsgBox max(Scorered.Caption, Scoreblue.Caption) & ” to ” & min(Scorered.Caption, Scoreblue.Caption), vbDefaultButton1, “Red goal”
       
        Else
        Scoreblue.Caption = Scoreblue.Caption + 1
        MsgBox max(Scorered.Caption, Scoreblue.Caption) & ” to ” & min(Scorered.Caption, Scoreblue.Caption), vbDefaultButton1, “Blue goal”
    End If
    pucky = Line1.Y1
    gagne = False
    Exit Sub
End If
If vpucky > 0 Or Abs(vpucky) < 15 Then
    touche = False
    Else
    touche1 = False
End If
If puckx < 0 Then
    Beep
    vpuckx = -0.8 * vpuckx
    puckx = -puckx
End If
If pucky < 0 Then
    If puckx < Line3.X2 And puckx > Line3.X1 Then
        gagne = True
        Exit Sub
    End If
    Beep
    vpucky = -0.8 * vpucky
    pucky = -pucky
End If
If puckx > 3200 Then
    Beep
    vpuckx = -0.8 * vpuckx
    puckx = 2 * 3200 – puckx
End If
If pucky > 5480 Then
    If puckx < Line3.X2 And puckx > Line3.X1 Then
        gagne = True
        Exit Sub
    End If
    Beep
    vpucky = -0.8 * vpucky
    pucky = 2 * 5480 – pucky
End If
puckx = puckx + vpuckx
pucky = pucky + vpucky
puck.Left = puckx
puck.Top = pucky
padx1 = padx2
pady1 = pady2
pad2x1 = pad2x2
pad2y1 = pad2y2
padx2 = paddle1.Left
pady2 = paddle1.Top
If puckx > pad2x2 Then
    paddle2.Left = paddle2.Left + min(vpuckx, cpu.Value)
    Else
    paddle2.Left = paddle2.Left – min(Abs(vpuckx), cpu.Value)
End If
pad2x2 = paddle2.Left
If pucky < Line1.X1 And vpucky < 20 Then
    If pad2y2 – pucky > -240 Then
        paddle2.Top = paddle2.Top – 240
        pad2y2 = paddle2.Top
        Else
        paddle2.Top = paddle2.Top + 100
        pad2y2 = paddle2.Top
    End If
    Else
    If pucky < 500 And pucky > pad2y2 Then
        paddle2.Top = paddle2.Top + 100
        pad2y2 = paddle2.Top
        Else
        If paddle2.Top > 0 Then
            paddle2.Top = paddle2.Top – 120
            pad2y2 = paddle2.Top
        End If
    End If
End If

If Not (touche) And ((puckx – padx2) ^ 2 + (pucky – pady2) ^ 2) ^ (1 / 2) < 240 Then
    a = 240 / ((puckx – padx2) ^ 2 + (pucky – pady2) ^ 2) ^ (1 / 2)
    puckx = padx2 + a * (puckx – padx2)
    pucky = pady2 + a * (pucky – pady2)
    Beep
    touche = True
    nvitesse vpuckx, vpucky, puckx – padx2 + 0.05, pady2 – pucky
    vpuckx = vpuckx + (padx2 – padx1) * Abs(puckx – padx2) / 240
    vpucky = vpucky + (pady2 – pady1) * Abs(pucky – pady2) / 240
End If
If Not (touche1) And ((puckx – pad2x2) ^ 2 + (pucky – pad2y2) ^ 2) ^ (1 / 2) < 240 Then
    a = 240 / ((puckx – padx2) ^ 2 + (pucky – pady2) ^ 2) ^ (1 / 2)
    puckx = pad2x2 + a * (puckx – pad2x2)
    pucky = pad2y2 + a * (pucky – pad2y2)
    Beep
    touche1 = True
    nvitesse vpuckx, vpucky, puckx – pad2x2 + 0.05, pad2y2 – pucky
    vpuckx = vpuckx + (pad2x2 – pad2x1) * Abs(puckx – pad2x2) / 240
    vpucky = vpucky + (pad2y2 – pad2y1) * Abs(pucky – pad2y2) / 240
End If
End Sub
Sub nvitesse(vx As Single, vy As Single, dx As Single, dy As Single)
Dim angle As Single
Dim vx1, vy1 As Single
angle = Atn(dy / dx)
vx1 = vy * Cos(angle) + vx * Sin(angle)
vy1 = vy * Sin(angle) – vx * Cos(angle)
vx = vx1 * Sin(angle) + vy1 * Cos(angle)
vy = vx1 * Cos(angle) – vy1 * Sin(angle)
End Sub

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s