Как мога да запазя напредъка на игра във Visual Basic 2010?

Създадох игра със скрити предмети, като тази: http://www.shazaml.com/archives/creating-a-hidden-object-game-in-silverlight-3, където играчът трябва да намери около 60 обекта в картина. Има осем етикета, всеки попълнен с низ, който съдържа името на обект за намиране. Това е взето от pictureArray().

 Sub pictureArray()

    Randomize()

    pictures.Add("Cat")
    pictures.Add("Spade")
    pictures.Add("Butterfly")
    pictures.Add("Shoes")
    pictures.Add("Radio")
    pictures.Add("Grasshopper")
    pictures.Add("Lamp")
    pictures.Add("Coconut")
    pictures.Add("Clock")
    pictures.Add("Lightbulb")
    pictures.Add("Binoculars")
    pictures.Add("Diamond")
    pictures.Add("Hammer")
    pictures.Add("Bell")
    pictures.Add("Snail")
    pictures.Add("Bag")
    pictures.Add("Camera")
    pictures.Add("Pencil")
    pictures.Add("Horseshoe")
    pictures.Add("Brush")
    pictures.Add("Gloves")
    pictures.Add("Ball")
    pictures.Add("Umbrella")
    pictures.Add("Gnome")
    pictures.Add("Mouse")
    pictures.Add("Crown")
    pictures.Add("Torch")
    pictures.Add("Paperclip")
    pictures.Add("Handprint")
    pictures.Add("Hat")
    pictures.Add("Car")
    pictures.Add("Star")
    pictures.Add("Wheel")
    pictures.Add("Book")
    pictures.Add("Triangle")
    pictures.Add("Die")
    pictures.Add("Glasses")
    pictures.Add("One")
    pictures.Add("Shell")
    pictures.Add("Apple")
    pictures.Add("Bowling Pin")
    pictures.Add("Spoon")
    pictures.Add("Magnifier")
    pictures.Add("Letter R")
    pictures.Add("Aeroplane")
    pictures.Add("Scissors")
    pictures.Add("Mask")
    pictures.Add("Bow")
    pictures.Add("Coin")
    pictures.Add("Frog")
    pictures.Add("Snake")
    pictures.Add("Key")
    pictures.Add("Rope")
    pictures.Add("Peg")
    pictures.Add("Bottle")
    pictures.Add("Belt")
    pictures.Add("Angel")
    pictures.Add("Pawprint")
    pictures.Add("Clover")
    pictures.Add("Phone")
    pictures.Add("Ring")

    lblOne.Text = (pictures(RandIndex))
    pictures.RemoveAt(RandIndex)
    RandIndex = RandGen.Next(0, pictures.Count)

    lblTwo.Text = (pictures(RandIndex))
    pictures.RemoveAt(RandIndex)
    RandIndex = RandGen.Next(0, pictures.Count)

    lblThree.Text = (pictures(RandIndex))
    pictures.RemoveAt(RandIndex)
    RandIndex = RandGen.Next(0, pictures.Count)

    lblFour.Text = (pictures(RandIndex))
    pictures.RemoveAt(RandIndex)
    RandIndex = RandGen.Next(0, pictures.Count)

    lblFive.Text = (pictures(RandIndex))
    pictures.RemoveAt(RandIndex)
    RandIndex = RandGen.Next(0, pictures.Count)

    lblSix.Text = (pictures(RandIndex))
    pictures.RemoveAt(RandIndex)
    RandIndex = RandGen.Next(0, pictures.Count)

    lblSeven.Text = (pictures(RandIndex))
    pictures.RemoveAt(RandIndex)
    RandIndex = RandGen.Next(0, pictures.Count)

    lblEight.Text = (pictures(RandIndex))
    pictures.RemoveAt(RandIndex)
    RandIndex = RandGen.Next(0, pictures.Count)

End Sub

Тъй като има много обекти за намиране, играчът може да иска да излезе от играта и да се върне към нея на по-късна дата, затова се чудех кой е най-добрият начин да запазя текущия напредък на играта, така че играчът да може продължават от там, където са спрели?

Направих някои проучвания, опитвайки се да намеря начин онлайн, но не го разбрах или не знам какво би било най-добро за тази игра?


person KSR5    schedule 03.10.2013    source източник


Отговори (1)


По принцип бихте създали КЛАС, който да съдържа списъка (от низ) и всяка от осемте текущи стойности във вашите етикети. След това използвайте XmlSerializer, за да запазите и заредите тази информация в XML файл.

Ето един много прост пример:

