Thread: Console games and movement

  1. #1
    Programming is fun, mkay?
    Join Date
    Oct 2001
    Posts
    490

    Question Console games and movement

    I am developing my first "game" in console. I know how to move around using gotoxy(x, y); but how do I use X and Y keys? I have seen games like tic-tac-toe where you press LEFT and it moves left. How do I do this in console?
    Website(s): http://www16.brinkster.com/trifaze/

    E-mail: [email protected]

    ---------------------------------
    C++ Environment: MSVC++ 6.0; Dev-C++ 4.0/4.1
    DirectX Version: 9.0b
    DX SDK: DirectX 8.1 SDK

  2. #2
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    use these with getch() or getchar whenever you want to test for keys.

    left arrow = 'K'
    up arrow = 'H'
    down arrow = 'P'
    right arrow = 'M'

    like

    Code:
    char ch;
    
    //do stuff...
    ch = getch();
    if(ch == 'H')
        //do something for the UP arrow
    ect...
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  3. #3
    Programming is fun, mkay?
    Join Date
    Oct 2001
    Posts
    490

    Question Well, how do I...

    How do I do it without having to press ENTER? I just want to make something move as soon as the key is pushed.
    Website(s): http://www16.brinkster.com/trifaze/

    E-mail: [email protected]

    ---------------------------------
    C++ Environment: MSVC++ 6.0; Dev-C++ 4.0/4.1
    DirectX Version: 9.0b
    DX SDK: DirectX 8.1 SDK

  4. #4
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    unfortunatly this is implementation dependant, usually people use getch() or getche() for this but it may not work with certain compilers.
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  5. #5
    Registered User
    Join Date
    Feb 2002
    Posts
    19
    This is what I do because I dont like getch() also its a lot more flexible though harder to learn.

    You will need to use the <windows.h> header

    first what I do is create standard input output handles something like

    HANDLE hStdIn, hStdOut; //create them
    //get the standard handles
    hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
    hStdIn = GetStdHandle(STD_INPUT_HANDLE);

    //then I create a buffer for input
    INPUT_RECORD inputBuffer;
    DWORD dwInputEvents; //and a counter for events read

    do //start a loop
    {
    //then i read an input event
    ReadConsoleInput(hStdIn, &inputBuffer, 1, &dwInputEvents);

    switch (inputBuffer.EventType){//put a switch in it for inputbuffer
    case KEY_EVENT: //cout key info when button is down
    if (inputBuffer.Event.KeyEvent.bKeyDown) {
    cout<<" Key Down = " <<
    inputBuffer.Event.KeyEvent.uChar.AsciiChar <<" Virtual code = "
    <<inputBuffer.Event.KeyEvent.wVirtualKeyCode<<endl ;
    }
    break;
    case MOUSE_EVENT: //cout a bunch of info about the moust
    cout<<" Mouse: "<<(inputBuffer.Event.MouseEvent.dwEventFlags == MOUSE_MOVED ? "moved " : "clicked")<<" X = "<< inputBuffer.Event.MouseEvent.dwMousePosition.X<<" Y = "<< iputBuffer.Event.MouseEvent.dwMousePosition.Y<<" "<<"button state "<<inputBuffer.Event.MouseEvent.dwButtonState< <"
    mouse state "<<inputBuffer.Event.MouseEvent.dwEventFlags
    <<endl;
    break;
    }//end switch
    // when we receive an esc down key, drop out of do loop
    }while (!(inputBuffer.EventType == KEY_EVENT&&
    inputBuffer.Event.KeyEvent.wVirtualKeyCode == VK_ESCAPE &&
    inputBuffer.Event.KeyEvent.bKeyDown));


    This should work.
    I got it out of a Microsoft consol examples that came with MVC++6.0

    Using this you can find the Key code and use and if statement for a specific button press such as up

    A lot of this stuff is really weird and uses a lot of structures
    It took me a long time to figure it out using all the help files and console function examples.

    Oh dont worry about the different data types such as DWORD and HANDLE. Just remember that they need to be CAPS.

  6. #6
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    Arrow keys return 2 chars; this will work: (SORRY ABOUT THE WEIRD FORMATTING)

    Code:
                        char input;
                        char arrow;
                        input = getch();
    		
    	if((int)input == -32)
    	{
    	arrow = getch();
    	switch(arrow)
    	{
    	case 'H':
    	{
    	}break;
    	
                        case 'P':
    	{
    	}break;
    	
                        case 'K':
    	{
    	}break;
    				
                        case 'M':
    	{
    	}break;
    	}
    
                        }
    Last edited by CodeMonkey; 03-16-2002 at 04:11 PM.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  7. #7
    Unregistered
    Guest
    If you use getch and still have to press enter, this means that your header files succ. Don't use thses header files and they don't such any more.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I need movement...
    By Finchie_88 in forum C++ Programming
    Replies: 1
    Last Post: 10-04-2004, 03:10 PM