I am building a game similar to the tron game. I want to use the arrow keys to control the movement of the player. I do not have all the code completed but attached what I have so far.

#include<iostream>
#include<string>
#include<conio.h>
#include<Windows.h>
#include<cstdlib>
#include<ctime>

using namespace std;

//Declarations
bool gameOver=false;
const int ROW=23;
const int COL=80;
int playerRow;
int playerCol;
int computerRow;
int computerCol;
char board[ROW][COL];
char key;
string compMove;
#define UP 72
#define LEFT 75
#define RIGHT 77
#define DOWN 80



void instructions();
void gameMap();
char playerMove(char input);
int computerMove();
void updateBoard();
bool crashTest();

int main()
{
    //Welcome and instruction screen
    instructions();

    //clears screen
    system("cls");

    //sets player and computers starting position
    board[10][20]='X';
    board[10][60]='*';

    //Draws the game board
    gameMap();

    while(!gameOver){

        if(_kbhit())key=_getch();{
    }
        playerMove(key);
        computerMove();
        gameOver=crashTest();
        updateBoard();
    }

        system("pause");
        return 0;
}

//instructions on how to play the game
void instructions()
{
    cout <<"\t\t\t ***Welcome to the world of Tron***";
    cout <<"\n\n The object of this game is to get the computer to run into your bikes trail.";
    cout <<"\n If you run into the computers trail, your own trail, or the outer bounderies,   you lose.";
    cout <<"\n To steer your bike use the four arrow keys.\n\n";

    system ("pause");

    return;
}

//Builds the game board
void gameMap()
{
    for (int i=0; i<23; i++)
    {
        for (int j=0; j<80; j++)
        {
            board[0][j]='@';
            board[22][j]='@';
            board[0][i]='@';
            board[79][i]='@';

            if (board[i][j]=='X')
            {
                board[i][j]='X';
            }

            else if (board[i][j] == '*')
            {
                board[i][j]='*';
            }

            else if (board[i][j]=='@')
            {
                board[i][j]='@';
            }

            else board[i][j]=' ';
            cout <<board[i][j];
        }
    }

    return;
}

char playerMove(char key)
{
    playerRow=10;
    playerCol=20;

    key=_getch();
    board[playerRow][playerCol];

    switch(key)
    {
        //move player up
        case UP :playerRow++;playerCol;

        break;

        //move player down
        case DOWN :playerRow--;playerCol;

        break;

        //move player left
        case LEFT : playerRow; playerCol--;

        break;

        //move player right
        case RIGHT : playerRow; playerCol++;

        break;

        return(key);
    }


}

int computerMove()
{
    //generates a random computer move
    return 0;
}

bool crashTest()
{
    //tests to see if the player or computer crashes
    return 0;
}

void updateBoard()
{
    //updates the computer and players position on the board
    board[playerRow][playerCol]='X';
    board[computerRow][computerCol]='*';
    cout <<board[playerRow][playerCol]<<board[computerRow][computerCol];
    return;
}

if _getch() returns either 0 or 224 then a special key such as Function keys, arrow keys, KeyUp,KeyDown, etc was hit. In tha case call _getch() again to find out what key was hit. Here is a complete list of codes. Because Function keys, arrow keys, etc return the same codes as normal keys one way to differentiate between them is to make the codes returned by special keys negative or just add 255 to them. Note: if you add 255 to the value you can not use data type char to hold them because the greatest value that can be stored in char is 127, or 255 in unsigned char.

Here is an example program.

#include <conio.h>
#include <iostream>
using namespace std;

int getkey()
{
    int x = _getch();
    if( x == 0 || x == 224)
        x = _getch() + 255;
    return x;

}

int main()
{
    int x = getkey();
    cout << x << '\n';
}

you can even try getch() instead of _getch()

#include <conio.h>
#include <iostream>
using namespace std;
int getkey()
{
    int x = getch() ; // detects which key is being pressed
    return x ;  // returns the ASCII value of the key being pressed
}

int main()
{
    int x = getkey();
    cout << x << '\n';
}

you can even try getch() instead of _getch()

Depends on which compiler you use. Microsoft compilers use _getch() while others might use getch() without the underscore. This is just one reason why conio.h is considered non-standard because its contents are not consistent from one compiler to the next.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.