So, starting out with an empty form, go to code view:
Code:
Public Class Form1
End Class
Let's put our image variables in:
Code:
Public Class Form1
'Image variables
Private m_BallImage As Bitmap = My.Resources.ball
Private m_PaddleImage As Bitmap = My.Resources.paddle
Private m_BackgroundImage As Bitmap = My.Resources.background
End Class
Okay! Now, the "real" variables:
Code:
Public Class Form1
'Image variables
Private m_BallImage As Bitmap = My.Resources.ball
Private m_PaddleImage As Bitmap = My.Resources.paddle
Private m_BackgroundImage As Bitmap = My.Resources.background
'Variables
Private m_BallLocation As Point = New Point(0, 0)
Private m_BallDirection As Point
Private m_Paddle1 As Integer = 0
Private m_Paddle2 As Integer = 0
End Class
Next, a method for initialization. We want this to be run when the form loads, so why not just put it in the Load handler? Double-click on your form to bring up this code:
Code:
Public Class Form1
'Image variables
Private m_BallImage As Bitmap = My.Resources.ball
Private m_PaddleImage As Bitmap = My.Resources.paddle
Private m_BackgroundImage As Bitmap = My.Resources.background
'Variables
Private m_BallLocation As Point = New Point(0, 0)
Private m_BallDirection As Point
Private m_Paddle1 As Integer = 0
Private m_Paddle2 As Integer = 0
'Load handler
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
End Sub
End Class
And we'll put some initialization in there. First of all, we'll set the form's size.
Code:
Me.ClientSize = New Size(800, 600)
Another use of the Size type! (Okay, I said I'd use constants, but I don't intend to change it from 800x600. You can if you want.)
Then, we want the ball to start at the center of the form:
Code:
m_BallLocation = New Point(CInt(Me.ClientSize.Width / 2 - m_BallImage.Width / 2), CInt(Me.ClientSize.Height / 2 - m_BallImage.Height / 2))
The paddles can start at 0, 0 - that's fine. Now, we want the ball to go in a random direction. We'll set the m_BallDirection variable using the Random object:
Code:
Dim r As New Random()
m_BallDirection = New Point(r.Next(-7, 8), r.Next(-7, 8))
There! Your code should now look like this:
Code:
Public Class Form1
'Image variables
Private m_BallImage As Bitmap = My.Resources.ball
Private m_PaddleImage As Bitmap = My.Resources.paddle
Private m_BackgroundImage As Bitmap = My.Resources.background
'Variables
Private m_BallLocation As Point = New Point(0, 0)
Private m_BallDirection As Point
Private m_Paddle1 As Integer = 0
Private m_Paddle2 As Integer = 0
'Load handler
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
Me.ClientSize = New Size(800, 600)
m_BallLocation = New Point(CInt(Me.ClientSize.Width / 2 - m_BallImage.Width / 2), CInt(Me.ClientSize.Height / 2 - m_BallImage.Height / 2))
Dim r As New Random()
m_BallDirection = New Point(r.Next(-7, 8), r.Next(-7, 8))
End Sub
End Class
Now, all that's left is the true game code, which is not as hard as it sounds. Coming up next!