I am programming Atari Breakout in VB.NET, and I need to program my ball to detect collisions with the paddle, blocks, and the edges of the gameboard. I have written out a small piece of code, but I am unsure of what to do next. Essentially, when the ball touches either the blocks, the paddle, or the edges of the game board, it needs to bounce off of it and move in a different direction. For the moment I am only focusing on the vertical movement of the ball, so the ball will move down, but then must move upwards when it hits the paddle.
The way I am moving the picturebox I am using as the ball is by setting up a timer, and every time the timer ticks, the ball will move at a certain pace. To achieve this, I use the following piece of code:
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Ball.Top += 10
End Sub
This piece of code can be adjusted to get the ball to move different directions. So for example, I can change the plus to a minus to get the ball to move upwards, and I can also use Ball.Left to control sideways movement. I can even combine the two together to get diagonal movement. I can also edit the speed at which the ball moves. Right now it is on 10, which is more than fast enough for the game I am making.
What I want to do is program the ball so that, when it is touching another object (in this case, a picturebox, which I am using as my paddle), the direction of travel is reversed, so it will move upwards instead of downwards. I tried to do this in the code written below, however this does not work as the ball simply stops when it gets close to the paddle, and won't move until I move the paddle, only for it to keep moving downwards.
Here is the code I have written:
Private Sub Ball_Move(sender As Object, e As EventArgs) Handles Ball.Move
If Ball.Bounds.IntersectsWith(Platform.Bounds) Then
Ball.Top -= 10
End If
End Sub
UPDATE:
I thought I'd quickly edit this just so I can show what I'm trying to do more clearly, as my code goes over the character limit for comments.
First of all, I'd like to thank @jmcilhinney for sharing the code. When I tried to use it in my program however, it didn't work properly. I tried to place it within the subroutine I am using for the movement of the ball (posted in my initial question, uses a timer to allow the ball to move). The main issue is that, when the program is run, the ball goes straight past my paddle, which is a green PictureBox at the location of 318, 397. Another issue is that, when I added the code to my program, it showed a lot of errors. Some of which I was able to amend using the quick actions menu, but some I was not able to amend. I've tried to move the code to different parts of the same subroutine to no avail. Here is the subroutine with the collision code in it that is giving me the errors:
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
'Direction of movement. +1 is down and -1 is up.
Private direction As Integer = &B1
Private Sub MoveControl(ctrl As Control)
Dim direction As Integer = Nothing
'Move the control is the current direction.
ctrl.Top += direction * 10
'If the control has reached the top or the bottom of the form...
If ctrl.Top <= 0 OrElse ctrl.Bottom >= ClientSize.Height Then
'...reverse the direction.
direction *= -1
End If
End Sub
Ball.Top += 10
End Sub
End Class
Some sections of the code look different to the code that was posted in @jmcilhinney's answer, but that is because I had to use the quick actions and refactorings menu to change some parts of the code so as to not give me any errors. I may have done something wrong here, because the code just doesn't seem to be working for me.
These are the errors I have with this code: Line 1 has error code BC30026, Line 4 has error code BC30247, Line 5 has error code BC30289, Line 15 has error code BC30188, Line 16 has error code BC30429.
UPDATE 2: I've managed to figure out something that has reduced the number of errors present in my program. What I've done is moved the piece of code calling the Timer subroutine to be after the 'Private direction As Integer' line. My code has gone from having five errors down to just two. Here is the code with the change I have made:
'This part of the code uses a timer to allow to the ball to move. It moves in a certain direction at a certain speed with every tick.'
Private increment As Integer = 10
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Private Sub MoveControl(ctrl As Control)
ctrl.Top += increment
If ctrl.Top <= 0 OrElse ctrl.Bottom >= ClientSize.Height Then
increment *= -1
End If
End Sub
End Class
My first error is present on line 3 (Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick). Here, I am getting error BC30026 'End Sub expected', and the other error I am getting is on line 4 (Private Sub MoveControl (ctrl as Control)). Here, I am getting error BC30289 'Statement cannot appear within a method body. End of method assumed.' However, when I tried to move that particular piece of code outside of the method, I got error BC30026 'End Sub expected.' I am so confused on what to do next. I am still relatively new to VB and I haven't tried something like this before, so this is probably why I am doing everything so wrong. Thanks to jmcilhinney for all the help so far, including providing me with code. I appreciate it a lot!
I should also mention that the code I used here is the second piece of code jmcilhinney posted, rather than the first one. That's why it is slightly different.