5 Replies - 669 Views - Last Post: 26 March 2012 - 05:50 PM Rate Topic: -----

#1 Super Grape Ape  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 26-March 12

Holding Highscores for a a game

Posted 26 March 2012 - 03:47 PM

So, I've been working on my Pacman-ish game, and I want to have a high score table be available to be seen on the main options screen. Therefore, I need to save score information, and be able to separate it into a table, specifically 3 columns (Name, Orbs Collected and Time Alive). Here is the code on the main game form:

Public Class Form1
    Dim mouthIsOpen As Boolean = False
    Dim cD As Integer = 4
    Dim obstInt As Integer = 0
    Dim moveright As Boolean = True
    Dim moveup As Boolean = True
    Dim i As Integer = 0
    Dim touchedTop As Boolean = False
    Dim touchedRight As Boolean = False
    Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
        If mouthIsOpen = False Then
            PictureBox1.Image = My.Resources.closed
            mouthIsOpen = True
        ElseIf mouthIsOpen = True Then
            If cD = 1 Then
                PictureBox1.Image = My.Resources.openleft
            ElseIf cD = 2 Then
                PictureBox1.Image = My.Resources.openright
            ElseIf cD = 3 Then
                PictureBox1.Image = My.Resources.opendown
            ElseIf cD = 4 Then
                PictureBox1.Image = My.Resources.openup
            End If
            mouthIsOpen = False
            My.Computer.Audio.Play("C:\Users\Kade\Desktop\wakawaka.wav")
        End If
    End Sub

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Timer1.Start()
        TimerU.Start()
        TimerObst.Start()
    End Sub

    Private Sub Form1_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        If e.KeyCode = 65 Then
            TimerL.Start()
            TimerR.Stop()
            TimerD.Stop()
            TimerU.Stop()
            cD = 1
        ElseIf e.KeyCode = 68 Then
            TimerR.Start()
            TimerL.Stop()
            TimerD.Stop()
            TimerU.Stop()
            cD = 2
        ElseIf e.KeyCode = 83 Then
            TimerD.Start()
            TimerL.Stop()
            TimerR.Stop()
            TimerU.Stop()
            cD = 3
        ElseIf e.KeyCode = 87 Then
            TimerU.Start()
            TimerL.Stop()
            TimerR.Stop()
            TimerD.Stop()
            cD = 4
        ElseIf e.KeyCode = 96 Then

        End If
    End Sub

    Private Sub TimerL_Tick(sender As System.Object, e As System.EventArgs) Handles TimerL.Tick
        PictureBox1.Left = PictureBox1.Left - 2
    End Sub

    Private Sub TimerR_Tick(sender As System.Object, e As System.EventArgs) Handles TimerR.Tick
        PictureBox1.Left = PictureBox1.Left + 2
    End Sub

    Private Sub TimerD_Tick(sender As System.Object, e As System.EventArgs) Handles TimerD.Tick
        PictureBox1.Top = PictureBox1.Top + 2
    End Sub

    Private Sub TimerU_Tick(sender As System.Object, e As System.EventArgs) Handles TimerU.Tick
        PictureBox1.Top = PictureBox1.Top - 2
    End Sub

    Private Sub TimerObst_Tick(sender As System.Object, e As System.EventArgs) Handles TimerObst.Tick
        If moveright = True Then
            Panel1.Left += 7
            Panel2.Left += 7
            Panel3.Left += 7
            Panel4.Left += 7
        Else
            Panel1.Left -= 7
            Panel2.Left -= 7
            Panel3.Left -= 7
            Panel4.Left -= 7
        End If

        If moveup = True Then
            Panel5.Top += 7
            Panel6.Top += 7
            Panel7.Top += 7
        Else
            Panel5.Top -= 7
            Panel6.Top -= 7
            Panel7.Top -= 7
        End If

        If Panel4.Left <= Me.ClientRectangle.Left Then
            moveright = True
        End If
        If Panel4.Left + Panel4.Width >= Me.ClientRectangle.Right Then
            moveright = False
        End If
        If Panel5.Top <= Me.ClientRectangle.Top Then
            moveup = True
        End If
        If Panel5.Top + Panel5.Height >= Me.ClientRectangle.Bottom Then
            moveup = False
        End If
    End Sub

    Private Sub TimerCount_Tick(sender As System.Object, e As System.EventArgs) Handles TimerCount.Tick
        i = i + 1
        Label2.Text = "Time Alive: " & i & " seconds"
    End Sub

    Private Sub TimerCheck_Tick(sender As System.Object, e As System.EventArgs) Handles TimerCheck.Tick
        If PictureBox1.Left <= Me.ClientRectangle.Left Then
            touchedRight = True
        End If
        If PictureBox1.Left + PictureBox1.Width >= Me.ClientRectangle.Right Then
            touchedRight = True
        End If
        If PictureBox1.Top <= Me.ClientRectangle.Top Then
            touchedTop = True
        End If
        If PictureBox1.Top + PictureBox1.Height >= Me.ClientRectangle.Bottom Then
            touchedTop = True
        End If

            If touchedRight = True Then
                Timer1.Stop()
                TimerL.Stop()
                TimerR.Stop()
                TimerD.Stop()
                TimerU.Stop()
                TimerCount.Stop()
                TimerObst.Stop()
                TimerCheck.Stop()
                MessageBox.Show("Congratulations, dev, you stayed alive for " & i & " seconds!")
                Me.Close()
            ElseIf touchedTop = True Then
                Timer1.Stop()
                TimerL.Stop()
                TimerR.Stop()
                TimerD.Stop()
                TimerU.Stop()
                TimerCount.Stop()
                TimerObst.Stop()
                TimerCheck.Stop()
                MessageBox.Show("Congratulations, dev, you stayed alive for " & i & " seconds!")
                Me.Close()
            End If
    End Sub
