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;
}