VB.net Game Using Picture Boxes Functions [closed] - vb.net

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am trying to develop a simple game using picture boxes
For example, if you have 3 different picture boxes each containing a unique picture
When a form loads, one box is visible while other two are invisible
The player has to click on the visible picture box before it becomes invisible (time is specified for the box to stay visible)
Example: box 1 stays visible for 5 seconds , if the box is not clicked during the 5 seconds box 1 become invisible , and another random box will become visible.
Of course , if the user click on the picture successfully his score is updated
Different levels can be made by making the time shorter
The code would probably be one single code placed at the form load
Any help? Thanks

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)

Use a timer with an interval of 5000 and then in the elapsed event handler
Timer.stop()
Losses = Losses + 1
selectNewPictureBox()
Time.start()
Then in the picture box handler
Timer.stop()
Wins = Wins + 1
selectNewPictureBox()
Timer.start()

Related

Wait for 1 second before starting code again - VB.NET

I desperately need help with a game I am making. For a bit of context, i am making a memory game and i have the following piece of code that is being troublesome. I have a bunch of labels on the form, 16 to be exact, with 1 randomly generated symbol placed in each. Each symbol appears in the labels twice.
------------------------------Continued----------------------------------------
'MsgBox("hello") 'used to check if the second inccorect press shows up - it does show but instantly changes colour
'''''''''''''''''NEED SOME CODE THAT PAUSES IT HERE'''''''''''''''
labels(0).ForeColor = Color.DarkRed
sender.ForeColor = Color.DarkRed
End If
flips = 1
End If
End If
tmrmemory.Enabled = True ' starts the timer after the user clicks the first label
End Sub
What's supposed to happen is that when the labels clicked don't match, it should show both the clicked labels for a short period before changing them both back to "DarkRed" which is the colour of the form's background.
I have tried using a timer but then i can't use sender.forecolor=color.darkred because it is not declared globally.
I have also tried using the command Threading.Thread.Sleep(500) but it still doesn't show the second incorrect click. I know that the code i have used works because when i use the message box, i can see both symbols and when the two clicks are correct, it stays.
Threading.Thread.Sleep(500) will actually pause your code for half a second. However during this time it won't do anything, not even refresh your controls. To get the effect you want, you need to call the YourControl.Refresh method before calling Threading.Thread.Sleep to force the control to redraw immediately.
On a side note, I would advise you not to call Threading.Thread.Sleep on UI thread. It will give a feeling of program hang. Instead do your work on a separate thread. You can either do all the work yourself right from creating a separate thread to destroying it, or use the BackgroundWorker control which has all the functionality built in.
Here is the link to an article I wrote a long time ago regarding BackgroundWorker that might be useful for you:
http://www.vbforums.com/showthread.php?680130-Correct-way-to-use-the-BackgroundWorker
Declare a variable outside the sub that stores what label should be flipped when the timer ends.
Label click sets
storedLabel = sender
Timer tick sets storedLabel.ForeColor = Color.DarkRed

Showing MsgBox() with nowait (no user input) is not the real issue

I had searched a lot how to display a Msgbox that will not wait the user input (pressing ok or cancel).
I found 3 solutiuons to this.
1- Display the MsgBox() in another thread or using BackgroundWorker()
2- Create a form that display the message, then closed the form by a timer and use it instead of Msgbox()
3- Using the API MessageBoxA()
Let say I have a loop from 1 to 100, and I want display a message for the i(counter)
When I test above 3 ways, I found that this is not the right way of doing it, I don't need a msgbox() to close by it self after showing the message, because that will display 100 dialog.
What I realy want is to display ONLY 1 MsgBox() and change the text accordingly.
I managed to do this using a a Form() as class and I did it using Application.DoEvents
I know it can be done using BackgroundWorker or Threading since alot of people advice against using Application.Doevents
Here is my code
Dim oWW As New WaitWindow With {.TopLevel = True,.TopMost = True,.StartPosition = FormStartPosition.CenterScreen}
oWW.Show(Me)
For i = 1 to 100
Threading.Thread.Sleep(500) ' Just to slowdown execution
oWW.SetMessage("Counter = " + i.ToString)
Next
oWW.Dispose()
Public Class WaitWindow
Sub SetMessage(ByVal Message As string)
lbl_message.Text = Message
Application.DoEvents
End Sub
End Class
WaitWindow is not more than a Form base class with a label (lbl_message)
That code works fine (display WaitWindowForm on center of currently displayed form only once, then I change the text)
I have 3 questions :
1- How to display the WaitWindowForm in the top right corner of my working form?
2- Is it possible to display the normal MsgBox() or MessageBox.Show() only once, then capture the text displayed and change it?
3- Which one is suitable for my issue (BackGroundWorker or Threading) and what the code in WaitWindow class I post will be if I decided to use Backgroundworker or Threading instead of Application.DoEvents (Changing the label text not showing new form with new text) ?
3 questions in one post.. humm.. who cares, I am not the one who will answer lol :)
Thanks in advance.
I think the issue that you're really encountering is that you're trying to use a message box for something it's not suited for. If you want to have text that constantly changes just add a text box in the upper right corner of your application and adjust it every time a new message needs to be shown.
You can also look up dialogu windows (search ".showdialog() vb.net" in google) might help as well.

