我是一名学生,我正准备在 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