Thread: if statement triggered by a delay

  1. #1
    Registered User
    Join Date
    Dec 2005

    if statement triggered by a delay

    Hello everyone, I'm a long time lurker, first time poster. I am currently making a version of Snake with C, and I am having a problem with making the characters move by themselves when the user does not input a direction. Currently, the setup involves a 3d array making up the board, character, etc. When a certain key is pressed, the character moves respectively depending on what position the character is already in. All I need to do is have the program ask for a character for a certain amount of time, and if the user fails to enter one, it uses the previous one. I have been working with some parts of time.h, with little success. For one reason or another, I cannot get the clock to do as I please.

    I am using Visual C++ 2003, with Windows XP.

    Any advice or help would be greatly appreciated. I have read the FAQs on the site, but cannot seem to figure it out. Thanks.

  2. #2
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    West Virginia
    If this is a graphics application, I'd suggest looking at using key down/key up events. In your event loop, check to see how long it has been since the user pressed a key (you got an appropriate key-down event) and if that has been too long ago, you make the move.

    It is better (for a game) for you to set up a timer event and always move the actors; if a key event moves the player, the player moves; otherwise, it doesn't.

    This is not really a C programming question, but a Windows programming question. Check the game or windows forums.
    Insert obnoxious but pithy remark here

  3. #3
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Out of scope
    This is a console snake game I saw a while back. It creates a variable pause_length to set the speed. It doesn't use time.h at all. You should check it out and see how it does it.

    It also uses conio.h pretty extensivly including the very not portable gotoxy() function. So if you aren't on a borland compiler, you may have some trouble compiling the program.
    Sent from my iPadŽ

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Usefulness of the "else if" statement
    By gn17 in forum C Programming
    Replies: 7
    Last Post: 08-12-2007, 05:19 AM
  2. temperature sensors
    By danko in forum C Programming
    Replies: 22
    Last Post: 07-10-2007, 07:26 PM
  3. Networking (queuing delay, avg packet loss)
    By spoon_ in forum A Brief History of
    Replies: 0
    Last Post: 09-05-2005, 11:23 AM
  4. if statement has no effect
    By samGwilliam in forum C++ Programming
    Replies: 6
    Last Post: 03-26-2005, 12:53 PM
  5. Uh-oh! I am having a major switch problem!
    By goodn in forum C Programming
    Replies: 4
    Last Post: 11-01-2001, 04:49 PM