Tagged: game, gaming, microsoft, pong, programming, racquetball, visual basic, xna
This topic was published by Jarret Buse and viewed 1578 times since "". The last page revision was "".
- AuthorPosts
As I had mentioned in my previous article, there are a few mistakes in the program. I left those in to show that even though there are mistakes, the program can still work. There are a few quirks that can make game play a little strange, but I will address those.
The first problem is that when a check is made to see if the ball is past the top, bottom or right edge, problems can arise. When the edge is reached, the program simply changes the ball direction. What if the ball is past the edge by four pixels and the direction is changed with a movement of three? The next time around the loop, the ball is still one pixel past the edge. The program determines it is past the edge and changes its direction again to go down and not up. The ball may become farther and farther past the window's edge on every loop. What occurs is that the ball appears to slide along the upper or lower wall. The problem may never rectify itself. To fix this problem, the code needs to set the ball position at the windows edge and change the ball direction:
'bottom If ball.pos.Y > (Window.ClientBounds.Height - t_ball.Height) Then ball.pos.Y = (Window.ClientBounds.Height - t_ball.Height) ball.v_speed *= -1 End If 'top If ball.pos.Y < 0 Then ball.pos.Y = 0 ball.v_speed *= -1 End If 'right If ball.pos.X > Window.ClientBounds.Width - t_paddle1.Width + 1 Then ball.pos.X = Window.ClientBounds.Width - t_paddle1.Width ball.h_speed *= -1 End If
An issue can happen with the paddle, as well, and the code needs to changed in a similar fashion:
If paddle1.pos.Y <= 1 Then paddle1.pos.Y = 1 End If If paddle1.pos.Y >= Window.ClientBounds.Height - t_paddle1.Height Then paddle1.pos.Y = Window.ClientBounds.Height - t_paddle1.Height - 1 End If
In this program, we can worry only about using the keyboard and remove any code for XBOX controllers (remove the following):
If currentPad.Triggers.Left > 0.0 Then paddle1.pos.Y -= paddle1.speed End If If currentPad.Triggers.Right > 0.0 Then paddle1.pos.Y += paddle1.speed End If
In the CheckCollisions() sub, there is a line which has the paddle width hardcoded. In the case that a new paddle image is used, [code]If ball.pos.X < paddle1.pos.X + 43 Then[/code] should be changed to [code]If ball.pos.X < paddle1.pos.X + paddle1.pos.X + t_paddle1.Width Then[/code]
Now, if the paddle image is changed the program will still work as it should.
I hope this helps clear up a few bugs. I found this code on the Internet and decided to use it as an XNA example. Sorry, I do not know the original source for the code or I would give credit for it where it is due.
Further Reading
- XNA Reading Guide - https://dcjtech.info/topic/gaming/#xna
- AuthorPosts