Help With Timing

This is a discussion on Help With Timing within the C++ Programming forums, part of the General Programming Boards category; Hey... Anyway below I have pasted an excerpt from my in-progress racing game (sorta). I was trying to use time.h ...

  1. #1
    Registered User BKW++'s Avatar
    Join Date
    Feb 2009
    Location
    South Carolina
    Posts
    3

    Help With Timing

    Hey... Anyway below I have pasted an excerpt from my in-progress racing game (sorta).
    I was trying to use time.h to have it where the opponent's car would move after 1 second regardless if the user has moved or not, but that's the problem. The opponent only moves after you move, so I would like to know how could I make the opponent's car keep on moving even if I haven't moved my car.

    Also I included the whole code (in case you want to look at it).

    Code:
    unsigned int c;              //For arrow keys.
    
    char lane[16];
    
    const char blnk = ' ';
    const char ucar = '>';
    const char ocar = '<';
    
    lane[0] = ucar;
    lane[1] = blnk;
    lane[2] = blnk;
    lane[3] = blnk;
    lane[4] = blnk;
    lane[5] = blnk;
    lane[6] = blnk;
    lane[7] = blnk;
    lane[8] = blnk;
    lane[9] = blnk;
    lane[10] = blnk;
    lane[11] = blnk;
    lane[12] = blnk;
    lane[13] = blnk;
    lane[14] = blnk;
    lane[15] = blnk;
    
    int a = 15;                   //For opponents car to move.
           
    while(1){
             
          cout<<"\n\
          ================\n\
          "<<lane[0]<<" "<<lane[1]<<" "<<lane[2]<<" "<<lane[3]<<" "<<lane[4]<<" "<<lane[5]<<" "<<lane[6]<<" "<<lane[7]<<"\n\
          ----------------\n\
          "<<lane[8]<<" "<<lane[9]<<" "<<lane[10]<<" "<<lane[11]<<" "<<lane[12]<<" "<<lane[13]<<" "<<lane[14]<<" "<<lane[15]<<"\n\
          ================\
          \n\n";
          
          time(&start_time);
          
          time(&cur_time);
          
          lane[a] = ocar;
          
          lane[a + 1] = blnk;
          
        a--;
               
               if (a < 0){
                     a = 15;
               }
         /* 
          if ( ( time(&cur_time) - time(&start_time) ) != 1){
               
          lane[a] = ocar;
          
          lane[a + 1] = blnk;
          
          a--;
               
               if (a < 0){
                     a = 15;
               }
          }
          
    */
          
          c = getch();
    
           if (c == 72 || c == 77){ //Up & Right - In Order
           
                    if (lane [8] = ucar){
                    lane[8] = blnk;
                    lane[0] = ucar;
                    }
                    
           }
           
           else if (c == 80 || c == 75){ //Down & Left - In Order
           
                    if (lane [0] = ucar){
                    lane[0] = blnk;
                    lane[8] = ucar;
                    }
                    
           }
           
           clrscr();
           
    }
    Thanks for any help,
    ~BW
    Attached Files Attached Files

  2. #2
    Not stupid, just stupider yaya's Avatar
    Join Date
    May 2007
    Location
    Earthland
    Posts
    204
    Use kbhit() to check if any keys have been pressed. You can use it like this:

    Code:
    if ( kbhit() )
    {
    	// check what keys have been hit
    }

  3. #3
    Registered User BKW++'s Avatar
    Join Date
    Feb 2009
    Location
    South Carolina
    Posts
    3
    THANKS VERY MUCH!

    Seriously this will help me out a lot in this and other stuff.

    But now I feel stupid over such a simple solution...

    But yeah, thanks again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Timing basic operations in C++
    By StevenGarcia in forum C++ Programming
    Replies: 9
    Last Post: 09-18-2007, 03:10 AM
  2. Performance Timing Function
    By rosicky2005 in forum C++ Programming
    Replies: 11
    Last Post: 05-31-2007, 04:09 PM
  3. My Timing System
    By jmd15 in forum Windows Programming
    Replies: 4
    Last Post: 01-01-2006, 11:43 PM
  4. Games - timing
    By Magos in forum Game Programming
    Replies: 7
    Last Post: 03-06-2004, 11:32 AM
  5. Timing in Windows
    By steinberg in forum Windows Programming
    Replies: 3
    Last Post: 07-14-2002, 01:43 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21