Solved

Visual basic ping pong game coutdown timer, random boxes and mouse control

Posted on 2003-11-28
26
1,208 Views
Last Modified: 2013-11-25
im am making a ping pong game in visual basic 6.0 for the game, i wish to make each game last about one minute and that the person who has the highest score at the end of the game wins. how could i go about doing this?


the second part of my question is, i wish to make random boxes appear on the form, which when hit by the ball will either make the ball go faster/slower, the bats bigger/smaller and the ball bigger/smaller. how could i do this.
the bats are called shape1 and shape2. and the ball is called shape3.

i also had an idea of controlling one of the bats with the mouse but ive absolutely no idea as how to go about this. i would be greatful for some ideas.
0
Comment
Question by:aidanmcg34
  • 10
  • 7
  • 4
  • +2
26 Comments
 
LVL 1

Expert Comment

by:TheBest
ID: 9841560
1. To make the game last for 1 minute, put a timer on the form, and activate it when the game starts (Timer1.Enabled = True). Set the interval to 60000, and put the 'game over' code in the Timer1_Timer procedure

2. To make a random box, draw a shape, but hide it somewhere. When you want the shape to appear, set Shape.Visible = True and set the Left, Top, Width, and Height values. Then, put a timer on the form, set it's interval to 10 or so, and put a collision test in Timer1_Timer()

good luck,
thebest ;)
0
 
LVL 1

Expert Comment

by:TheBest
ID: 9841563
for mouse control, put the prodcedure to move the paddle in Form_MouseMove procedure
0
 

Author Comment

by:aidanmcg34
ID: 9842547
what would the game over code be? ive been doing well with the project so far but have really hit a wall. thanks for your help so far
0
 
LVL 9

Expert Comment

by:TooKoolKris
ID: 9842972
If you want the timer to be seen then create a label on the form and start it at 60. Have a timer that changes the number on the label -1 for each second and For Next it 60 times. You can use the textchange event to know when it reaches 0 and then stop the game.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 9843007
Here is some real basic code:  Create a new project and add a rectangle shape, a timer, and a label.

Regards,

Idle_Mind

' Code Follows...
Private endTime As Date

Private Sub Form_Load()
    Timer1.Interval = 250
    endTime = DateAdd("s", 60, Now())
End Sub

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If X <= (Me.Width - Shape1.Width - 100) Then
        Shape1.Move X, 0
    End If
End Sub

Private Sub Timer1_Timer()
    Dim timeLeft As Integer
    timeLeft = DateDiff("s", endTime, Now())
    Label1.Caption = timeLeft
    If timeLeft >= 0 Then
        Timer1.Enabled = False
        MsgBox "Game Over"
    End If
End Sub
0
 

Author Comment

by:aidanmcg34
ID: 9843268
the mousemove event seems to be what im looking for i am looking to move it up and down rather than horizontally so i edited the code to suit this. however how could i stop the shape1 from going any higher than 855 (i think its twips) from the top and 735  from the bottom.

the timer countdown also works, however i have a splash screen and it has a gradient background which runs on a timer but this is only because i did not know how to get it to run as soon as the form opens. i tried private sub form_load() however this did not work. here is the first part of the code that seemed to be causing the most trouble.

Private Sub Timer3_Timer()
Dim Button As Integer
Dim Shift As Integer
Dim X As Single, Y As Single
Dim background As Boolean


'calculation variables for r,g,b gradiency
Dim VR, VG, VB As Single
'colors of the picture boxes
Dim Color1, Color2 As Long
'r,g,b variables for each picture box
Dim R, G, B, R2, G2, B2 As Integer
'calculation variable for extracting the rgb values
Dim temp As Long


the timer method works ok, however the speed of the ball is severly slowed down and seems jumpy. the ball is called shape3 and moves through a timer which increases its position on the form by a pixel a second (or something along those lines). how could i fix this?
0
 
LVL 9

Expert Comment

by:TooKoolKris
ID: 9843420
You might to have to build an algorithm for your mathematical needs. The table itself is going to be in a fixed location on the screen yes? And the ball is going to be moving over it in a somewhat horizontal & diagonal angle correct? Well you need to build you a defined grid with those fixed points of the table. Then you need to understand the pattern in the changes of coordinates(slope) as you draw a lines across the table. I know it sounds easier said then done, believe me but if you could manage the routine so that the computer does all the work then what you could end up with is a collection of random pre defined shots across the table and back that enable the pixel in predefined patterns. Thus making it appear that something is going back and forth across the table on randomly selected patterns. All you would have to do is call a function and pass some data.

