- 'I added this to a module
- Public Type POINTAPI
- X As Long
- Y As Long
- End Type
- Public Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
- Public ballDirectionX As Integer
- Public ballDirectionY As Integer
- Public ballSpeed As Integer
- Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
- ' I added everything below to my form - performance will depend on your machine, just try to tweak it
- Private Sub Form_Load()
- ' Initialize ball direction and speed
- ballDirectionX = 12
- ballDirectionY = 3
- ballSpeed = 8250
- ' Initialize the starting positions of the paddles
- ' Place the left paddle (paddle1) towards the left side of the form
- paddle1.Top = 222
- paddle1.Left = 310 ' Adjust this value to move paddle1 more to the left as needed
- ' Place the ball towards the left side, near the left paddle
- ball.Top = 0
- ball.Left = 0 ' Adjust this value to position the ball more to the left as needed
- ' Initialize the position of the right paddle (paddle2)
- paddle2.Top = Me.InsideHeight / 2 - paddle2.Height / 2
- ' Set the Timer Interval for continuous movement
- Me.TimerInterval = 2
- End Sub
- Private Sub Form_Resize()
- ' If you need to reposition elements on form resize
- ball.Left = (Me.InsideWidth - ball.Width) / 22
- ball.Top = (Me.InsideHeight - ball.Height) / 220
- End Sub
- Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
- ' Move paddle1 with the mouse (Optional)
- If Y > 4 And Y < Me.InsideHeight - paddle1.Height Then
- paddle1.Top = Y
- End If
- If Y < Box7.Top Then
- paddle1.Top = Box7.Top
- ElseIf Y + paddle1.Height > Box7.Top + Box7.Height Then
- paddle1.Top = Box7.Top + Box7.Height - paddle1.Height
- Else
- paddle1.Top = Y
- End If
- End Sub
- Private Sub Form_Timer()
- ' Ball movement logic
- ball.Left = ball.Left + ballDirectionX
- ball.Top = ball.Top + ballDirectionY
- ' Collision detection with the top and bottom of Box7
- If ball.Top < Box7.Top Then
- ball.Top = Box7.Top ' Position the ball at the top boundary of Box7
- ballDirectionY = -ballDirectionY ' Reverse the vertical direction
- ElseIf ball.Top + ball.Height > Box7.Top + Box7.Height Then
- ball.Top = Box7.Top + Box7.Height - ball.Height ' Position the ball at the bottom boundary of Box7
- ballDirectionY = -ballDirectionY ' Reverse the vertical direction
- End If
- ' Collision detection with the paddles
- If (ball.Left <= paddle1.Left + paddle1.Width And ball.Top + ball.Height >= paddle1.Top And ball.Top <= paddle1.Top + paddle1.Height) _
- Or (ball.Left + ball.Width >= paddle2.Left And ball.Top + ball.Height >= paddle2.Top And ball.Top <= paddle2.Top + paddle2.Height) Then
- ballDirectionX = -ballDirectionX
- End If
- ' Check for collision with the right side of Box7
- If ball.Left + ball.Width > Box7.Left + Box7.Width Then
- If ball.Top + ball.Height < paddle2.Top Or ball.Top > paddle2.Top + paddle2.Height Then
- Me.decision.Caption = "Player 1 Wins!"
- Me.TimerInterval = 0 ' Stop the game
- Else
- ballDirectionX = -ballDirectionX ' Reflect the ball
- End If
- End If
- ' Check for collision with the left side of Box7
- If ball.Left < Box7.Left Then
- If ball.Top + ball.Height < paddle1.Top Or ball.Top > paddle1.Top + paddle1.Height Then
- Me.decision.Caption = "Player 2 Wins!"
- Me.TimerInterval = 0 ' Stop the game
- Else
- ballDirectionX = -ballDirectionX ' Reflect the ball
- End If
- End If
- ' Refresh the form to update the positions of the controls
- Me.Repaint
- End Sub
- Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
- Const KEY_UP As Integer = 38 ' Up Arrow Key for paddle1
- Const KEY_DOWN As Integer = 40 ' Down Arrow Key for paddle1
- Const KEY_P As Integer = 80 ' P key for paddle2
- Const KEY_L As Integer = 76 ' L key for paddle2
- Const PaddleMoveSpeed As Integer = 40 ' Increase this value to make paddle1 move faster
- Const PaddleMoveSpeed2 As Integer = 40 ' Increase this value to make paddle2 move faster
- Select Case KeyCode
- Case KEY_UP
- ' Move paddle1 up faster
- If paddle1.Top > Box7.Top Then
- paddle1.Top = paddle1.Top - PaddleMoveSpeed
- End If
- KeyCode = 0 ' Prevent further processing of the key
- Case KEY_DOWN
- ' Move paddle1 down faster
- If paddle1.Top + paddle1.Height < Box7.Top + Box7.Height Then
- paddle1.Top = paddle1.Top + PaddleMoveSpeed
- End If
- KeyCode = 0 ' Prevent further processing of the key
- Case KEY_P
- ' Move paddle2 up
- If paddle2.Top > Box7.Top Then
- paddle2.Top = paddle2.Top - PaddleMoveSpeed2
- End If
- KeyCode = 0 ' Prevent further processing of the key
- Case KEY_L
- ' Move paddle2 down
- If paddle2.Top + paddle2.Height < Box7.Top + Box7.Height Then
- paddle2.Top = paddle2.Top + PaddleMoveSpeed2
- End If
- KeyCode = 0 ' Prevent further processing of the key
- End Select
- End Sub