Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Save and Load state

generalengeneralen Posts: 68Member
Hi!

I have written a Snake Game in VB..Now I want so that the user can save the state of the snake in a file and then load the file and start from there..Anyone in here who can help me? I cant find anything easy to understand on the net...PLEASE



Comments

  • BitByBit_ThorBitByBit_Thor Posts: 2,444Member
    : Hi!
    :
    : I have written a Snake Game in VB..Now I want so that the user can save the state of the snake in a file and then load the file and start from there..Anyone in here who can help me? I cant find anything easy to understand on the net...PLEASE
    :
    :
    :
    :

    What part do you need?

    I'd suggest using Binary files (will allow you to store any type of data in any custom format)

    Here's an example:
    [code]
    Dim nBuffer As Integer [green]'The file buffer number, ranging from 1 To 255[/green]
    Dim sFileName As String
    Dim nLen As Integer [green]'Lenght of the data[/green]
    Dim sData As String [green]'The actual data in a string[/green]

    [green]
    'This code will open a textfile in binary mode.
    'There are other modes for opening a file, but I prefer
    ' binary mode for it's wide possibilities, and it doesn't have any
    ' nasty side effects to it
    'With Binary Files, you can open anything. You can also create your
    ' own file formats!
    [/green]

    sFileName = "C:A file.txt"
    nBuffer = FreeFile [green]'Obtain a free file number[/green]
    [green]'Open sFileName with binary mode for reading and writing,
    ' while locking the file from write operations,
    ' using nBuffer as filenumber[/green]
    Open sFileName For Binary Access Read Write Lock Write As nBuffer
    [green]'Get the lenght of the file[/green]
    nLen = LOF(nBuffer)
    [green]'Create a buffer[/green]
    sData = Space$(nLen)
    [green]'Get retrieves data from a file.
    'Syntax: Get FileNumber, BytePosition, DataBuffer
    'If you leave BytePosition out, it will be the next byte in the file
    'When you have just opened it, it will be byte 1[/green]
    Get nBuffer, , sData
    [green]'Put writes data to a file
    'For arrays and I think also fixed lenght strings
    ' (and possibly variables too)
    ' a header is saved before the data. With anything else,
    ' only the raw data is saved.
    'Syntax: Put FileNumber, BytePosition, Data
    'This examples converts the text to upper case and writes it back[/green]
    Put nBuffer, , UCase$(sData)

    [green]'Close the file[/green]
    Close nBuffer
    [green]'Filebuffer nBuffer is now free for use again[/green]

    [green]'Now we can do something with the data we loaded
    'For now, just use a messagebox to show what we got from the file[/green]
    MsgBox sData
    [/code]

    Greets...
    Richard

Sign In or Register to comment.