Just a thought.
0
 

Author Comment

by:aidanmcg34
ID: 9843560
no it has to be done where the position of the ball is moved a certain amout every millisecond
all i need is for another way to trigger the gradient background as soon as the form starts rather than  having to use a timer because this is the problem as when i remove it the program works fine but when i have it running it hampers the program.

i had the idea that when the splash screen is to be closed that the timer could be stopped. currently when the user clicks on the close button, it runs "main.show"

however i dont know how i could go about this.
0
 
LVL 1

Expert Comment

by:TheBest
ID: 9843568
to stop shape1 from going any higher than 855 twips or any lower than 735 (from the bottom) , in the procedure where you move the paddle, type the following:
....
If Shape1.Top < 855 then Shape1.Top = 855
If Shape1.Top + Shape1.Height > Form1.Height - 735 then Shape1.Top = Form1.Height - 735
....

hope this helps,
daniel (aka. TheBest)
0
 

Author Comment

by:aidanmcg34
ID: 9843647
oh yes another thing i meant to say was how can i hide the cursor when the game is in progress

thanks for the code daniel but i had that in already and the top works ok but the paddle goes to the bottom and it takes a big jump off the form.
0
 

Author Comment

by:aidanmcg34
ID: 9843685
also when the ball hits the walls or the bat it plays a wave file but the ball seems to pause while the sound is being played. how can i have it that the ball continues in a smooth path.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 9843981
Here is how to show/hide the mouse pointer.  Just use:

ShowPointer True ' Show the mouse

or

ShowPointer False ' Hide the mouse

Regards,

Idle_MInd

' Code Follows...
Private Declare Function ShowCursor Lib "user32" (ByVal bShow As Long) As Long

Private Sub ShowPointer(pointer As Boolean)
    If pointer Then
        Do While ShowCursor(1) < 0
            DoEvents
        Loop
    Else
        Do While ShowCursor(0) >= 0
            DoEvents
        Loop
    End If
End Sub
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 9844009
Here is how to play sounds.  The three examples below all have different results.  The code effect is annotated in the comments on each line.

Regards,

Idle_Mind

Private Declare Function sndPlaySound32 Lib "winmm.dll" Alias "sndPlaySoundA" _
    (ByVal lpszSoundName As String, ByVal uFlags As Long) As Long

Private Const SND_ASYNC = &H1
Private Const SND_LOOP = &H8
Private Const SND_SYNC = &H0

Private Sub Command1_Click()
     sndPlaySound32 "shutdown.wav", SND_ASYNC ' code continues immediately
     sndPlaySound32 "shutdown.wav", SND_SYNC ' code does not return until sound completely plays
     sndPlaySound32 "Alarm.wav", SND_ASYNC Or SND_LOOP ' code continues immediately and sound loops
End Sub
0
 

Author Comment

by:aidanmcg34
ID: 9845350
the sound worked but when i set the showmouse as false, it hides the mouse altogether even when the program isnt working.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 9845363
Exactly.  You need to show it again on your form unload.

Private Sub Form_Unload(Cancel As Integer)
    ShowPointer True
End Sub
0
 

Author Comment

by:aidanmcg34
ID: 9857182
is there any way i could get my game to take a screen dump of the whole form when the game is in progress by just pressing a certain button eg Ctrl+P or something along those lines.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 9858591
How many more questions can you tack into one post?....   ;-)

Did you want the whole screen or just your form?  
Do you want the screen capture to appear on one of your forms or just be saved to a file?
Do you want a systemwide keypress or just one local to your form?

There are many examples of how to do this in EE.

Most of them send the "prt scr" keypress to the system putting a "screen capture" on the clipboard and then take the contents of the clipboard and place it in a picturebox.  Then you just save the contents of the picturebox using SavePicture().

So many questions...so little time.

Idle_Mind

This will take a picture of the screen and save it in App.Path & "Capture1.bmp" when the button is pressed.  You could obviously send the picture to an invisible form to make the process seem seamless.

Create a new project and add a commandbutton and a picturebox:

'Code Follows....
Option Explicit

Private Declare Function MapVirtualKey Lib "user32" _
  Alias "MapVirtualKeyA" (ByVal wCode As Long, _
  ByVal wMapType As Long) As Long

Private Declare Sub keybd_event Lib "user32" _
  (ByVal bVk As Byte, ByVal bScan As Byte, _
   ByVal dwFlags As Long, ByVal dwExtraInfo As Long)

Private Const VK_MENU = &H12
Private Const VK_SNAPSHOT = &H2C
Private Const KEYEVENTF_KEYUP = &H2

