Solved

TWO QUESTIONS = 100 Points

Posted on 1999-01-26
163 Views
First: I would like to be able to put a "blank" picture over each of the Match Game pictures below. (like the back of a deck of cards). When someone clicks the card, the picture behind the card would be visible until they clicked another card to try and match the picture.
Second: I would like a way to make the pictures position move at random each time a new game is started. In brevity:
"A simple match game where you click on a picture and try and match it to another picture and each new game would put the pictures into another position at random". Here is the code that I have now...
Thanks

Option Explicit
' A CHILD'S GAME THAT HAS SIX IMAGES AND A RESET BUTTON.
' CLICKING TWO IDENTICAL IMAGES MAKES THEM DISAPPEAR.

Private Declare Function PlaySound Lib "winmm.dll" Alias "PlaySoundA" (ByVal lpszName As String, ByVal hModule As Long, ByVal dwFlags As Long) As Long

Dim PicClicked(12) As Boolean    ' A Yes/No array, indexed 0 to 11
Dim ClickNum As Integer         ' Keeps track of clicks

Private Sub Command1_Click()    'reset button
Dim i As Integer
Dim rvalue As Double

For i = 0 To 11
PicClicked(i) = False
Image1(i).Visible = True
Image1(i).BorderStyle = 0
Next i
ClickNum = 0
Me.Refresh
rvalue = PlaySound("c:\windows\media\office97\chimes.wav", 0, 0)

End Sub

Private Sub Command2_Click()
End
End Sub

Dim i As Integer
Dim rvalue As Double
For i = 0 To 11
PicClicked(i) = False
Next i
rvalue = PlaySound("c:\windows\media\office97\chimes.wav", 0, 0)

End Sub

Private Sub Image1_Click(Index As Integer)
Dim i As Integer
Dim rvalue As Double
' When an image is clicked, this subroutine is called

ClickNum = ClickNum + 1 ' increment the click counter

If ClickNum = 1 Then    ' No clicks before the present one
PicClicked(Index) = True    ' Image(Index) has been clicked
Image1(Index).BorderStyle = 1
ClickNum = 1    ' increment click counter

ElseIf ClickNum = 2 Then    ' The present click is the second one
PicClicked(Index) = True ' A second image has been clicked
Image1(Index).BorderStyle = 1
Me.Refresh
ClickNum = 0    ' reset click counter

' image(0) is the same as image (2), etc.
' see if they have both been clicked
If PicClicked(0) = True And PicClicked(2) = True Then
Success 0, 2
Exit Sub

ElseIf PicClicked(1) = True And PicClicked(9) = True Then
Success 1, 9
Exit Sub

ElseIf PicClicked(3) = True And PicClicked(4) = True Then
Success 3, 4
Exit Sub
ElseIf PicClicked(5) = True And PicClicked(8) = True Then
Success 5, 8
Exit Sub
ElseIf PicClicked(6) = True And PicClicked(7) = True Then
Success 6, 7
Exit Sub
ElseIf PicClicked(10) = True And PicClicked(11) = True Then
Success 10, 11
Exit Sub
Else
rvalue = PlaySound("c:\Match\tryagain.wav", 0, 0)
For i = 0 To 11
Image1(i).BorderStyle = 1
Next i
Me.Refresh
rvalue = PlaySound("c:\Match\laser.wav", 0, 0)
For i = 0 To 11
Image1(i).BorderStyle = 0
Next i
Me.Refresh
rvalue = PlaySound("c:\Match\laser.wav", 0, 0)
For i = 0 To 11
Image1(i).BorderStyle = 1
Next i
Me.Refresh
rvalue = PlaySound("c:\Match\laser.wav", 0, 0)
For i = 0 To 11
Image1(i).BorderStyle = 0
Next i
Me.Refresh

End If

For i = 0 To 11
PicClicked(i) = False   ' reset the game after two clicks
Image1(i).BorderStyle = 0
Next i
End If

End Sub

Private Sub Success(img1 As Integer, img2 As Integer)
Dim i As Integer
Dim rvalue As Long
' Values for img1 and img2 are passed to the subroutine
' when it is called, e.g. Success 0, 2 calls this sub, and
' sets img1 to 0 and img2 to 2.
' The two images are then "turned off"

Image1(img1).Visible = False
Image1(img2).Visible = False
Me.Refresh
For i = 0 To 11
PicClicked(i) = False   ' reset values
Next i

' Play the goodjob sound
rvalue = PlaySound("c:\Match\goodjob.wav", 0, 0)

' If any images left, go back and continue play
For i = 0 To 11
If Image1(i).Visible = True Then Exit Sub
Next i

' Otherwise, play the applause sound and reset the game
rvalue = PlaySound("c:\Match\yes.wav", 0, 0)
For i = 0 To 11
PicClicked(i) = False
Image1(i).Visible = True
Image1(i).BorderStyle = 0
Next i
ClickNum = 0
Me.Refresh

