游戏的高分

时间:2017-09-04 12:14:14

标签: vb.net

我想在游戏中丢失所有生命后,在一个单独的表格上显示高分,包括玩家名称。我目前有高分形式的代码 - 但它只显示数组中的一个分数,我想存储10个分数和玩家的名字。

Imports System.IO
'Code allows the computer to read from the text file containing player scores.'
Public Class Form3
    Dim highscore(9) As Integer ' array for highscores
    Dim playername(9) As String


    Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim i, value1, value2 As Integer 'player score variables

        Dim DataFile As StreamWriter
        '  Static save As String
        value1 = lblScore.Text ' assigns the players score to value1
        value2 = lblScore.Text ' assigns the players score to value2
        i = 0
        DataFile = New StreamWriter("D:\mmend12\My Documents\IPT\Projects\Brandon Moss - Major Project Assignment\Cosmic Crusade\Cosmic Crusade\bin\Debug\HighScore.txt")
        i = 0
        For i = 0 To 9
            If highscore(i) < value1 Then 'checks to see if the players score is bigger than the score
                value1 = highscore(i) ' assigns the value of highscore to value 1
                highscore(i) = value2 'assigns the players score to the highscore
                value2 = value1 ' assigns the previous highscore to value1
            End If
            lbxHighScores.Items.Add(highscore(i)) ' displays the scores
            'lbxNames.Items.Add(playername(i))
            DataFile.WriteLine(highscore(i)) ' saves the highscores to disk
            '   DataFile.WriteLine(playername(i))
        Next
        DataFile.Close()
    End Sub


    Private Sub btnShowScores_Click(sender As Object, e As EventArgs) Handles btnShowScores.Click
        Dim DataFile As StreamReader
        'Declares the variable name "DataFile" as an instance of StreamWriter, meaning that DataFile will behave like a StreamWriter object.'
        'Dim J As Integer
        DataFile = New StreamReader("D:\mmend12\My Documents\IPT\Projects\Brandon Moss - Major Project Assignment\Cosmic Crusade\Cosmic Crusade\bin\Debug\HighScore.txt")
        'Using the file path/address written above, a test.txt file is added in this folder.'
        'For J = 1 To 10
        'Creates a fixed loop, that loops 10 times

        For i = 0 To 9
            highscore(i) = DataFile.ReadLine
            lbxHighScores.Items.Add(DataFile.ReadLine)
        Next
        DataFile.Close()
        'Closes the data file. 
    End Sub

在所有生命遗失之后,我在游戏形式中拥有此代码:

If PlayersLives = 0 Then
                End If
                PlayerName = InputBox("Enter your username ")
                lblName.Text = PlayerName
                Form3.lblScore.Text = lblScoreNumber.Text
                Form3.lblPlayer.Text = lblName.Text

任何人都可以提供此方面的帮助。

1 个答案:

答案 0 :(得分:0)

我恭敬地建议重写Form3代码,如下所示。

我假设当您点击ListBox中的名称时,关联的分数会显示在LblScore

无论如何回到代码。这样,保存和加载分数的代码将与Load事件分开,修改高分列表的代码也会被分离出来。以后维护/修改会更容易。与控件分开操作数据也更好,以避免用户无意中修改的风险。

现在,每个玩家及其分数都存储在班级Player的实例中,并且所有玩家都存储在名为List (Of Player)的{​​{1}}

现在还有一个名为highScores的子使用UpdateListBox列表作为数据源。要显示的项目是每个玩家的highScores属性,当您单击玩家名称时返回的值是分数,然后分配给.Name而无需循环遍历数组或列表。

lblScore

在您的代码中添加高分,请使用类似

的内容
Public Class Form3

    'Code allows the computer to read from the text file containing player scores.'
    Friend Class Player
        Public Property Score As Integer
        Public Property Name As String
    End Class

    Dim highscores As New List(Of Player) ' array for highscores

    Public Sub AddScoreIfHighEnough(player As String, score As Integer)
        'make sure that the high score list is sorted first
        highscores = highscores.OrderByDescending(Function(x) x.Score).ToList
        'work down though the list
        For Each plyr As Player In highscores
            'if the score is higher than the current item in the list,
            'insert the new score there and exit the loop
            If score > plyr.Score Then
                highscores.Insert(highscores.IndexOf(plyr), New Player With {.Name = player, .Score = score})
                Exit For
            End If
        Next
        highscores.Add(New Player With {.Name = player, .Score = score})
        'if after the loop has completed, the number of items is
        'greater than 10, remove the lowest
        If highscores.Count > 10 Then
            highscores.Remove(highscores.Last)
        End If
        UpdateListbox()
        SaveScoreData()
    End Sub

    Private Sub SaveScoreData()
        Using DataFile As New StreamWriter("D:\mmend12\My Documents\IPT\Projects\Brandon Moss - Major Project Assignment\Cosmic Crusade\Cosmic Crusade\bin\Debug\HighScore.txt")
            For Each plyr As Player In highscores
                DataFile.WriteLine(plyr.Name) ' saves the name to disk
                DataFile.WriteLine(plyr.Score.ToString) 'saves the score
            Next
        End Using
    End Sub

    Private Sub LoadScoreData()
        highscores.Clear()
        Using DataFile As New StreamReader("D:\mmend12\My Documents\IPT\Projects\Brandon Moss - Major Project Assignment\Cosmic Crusade\Cosmic Crusade\bin\Debug\HighScore.txt")
            While Not DataFile.EndOfStream
                Dim tempPlayerScore As New Player With {
                    .Name = DataFile.ReadLine, ' saves the name to disk
                    .Score = CInt(DataFile.ReadLine) 'saves the score
                    }
                highscores.Add(tempPlayerScore)
            End While
        End Using
        UpdateListbox()
    End Sub

    Private Sub UpdateListbox()
        lbxHighScores.Items.Clear()
        For Each plyr As Player In highscores
            lbxHighScores.Items.Add(plyr.Name & "  " & plyr.Score)
        Next
    End Sub

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    LoadScoreData()
End Sub

Private Sub LbxHighScores_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lbxHighScores.SelectedIndexChanged
    If Not IsNothing(lbxHighScores.SelectedItem) Then
        lblScore.Text = CType(lbxHighScores.SelectedItem, Player).Score.ToString
    End If
End Sub
End Class