Solved

Memory Game Source Code Needed

Posted on 1999-01-20
3
204 Views
Last Modified: 2006-11-17
I want to make a small program in visual basic 5.0 This will be my first program ever. What I want to do is have my daughter click on pictures. If she clicks two pictures that match they will disappear. I would like a working example that I could run and study how it works. I'm not much for the technical areas right now, but I think that I would be able to figure the example out by observing how it runs. All I have is 66 Points. You are welcome to them for an example. Thank You Very Much !
0
Comment
Question by:moejoes
  • 2
3 Comments
 
LVL 1

Accepted Solution

by:
hddp666 earned 60 total points
ID: 1469776
Put an image (not a picture box) control on a form, copy it, and then paste it 5 times to create a control array: Image(i), i = 0 to 5. Change the Picture property of each image separately, such that Image(0) = Image(2), Image(1) = Image(5) and Image(3) = Image(4). You can also resize the images separately. Put a button on the form, and make RESET its caption. Paste the following code into the form's declarations section.


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(6) As Boolean    ' A Yes/No array, indexed 0 to 5
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 5
    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 Form_Load()
Dim i As Integer
Dim rvalue As Double
For i = 0 To 5
    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(5) = True Then
        Success 1, 5
        Exit Sub
   
    ElseIf PicClicked(3) = True And PicClicked(4) = True Then
        Success 3, 4
        Exit Sub
    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
   
    For i = 0 To 5
        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 5
        PicClicked(i) = False   ' reset values
    Next i
   
    ' 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)
    For i = 0 To 5
        PicClicked(i) = False
        Image1(i).Visible = True
        Image1(i).BorderStyle = 0
    Next i
    ClickNum = 0
    Me.Refresh

End Sub

0
 
LVL 1

Expert Comment

by:hddp666
ID: 1469777
You might have to change the paths and/or names of the .WAV files to match what is on your computer.
0
 

Author Comment

by:moejoes
ID: 1469778
Thanks hddp666. You did a very good job as far as I'm concerned. It works for me ! I will single you out from now on !!!
Thanks Again.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
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…
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…

789 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