Link to home
Start Free TrialLog in
Avatar of bassendoughboy
bassendoughboy

asked on

TROUBLE WITH TIMERS

I'm try to write a small game that uses a main timer to control how long the person can play as a total and a another timer that controls how long each part will last. example: start button is clicked main timer starts running, then player clisks the NEXT button play timer starts and runs for x amount of seconds if button A is pressed before time runs out time is longed and then it waits for the NEXT button if no button is pressed timer runs out and then longs thsi and waits for the NEXT button to be pressed and it starts al over again. Any help with setting the structer of this up would be nice
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

There are several ways to go about it.  It this even close to what you are looking for?

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents Timer1 As System.Windows.Forms.Timer
    Friend WithEvents Button2 As System.Windows.Forms.Button
    Friend WithEvents Label2 As System.Windows.Forms.Label
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.components = New System.ComponentModel.Container
        Me.Button1 = New System.Windows.Forms.Button
        Me.Label1 = New System.Windows.Forms.Label
        Me.Timer1 = New System.Windows.Forms.Timer(Me.components)
        Me.Button2 = New System.Windows.Forms.Button
        Me.Label2 = New System.Windows.Forms.Label
        Me.SuspendLayout()
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(8, 8)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(96, 24)
        Me.Button1.TabIndex = 0
        Me.Button1.Text = "Start Game"
        '
        'Label1
        '
        Me.Label1.Location = New System.Drawing.Point(112, 8)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(168, 24)
        Me.Label1.TabIndex = 1
        Me.Label1.Text = "Label1"
        Me.Label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        '
        'Timer1
        '
        '
        'Button2
        '
        Me.Button2.Enabled = False
        Me.Button2.Location = New System.Drawing.Point(8, 40)
        Me.Button2.Name = "Button2"
        Me.Button2.Size = New System.Drawing.Size(96, 24)
        Me.Button2.TabIndex = 2
        Me.Button2.Text = "Next Turn"
        '
        'Label2
        '
        Me.Label2.Location = New System.Drawing.Point(112, 40)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(168, 24)
        Me.Label2.TabIndex = 3
        Me.Label2.Text = "Label2"
        Me.Label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 266)
        Me.Controls.Add(Me.Label2)
        Me.Controls.Add(Me.Button2)
        Me.Controls.Add(Me.Label1)
        Me.Controls.Add(Me.Button1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private gameOver As Date
    Private turnOver As Date
    Private gameTimeLeft As TimeSpan
    Private gameSecs As Integer = 60
    Private turnSecs As Integer = 10

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Timer1.Enabled = False
        Timer1.Interval = 250
        Label1.Text = gameSecs
        Label2.Text = turnSecs
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Button1.Enabled = False
        Button2.Enabled = True
        Label1.Text = gameSecs
        Label2.Text = turnSecs
        gameOver = DateTime.Now().AddSeconds(gameSecs)
        turnOver = DateTime.Now().AddSeconds(turnSecs)
        Timer1.Enabled = True
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        If gameTimeLeft.TotalSeconds > turnSecs Then
            Label2.Text = turnSecs
            turnOver = DateTime.Now.AddSeconds(turnSecs)
        Else
            Label2.Text = CInt(gameTimeLeft.TotalSeconds)
            turnOver = DateTime.Now().AddSeconds(gameTimeLeft.TotalSeconds)
        End If
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim n As DateTime = DateTime.Now()
        gameTimeLeft = gameOver.Subtract(n)
        If gameTimeLeft.TotalSeconds > 0 Then
            Label1.Text = CInt(gameTimeLeft.TotalSeconds)
            Dim turnTS As TimeSpan = turnOver.Subtract(n)
            If turnTS.TotalSeconds > 0 Then
                Label2.Text = CInt(turnTS.TotalSeconds)
            Else
                Label2.Text = "0"
                Timer1.Enabled = False
                MsgBox("Game Over")
                Button2.Enabled = False
                Button1.Enabled = True
            End If
        Else
            Label1.Text = "0"
            Timer1.Enabled = False
            MsgBox("Game Over")
            Button2.Enabled = False
            Button1.Enabled = True
        End If
    End Sub

End Class
Avatar of bassendoughboy
bassendoughboy

ASKER

I do thank you for your fast responce.
This close to what I need, But my problem seems to be able to keep the gameSecs "which I will have to make into mins" countimg down to end the game . also after the button is clicked or the turnSecs runs out i need the gamesec to keep running and waiting on another button to be clicked to start the turnsec to counting and waithing on another click of to run out of time. I gues I need three buttons in all. sort of like cklick  start to get game closk to counting down and then another to start the turn and one to end turn. I will try to use what you have given me. any other help would be grate.
Any other help?
I'd be glad to help more but I don't quite understand how all the timers should work yet.  Explain in great detail how each timer should work and react to button pushes.

~IM
You start the program, it sets there until the start button is clicked, this starts maintimer  running for lets say 3 min this is how long the total game will last, then there is a button called next this will start the playtime which is 5 sec with in this 5 sec another button called hit should be pressed this wil stop the playtimer and set it back to 0 and wait for the next button to be clicked again or another button to be clicked called pass if no button is clicked timer runs out and retuns back to a waiut for  the next button, all the while the maintimer is counting down. each time the hit button is clicked this will put the value of the playtimer into a label which I hope to be able to send to a log or file.
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for the help I should be able to incorparate this into my program.