End Sub
0
Question by:bendown
• 4
• 3

LVL 1

Accepted Solution

hddp666 earned 100 total points
ID: 1470801
I'll change the code and post it here.
0

Author Comment

ID: 1470802
Thank You hddp666. Looking forward to the change !!!
0

LVL 1

Expert Comment

ID: 1470803
Cut the old code from your form, and save it. Paste this replacement code into your form. As before, some of the .WAV files might need to be changed, or the ImgPath value that is set when the form loads.

Option Explicit
' A CHILD'S GAME THAT HAS SIX IMAGES AND A BUTTON ON A FORM.
' PUT ONE IMAGE ON A FORM, COPY IT AND THEN PASTE IT REPEATEDLY
' TO CREATE A CONTROL ARRAY.
' CHANGE THE PICTURE PROPERTY, SIZE, ETC. OF EACH ONE SEPARATELY.

' CLICKING TWO IDENTICAL IMAGES MAKES THEM DISAPPEAR

Private Declare Function PlaySound Lib "winmm.dll" Alias "PlaySoundA" (ByVal lpszName As String, ByVal hModule As Long, ByVal dwFlags As Long) As Long

Dim ClickNum As Integer         ' Keeps track of clicks
Dim Pic(6) As String           ' Picture names
Dim Clicked(2) As Integer       ' Which 2 images are clicked
Dim ImgPath As String           ' Where the pictures are

Sub Shuffle()
Dim i, j As Integer
Dim ImgName As String
Dim upperbound As Single
Dim lowerbound As Single
Dim rvalue As Double

upperbound = 5      ' upperbound + 1 = number of images on form
lowerbound = 0

For i = 0 To 5      ' No picture names yet
Pic(i) = ""
Next i

For i = 1 To 3
' Get the first image's name
ImgName = "Img" & i & ".bmp"

' Put the image in one spot
Do
' Generate a random number between 0 and 5
j = Int((upperbound - lowerbound + 1) * Rnd + lowerbound)
If Pic(j) = "" Then
Pic(j) = ImgPath & ImgName
Exit Do
End If
Loop
' Put the same image in another spot
Do
j = Int((upperbound - lowerbound + 1) * Rnd + lowerbound)
If Pic(j) = "" Then
Pic(j) = ImgPath & ImgName
Exit Do
End If
Loop
Next i

For i = 0 To 5
Image1(i).BorderStyle = 0
Image1(i).Visible = True
Next i

ClickNum = 0
Me.Refresh
rvalue = PlaySound("c:\windows\media\office97\chimes.wav", 0, 0)

End Sub

Private Sub Command1_Click()    'reset button

Shuffle     ' Call the shuffle routine

End Sub

ImgPath = "C:\Program Files\DevStudio\Memory Game\"

Shuffle

End Sub

Private Sub Image1_Click(Index As Integer)
Dim i As Integer
Dim rvalue As Double

' When an image is clicked, this subroutine is called

ClickNum = ClickNum + 1 ' increment the click counter

If ClickNum = 1 Then    ' No clicks before the present one
Clicked(1) = Index
Image1(Index).BorderStyle = 1

ElseIf ClickNum = 2 Then

If Index = Clicked(1) Then  ' Same image clicked twice
ClickNum = 1
Exit Sub
End If

Clicked(2) = Index
Image1(Index).BorderStyle = 1
Me.Refresh

If Pic(Clicked(1)) = Pic(Clicked(2)) Then
Success Clicked(1), Clicked(2)
Else
rvalue = PlaySound("c:\windows\media\office97\laser.wav", 0, 0)
For i = 0 To 5
Image1(i).BorderStyle = 1
Next i
Me.Refresh
rvalue = PlaySound("c:\windows\media\office97\laser.wav", 0, 0)
For i = 0 To 5
Image1(i).BorderStyle = 0
Next i
Me.Refresh
rvalue = PlaySound("c:\windows\media\office97\laser.wav", 0, 0)
For i = 0 To 5
Image1(i).BorderStyle = 1
Next i
Me.Refresh
rvalue = PlaySound("c:\windows\media\office97\laser.wav", 0, 0)
For i = 0 To 5
Image1(i).BorderStyle = 0
Next i
Me.Refresh

End If
ClickNum = 0    ' reset click counter
End If

End Sub

Private Sub Success(img1 As Integer, img2 As Integer)
Dim i As Integer
Dim rvalue As Long
' Values for img1 and img2 are passed to the subroutine
' The two images are then "turned off"

Image1(img1).Visible = False
Image1(img2).Visible = False
Me.Refresh

' Play the whoosh sound
rvalue = PlaySound("c:\windows\media\office97\whoosh.wav", 0, 0)

