Important alert: (current site time 7/16/2013 4:09:54 AM EDT)
 

VB icon

Code for a basic Pong style game!

Email
Submitted on: 6/4/1999
By: Matthew Eagar 
Level: Not Given
User Rating: By 4 Users
Compatibility: VB 3.0, VB 4.0 (16-bit), VB 4.0 (32-bit), VB 5.0, VB 6.0
Views: 70138
 
     Makes a little pong style game
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
'**************************************
' Name: Code for a basic Pong style game!
' Description:Makes a little pong style game
' By: Matthew Eagar
'
' Assumes:Nothin much, just a very basic knowledge of VB programming.
'
'This code is copyrighted and has' limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=1971&lngWId=1'for details.'**************************************

Dim vmom As Integer 'holds the ball's vertical momentum
Dim hmom As Integer 'holds the ball's horizontal momentum
Private Sub Form_Load()
Randomize
'make the vertical and horizontal momentums random
vmom = 100 + Int(Rnd * 200)
hmom = 100 + Int(Rnd * 200)
End Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
'move the paddle to the mouse's position
Shape1.Left = X - (Shape1.Width / 2)
End Sub
Private Sub Timer1_Timer()
'move the ball, based on the virtical and horizontal momenutm
Shape2.Top = Shape2.Top + vmom
Shape2.Left = Shape2.Left + hmom
'see if the ball is hitting the surface of the paddle
 If (Shape2.Top + Shape2.Height) > Shape1.Top Then
If Shape2.Left + Shape2.Width >= Shape1.Left And Shape2.Left <= Shape1.Left + Shape1.Width Then
vmom = -vmom
End If
End If
'see if the ball has hit the edge of the screen
If (Shape2.Left + Shape2.Width) > Form1.Width Then
Shape2.Left = Form1.Width - Shape2.Width
hmom = -hmom 'this reverses it ball's direction
ElseIf Shape2.Left < 0 Then
Shape2.Left = 0
hmom = -hmom 'this reverses it ball's direction
ElseIf Shape2.Top < 0 Then
Shape2.Top = 0
vmom = -vmom 'this reverses it ball's direction
ElseIf Shape2.Top > Form1.Height Then
MsgBox "You lost!"
Timer1.Enabled = False
End If
End Sub


Other 9 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Not Given category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

6/4/1999 4:16:00 PMMatt

Whoops! I made a mistake in the timer1_timer() procedure: this


If Shape2.Left + Shape2.Width >= Shape1.Left _
Shape2.Left <= Shape2.Left +

should be changed to
If Shape2.Left + Shape2.Width >= Shape1.Left _
And Shape2.Left <= Shape2.Left +

The line was too long so I moved it down to the next one, and deleted a word.

(If this comment was disrespectful, please report it.)

 
6/4/1999 9:37:00 PMkyle

hey,
I just got VB yesterday and i dont know what im doing so i thought i would start with something easy, so i tried this. Well i got the paddle to work but the ball is just sitting there, what should I do?
(If this comment was disrespectful, please report it.)

 
6/5/1999 7:33:00 AMLolindrath

Set your timer interval to 1, then it will work
(If this comment was disrespectful, please report it.)

 
6/5/1999 12:37:00 PMKyle

When your supposed to to change that spot where you missed a word in the code i keep getting a syntax error. What do i do?
(If this comment was disrespectful, please report it.)

 
6/5/1999 6:45:00 PMIan Ippolito

I got it to work:
Just use this code where you are getting the syntax error:
If Shape2.Left + Shape2.Width >= Shape1.Left _
And Shape2.Left <= Shape1.Left + Shape1.Width Then
(If this comment was disrespectful, please report it.)

 
6/7/1999 2:49:00 AM||ooser

everything works but the ball just goes right through the paddle
like it doesnt bounce off
can you help?
(If this comment was disrespectful, please report it.)

 
6/7/1999 7:02:00 PMMatt

Whoops, sorry people, made a major glitch in there. When the ball is on the right side of the paddle, it bounces like it hits the paddle. this block of code:

If (Shape2.Top + Shape2.Height) > Shape1.Top Then
If Shape2.Left + Shape2.Width >= Shape1.Left _
Shape2.Left <= Shape2.Left + Shape2.Width Then
vmom = -vmom
End If

should be changed to this:

If (Shape2.Top + Shape2.Height) > Shape1.Top Then
If Shape2.Left + Shape2.Width >= Shape1.Left And Shape2.Left <= Shape1.Left + Shape1.Width Then
vmom = -vmom
End If

Sorry for any inconvenience, I don't know how I missed something like that.
(If this comment was disrespectful, please report it.)

 
6/8/1999 2:25:00 PMVBbob

everything works great, just one problem. the ball doesn't bounce of the side of the form! can anyone help?
(If this comment was disrespectful, please report it.)

 
6/8/1999 6:39:00 PMCraig

Just wanted to say thankyou. Stuff like this really helps and means a lot. Thanks again!
(If this comment was disrespectful, please report it.)

 
6/9/1999 10:50:00 AMAbdul

umm...this code is real messed up. Hey Matt, can you repost this entire code. Or send me the form or something?
I really want to get this to work.
(If this comment was disrespectful, please report it.)

 
6/11/1999 11:02:00 AMIwinky

