Click to See Complete Forum and Search --> : [RESOLVED] Dance Mat Game Scoring


asf14
April 28th, 2010, 10:23 PM
Hello everyone,

i've created a dance mat game similar to Dance Dance Revolution. It has 4 arrows and when no step is taken it's a binary value 1111. When the up arrow is stepped on the binary value is 1110. I'm having problems with my scoring guide. I'm using streamwriter and streamreader to record the time stamp and the binary value of each step taken and once the game is done I have the file display the timestamps and the binary values. For right now I just want it to add one point for whenever it registers a step taken. This is the code I have that records the timestamp and the binary value. lblBitValue is the value that changes from a 1 to 0 binary.


Private Sub tmrReadInputs_Tick(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles tmrReadInputs.Tick
Dim I As Short
Dim ULStat As MccDaq.ErrorInfo
Dim DataValue As UInt16

tmrReadInputs.Stop()
' read FirstPortA digital input and display

' Parameters:
' PortNum :the input port
' DataValue :the value read from the port

ULStat = DaqBoard.DIn(PortNum, DataValue)
If ULStat.Value <> MccDaq.ErrorInfo.ErrorCode.NoErrors Then Stop

' display the value collected from the port

lblPortRead.Text = DataValue.ToString("0")

' parse DataValue into bit values to indicate on/off status

For I = 0 To 3
If (Convert.ToInt32(DataValue) And CInt((2 ^ I))) <> 0 Then
lblBitVal(I).Text = "1" 'NO STEP TAKEN
Else
lblBitVal(I).Text = "0" 'STEP TAKEN

TextBox1.Text = Trim(Format(Now, "hh:mm:ss tt "))

Dim GameFile As New System.IO.StreamWriter("C:\SavedGame.txt", OnGoingGame)
GameFile.WriteLine(TextBox1.Text & DataValue)
GameFile.Close()

OnGoingGame = True


End If
Next I

tmrReadInputs.Start()

End Sub



For the Scoring form I have the following:

Public Class Score
Public lblBitVal As System.Windows.Forms.Label()
Const PortNum As MccDaq.DigitalPortType = MccDaq.DigitalPortType.FirstPortA ' set port to use
Private DaqBoard As MccDaq.MccBoard = New MccDaq.MccBoard(0)l
Dim I As Short


Private Sub Score_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim ScoreValue As Decimal = 0
'ULStat = DaqBoard.DIn(PortNum, DataValue)
Dim GameScore As String = "C:\SavedGame.txt"
If System.IO.File.Exists(GameScore) = True Then
Dim objReader As New System.IO.StreamReader(GameScore)
ValueBox.Text = objReader.ReadToEnd
objReader.Close()
If lblBitVal(I).Text = "0" Then
ScoreValue += 1
Else
ScoreValue -= 0.5
End If
TextBox1.Text = ScoreValue
End Sub


It won't register lblBitVal in this form. It says "Object reference not set to an instance of an object." Also, this only adds 1 if there is a file opened. I need to it read the file and at least add a point for each time stamp that is recorded.

Let me know if you guys know how I can go about this since my game is almost complete :). THANKS AGAIN!

asf14
April 29th, 2010, 01:51 PM
I need the following code:
Dim intLines As Integer = ValueBox.Lines.Length
TextBox1.Text = intLines

which counts the number of lines in the text file, but is there a way to compare this file to another text file?

DataMiser
April 29th, 2010, 01:55 PM
Of course.

For example you could read both files in thier entirety or one line at a time and compare some or all,

What is it you are looking to do?

DataMiser
April 29th, 2010, 02:01 PM
You need to use code tags so your code can be read on the forum, very hard to follow as posted.

Looks like you are trying to access lblBitVal from a different class where it is not known.

asf14
April 29th, 2010, 03:57 PM
I would like to read the files line by line and compare if the timestamps are the same but before I do that I want to subtract the start time of my game to the time when the user steps on the dance mat. I have 3 text boxes showing the time when the start button was pressed, the second text box shows the time when the user steps on the dance and for the third text box I want it to show elapsed time. I have the following code under my start button:

start_time =Trim(Format(Now, "hh:mm:ss tt "))


In the main vb.net code:

Static current_time As DateTime
Static start_time As DateTime
Dim elapsed_time As TimeSpan

current_time = Trim(Format(Now, "hh:mm:ss tt "))
CurrentTimeBox.Text = current_time

elapsed_time = current_time.Subtract(start_time)

TextBox1.Text = elapsed_time


Sorry, I don't know where the code tags are? I'm trying to add less code :)

But I get an error for last line with elapsed_time saying system.timespan cannot be converted to string. Would it be better to subtract or is this an elapsed time function in vb.net? Thanks for your time and help guys!!

DataMiser
April 29th, 2010, 04:09 PM
To add code tags you can simply type

[ code ] Your code here [ /code ] without the spaces

Timespan has additional properties type a period after elapsed_time and you will see some of them.

There is also the DateDiff() function in VB that is still supported.

asf14
April 29th, 2010, 04:15 PM
Thanks Again DataMiser for your help! I added this:
TextBox1.Text = elapsed_time.Duration.Seconds