' If any images left, go back and continue play
For i = 0 To 5
If Image1(i).Visible = True Then Exit Sub
Next i

' Otherwise, play the applause sound and reset the game
rvalue = PlaySound("c:\windows\media\office97\applause.wav", 0, 0)
Shuffle

End Sub
0

LVL 1

Expert Comment

ID: 1470804
In the ImgPath directory, put the following graphic files: Img1.bmp, Img2.bmp, Img3.bmp, and CardBack.bmp.
0

Author Comment

ID: 1470805
Thank You. When I get some more points, I will Point Another Question to you. It will concern the same code as per above.
Thanks
0

LVL 1

Expert Comment

ID: 1470806
THIS CODE WILL SUPPORT 20 IMAGES ON A FORM, AND TEN DIFFERENT PICTURES (Img1.bmp - Img10.bmp). PASTE IT INTO A FORM TO REPLACE THE EARLIER CODES.

Option Explicit

Private Declare Function PlaySound Lib "winmm.dll" Alias "PlaySoundA" (ByVal lpszName As String, ByVal hModule As Long, ByVal dwFlags As Long) As Long

Dim ClickNum As Integer         ' Keeps track of clicks
Dim Pic(20) As String           ' Picture names
Dim Clicked(2) As Integer       ' Which 2 images are clicked
Dim ImgPath As String           ' Where the pictures are

Sub Shuffle()
Dim i, j As Integer
Dim ImgName As String
Dim upperbound As Single
Dim lowerbound As Single
Dim rvalue As Double

upperbound = 19      ' upperbound + 1 = number of images on form
lowerbound = 0

For i = 0 To 19     ' No picture names yet
Pic(i) = ""
Next i

For i = 1 To 10
' Get the first image's name
ImgName = "Img" & i & ".bmp"

' Put the image in one spot
Do
' Generate a random number between 0 and 19
j = Int((upperbound - lowerbound + 1) * Rnd + lowerbound)
If Pic(j) = "" Then
Pic(j) = ImgPath & ImgName
Exit Do
End If
Loop
' Put the same image in another spot
Do
j = Int((upperbound - lowerbound + 1) * Rnd + lowerbound)
If Pic(j) = "" Then
Pic(j) = ImgPath & ImgName
Exit Do
End If
Loop
Next i

For i = 0 To 19
Image1(i).BorderStyle = 0
Image1(i).Visible = True
Next i

ClickNum = 0
Me.Refresh
rvalue = PlaySound("c:\windows\media\office97\laser.wav", 0, 0)

End Sub

ImgPath = "C:\Program Files\DevStudio\Memory Game\"
Shuffle
End Sub

Private Sub Image1_Click(Index As Integer)
Dim i As Integer
Dim rvalue As Double

' When an image is clicked, this subroutine is called

ClickNum = ClickNum + 1 ' increment the click counter

If ClickNum = 1 Then    ' No clicks before the present one
Clicked(1) = Index
Image1(Index).BorderStyle = 1

ElseIf ClickNum = 2 Then

If Index = Clicked(1) Then  ' Same image clicked twice
ClickNum = 1
Exit Sub
End If

Clicked(2) = Index
Image1(Index).BorderStyle = 1
Me.Refresh

If Pic(Clicked(1)) = Pic(Clicked(2)) Then
Success Clicked(1), Clicked(2)
Else
rvalue = PlaySound("c:\windows\media\office97\laser.wav", 0, 0)

For i = 0 To 19
Image1(i).BorderStyle = 0
Next i
Me.Refresh

End If
ClickNum = 0    ' reset click counter
End If

End Sub

Private Sub Success(img1 As Integer, img2 As Integer)
Dim i As Integer
Dim rvalue As Long
' Values for img1 and img2 are passed to the subroutine
' The two images are then "turned off"

Image1(img1).Visible = False
Image1(img2).Visible = False
Me.Refresh

' Play the whoosh sound
rvalue = PlaySound("c:\windows\media\office97\whoosh.wav", 0, 0)

' If any images left, go back and continue play
For i = 0 To 19
If Image1(i).Visible = True Then Exit Sub
Next i

' Otherwise, play the applause sound and reset the game
rvalue = PlaySound("c:\windows\media\office97\applause.wav", 0, 0)
Shuffle

End Sub

Private Sub Command1_Click()     ' Reset button
Shuffle
End Sub

0

Author Comment

ID: 1470807
You are too much hddp666 ! I really appreciate the extra code above. I have a friend who works with me and he will be coming to this area of cyber space real soon. He will be Pointing a Question to you if its O.K. He likes the Match Code and will be asking you How To Change The Pictures so that there will be Sets of Pictures instead of just the 20. Like if you wanted Three Sets Of Pictures. It will be worth 200 points.
Thank You Again...
0

Featured Post

Suggested Solutions

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…