End Class


However, I have no idea where to start with the high scores table. Any help is appreciated, thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Holding Highscores for a a game

#2 sela007  Icon User is offline

  • D.I.C Addict

Reputation: 138
  • View blog
  • Posts: 838
  • Joined: 21-December 11

Re: Holding Highscores for a a game

Posted 26 March 2012 - 04:02 PM

where do you want to save high scores(text?,xml?,some database?..)
The simplest way is to create one datatable and save/read high scores with xml. And show your high scores with datagrid.
Was This Post Helpful? 0
  • +
  • -

#3 Super Grape Ape  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 26-March 12

Re: Holding Highscores for a a game

Posted 26 March 2012 - 04:43 PM

View Postsela007, on 26 March 2012 - 04:02 PM, said:

where do you want to save high scores(text?,xml?,some database?..)
The simplest way is to create one datatable and save/read high scores with xml. And show your high scores with datagrid.


Just an XML file would be good, but I'm not really sure how to go about it.
Was This Post Helpful? 0
  • +
  • -

#4 sela007  Icon User is offline

  • D.I.C Addict

Reputation: 138
  • View blog
  • Posts: 838
  • Joined: 21-December 11

Re: Holding Highscores for a a game

Posted 26 March 2012 - 05:15 PM

All you need is one data table. Add three columns (Name, Orbs Collected and Time Alive) to table. When the game is over just add values to table. Then you can sort your data by high score,if you want to save just 10 best results then just remove all rows above 10. After that just save to xml.

Create table and add columns:
Dim tbl as New DataTable
tbl.Columns.Add...'add first column
tbl.Columns.Add...'add second column
tbl.Columns.Add...'add third column


Load existing high scores:
tbl.ReadXml(filename)


Add values to table:
tbl.Rows.Add("name",orbs collected,time alive)



Saving to xml
tbl.WriteXml(filename)


To show high scores in datagridview:
dgv1.DataSource = tbl

Was This Post Helpful? 1
  • +
  • -

#5 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2216
  • View blog
  • Posts: 9,351
  • Joined: 29-May 08

Re: Holding Highscores for a a game

Posted 26 March 2012 - 05:19 PM

Or create custom class, which encapsulates the concept of a Player's Score

Public Class PlayerScore
  
End Sub


Then you can have a

Dim Scores As New List(Of PlayerScore)

Sorting is a simple LINQ Query
Scores = (From s In Scores OrderBy s.Score Descending).ToList()


Saving your objects.

This post has been edited by AdamSpeight2008: 26 March 2012 - 05:20 PM

Was This Post Helpful? 1
  • +
  • -

#6 sela007  Icon User is offline

  • D.I.C Addict

Reputation: 138
  • View blog
  • Posts: 838
  • Joined: 21-December 11

Re: Holding Highscores for a a game

Posted 26 March 2012 - 05:50 PM

there are many ways, pick one, start typing and if you get stuck somewhere just post your code.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1