Results 1 to 9 of 9

Thread: Do I have to write everything.

  1. #1

    Thread Starter
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    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
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

  2. #2
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    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..

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    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

  4. #4
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: Do I have to write everything.

    A little code for my above post....
    VB Code:
    1. Dim I As Integer = 90  'declared outside the timer
    2.  
    3.         'in the timer tick event (1000 = interval, which equals 1 second)
    4.         If I = 0 Then      'disables timer once countdown is reached
    5.             thistimer.enabled = False      'thistimer is your timer name
    6.         End If
    7.         Label1.Text = I     'format this I for the time string, if you want 1:30 instead of 90
    8.         I = I - 1
    Last edited by gigemboy; Sep 27th, 2005 at 06:02 PM.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. Private seconds As Integer = 3
    2.     Private startup As Boolean = True
    3.  
    4.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    5.         Me.Timer1.Interval = 1000
    6.         Me.Timer1.Start()
    7.     End Sub
    8.  
    9.     Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    10.         If startup Then
    11.             Me.Label1.Text = Me.seconds.ToString()
    12.         Else
    13.             Dim time As TimeSpan = TimeSpan.FromSeconds(Me.seconds)
    14.  
    15.             Me.Label1.Text = String.Format("{0}:{1:d2}", time.Minutes, time.Seconds)
    16.         End If
    17.  
    18.         If seconds = 0 Then
    19.             Me.Timer1.Stop()
    20.         Else
    21.             Me.seconds -= 1
    22.         End If
    23.     End Sub
    24.  
    25.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    26.         Me.seconds = 90
    27.         Me.startup = False
    28.         Me.Timer1.Start()
    29.     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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,168

    Re: Do I have to write everything.

    here's an ugly one
    VB Code:
    1. Dim sec2 As Integer = 60
    2.  
    3.     Dim sec As Integer = 30
    4.  
    5.     Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    6.  
    7.         If sec <> 0 Or sec2 <> 0 Then
    8.             If sec > 0 Then
    9.                 Label1.Text = ""
    10.                 sec = sec - 1
    11.                 If sec >= 10 Then
    12.                     Label1.Text = "1:" & sec
    13.                 Else
    14.                     Label1.Text = "1:0" & sec
    15.                 End If
    16.             Else
    17.                 Label1.Text = ""
    18.                 sec2 = sec2 - 1
    19.                 If sec2 >= 10 Then
    20.                     Label1.Text = "00:" & sec2
    21.                 Else
    22.                     Label1.Text = "00:0" & sec2
    23.                 End If
    24.             End If
    25.         End If
    26.  
    27.     End Sub
    28. End Class

  7. #7
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    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...

  8. #8
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,168

    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.

  9. #9
    Frenzied Member Phill64's Avatar
    Join Date
    Jul 2005
    Location
    Queensland, Australia
    Posts
    1,201

    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:
    1. Private counter As New TimeSpan(0, 1, 30) 'set counter to 1 minute and 30 seconds
    2.     Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    3.         counter = counter.Subtract(New TimeSpan(0, 0, 1)) 'subtract 1 second
    4.         TextBox1.Text = counter.Minutes & ":" & counter.Seconds 'display time
    5.     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
  •  



Click Here to Expand Forum to Full Width