Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

TROUBLE WITH TIMERS

Posted on 2004-10-15
7
249 Views
Last Modified: 2010-04-24
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
0
Comment
Question by:bassendoughboy
  • 4
  • 3
7 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 12322193
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
0
 

Author Comment

by:bassendoughboy
ID: 12322457
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.
0
 

Author Comment

by:bassendoughboy
ID: 12383399
Any other help?
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 12383546
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
0
 

Author Comment

by:bassendoughboy
ID: 12399395
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.
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 12401270
Something more like this then?

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 btnStart As System.Windows.Forms.Button
    Friend WithEvents btnNext As System.Windows.Forms.Button
    Friend WithEvents btnHit As System.Windows.Forms.Button
    Friend WithEvents btnPass As System.Windows.Forms.Button
    Friend WithEvents lblGameTime As System.Windows.Forms.Label
    Friend WithEvents lblTurnTime As System.Windows.Forms.Label
    Friend WithEvents gameTimer As System.Windows.Forms.Timer
    Friend WithEvents turnTimer As System.Windows.Forms.Timer
    Friend WithEvents lstHitLog As System.Windows.Forms.ListBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.components = New System.ComponentModel.Container
        Me.btnStart = New System.Windows.Forms.Button
        Me.lblGameTime = New System.Windows.Forms.Label
        Me.gameTimer = New System.Windows.Forms.Timer(Me.components)
        Me.btnNext = New System.Windows.Forms.Button
        Me.lblTurnTime = New System.Windows.Forms.Label
        Me.btnHit = New System.Windows.Forms.Button
        Me.btnPass = New System.Windows.Forms.Button
        Me.turnTimer = New System.Windows.Forms.Timer(Me.components)
        Me.lstHitLog = New System.Windows.Forms.ListBox
        Me.SuspendLayout()
        '
        'btnStart
        '
        Me.btnStart.Location = New System.Drawing.Point(8, 8)
        Me.btnStart.Name = "btnStart"
        Me.btnStart.Size = New System.Drawing.Size(104, 24)
        Me.btnStart.TabIndex = 0
        Me.btnStart.Text = "Start"
        '
        'lblGameTime
        '
        Me.lblGameTime.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.lblGameTime.Location = New System.Drawing.Point(120, 8)
        Me.lblGameTime.Name = "lblGameTime"
        Me.lblGameTime.Size = New System.Drawing.Size(104, 24)
        Me.lblGameTime.TabIndex = 1
        Me.lblGameTime.Text = "0:00.0"
        Me.lblGameTime.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        '
        'gameTimer
        '
        '
        'btnNext
        '
        Me.btnNext.Enabled = False
        Me.btnNext.Location = New System.Drawing.Point(8, 40)
        Me.btnNext.Name = "btnNext"
        Me.btnNext.Size = New System.Drawing.Size(104, 24)
        Me.btnNext.TabIndex = 2
        Me.btnNext.Text = "Next"
        '
        'lblTurnTime
        '
        Me.lblTurnTime.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.lblTurnTime.Location = New System.Drawing.Point(120, 40)
        Me.lblTurnTime.Name = "lblTurnTime"
        Me.lblTurnTime.Size = New System.Drawing.Size(104, 24)
        Me.lblTurnTime.TabIndex = 3
        Me.lblTurnTime.Text = "0.0"
        Me.lblTurnTime.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        '
        'btnHit
        '
        Me.btnHit.Enabled = False
        Me.btnHit.Location = New System.Drawing.Point(64, 72)
        Me.btnHit.Name = "btnHit"
        Me.btnHit.Size = New System.Drawing.Size(48, 24)
        Me.btnHit.TabIndex = 5
        Me.btnHit.Text = "Hit"
        '
        'btnPass
        '
        Me.btnPass.Enabled = False
        Me.btnPass.Location = New System.Drawing.Point(64, 104)
        Me.btnPass.Name = "btnPass"
        Me.btnPass.Size = New System.Drawing.Size(48, 24)
        Me.btnPass.TabIndex = 6
        Me.btnPass.Text = "Pass"
        '
        'turnTimer
        '
        '
        'lstHitLog
        '
        Me.lstHitLog.Location = New System.Drawing.Point(120, 72)
        Me.lstHitLog.Name = "lstHitLog"
        Me.lstHitLog.Size = New System.Drawing.Size(104, 121)
        Me.lstHitLog.TabIndex = 7
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(232, 198)
        Me.Controls.Add(Me.lstHitLog)
        Me.Controls.Add(Me.btnPass)
        Me.Controls.Add(Me.btnHit)
        Me.Controls.Add(Me.lblTurnTime)
        Me.Controls.Add(Me.btnNext)
        Me.Controls.Add(Me.lblGameTime)
        Me.Controls.Add(Me.btnStart)
        Me.Name = "Form1"
        Me.Text = "Timer Demonstration"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private gameStart As Date
    Private gameOver As Date

    Private turnStart As Date
    Private turnOver As Date

    Private gameSecs As Integer = 180
    Private turnSecs As Integer = 5

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        gameTimer.Enabled = False
        gameTimer.Interval = 50
        turnTimer.Enabled = False
        turnTimer.Interval = 50
    End Sub

    Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
        btnStart.Enabled = False
        lstHitLog.Items.Clear()
        gameStart = DateTime.Now()
        gameOver = gameStart.AddSeconds(gameSecs)
        gameTimer.Enabled = True
        btnNext.Enabled = True
    End Sub

    Private Sub gameTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles gameTimer.Tick
        Dim n As DateTime = DateTime.Now()
        Dim gameTime As TimeSpan = n.Subtract(gameStart)

        lblGameTime.Text = gameTime.Minutes & ":" & Format(gameTime.Seconds, "00") & "." & gameTime.Milliseconds.ToString.Substring(0, 1)
        If n > gameOver Then
            gameTimer.Enabled = False
            turnTimer.Enabled = False
            btnStart.Enabled = True
            btnNext.Enabled = False
            btnHit.Enabled = False
            btnPass.Enabled = False
            MsgBox("Game Over")
        End If
    End Sub

    Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
        btnNext.Enabled = False
        turnStart = DateTime.Now()
        turnOver = turnStart.AddSeconds(turnSecs)
        turnTimer.Enabled = True
        btnHit.Enabled = True
        btnPass.Enabled = True
    End Sub

    Private Sub turnTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles turnTimer.Tick
        Dim n As DateTime = DateTime.Now()
        Dim turnTime As TimeSpan = n.Subtract(turnStart)

        lblTurnTime.Text = Format(turnTime.TotalSeconds, "0.0")
        If n > turnOver Then
            turnTimer.Enabled = False
            lstHitLog.Items.Add("Time Expired")
            lblTurnTime.Text = "0.0"
            btnNext.Enabled = True
            btnHit.Enabled = False
            btnPass.Enabled = False
        End If
    End Sub

    Private Sub btnHit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHit.Click
        turnTimer.Enabled = False
        lstHitLog.Items.Add(lblTurnTime.Text)
        lblTurnTime.Text = "0.0"
        btnHit.Enabled = False
        btnPass.Enabled = False
        btnNext.Enabled = True
    End Sub

    Private Sub btnPass_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPass.Click
        turnTimer.Enabled = False
        lstHitLog.Items.Add("Passed")
        lblTurnTime.Text = "0.0"
        btnHit.Enabled = False
        btnPass.Enabled = False
        btnNext.Enabled = True
    End Sub

End Class
0
 

Author Comment

by:bassendoughboy
ID: 12462296
Thanks for the help I should be able to incorparate this into my program.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question