[Solved] VB.net Game Using Picture Boxes Functions [closed]


I would probably use a stopwatch – a timer would be difficult because i dont know of a way to reset the count back to 0 when a user clicks sucessfully.

declare a stopwatch like so:

Private maxWaitTimer As New Stopwatch

then, perhaps a ‘game loop’ type of thing could be used in your form load event… maybe something like this:

maxWaitStopwatch.Start()

While(GameIsRunning)
    If maxWaitStopwatch.ElapsedMilliseconds > 5000 Then
        Losses = Losses + 1
        selectNewPictureBox()
        maxWaitStopwatch.Restart()
    Else
        Application.DoEvents() 'this gives the program a chance to execute the picture box click event, among other things (resize, drag, etc... since we are spinning in a loop)    
    End If
    'System.Threading.Thread.Sleep(100) 'uncommenting this line will prevent it from maxing out your processor, although it will be slightly less responsive
End While

and your picture boxes could implement something like this:

Wins = Wins + 1
selectNewPictureBox()
maxWaitStopwatch.Restart()

basically, your program spins around in a loop, checking to see if the timer is elapsed, and if it is, it moves the picture.

the click event increments the score – it has a chance to be run during the ‘application.doevents()’ portion of the loop.

adding a sleep(100) will slow it down very slightly (and make it slightly more innaccurate, by about 100ms), but it will prevent it from using tons of CPU. you probably wont notice the difference in speed.

there may be better ways to do this, though…


EDIT – reflecting what steven said, it would be better if you used a timer instead of a loop:

use stop() when the user clicks the picture, and then call start() after.

(i didnt realize that would reset it, but apparently it does)

7

solved VB.net Game Using Picture Boxes Functions [closed]