1. 'I added this to a module
  2. Public Type POINTAPI
  3. X As Long
  4. Y As Long
  5. End Type
  6. Public Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
  7. Public ballDirectionX As Integer
  8. Public ballDirectionY As Integer
  9. Public ballSpeed As Integer
  10. Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
  11. ' I added everything below to my form - performance will depend on your machine, just try to tweak it
  12. Private Sub Form_Load()
  13. ' Initialize ball direction and speed
  14. ballDirectionX = 12
  15. ballDirectionY = 3
  16. ballSpeed = 8250
  17. ' Initialize the starting positions of the paddles
  18. ' Place the left paddle (paddle1) towards the left side of the form
  19. paddle1.Top = 222
  20. paddle1.Left = 310 ' Adjust this value to move paddle1 more to the left as needed
  21. ' Place the ball towards the left side, near the left paddle
  22. ball.Top = 0
  23. ball.Left = 0 ' Adjust this value to position the ball more to the left as needed
  24. ' Initialize the position of the right paddle (paddle2)
  25. paddle2.Top = Me.InsideHeight / 2 - paddle2.Height / 2
  26. ' Set the Timer Interval for continuous movement
  27. Me.TimerInterval = 2
  28. End Sub
  29. Private Sub Form_Resize()
  30. ' If you need to reposition elements on form resize
  31. ball.Left = (Me.InsideWidth - ball.Width) / 22
  32. ball.Top = (Me.InsideHeight - ball.Height) / 220
  33. End Sub
  34. Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
  35. ' Move paddle1 with the mouse (Optional)
  36. If Y > 4 And Y < Me.InsideHeight - paddle1.Height Then
  37. paddle1.Top = Y
  38. End If
  39. If Y < Box7.Top Then
  40. paddle1.Top = Box7.Top
  41. ElseIf Y + paddle1.Height > Box7.Top + Box7.Height Then
  42. paddle1.Top = Box7.Top + Box7.Height - paddle1.Height
  43. Else
  44. paddle1.Top = Y
  45. End If
  46. End Sub
  47. Private Sub Form_Timer()
  48. ' Ball movement logic
  49. ball.Left = ball.Left + ballDirectionX
  50. ball.Top = ball.Top + ballDirectionY
  51. ' Collision detection with the top and bottom of Box7
  52. If ball.Top < Box7.Top Then
  53. ball.Top = Box7.Top ' Position the ball at the top boundary of Box7
  54. ballDirectionY = -ballDirectionY ' Reverse the vertical direction
  55. ElseIf ball.Top + ball.Height > Box7.Top + Box7.Height Then
  56. ball.Top = Box7.Top + Box7.Height - ball.Height ' Position the ball at the bottom boundary of Box7
  57. ballDirectionY = -ballDirectionY ' Reverse the vertical direction
  58. End If
  59. ' Collision detection with the paddles
  60. If (ball.Left <= paddle1.Left + paddle1.Width And ball.Top + ball.Height >= paddle1.Top And ball.Top <= paddle1.Top + paddle1.Height) _
  61. Or (ball.Left + ball.Width >= paddle2.Left And ball.Top + ball.Height >= paddle2.Top And ball.Top <= paddle2.Top + paddle2.Height) Then
  62. ballDirectionX = -ballDirectionX
  63. End If
  64. ' Check for collision with the right side of Box7
  65. If ball.Left + ball.Width > Box7.Left + Box7.Width Then
  66. If ball.Top + ball.Height < paddle2.Top Or ball.Top > paddle2.Top + paddle2.Height Then
  67. Me.decision.Caption = "Player 1 Wins!"
  68. Me.TimerInterval = 0 ' Stop the game
  69. Else
  70. ballDirectionX = -ballDirectionX ' Reflect the ball
  71. End If
  72. End If
  73. ' Check for collision with the left side of Box7
  74. If ball.Left < Box7.Left Then
  75. If ball.Top + ball.Height < paddle1.Top Or ball.Top > paddle1.Top + paddle1.Height Then
  76. Me.decision.Caption = "Player 2 Wins!"
  77. Me.TimerInterval = 0 ' Stop the game
  78. Else
  79. ballDirectionX = -ballDirectionX ' Reflect the ball
  80. End If
  81. End If
  82. ' Refresh the form to update the positions of the controls
  83. Me.Repaint
  84. End Sub
  85. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
  86. Const KEY_UP As Integer = 38 ' Up Arrow Key for paddle1
  87. Const KEY_DOWN As Integer = 40 ' Down Arrow Key for paddle1
  88. Const KEY_P As Integer = 80 ' P key for paddle2
  89. Const KEY_L As Integer = 76 ' L key for paddle2
  90. Const PaddleMoveSpeed As Integer = 40 ' Increase this value to make paddle1 move faster
  91. Const PaddleMoveSpeed2 As Integer = 40 ' Increase this value to make paddle2 move faster
  92. Select Case KeyCode
  93. Case KEY_UP
  94. ' Move paddle1 up faster
  95. If paddle1.Top > Box7.Top Then
  96. paddle1.Top = paddle1.Top - PaddleMoveSpeed
  97. End If
  98. KeyCode = 0 ' Prevent further processing of the key
  99. Case KEY_DOWN
  100. ' Move paddle1 down faster
  101. If paddle1.Top + paddle1.Height < Box7.Top + Box7.Height Then
  102. paddle1.Top = paddle1.Top + PaddleMoveSpeed
  103. End If
  104. KeyCode = 0 ' Prevent further processing of the key
  105. Case KEY_P
  106. ' Move paddle2 up
  107. If paddle2.Top > Box7.Top Then
  108. paddle2.Top = paddle2.Top - PaddleMoveSpeed2
  109. End If
  110. KeyCode = 0 ' Prevent further processing of the key
  111. Case KEY_L
  112. ' Move paddle2 down
  113. If paddle2.Top + paddle2.Height < Box7.Top + Box7.Height Then
  114. paddle2.Top = paddle2.Top + PaddleMoveSpeed2
  115. End If
  116. KeyCode = 0 ' Prevent further processing of the key
  117. End Select
  118. End Sub