Thread: getch

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    45

    getch

    I am in the process of writing an old invaders game of sorts. When I ran into a problem, I was using

    Code:
    key = getch ( );
    switch ( key )
    {
    case '4':
    //go left
    break;
    
    case '6':
    //go right
    break;
    
    case '8':
    //go up
    break;
    
    case '2':
    //go down
    break;
    }
    when I realized then when I reach the getch statement, my programs halts. And waits for user input, that is a very serious problem. How can I get around this.

    p:s: the enemy ships and their bullets only move when I move.

  2. #2
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    You need an event checking method. I only know how to do this using SDL_PollEvent, which requires (surprise) SDL. A friend of mine once made a console based space invaders game. So it is possible. I don't know how, but I would be interested to find out.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    45
    in visual basic there is a keypress command, does C have a similar command that doesnt halt the program?

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    45
    aside from learning another language, any other ideas?

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The function for checking the "is there anything from the keyboard" is system and compiler dependant, but give "kbhit()" a try - it may work, depending on what environment you are programming in. It won't work under Linux, that's for sure.

    --
    Mats

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Try learning the O/S you're writing this for or a library that covers this stuff.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >p:s: the enemy ships and their bullets only move when I move.
    One idea is to use kbhit() to check whether a key has been pressed.
    Code:
    if (kbhit())
    {
    	key = getch ( );
    	switch ( key )
    	{
    	case '4':
    	//go left
    	break;
    
    	case '6':
    	//go right
    	break;
    
    	case '8':
    	//go up
    	break;
    
    	case '2':
    	//go down
    	break;
    	}
    }
    /* Other code */

  8. #8
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    Yup and linux doesn't have kbhit() so u could use the ncurses library (or graphics library of your choice) to detect for a keystroke... It's in any good game library, just a matter of finding it,

  9. #9
    Registered User
    Join Date
    Apr 2007
    Posts
    45
    kbhit is a step in the right direction. But now if I hit the left key, my ship keeps moving left until I till it to go somewhere else.

  10. #10
    Registered User
    Join Date
    Apr 2007
    Posts
    45
    in other words its acting like a toggle switch without the off command.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Post some more code, in particular the whole loop containing moving commands, kbhit() and getch() calls.
    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.

  12. #12
    Registered User
    Join Date
    Apr 2007
    Posts
    45
    swoopy has posted the code already.

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    So you have no code to move any object outside of the block guarded by if ( kbhit() ) ?
    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.

  14. #14
    Registered User
    Join Date
    Apr 2007
    Posts
    45
    The actual code is over 3000 characters , I am just working on the movement system as of now. And right now when I hit the left movement key, it keeps on moving until I change the direction.

    *edit*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Pls repair my basketball program
    By death_messiah12 in forum C++ Programming
    Replies: 10
    Last Post: 12-11-2006, 05:15 AM
  3. Pause a C program without getch()
    By swgh in forum C Programming
    Replies: 4
    Last Post: 02-20-2006, 11:24 AM
  4. Clearing input buffer after using getch()
    By milkydoo in forum C++ Programming
    Replies: 3
    Last Post: 07-21-2003, 11:04 PM
  5. Problems with getch()
    By GrNxxDaY in forum C++ Programming
    Replies: 14
    Last Post: 08-12-2002, 02:11 AM