I CAN'T MAKE THIS WORK!!!!
The ball doesn't move at all, please e-mail me with the code on how to make this work.

Iwinky
Iwinky@fcmail.com
(If this comment was disrespectful, please report it.)

 
6/11/1999 5:31:00 PMNic

Hey the ball just goes off the form!! Could you email me the forms!! PLEASE !!
(If this comment was disrespectful, please report it.)

 
6/15/1999 3:22:00 AMPugZ

I cant even get the code to work at all!! no ball no paddle no nothig. just errors.. please repost the code
(If this comment was disrespectful, please report it.)

 
6/16/1999 10:31:00 PMPete

Fun code :-) I saw a game like this once, put some obstacles and get some points for it... multiple balls!!!
(If this comment was disrespectful, please report it.)

 
6/17/1999 4:38:00 AMStefan Grönberg

Do get the ball movin' u need add time1
and set the Intervall to what ever u whant, Remember that the interval in is millesecouns, and that means 1000 = 1 sec..

/Stefan "Trash Software" Grönberg
(If this comment was disrespectful, please report it.)

 
6/17/1999 2:20:00 PMDrunk_Cowboy

Will some one please send me the forms and .vbx file please my email is The_Drunk_Cowboy@yahoo.com
(If this comment was disrespectful, please report it.)

 
6/18/1999 12:58:00 AMEtienne

I sent your code to all people who asked it.
Your game is very funny.
Many Thanks
(If this comment was disrespectful, please report it.)

 
6/21/1999 1:19:00 PMTiggermcat

This wont work for me at all errors
everywhere someone e-mail me the
project in none .exe plz in .frm
(If this comment was disrespectful, please report it.)

 
6/21/1999 5:24:00 PMMatt

I don't know what to tell you people! It works fine for me! I just cut and paste this code into a new project, add 2 shapes, and a timer with an interval of 1! It works without a single error! I'm running VB 6, but I have tested this code in vb 3 and it works fine!
(If this comment was disrespectful, please report it.)

 
6/22/1999 9:05:00 PMCob

Ok, im mad, i have been making my own pong game without knowing this one is here, my pong has a scoring, 2 player, pause, changable ball color, backround music that you can change, and im making a online portion,
(If this comment was disrespectful, please report it.)

 
6/22/1999 9:14:00 PMVlado

I really like all your codes this one i liked too it works perfect
(If this comment was disrespectful, please report it.)

 
6/23/1999 10:57:00 PMKomodo_Dragon

send me the working code please at my
e-Mail, because ive tried alot of things and this code is just not working for me! ok?
thanks
(If this comment was disrespectful, please report it.)

 
6/26/1999 5:41:00 AMccjx

I noticed something strange. When the ball falls to the left of the bouncer, it bounces in then out. Why cant it bounce away and gets out of the screen?
(If this comment was disrespectful, please report it.)

 
6/28/1999 1:18:00 AMccjx

NOTE! This source code is the foundation of a pong game. But it is not complete and maybe some of you can go and ask Matt to complete it. But, for a full version of the game, come here for Cool Pong!
http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=2199
(If this comment was disrespectful, please report it.)

 
8/3/1999 2:30:00 AMStics

I'm new in VB and this code works fine
for a start!
Thanks for simple good code!
(If this comment was disrespectful, please report it.)

 
8/16/1999 11:33:00 PMMatt

I just submitted much better and more advanced pong game, so if you have a strong grasp of this basic one, then you should check it out!
(If this comment was disrespectful, please report it.)

 
10/17/1999 11:10:00 PMAndré Henn

Can you please send me the whole project as a zip file

Thank You Very Much

André
(If this comment was disrespectful, please report it.)

 
4/30/2000 5:54:57 AMRob

great start for a pong game works fine for me thanks
(If this comment was disrespectful, please report it.)

 
6/25/2000 9:08:33 PMmichael patterson

people are such loosers. they cant learn for themselves!
(If this comment was disrespectful, please report it.)

 
11/24/2000 2:49:59 PMthx

your code helped me make my games collision control thx :-)
(If this comment was disrespectful, please report it.)

 
8/14/2001 10:45:07 AMSimon

Can some one send me all the code I still can't get it to work Simonshugar@hotmail.com
(If this comment was disrespectful, please report it.)

 
10/11/2002 9:05:50 PM

what do i put on the acual form

(If this comment was disrespectful, please report it.)

 
10/11/2002 9:07:26 PM

what do i put on the accual form
(If this comment was disrespectful, please report it.)

 
11/30/2005 8:52:11 PMm4nik4

i have got the ball to rebound of the top of the form as i am new to VB i do not understand Private Sub Form_MouseMove... can some one send me the vbp..so i can try and work it out that way please cuz i am really stuck any feedback welcome and recived with thanks xXx
(If this comment was disrespectful, please report it.)

 
5/12/2010 12:01:50 PMHailey

I'm having trouble with keeping the ball on the screen because I have the paddles going horizontaly. so how do I fix it?
(If this comment was disrespectful, please report it.)

 
5/12/2010 12:03:43 PMHailey

also the ball goes pass the paddle even if it's lined up to hit it.
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.