program not wait for a key press but if it is pressed should do other specified job

This is a discussion on program not wait for a key press but if it is pressed should do other specified job within the C Programming forums, part of the General Programming Boards category; I am writing a program for making the game snakes . I am already successful in making the snake move ...

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    3

    Question program not wait for a key press but if it is pressed should do other specified job

    I am writing a program for making the game snakes.
    I am already successful in making the snake move continuously.

    Now what I want is that my snake should not wait for a key to be pressed that is it should continue to move in the direction it is moving, but if a particular key is pressed it should change its direction to what is specified by that key.

    for example if the snake is moving in right direction and somebody presses 'v' (for down direction), it should start moving downwards.

    If I use getch(), the snake starts waiting for the key press and does not move contiously in the direction it was previously moving.

    Is there any way I could make a particular part of my program execute when a particular key is pressed without waiting for a key press?

  2. #2
    Registered User
    Join Date
    Jul 2003
    Posts
    18
    Hi Aarti
    I think u cud change ur code to something like this.Use a loop to find if any key is pressed and if that key is a direction key then take appropriate action required.Just use something like this.

    a do loop

    do
    {
    if(kbhit())
    {
    keypress=(char)getch();
    if((keypress==rightdirection_key)||(keypress=leftd irection_key)||
    (keypress=updirection_key)||(keypress==downdirecti on_key))
    direction=keypress;
    }
    }

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Yes. There is no ANSI way to do it, but yes, it can be done. In DOS, look at 'kbdhit'. In *nix, look into the 'curses' library.

    (Isn't this in the FAQ? I haven't bothered checking recently, but I know it's in a FAQ some place...)

    [edit]Curses (no pun intended) foiled again![/edit]


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A key press funtion
    By getinhigh4life in forum Game Programming
    Replies: 3
    Last Post: 08-08-2008, 10:58 PM
  2. Totally confused on assigment using linked lists
    By Uchihanokonoha in forum C++ Programming
    Replies: 8
    Last Post: 01-05-2008, 03:49 PM
  3. Replies: 16
    Last Post: 11-23-2007, 12:48 PM
  4. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  5. BST/Red and Black Tree
    By ghettoman in forum C++ Programming
    Replies: 0
    Last Post: 10-24-2001, 10:45 PM

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