Thursday, November 4, 2010

Pong in VB.NET!

Hey there everyone, today I am going to show you how you can make pong in visual basic.net.  Please note that I am not going to put all of the code up here because that would be crazy.

This is what the main form looks like:

This is all drawn with objects from the tool bar, no special graphics experience required.

Now lets look at some code:
First of all, here are the variables used:

Dim speed As Single = 10 'This stores the speed of the ball
Dim rndInst As New Random() 'This is a random number generator
Dim xAng As Single = Math.Cos(rndInst.Next(5, 10)) 'X and Y
Dim yAng As Single = Math.Sin(rndInst.Next(5, 10)) 'Angles
Dim xVel As Single = xAng * speed 'The x and y velocities
Dim yVel As Single = yAng * speed 'Computed from angle and speed
Dim pScore As Int16 = 0 'Player's score
Dim cScore As Int16 = 0 'Computer's score
Dim cSpeed As Int16 = 10 'Movement speed of computer (difficulty)
Dim p As Boolean 'Is the game paused?



I used a timer to control the speed of the game, that way it doesn't go super fast on your brand new gaming computer.  To pause the game I used the code:


p = True
gameTimer.Stop()
speedTimer.Stop()
lblPaused.Visible = True


The speed timer is used to increase the speed every few seconds to make the game progress (otherwise you would be hitting the ball between you and the computer until you or your computer died).  


I made the user control their paddle by moving the mouse, their paddle would simply stay with the mouse as it moved.  The following code was used to accomplish this:



If e.Y > 29 And e.Y < Me.Height - 30 - playerPaddel.Height Then
  playerPaddel.Location = New Point(playerPaddel.Location.X, e.Y)
End If


This code was placed in the 'mouseMove' form event.  The logic in the if statement was used to keep the paddle within the vertical bounds of the screen.


The computer's paddle tried to follow the ball no matter where the ball was in the hopes that it would be in the way of the ball when the ball reached it's side.  The following code was used for the 'AI':



If gameBall.Location.Y > compPaddle.Location.Y Then
If compPaddle.Location.Y < Me.Height - 40 - playerPaddel.Height Then
If compPaddle.Location.Y - (gameBall.Location.Y) > -cSpeed Then
   compPaddle.Location = New Point(compPaddle.Location.X, gameBall.Location.Y)
Else
   compPaddle.Location = New Point(compPaddle.Location.X, compPaddle.Location.Y + cSpeed)
End If


The first if statement just decides if the computer needs to move.  If it is already at the same level as the ball, then it does not need to move.  The second if statement decides if the computer paddle needs to move the full amount it can, or if a smaller amount will get it where it wants to be.  Finally, if it did need to move, and it did need to go as far as it can, then that is exactly what it does.


To update the location of the game ball, the following code was used:


gameBall.Location = New Point(gameBall.Location.X + xVel, gameBall.Location.Y + yVel)


I think that is pretty self-explanatory, so I will move on.  (On a completly unrelated note, the text editor I am using is saying that 'yVel' is spelled wrong, but 'xVel' is not.  I don't know what the deal is there, I am pretty sure that xVel is not a word.)  


The last piece of interesting code is the code that decides when the ball has hit a paddle.  The following was used to complete this task:



If gameBall.Bounds.IntersectsWith(playerPaddel.Bounds) Then
   gameBall.Location = New Point(playerPaddel.Location.X - gameBall.Size.Width, gameBall.Location.Y)
   xVel = -xVel
End If


The same code was used for the walls and for the computer's paddle.  It basically asks if the bounds of the ball are intersecting with the bounds of the player's paddle.  If they are, it changes the position of the ball so that it is not inside the paddle and then makes the ball travel in the opposite x direction.  This results in a very realistic bounce effect without even using elastic physics!


Well, that is pretty much all I have to say,  I hope you enjoyed this and hopefully learned something.  Until next time, 'Happy Coding!'