|
-
Sep 27th, 2005, 05:34 PM
#1
Do I have to write everything.
Hi All,
Need help for my game!
First I have a Countdown before they start, then I have a Countdown before the game stops.
My question is, do I have to write the hole code, from 1' 30'' -> 0 ore can I write it on another way? See my code:
Private Sub Timer3_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer3.Tick
i = i + 1
If i = 1 Then
Label1.Text = "3"
ElseIf i = 2 Then
Label1.Text = "2"
ElseIf i = 3 Then
Label1.Text = "1"
ElseIf i = 4 Then
Label1.Text = "0"
ElseIf i = 5 Then
Label1.Text = "START GAME!"
ElseIf i = 6 Then
i = i - 1
Label1.Text = "1:30"
ElseIf i = 7 Then
Label1.Text = "1:29"
ElseIf i = 8 Then
Label1.Text = "1:28"
ElseIf i = 9 Then
Label1.Text = "1:28"
ElseIf i = 10 Then
Label1.Text = "1:27"
and more and more............
End If
End Sub
Thanks for your help,
sparrow1
-
Sep 27th, 2005, 05:50 PM
#2
Re: Do I have to write everything.
Set the countdown as another timer... set the tick interval to 1 second, which is 1000, then have a counter (declared outside the timer), set to like 90... (for 90 seconds).... in the tick event, change the label text to equal the counter variable, after you have decreased the counter by 1... then add some method to format the seconds into minutes..
-
Sep 27th, 2005, 05:51 PM
#3
Re: Do I have to write everything.
label.text = 4-i
is close to the first part.
Bascially, you need to find the relationship between i and the counter. In the first part, you are counting down as i is counting up, so using something like the line I just mentioned will do a much better job than that If...Else ladder.
The relationship in the second part is not quite as obvious. However, you are really counting down from 90sec, with i starting at 8. Therefore 98-i = the time you want in seconds for each i.
The next part is to format it. The easiest way to do that, though not necessarily the most efficient, is with an if statement, but something like this ought to work even better:
v=98-i
label.text = v\60 & ":" & v mod 60
I think that should do it.
My usual boring signature: Nothing
 
-
Sep 27th, 2005, 05:57 PM
#4
Re: Do I have to write everything.
A little code for my above post....
VB Code:
Dim I As Integer = 90 'declared outside the timer
'in the timer tick event (1000 = interval, which equals 1 second)
If I = 0 Then 'disables timer once countdown is reached
thistimer.enabled = False 'thistimer is your timer name
End If
Label1.Text = I 'format this I for the time string, if you want 1:30 instead of 90
I = I - 1
Last edited by gigemboy; Sep 27th, 2005 at 06:02 PM.
-
Sep 27th, 2005, 06:01 PM
#5
Re: Do I have to write everything.
You'll no doubt have to fiddle with this a little but it'll get you well on the way:
VB Code:
Private seconds As Integer = 3
Private startup As Boolean = True
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Timer1.Interval = 1000
Me.Timer1.Start()
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If startup Then
Me.Label1.Text = Me.seconds.ToString()
Else
Dim time As TimeSpan = TimeSpan.FromSeconds(Me.seconds)
Me.Label1.Text = String.Format("{0}:{1:d2}", time.Minutes, time.Seconds)
End If
If seconds = 0 Then
Me.Timer1.Stop()
Else
Me.seconds -= 1
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.seconds = 90
Me.startup = False
Me.Timer1.Start()
End Sub
Edit:
Wow. Three other posts while I was typing my code. Good to see such enthusiasm.
Last edited by jmcilhinney; Sep 27th, 2005 at 06:33 PM.
-
Sep 27th, 2005, 06:26 PM
#6
Frenzied Member
Re: Do I have to write everything.
here's an ugly one
VB Code:
Dim sec2 As Integer = 60
Dim sec As Integer = 30
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If sec <> 0 Or sec2 <> 0 Then
If sec > 0 Then
Label1.Text = ""
sec = sec - 1
If sec >= 10 Then
Label1.Text = "1:" & sec
Else
Label1.Text = "1:0" & sec
End If
Else
Label1.Text = ""
sec2 = sec2 - 1
If sec2 >= 10 Then
Label1.Text = "00:" & sec2
Else
Label1.Text = "00:0" & sec2
End If
End If
End If
End Sub
End Class
-
Sep 27th, 2005, 06:32 PM
#7
Re: Do I have to write everything.
Your code may be ugly to you.. but all they'll see is exactly what sparrow was looking for If ugly works... then im all for ugly...
-
Sep 27th, 2005, 11:27 PM
#8
Frenzied Member
Re: Do I have to write everything.
well, by ugly, i meant it's a code you can't recycle to use on bigger cases. But it's to the point.
-
Sep 28th, 2005, 12:44 AM
#9
Re: Do I have to write everything.
you could simplify that more by declaring the timspan outside the timer, and using subtract on it.
like this
VB Code:
Private counter As New TimeSpan(0, 1, 30) 'set counter to 1 minute and 30 seconds
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
counter = counter.Subtract(New TimeSpan(0, 0, 1)) 'subtract 1 second
TextBox1.Text = counter.Minutes & ":" & counter.Seconds 'display time
End Sub
plus add your (if timespan.seconds = 0 and minutes = 0) etc
EDIT NOTE: and use Jm's format method if double digits is prefered
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|