pictures keep changing on the form, there should be at least 5 pictures on every different groupbox using loop

I want a way of loading 5 pictures on every refresh. i have over 300 pictures in the database and i want them to load every 5 picture on form load. and then they sray there for 10sec and then the next 5 loads n keeps loading till al the pictures are loaded then it restarts again from the start.
it should be using a loop. because i have a dynamic way of loading my pictures and textboxes. so how could i jst load 5 pictured and then the next 5 after the first 5 have disappeared. all should happen on one form. maybe even use ashock.. or any other way.
or any other way.
you can do like this, First get all those 300 pictures in the load event and store that in a datatable (using datatable will shrinks the possibility to connect to back end multiple times).Then use a timer in your form and set its interval for 5000 ms(i.e 5 seconds). use a global variable to to track how many pictures are shown, increment it by 5 for every tick event's call(Tick event will get fired when interval assigned by you gets elapsed. - ex: for every 5 seconds). now inside the tick event, get five pictures from the datatable(use that global variable to get the next 5 images). Display those five images by replacing the old ones.and once that global variable reaches 300 again reset it to 0. This cycle will ends only if you stop the timer.
Hope this will give you an idea to accompolish your task.

Visual Basic form .close() method

I have the below snippet of code:
'Handle level specific properties
Select Case ScoreCard.CurrentDifficulty
Case 1
intImageCount = 2 'This is the number of images to show at any given time on screen +1
'debug
ScoreCard.CurrentDifficulty = 6
Case 2
intImageCount = 3 'This is the number of images to show at any given time on screen +1
Case 3
intImageCount = 5 'This is the number of images to show at any given time on screen +1
Case 4
intImageCount = 2 'This is the number of images to show at any given time on screen +1
Case 5
intImageCount = 5 'This is the number of images to show at any given time on screen +1
Case 6
frmLevel3_HouseOfMirrors.Show()
Me.Close()
Return
End Select
When case 6 is executed frm3_HouseOfMirrors.Show() executes and my new form opens. Me.close executes as well but my problem is that the script then gets to the return line. Isn't me.Close() suppose to stop all execution of code on the current form and unload its self from memory?
Just call frmLvl3_HouseOfMirrors.ShowDialog() instead of .Show(), this will stop the execution of code until the new form is closed.
Or if you want to cancel the execution of the rest of code try the Exit instruction. You have to detect you want to finish and add it outside this Sub, because .Close() didnt stop the execution of code.
No, the "close" method just closes the form, but the program execution will continue. If you want to stop code execution until a form is closed, you could make it modal.
In VBA it would look like this:
frmLevel3_HouseOfMirrors.Show vbModal
Isn't me.Close() suppose to stop all execution of code on the current form and unload its self from memory?
No. Close does exactly what it says: it closes the visual, interactive representation of the form.1 It doesn’t affect code execution directly. It does make sure that Form_Closing and then Form_Closed are called, however. But the rest of the code execution is unaffected; in particular, the current method runs through normally. After that, other methods on the form may or may not be called as necessary (and, as mentioned, Closing and Closed will be called).
1 And, yes, it releases the form’s resources unless the form was shown via ShowDialog rather than plain Show.

Display time left when running

I have a form with a few buttons which execute code when pressed like running validations on the database.
Some code can run for a few minutes so is there any way to show the time remaining or a message to display the % of process completed?
Or pop out a message when code evaluation starts and the message should disappear once code running is completed?
What you are probably looking for is a "Progress Bar".
I've used the Microsoft ProgressBar control (you can find it under Insert->ActiveX Control), and it's not that hard to use. Just set the value of it to a percentage (as an integer, not a decimal).
'foo, being the ProgressBar
me.foo = 70 '70%
There is some good info here on another method: http://www.granite.ab.ca/access/progressbar.htm
In order to do this the "normal" way, you'd need to run your validation in another thread and have it report its progress back to the UI thread. However, I don't believe VBA supports any kind of multithreading.
If your validation routines involve a loop, or even just many separate discrete operations, you can try inserting a DoEvents statement in between loop iterations (or operations), and then have your progress display updated periodically (say, in an Application_OnTime event handler).
I usually have a form I name frmProgress or whatever, with a cancel button and a label for displaying a status message. Then embedded in the form code I have a boolean called bCancel, and when you hit the cancel button it simply sets bCancel as true.
Also in this code I have a routine called ShowPercDone( Idx , NumIdc ) where Idx is the step the code is on, and NumIdc is the number of steps the code will take (assuming each step takes the same amount of time). This works well when I'm running through a for loop, but basically any time I want to display a status update I just call the routine in the form with my message, which I should add runs the doevents command for me.
So that's how the status form works. In the macro I run, I start out by just calling frmProgress.show (0) so that it lets you click the cancel button. Then in my loop when I update the status message I then check frmProgress.bCancel and if it's true I exit out of the macro.
Hope that helps.
Finally to be simple i decided to use the method given here
http://oreilly.com/pub/h/3330#code