Visual Basic - 在我的游戏中为我的高分制作上限

时间:2013-04-20 17:34:39

标签: vb.net

我是一名学生,我正准备在C级课程中提供我的编程。

我正处于项目的最后阶段,我编写了一个高分的游戏。高分可以将记分保存在记事本中,这样当游戏重新启动时,它可以再次读取旧的高分。

问题是,每次新的高分都会不断增加新的分数。所以,如果我玩了100场比赛就有100个高分,我不希望这样。我想对我的高分进行限制。我在想15。

我有两个想法。第一个想法是程序只读取前15个高分。 第二个想法是我创建一个读取旧的高分数列表的子,然后将其与新的高分数进行比较,并检查是否需要更新旧的高分数列表。

但问题是,我编程很麻烦..我现在上传我的高分潜艇,并请你帮忙。

这是我写高分榜的地方

Private Sub skrivhighscore()
    ReDim Preserve HighscoreNavn(UBound(HighscoreNavn) + 1)
    ReDim Preserve HighscoreLevel(UBound(HighscoreLevel) + 1)

'insets the new highscore on the right place so that level is on top
        For i As Integer = 1 To UBound(HighscoreLevel)
        If (level > HighscoreLevel(i)) Then
            'the new highscore is bigger than highscorecount(i). inset the new here but first move the others 
            For j As Integer = UBound(HighscoreLevel) To i + 1 Step -1
                'kopier array(j-1) til array(j)
                HighscoreNavn(j) = HighscoreNavn(j - 1)
                HighscoreLevel(j) = HighscoreLevel(j - 1)
            Next j
            HighscoreNavn(i) = brugernavn
            ' sets highscore name to username. brugernavn = username
            HighscoreLevel(i) = level
            'set shighscorelvel(I) to the level the user died on.
            Exit For
        End If
    Next i

    'clear username and change level to 0
    brugernavn = ""
    level = 0

    'writes highscore to a file so it can be read next time
    highscoreboardskriv()

End Sub    

这是我将高分榜保存到记事本的地方

 Private Sub highscoreboardskriv()
    ' create a file at the same place as the game 
    'append:=False means overwrite and not repleace

    fileWriter = My.Computer.FileSystem.OpenTextFileWriter("HighScore.txt", append:=False)
    For i As Integer = 1 To UBound(HighscoreLevel)
        ' In every line the name is on the first   20 spaces and the score is from space   22
        'example: "Player1            :   2"
        If (HighscoreNavn(i) IsNot Nothing) AndAlso (HighscoreNavn(i).Trim <> "") Then
            ' If username is empty, it will not be written in the highscorelist. by using this method a highscore can be removed. 
            FileLine = HighscoreNavn(i).PadRight(20) & ":" & HighscoreLevel(i).ToString.PadLeft(5)
            fileWriter.WriteLine(FileLine)
        End If
    Next i

    fileWriter.Close()

End Sub

这是我展示我的高分榜

的地方
Private Sub Highscore()
    'Now we read both information again from the file 

    Dim HighScoreText As String = ""

    'highscorelist line by line
    For i As Integer = 1 To UBound(HighscoreLevel)
        'takes every spaces in an array (we dont use the first line)
        If (HighscoreNavn(i) IsNot Nothing) AndAlso (HighscoreNavn(i).Trim <> "") Then
            If (HighScoreText <> "") Then
                'new line by every highscore except the first line
                HighScoreText = HighScoreText & vbNewLine
            End If
            'line 1 example: "player1            :   230"
            HighScoreText = HighScoreText & HighscoreNavn(i).PadRight(20) & ":" & HighscoreLevel(i).ToString.PadLeft(5)
        End If
    Next i

    'show highscorelist to user
    MsgBox(HighScoreText, Title:="Highscore list")

End Sub

这是我从记事本中读取我的高分列表的地方:

Public Sub highscoreboardlæs()

Public HighscoreNavn(0) As String
Public HighscoreLevel(0) As Integer
Public level As Integer = 0
Public fileReader As System.IO.StreamReader
Public FileLine As String

    Try
        fileReader = My.Computer.FileSystem.OpenTextFileReader("HighScore.txt")

        FileLine = fileReader.ReadLine()

        While (FileLine <> "") 'loop så længe der er linjer i filen, for at få alle highscores med

            'extend array with 1 extra line

            ReDim Preserve HighscoreNavn(UBound(HighscoreNavn) + 1)
            ReDim Preserve HighscoreLevel(UBound(HighscoreLevel) + 1)


            HighscoreNavn(UBound(HighscoreNavn)) = Mid(FileLine, 1, 20)
            HighscoreLevel(UBound(HighscoreLevel)) = Val(Mid(FileLine, 22, 5))

            'read next line from the file
            FileLine = fileReader.ReadLine()
        End While

        fileReader.Close()
    Catch
        'i use try method, else it will crash if there arent any highscorelist
    End Try
    'MsgBox("Highscore: " & vbNewLine & fileReader.ReadLine() & vbNewLine & fileReader.ReadLine() & vbNewLine & fileReader.ReadLine())
End Sub

1 个答案:

答案 0 :(得分:0)

如果您只想在文件中保留15个分数,您可以将所有分数读入数组,对其进行排序并将最高的15分写回文件中。 如果您有更长的得分列表,我不建议您这样做,因为您将所有分数加载到内存中,这可能会给您带有更大列表的OOM(Out Of Memory)异常,但在您的情况下它完全没问题。

Sub AddScore(score%, filePath$)
    Dim Scores As New List(Of Integer)

    ' Loads all the scores into our list
    Using Sr As New IO.StreamReader(filePath)
        Do Until Sr.EndOfStream
            Scores.Add(Sr.ReadLine)
        Loop
    End Using

    Scores.Sort() ' Sort it
    Scores.Reverse() ' Reverse it, as the the highest scores is at the bottom. You would property want to just read trough the last 15 items in the list rather than reversing it, but I'm just being lazy.

    ' Write the first 15
    Using Sw As New IO.StreamWriter(filePath, False)
        For i = 0 To Math.Min(Scores.Count - 1, 14)
            Sw.WriteLine(Scores(i))
        Next
    End Using
End Sub

哦,顺便问一下,你的瑞典语吧?你介意给我发一个PM,因为我对编程学校在瑞典的兴趣很感兴趣。