Private Sub Command1_Click()
    Picture1.AutoSize = True
    CaptureDesktop Picture1
    Picture1.Picture = Picture1.Image
    SavePicture Picture1.Picture, App.Path & "\capture1.bmp"
End Sub

Private Sub CaptureDesktop(oPictureBox As Object)
    On Error GoTo Errhandler
   
    Clipboard.Clear
   
    keybd_event VK_MENU, 0, 0, 0
    DoEvents

    keybd_event VK_SNAPSHOT, 1, 0, 0
    DoEvents
   
    keybd_event VK_MENU, 0, KEYEVENTF_KEYUP, 0
    DoEvents

    Set oPictureBox.Picture = Clipboard.GetData(0)
    Clipboard.Clear
Errhandler:
End Sub
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 9872541
How is the project going aidanmcg34?
0
 

Author Comment

by:aidanmcg34
ID: 9873440
not too bad just have a form that opens up with options for the game it contains options buttnos and whenever someone choses one, it will make the bat size either small medium or large. however i have the values set that i want to change in the main form
the bat is called shape1 and i have it in my options form (frmOptions)


Private Sub batsizemedium_Click()
main.Shape1.Height = 1455
main.Shape2.Height = 1455
End Sub

and then a save options button butthe button has no code and i dont know how to get it to change the values in the main form so that the bat sizes are increased.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 9874704
There are two basic ways to utilize option buttons:

1) Separate Option Buttions within the same Container

2) Option Buttons in a Control Array within the same Container

Option Buttons at the same container level are mutually exclusive.  This means that only one of them can be selected at a time.

The form itself is a container so any option buttons placed directly on the form become part of the same group.  If you were to place a Frame or PictureBox onto the form, and then draw Option Buttons into them, they would be part of a different option button group.

I think you have already figure out Method 1.

To use Method 2, draw an option button on the form and then Press CTRL-C followed by CTRL-V.  Say yes to the question.  Hit CTRL-V several more times to add more Option Buttons to the Control Array.  When you double click the Option Buttons in the Control Array, you will get code like this:

Private Sub Option1_Click(Index As Integer)

End Sub

The Index value tells you which Option Button was selected.  You can use programmitacally in an If...Then or a Select Case structure like this:

Private Sub Option1_Click(Index As Integer)
    Debug.Print "Option1(" & Index & ")"
    Select Case Index
        Case 0
            MsgBox "Small"
        Case 1
            MsgBox "Medium"
        Case 2
            MsgBox "Large"
    End Select
End Sub

I suspect what you have done is use hardcoded values in your calculation involving your bats?  If you have, then you can either:

1) Use a global variable instead and change the value of the global variable when the bat size is changed.

2) Use the bat size directly, main.Shape2.Height, in your calculations.

I am making sense?

Idle_Mind
0
 

Author Comment

by:aidanmcg34
ID: 9888155
yes thanks i got them going only problem was the ball speed option boxes. when i move the ball an xinc and yinc values change moving the ball so i typed in main.xinc=10 and the same for yinc however this wouldnt work whereas it did with the bat size and ball size. but ill try your case code.

i have to do the random boxes now, but i dont know how to get it to appear randomly or to detect when the ball hits it. the ball is called shape3 and i want the box to have 1 of four pictures on it which appear randomly as well. the box when hit with the ball will either make the ball biger/smaller or the bats biger or smaller. the person that hits the box with the ball should be the one whos bat gets biger. i think perhaps i could do this where xinc>0 then shape1.height = 1800 (which will say that if ball is coming from right hand side then the right hand bat will be increased and the same if xinc<0 well something along those lines anyway.)
finding this hard at the minute and really cant think what the code would be to get it workin. tryin to push this on abit and to have it finished this wednesday.
0
 

Author Comment

by:aidanmcg34
ID: 9892044
got the ball speed fixed only two problems i have left is the restarting the game after the 60 seconds are up and the random boxes. have looked up various collision code samples on the net but have not found the very useful. i will solve the timer problem its just a matter of messing about with the code till i get it right however im still stuck with the random boxes and getin them to do different things.
0
 
LVL 1

Expert Comment

by:TheBest
ID: 10180774
if you REALLY want to create a game easily, try Klik & Play - it's old now, but free for 'educational use' - here is the download link:
http://www.clickteam.com/webftp/files/6/10/KPSchool.exe
0
 

Accepted Solution

by:
PashaMod earned 0 total points
ID: 10624623
PAQed - no points refunded (of 200)

PashaMod
Community Support Moderator
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now