?
Solved

VB Matching Game button code

Posted on 2003-02-26
3
Medium Priority
?
332 Views
Last Modified: 2010-04-07
Thanks to all those who helped me with the first part of this matching game. I'm now working on the second part and I have come into some difficulty in programing the button code. What I want to happen is after the second click of any click combination on the button grid the two buttons clicked will stay invisible for a few seconds so the user can see the caption of the label under it. If the two labels that are visible are = then they will stay invisible, if not then after the few second delay the buttons clicked will become visible once more. As the program is now, after the second click the first button clicked does the delay properly but the second clicked never becomes visible. If within the timer i put a DoEvents command then both buttons perform the timer correctly but during the pause it is possible to click on other buttons and break the timer cycle. Here is the code. I'll put the DoEvents command in and mark where it is. Thanks for the help.

Dim n As Integer
Dim clck1, clck2, sqr1, sqr2, oecnt As Integer
Dim clckcnt As Integer
Dim m_lngDelayTime As Long
Dim m_lngEndTime As Long


Private Sub Command1_Click(Index As Integer)
Command1(Index).Visible = False
clckcnt = clckcnt + 1
Label3.Caption = clckcnt
oecnt = oecnt + 1
If oecnt Mod 2 = 1 Then
    sqr1 = Index
    clck1 = Label1(Index).Caption
    Command1(sqr1).Visible = False
Else
    sqr2 = Index
    clck2 = Label1(Index).Caption
    Command1(sqr2).Visible = False
End If

If oecnt Mod 2 = 0 Then
    If clck1 = clck2 Then
        Label2.Caption = "MATCH !"
    Else
        m_lngDelayTime = 1.5 'Set Delay in Seconds.
        m_lngEndTime = Second(Time) + m_lngDelayTime

        If m_lngEndTime > 59 Then
        'Make sure Seconds is Not Greater than 59.
            m_lngEndTime = m_lngEndTime - 59
        End If
        Do Until Second(Time) >= m_lngEndTime
        DoEvents
        Loop
        Command1(sqr1).Visible = True
        Command1(sqr2).Visible = False
        oecnt = oecnt - 2
        Label2.Caption = ""
    End If
End If
If oecnt = 16 Then
    Label2.Caption = "You Win !"
    clckcnt = 0
End If
End Sub

Private Sub Command17_Click()
Label3.Caption = clckcnt
For n = 0 To 15
Command1(n).Visible = True
Next n
Frame2.Visible = False
Randomize Timer
mystring = "1122334455667788"
For n = 0 To 15
whichnumber = 1 + Int(Rnd * Len(mystring))
Label1(n).Caption = Mid$(mystring, whichnumber, 1)
mystring = Mid$(mystring, 1, whichnumber - 1) + Mid$(mystring, whichnumber + 1)
Next n
End Sub

Private Sub Command18_Click()
End
End Sub


Any ideas are welcome.
Thanks for the help.
0
Comment
Question by:Rocket8411
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 5

Expert Comment

by:Rhaedes
ID: 8028608
Hello again. Try the following....

Declare these variables:
*********************************
Dim buttonsDown As Integer 'Contains how many buttons pressed per turn
Dim buttonSequence(2) As Integer 'Contains the identity of the 1st and 2nd buttons
*********************************

Change the Command1 sub to this:
***************************************
Private Sub Command1_Click(Index As Integer)
Dim howMany As Integer 'Keeps the score

clckcnt = clckcnt + 1 'Add one to the click count

Label2.Caption = ""
Label3.Caption = clckcnt
buttonsDown = buttonsDown + 1 'Keep track of how many buttons have been pressed

If buttonsDown > 2 Then Exit Sub 'Do nothing if the user tries to click more than 2

buttonSequence(buttonsDown) = Index 'Store two buttons clicked
Command1(Index).Visible = False

If buttonsDown <> 2 Then Exit Sub 'Only one button down, so exit

If Label1(buttonSequence(1)).Caption = Label1(buttonSequence(2)).Caption Then
Label2.Caption = "MATCH !"
buttonsDown = 0

For n = 0 To 15 'Check the score
If Command1(n).Visible = False Then howMany = howMany + 1
Next n
If howMany = 16 Then Label2.Caption = "You win!"
Exit Sub
End If

Timer1.Interval = 5000
Timer1.Enabled = True
End Sub
***************************************

Add a timer (timer1) to the form, and add this code:
***************************************
Private Sub Timer1_Timer()
Command1(buttonSequence(1)).Visible = True
Command1(buttonSequence(2)).Visible = True
buttonsDown = 0
Timer1.Enabled = False
End Sub
***************************************

Hope this helps,
Kindest regards,
Rhaedes
0
 

Author Comment

by:Rocket8411
ID: 8036927
Hey,

I tried that code and it works good. I just had to position the clck count within the <> 2 IF statement so that after two clicks when you exit the Sub it does'nt add clicks to the buttons you press while waiting for the timer to stop. There is only one problem. When there is a Match the buttons do not stay invisible. The user needs to be able to see that after a match is made the buttons do not cover up those labels that have been matched again. Any ideas?

Thanks
0
 
LVL 5

Accepted Solution

by:
Rhaedes earned 340 total points
ID: 8037574
Hmmm.... That's odd. It work perfectly for me. I have included the change on the click count that you mention: including that fact that matching pairs stay visible. Here is my compete code. If you have more code, please post it: that could be the source of the interference.
Kindest regards,
Rhaedes

Dim clckcnt As Integer
Dim buttonsDown As Integer 'Contains how many buttons pressed per turn
Dim buttonSequence(2) As Integer 'Contains the identity of the 1st and 2nd buttons

Private Sub Command17_Click()
clckcnt = 0
Label3.Caption = clckcnt
buttonsDown = 0

For n = 0 To 15
Command1(n).Visible = True
Next n
Frame2.Visible = False
Randomize Timer
mystring = "1122334455667788"
For n = 0 To 15
whichnumber = 1 + Int(Rnd * Len(mystring))
Label1(n).Caption = Mid$(mystring, whichnumber, 1)
mystring = Mid$(mystring, 1, whichnumber - 1) + Mid$(mystring, whichnumber + 1)
Next n
End Sub

Private Sub Command18_Click()
End
End Sub

Private Sub Command1_Click(Index As Integer)
Dim howMany As Integer 'Keeps the score

Label2.Caption = ""

buttonsDown = buttonsDown + 1 'Keep track of how many buttons have been pressed

If buttonsDown > 2 Then Exit Sub 'Do nothing if the user tries to click more than 2

clckcnt = clckcnt + 1 'Add one to the click count
Label3.Caption = clckcnt

buttonSequence(buttonsDown) = Index 'Store two buttons clicked
Command1(Index).Visible = False

If buttonsDown <> 2 Then Exit Sub 'Only one button down, so exit

If Label1(buttonSequence(1)).Caption = Label1(buttonSequence(2)).Caption Then
Label2.Caption = "MATCH !"
buttonsDown = 0

For n = 0 To 15 'Check the score
If Command1(n).Visible = False Then howMany = howMany + 1
Next n
If howMany = 16 Then Label2.Caption = "You win!"
Exit Sub
End If

Timer1.Interval = 5000
Timer1.Enabled = True
End Sub


Private Sub Timer1_Timer()
Command1(buttonSequence(1)).Visible = True
Command1(buttonSequence(2)).Visible = True
buttonsDown = 0
Timer1.Enabled = False
End Sub
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month15 days, 10 hours left to enroll

743 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