Link to home
Start Free TrialLog in
Avatar of falahat
falahat

asked on

Save Puzzle

I've been Used a  VB6  Puzzle Code  ,  that let the game player choose a picture, devide it into no of Pieces, randomize these parts, then let the  game player  rearrange them again, and it is working fine.
I Need to add extra feature for the game, if the game player start the game, and  would like to continue later,  I'd like to give him the ability to save his work and continue later, which means he load the picture he want, then the system  devided it to  n number of  Pieces and randomize it, he soved  a 80% of the puzzle, and he would like to continue later, and he don't like to start from the   scrach.
I'm facing a problem in saving the picture after deviding it into Pieces and randomizing it .
i used ready made code.
kindly   see this link :
http://www.geocities.com/emu8086/vb/

then   click  on :
Create and Solve a picture Puzzle
 
and download the zip File to see the code.

i need to add my code after this line :
myPuzzle1.RandomizePictures
----
I put this line of code :

SavePicture myPuzzle1.MyPicture, "TEST33.jpg"

but the system saves the same picture befor randomizing.



hopefully you will find me  a solution.



Avatar of veeru_friend
veeru_friend

Hello my dear friend

The state ment below
SavePicture myPuzzle1.MyPicture, "veeru2.bmp"

call's the below function in myPuzzle usercontrol

Public Property Get MyPicture() As Picture
    Set MyPicture = picMAIN.Picture
End Property

where you are returning picMain.Picture

but you are not change picMain.Picture (i am mean re-painting)
in RandomizePictures() or in dividePICTURES() function

just you are changing positions of picD() picture array elements

just repaint picMain.Picture after RandomizePicture function will give you expected result.





Add the following to the declaration area of myPuzzle in project2:

Public FileName As String

Now add the following to the bottom of myPuzzle in project2 after the last property (MyPicture)

Public Function NumSubPics() As Integer
    NumSubPics = picD.Count
End Function

Public Property Get PicTop(Index As Integer) As Long
    PicTop = picD(Index).Top
End Property
Public Property Let PicTop(Index As Integer, NewValue As Long)
    picD(Index).Top = NewValue
End Property

Public Property Get PicLeft(Index As Integer) As Long
    PicLeft = picD(Index).Left
End Property
Public Property Let PicLeft(Index As Integer, NewValue As Long)
    picD(Index).Left = NewValue
End Property

Now add two command buttons to the form in project1.  Call them cmdSaveGame and cmdLoadGame:
Add this code to the form
Private Sub cmdLoadGame_Click()
    Dim hFile As Integer, i As Integer
    Dim intTemp As Integer
    Dim lngTemp As Long
    Dim strFilename As String
    strFilename = Space(1024)
   
    If Len(Dir(App.Path & "\Puzzle.sav")) > 0 Then
        MsgBox "Saved game doesn't exist"
        Exit Sub
    End If

    hFile = FreeFile
    Open App.Path & "\Puzzle.sav" For Binary As #hFile
    Get #hFile, , strFilename
    Set myPuzzle1.MyPicture = LoadPicture(Trim(strFilename))
    Get #hFile, , intTemp
    myPuzzle1.RowCount = intTemp
    Get #hFile, , intTemp
    myPuzzle1.ColCount = intTemp
   
    myPuzzle1.dividePICTURES
   
    For i = 0 To myPuzzle1.NumSubPics - 1
        Get #hFile, , lngTemp
        myPuzzle1.pictop(i) = lngTemp
        Get #hFile, , lngTemp
        myPuzzle1.picleft(i) = lngTemp
    Next i
    Close #hFile
End Sub

Private Sub cmdSaveGame_Click()
    Dim hFile As Integer, i As Integer
   
   
    If Len(Dir(App.Path & "\Puzzle.sav")) > 0 Then
        Kill App.Path & "\Puzzle.sav"
    End If
    hFile = FreeFile
    Open App.Path & "\Puzzle.sav" For Binary As #hFile
    Put #hFile, , Left$(myPuzzle1.FileName & Space(1024), 1024)
    Put #hFile, , myPuzzle1.RowCount
    Put #hFile, , myPuzzle1.ColCount
    For i = 0 To myPuzzle1.NumSubPics - 1
        Put #hFile, , myPuzzle1.pictop(i)
        Put #hFile, , myPuzzle1.picleft(i)
    Next i
    Close #hFile
   
   
End Sub
It saves the filename, number of rows and columns, then the top and left of each indexed piece of the puzzle.  When loading, it reassigns those properties to the puzzle.
Avatar of falahat

ASKER

thanks my dear friend (veeru_friend)  for help
but can you explain more to me how to do the   repaint , the full statment
Avatar of falahat

ASKER

thank you PaulHews   for help
but there is a problem in writing in the file can you double check  cmdSaveGame procedure
because it gave me the message  "Saved game doesn't exist", I Opend the Puzzle.sav file
it contained the  following  
"                                                                                                                                                                                                            ژ   ٹ   G   ٹ   ژ       ژ   \       ٹ           G   .   G           \   ژ   .       .   "
   myPuzzle1.RandomizePictures
This statement will change the picture based on the number of rows and columns selected.
So when you save the picture using SavePicture myPuzzle1.MyPicture, "TEST33.jpg" after this myPuzzle1.RandomizePictures, this would save the changed picture & not the one you are trying to save.
So use the statement SavePicture myPuzzle1.MyPicture, "TEST33.jpg" first before you use the myPuzzle1.RandomizePictures

   
Saving the picture will ONLY save the picture.  Considering you must first load the picture from a file, that does very little to help, so I put the file name in the saved game file.  

>it gave me the message  "Saved game doesn't exist"

Have you made modifications to the code other than what I stated?  I tested this against the downloaded project and it worked fine.  The only time you should get the "does not exist" message is when the file itself does not exist.  So if you've changed the code to write the file to a different location for example, you will have this problem.  Assuming you haven't changed the code, the length of the saved game file should be 1132 bytes.  It's a binary file, so you may not be able to read it correctly with Notepad.
Avatar of falahat

ASKER

dear  PaulHews  
kindly what you will get by this line:
Put #hFile, , Left$(myPuzzle1.FileName & Space(1024), 1024)
It saves the filename to the file, padded with spaces at the end until the entire string is 1024 bytes long.  The purpose of padding the string is because when we read it back in, we need to know how many bytes to read back in.  Every other variable that we write to the file has a fixed length, except the string.
Avatar of falahat

ASKER

dear  PaulHews  
Regarding the line  Put #hFile, , Left$(myPuzzle1.FileName & Space(1024), 1024)
the  myPuzzle1.FileName  doesn't get the file name, I put the the file name hardcoded and it is working fine.
so thank you ...

There is another question related to this question:
How to store the new image resulted from dividing and randomizing the original picture, means I Need to store
the new image itself, not its sub images places?
ASKER CERTIFIED SOLUTION
Avatar of PaulHews
PaulHews
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of falahat

ASKER

Dear   friends
There is another question related to this question:
How to store the new image resulted from dividing and randomizing the original picture, means I Need to store
the new image itself, not its sub images places?