Public Class Form1

    Private RandGen As New Random
    Private Pictures As New List(Of String)
    Private DataFile As String = System.IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.CurrentUserApplicationData, "GameState.xml")

    Sub pictureArray()
        Pictures.Clear()
        pictures.Add("Cat")
        pictures.Add("Spade")
        pictures.Add("Butterfly")
        pictures.Add("Shoes")
        pictures.Add("Radio")
        pictures.Add("Grasshopper")
        pictures.Add("Lamp")
        pictures.Add("Coconut")
        pictures.Add("Clock")
        pictures.Add("Lightbulb")
        pictures.Add("Binoculars")
        pictures.Add("Diamond")
        pictures.Add("Hammer")
        pictures.Add("Bell")
        pictures.Add("Snail")
        pictures.Add("Bag")
        pictures.Add("Camera")
        pictures.Add("Pencil")
        pictures.Add("Horseshoe")
        pictures.Add("Brush")
        pictures.Add("Gloves")
        pictures.Add("Ball")
        pictures.Add("Umbrella")
        pictures.Add("Gnome")
        pictures.Add("Mouse")
        pictures.Add("Crown")
        pictures.Add("Torch")
        pictures.Add("Paperclip")
        pictures.Add("Handprint")
        pictures.Add("Hat")
        pictures.Add("Car")
        pictures.Add("Star")
        pictures.Add("Wheel")
        pictures.Add("Book")
        pictures.Add("Triangle")
        pictures.Add("Die")
        pictures.Add("Glasses")
        pictures.Add("One")
        pictures.Add("Shell")
        pictures.Add("Apple")
        pictures.Add("Bowling Pin")
        pictures.Add("Spoon")
        pictures.Add("Magnifier")
        pictures.Add("Letter R")
        pictures.Add("Aeroplane")
        pictures.Add("Scissors")
        pictures.Add("Mask")
        pictures.Add("Bow")
        pictures.Add("Coin")
        pictures.Add("Frog")
        pictures.Add("Snake")
        pictures.Add("Key")
        pictures.Add("Rope")
        pictures.Add("Peg")
        pictures.Add("Bottle")
        pictures.Add("Belt")
        pictures.Add("Angel")
        pictures.Add("Pawprint")
        pictures.Add("Clover")
        pictures.Add("Phone")
        pictures.Add("Ring")

        Dim RandIndex As Integer
        lblOne.Text = (pictures(RandIndex))
        pictures.RemoveAt(RandIndex)
        RandIndex = RandGen.Next(0, pictures.Count)

        lblTwo.Text = (pictures(RandIndex))
        pictures.RemoveAt(RandIndex)
        RandIndex = RandGen.Next(0, pictures.Count)

        lblThree.Text = (pictures(RandIndex))
        pictures.RemoveAt(RandIndex)
        RandIndex = RandGen.Next(0, pictures.Count)

        lblFour.Text = (pictures(RandIndex))
        pictures.RemoveAt(RandIndex)
        RandIndex = RandGen.Next(0, pictures.Count)

        lblFive.Text = (pictures(RandIndex))
        pictures.RemoveAt(RandIndex)
        RandIndex = RandGen.Next(0, pictures.Count)

        lblSix.Text = (pictures(RandIndex))
        pictures.RemoveAt(RandIndex)
        RandIndex = RandGen.Next(0, pictures.Count)

        lblSeven.Text = (pictures(RandIndex))
        pictures.RemoveAt(RandIndex)
        RandIndex = RandGen.Next(0, pictures.Count)

        lblEight.Text = (pictures(RandIndex))
        pictures.RemoveAt(RandIndex)
        RandIndex = RandGen.Next(0, pictures.Count)
    End Sub

    Private Sub btnSave_Click(sender As System.Object, e As System.EventArgs) Handles btnSave.Click
        Dim gs As New GameState
        gs.Pictures = Pictures
        gs.lblOne = lblOne.Text
        gs.lblTwo = lblTwo.Text
        gs.lblThree = lblThree.Text
        gs.lblFour = lblFour.Text
        gs.lblFive = lblFive.Text
        gs.lblSix = lblSix.Text
        gs.lblSeven = lblSeven.Text
        gs.lblEight = lblEight.Text

        Try
            Dim xml As New System.Xml.Serialization.XmlSerializer(gs.GetType)
            Using fs As New System.IO.FileStream(DataFile, IO.FileMode.OpenOrCreate, IO.FileAccess.Write)
                xml.Serialize(fs, gs)
            End Using
            MessageBox.Show("Game Saved!")
        Catch ex As Exception
            MessageBox.Show(ex.ToString, "Error Saving Game")
        End Try
    End Sub

    Private Sub btnLoad_Click(sender As System.Object, e As System.EventArgs) Handles btnLoad.Click
        If System.IO.File.Exists(DataFile) Then
            Try
                Dim gs As GameState
                Dim xml As New System.Xml.Serialization.XmlSerializer(GetType(GameState))
                Using fs As New System.IO.FileStream(DataFile, IO.FileMode.Open, IO.FileAccess.Read)
                    gs = DirectCast(xml.Deserialize(fs), GameState)
                    Pictures = gs.Pictures
                    lblOne.Text = gs.lblOne
                    lblTwo.Text = gs.lblTwo
                    lblThree.Text = gs.lblThree
                    lblFour.Text = gs.lblFour
                    lblFive.Text = gs.lblFive
                    lblSix.Text = gs.lblSix
                    lblSeven.Text = gs.lblSeven
                    lblEight.Text = gs.lblEight
                    MessageBox.Show("Game Loaded!")
                End Using
            Catch ex As Exception
                MessageBox.Show(ex.ToString, "Error Loading Game")
            End Try
        Else
            MessageBox.Show("No Saved Game Found")
        End If
    End Sub

End Class

<Serializable()> _
Public Class GameState
    Public Pictures As New List(Of String)
    Public lblOne As String
    Public lblTwo As String
    Public lblThree As String
    Public lblFour As String
    Public lblFive As String
    Public lblSix As String
    Public lblSeven As String
    Public lblEight As String
End Class
person Idle_Mind    schedule 03.10.2013