Thread: Timer in C

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    61

    Timer in C

    Hi, I want to create a Hangman game with timer..
    If the user cannot guess the word with in 1:30 min the game will stop and the word will be printed.
    Can you help me to create the timer..
    Thanks

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Are you still using TurboCrap?

    Or is this a whole new guessing game.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    21
    You might try usleep() with an incrementing variable to visually time it. You will have to loop it. usleep() uses micro seconds so usleep(1000000) is one second. And you have to #include<unistd.h>..

    Hope this helps!!

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Something different:


    Go here, and d/l the timer program:
    Orzeszek Timer

    Rename it OrzTimer.exe

    in a plain text editor, create the run.bat file, with:
    @echo off
    OrzTimer.exe 15 seconds

    and save it into the same directory where this
    program will be placed.Change the seconds in
    the bat file to suit your needs.

    Start it, right click the center of the window
    and select "options". Set it to close when done.

    Compile and run this program.

    Code:
    #include <stdio.h>
    #include <process.h>
    
    int main (){
       int i;
       _spawnl(_P_NOWAIT, "run.bat", NULL);
       for(i=0;i<14;i++) {
          printf("%Your game code goes in here\n",i);
          _sleep(1);
          
       }
          
       printf("\n");
    
       return 0;
    }

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    I would guess that the OP wants to be able to type in guesses whilst the timer is counting down, rather than just blocking for some specified time and then saying "game over", whilst the user furiously pounded the keyboard to an unresponsive program.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Salem View Post
    I would guess that the OP wants to be able to type in guesses whilst the timer is counting down, rather than just blocking for some specified time and then saying "game over", whilst the user furiously pounded the keyboard to an unresponsive program.

    That is a non-blocking spawn(). Game remains very responsive. The only glitch with it is that you have to click on the game window to return focus to that window.

    Seems like the clock should start, and THEN the game window should spawn and run, to avoid that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Timer?
    By alexnb185 in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 11:54 AM
  2. Timer again.
    By geek@02 in forum Windows Programming
    Replies: 1
    Last Post: 05-04-2005, 10:19 PM
  3. Timer?
    By Disturbed1 in forum Game Programming
    Replies: 2
    Last Post: 06-03-2003, 05:09 PM
  4. need help with a timer
    By revelation437 in forum C++ Programming
    Replies: 10
    Last Post: 03-27-2003, 11:25 AM
  5. how to set timer
    By marianne in forum C Programming
    Replies: 1
    Last Post: 02-12-2003, 10:21 PM