but when start time is 5:12:44
current time is : 5:12:45

it gives the elapsed time as 57

I want it to show 0:00:01 for the elapsed time... I'm not sure why it gives me 57 :(

DataMiser
April 29th, 2010, 04:21 PM
Show your code that you are using now.

asf14
April 29th, 2010, 04:24 PM
Static current_time As Date = Date.Now
Static start_time As Date = Date.Now
Dim elapsed_time As TimeSpan
For I = 0 To 3
If (Convert.ToInt32(DataValue) And CInt((2 ^ I))) <> 0 Then
lblBitVal(I).Text = "1"
Else
lblBitVal(I).Text = "0"


Dim GameFile As New System.IO.StreamWriter("C:\SavedGame.txt", OnGoingGame)
GameFile.WriteLine(StartTimeBox.Text & DataValue)
GameFile.Close()

OnGoingGame = True
'CurrentTimeBox.Text = Trim(Format(Now, "hh:mm:ss tt "))
current_time = Trim(Format(Now, "hh:mm:ss tt "))
CurrentTimeBox.Text = current_time
elapsed_time = current_time.Subtract(start_time)

TextBox1.Text = elapsed_time.Duration.Seconds
End If

DataMiser
April 29th, 2010, 04:28 PM
I think it is in your property usage.

Have a look at this.

Public Class Form1
Private StartTime As DateTime

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
StartTime = Now()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim EndTime As DateTime = Now()
Dim Elapsed_time As TimeSpan = EndTime.Subtract(StartTime)

Label1.Text = Elapsed_time.Seconds
End Sub
End Class

The starttime is set when button one is pressed.
The second button causes the number of elapsed seconds to display in a label on the form

asf14
April 29th, 2010, 04:40 PM
Ah Thank you again! Is there anyway to only show the time in "hh:mm:ss tt" ?? Sorry only reason I'm asking is that I'm saving just the time stamps and binary value in a text file using StreamWriter and then using StreamReader to display the timestamps with the binary value. So when i compare the two files, the dates will not be in the other file.

dglienna
April 29th, 2010, 04:56 PM
Look up String.Format()

asf14
April 29th, 2010, 09:15 PM
Great thanks dglienna! I used Trim(Format(Now, "hh:mm:ss tt"))

dglienna
April 30th, 2010, 12:06 PM
Pull down Thread Tools from the top, and MARK AS RESOLVED!

asf14
May 5th, 2010, 12:20 AM
It is really odd but now the scoring doesn't work.

When I step on the dance mat the start time textbox writes: 1:14:24am
Then when I step on my dance mat the end time text box writes: 1:14:28am
The elapsed time box writes: 4

Then when I view the text file it saved in, it says 28 instead of 4. It is somehow reading the seconds time from the end time text box. I tried changing the elapsed time to read total seconds but that did not work.



Dim endTime As DateTime = Trim(Format(Now, "hh:mm:ss tt"))
Dim elapsedTime As TimeSpan = endTime.Subtract(startTime)
EndTimeBox.Text = endTime
ElapsedTimeBox.Text = elapsedTime.Seconds 'display the elapsed time


Dim GameFile As New System.IO.StreamWriter("C:\SavedSurvive.txt", OnGoingGame)
GameFile.WriteLine(ElapsedTimeBox.Text & " : " & DataValue)
GameFile.Close()

OnGoingGame = True

End If

asf14
May 5th, 2010, 01:30 AM
GameFile.WriteLine(ElapsedTimeBox.Text & " : " & DataValue)


Now, it displays:

4 : 254
28 : 254

It displays the correct DataValue but it now displays the end time seconds along with the elapsed time. Why is it doing this? :(

asf14
May 5th, 2010, 08:38 AM
Hey guys! I'm still stuck on why this code adds the second's value of my end time. If you know how to fix this problem please let me know!

DataMiser
May 5th, 2010, 11:20 AM
Can't tell what is going on based on that little piece of code, could be getting called a second time from somewhere else, or perhaps there is another instance of code somewhere that writes to the file. Best bet is to set a break point and step through the code line by line and see what is happening.

asf14
May 5th, 2010, 11:28 AM
I have 3 different songs and the same code is in both (different text files and declared differently)! I'll try setting a break point! Thanks DataMiser

asf14
May 5th, 2010, 11:43 AM
It still takes the second's value from the end time... even though I clearly stated to only take the text in the elapsed time text box!

DataMiser
May 5th, 2010, 12:25 PM
Step through the code check the contents of your vars along the way [the ones related to the file] and see what you have there.

asf14
May 7th, 2010, 10:26 AM
Hey!

I added breakpoints and I don't see why the text file saves the end time's seconds time. It clearly says to only take the elasped_time textbox value.

Also, is there a way to make the text file read the values in numerical order (1-100)? My values display in a random order.

Thanks!

DataMiser
May 7th, 2010, 11:49 AM
Did you step through the code after the breakpoint and check the values of the vars related?

What do you mean by reading in numerical order? The file should be read and displayed as it was written originally.