9 Replies - 819 Views - Last Post: 22 May 2013 - 10:44 AM Rate Topic: -----

#1 wellerdta  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 11-May 13

Countdown timer where you fail if you don't press a button in time

Posted 11 May 2013 - 05:16 AM

Hi,
I've just signed up to the forums and I'm just starting to learn code as a whole. I'm using C# within visual studio.

I'm making a text based game with 3 options which you have to choose to move forward. I'm trying to implement a timer e.g. 10 seconds.

I want to make it so if you don't press a choice within the time frame you fail.
I've looked on tutorials and I can't get my head around it.

I was hoping for some help or some guidance if possible.

Is This A Good Question/Topic? 0
  • +

Replies To: Countdown timer where you fail if you don't press a button in time

#2 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3325
  • View blog
  • Posts: 11,246
  • Joined: 12-December 12

Re: Countdown timer where you fail if you don't press a button in time

Posted 11 May 2013 - 05:29 AM

There is a reasonable introduction to the Timer here at dotnetperls.

The Timer can use a repeated interval or you can set the interval to 10s (10000) and turn off the timer when this interval expires.

This post has been edited by andrewsw: 11 May 2013 - 05:41 AM

Was This Post Helpful? 0
  • +
  • -

#3 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3325
  • View blog
  • Posts: 11,246
  • Joined: 12-December 12

Re: Countdown timer where you fail if you don't press a button in time

Posted 11 May 2013 - 06:00 AM

Here's a simple guide:

Add a Timer control to a form (I'm assuming a WinForm). Use the Properties Window to rename the control and set its Interval to 10000 (10 seconds).

Add a button that can be used to start the timer:

        private void btnStart_Click(object sender, EventArgs e) {
            //timer1.Interval = 10000;
            timer1.Start();
        }

If the user clicks a second button within 10 secs then the timer can be stopped:

        private void btnStop_Click(object sender, EventArgs e) {
            timer1.Stop();
            MessageBox.Show("You clicked in time!");
        }

Use the Properties Window again and find the Tick event of the timer. Double-click this and add the following code:

        private void timer1_Tick(object sender, EventArgs e) {
            timer1.Stop();
            MessageBox.Show("You failed!");
        }

This code will run if the interval (10 secs) has elapsed and the user hasn't clicked the button to stop the timer.

This post has been edited by andrewsw: 11 May 2013 - 06:03 AM

Was This Post Helpful? 1
  • +
  • -

#4 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3325
  • View blog
  • Posts: 11,246
  • Joined: 12-December 12

Re: Countdown timer where you fail if you don't press a button in time

Posted 11 May 2013 - 06:18 AM

Of course, it is sensible to have the Stop button initially disabled, and only enable it if they click Start:

        private void btnStart_Click(object sender, EventArgs e) {
            timer1.Start();
            btnStart.Enabled = false;
            btnStop.Enabled = true;
        }

        private void btnStop_Click(object sender, EventArgs e) {
            if (timer1.Enabled) {
                timer1.Stop();
                MessageBox.Show("You clicked in time!");
            }
            btnStart.Enabled = true;
            btnStop.Enabled = false;
        }

        private void timer1_Tick(object sender, EventArgs e) {
            timer1.Stop();
            MessageBox.Show("You failed!");
            btnStart.Enabled = true;
            btnStop.Enabled = false;
        }

Was This Post Helpful? 1
  • +
  • -

#5 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5466
  • View blog
  • Posts: 11,742
  • Joined: 02-June 10

Re: Countdown timer where you fail if you don't press a button in time

Posted 11 May 2013 - 07:14 AM

andrewsw said:

Add a Timer control to a form (I'm assuming a WinForm).

Probably not. The OP's description screams console app to me.

wellerdta said:

I'm making a text based game with 3 options which you have to choose to move forward.


You're not going to like this, but it needs to be put out there...

View Postwellerdta, on 11 May 2013 - 06:16 AM, said:

I'm just starting to learn code as a whole ...

I'm making a text based game


I'm going to stop ya right there. These two statement don't go together. You need to learn the language and the foundation concepts before you can begin designing a program from scratch. Otherwise you might as well be saying "I know nothing about auto mechanics and I can't even drive yet, so I thought I'd start out by designing a race car and building it."

At the very least, work your way though one "Learning C#" book cover to cover, along with our Learning C# series. Just learn and follow the lessons for that much.


My standard beginner resources post - Updated Apr 2013


Plan your study route:
There are three routes people seem to take when learning programming.
  • Just start trying to create programs
  • Start taking apart other programs and try to figure out the language by reverse engineering
  • Follow a guided learning course (school or self-teaching books)


For the life of me I can't figure out why people try 1 & 2. I strongly suggest taking the guided learning approach. Those book authors go in a certain order for a reason: They know what they're doing and they know the best order to learn the materials.

  • First learn the language by working 2-5 "Learn C# in 30 days" type books cover to cover.
  • Do a dozen on-line tutorial projects where you build what you're told to build, the way you are told to build it WITH AN EXPLANATION OF WHY so you can learn.
  • Learn to plan before you type.
  • THEN you start designing software with a purpose.



I don't learn from reading books: I learn by doing.
Spoiler


Newbie/Rookie said:

I have little/no programming experience but I need to write a program by Friday that does XYZ.
Spoiler



Resources, references and suggestions for new programmers.
Spoiler

Was This Post Helpful? 1
  • +
  • -

#6 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3325
  • View blog
  • Posts: 11,246
  • Joined: 12-December 12

Re: Countdown timer where you fail if you don't press a button in time

Posted 11 May 2013 - 07:24 AM

@tlhIn`toq
Hi. Yes, I noticed the mention of a text-based game, but the OP also mentioned 'pressing a choice'. Disney matter :)
Was This Post Helpful? 0
  • +
  • -

#7 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5466
  • View blog
  • Posts: 11,742
  • Joined: 02-June 10

Re: Countdown timer where you fail if you don't press a button in time

Posted 11 May 2013 - 08:40 AM

I'd bet that's pressing a key.

The monster is in front of you.  What do you do?
Press a key for your choice
R - Retreat
A - Attack
T - Invite the monster to a cup of tea
> _

Was This Post Helpful? 1
  • +
  • -

#8 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3325
  • View blog
  • Posts: 11,246
  • Joined: 12-December 12

Re: Countdown timer where you fail if you don't press a button in time

Posted 11 May 2013 - 08:57 AM

D - Stand still and hope he doesn't see you
Was This Post Helpful? 1
  • +
  • -

#9 wellerdta  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 11-May 13

Re: Countdown timer where you fail if you don't press a button in time

Posted 12 May 2013 - 10:50 AM

Thanks for the replies. I am going through a tutorial book at the moment but I'm just messing about with things and I may be jumping ahead of what I ca do.
Its been a good read though and a nice learnjg experience so far.
Thanks again.
Was This Post Helpful? 0
  • +
  • -

#10 Michael26  Icon User is offline

  • DIC-head, major DIC-head
  • member icon

Reputation: 353
  • View blog
  • Posts: 1,513
  • Joined: 08-April 09

Re: Countdown timer where you fail if you don't press a button in time

Posted 22 May 2013 - 10:44 AM

After you learn about object oriented programming you'll see it is much easier to make this kind of programs, you'll have class for players and their properties and methods, same goes to the monster class etc :)
Have fun learning.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1