Hello I have a tron game that I have created for class. I can't quite figure out how to do the player trails. I tried to do something with if statements but it does not do anything to the game. Also I am not sure how to post the code so I am just going to copy and paste sorry if I do it wrong.
void setWindow(); // Function creates the right size window.
void instructions(); // Holds the instructions for the game.
void display(); // Creates the starting game board.
void userInput(); // Gets the users keyboard hit and moves the bike.
void computer(); // Gets a random number to decide where the AI is moving.
void update(); // Updates the game board.
bool collisionTest(); // Tests to see if any bikes collide.
void result(); // Shows the ending text of the winner.
void trail();
HANDLE wHnd;
HANDLE rHnd;
bool gameWon = false; // Starts the game not being won, so that the while loop will begin.
int computerX = 18; // AI starting position.
int computerY = 60;
int playerX = 18; // players starting position.
int playerY = 20;
int compTrailX;
int compTrailY;
const int y = 80; // The game board dimensions.
const int x = 23;
char gameBoard[x] [y]; // The array that is the game board.
int _tmain(int argc, _TCHAR* argv[])
{
setWindow(); // Displays the correct window.
instructions(); // Brings up the instructions and then clears for the game board.
display(); // Displays the game board.
while (!gameWon) // If there is no collisions the game will go on.
{
userInput(); // Gets user input and moves each time it updates.
computer(); // Randomly gets AI's move each time it updates.
update(); // Updates the gameboard with correct moves.
gameWon = collisionTest(); // Sees if there is a collision from the bool function collisionTest().
Sleep(50); // Slows the loop down so the computer doesnt just rush into a wall.
}
result(); // When the game is over and loop stops it will display who won.
SetConsoleTitle(TEXT("My Tron Game")); // Sets a title for the window.
SMALL_RECT windowSize = {0,0,80,23}; // makes the correct window size for the game board.
SetConsoleWindowInfo(wHnd, TRUE, &windowSize);
COORD bufferSize = {80,23};
SetConsoleScreenBufferSize(wHnd, bufferSize);
}
void instructions()
{
cout << "Welcome to Tron. The computer is ready to battle you" << endl;
cout << "To move your bike use I for up, K for down, L for right and J for left" << endl;
cout << "Do you have what it takes? Type go when you are ready." << endl;
system("pause");
}
void display()
{
system("cls");
for (int i = 0; i < x; i++)
{
for (int j = 0; j < y; j++)
{
gameBoard[i] [j] = ' '; // Makes all of the spots a blank.
gameBoard[computerX] [computerY] = '*'; // Makes the starting spot for the AI.
gameBoard[playerX] [playerY] = 'X'; // Makes the starting spot for the player.
cout << gameBoard[i] [j]; // Displays the game board.
}
}
}
void userInput()
{
if (kbhit())
{
switch (getch()) // When the keyboard is hit it gets the character.
{
case 'i': // if i is pressed then it will move the player upwards.
playerX = playerX - 1;
break;
case 'k': // if k is pressed then it will move the player downwards.
playerX = playerX + 1;
break;
case 'l': // if l is pressed then it will move the player right.
playerY = playerY + 1;
break;
case 'j': // if j is pressed then it will move the player left.
playerY = playerY - 1;
break;
}
}
}
void computer()
{
int move;
srand ( time(NULL) );
move = rand() % 4 + 1; // Selects a random number 1 - 4.
if (move == 1) // If the number is 1 it will move the AI upwards.
{
computerX = computerX - 1;
}
else if (move == 2) // If the number is 2 it will move the AI downwards.
{
computerX = computerX + 1;
}
else if (move == 3) // If the number is 3 it will move the AI right.
{
computerY = computerY + 1;
}
else if (move == 4) // If the number is 4 it will move the AI left.
{
computerY = computerY - 1;
}
}
bool collisionTest()
{
if (computerX == 0 || computerX == 22 || computerY == 0 || computerY == 79 || playerX == 0 || playerX == 22 || playerY == 0 || playerY == 79)
{
// If any players hit the wall then the game is over.
return true;
}
else if(computerX == playerX && computerY == playerY)
{
// If the players hit each other then the game is over.
return true;
}
else
{
// If none of those happen then the game isnt over yet.
return false;
}
}
void update()
{
system("cls");
for (int i = 0; i < x; i++)
{
for (int j = 0; j < y; j++)
{
if (gameBoard[i] [j] = 'X') // If there is an X there then leave it an X. For the trail.
{
gameBoard[i] [j] = 'X';
}
else if (gameBoard[i] [j] = '*')
{
gameBoard[i] [j] = '*';
}
else
{
gameBoard[i] [j] = ' ';
}
gameBoard[i] [j] = ' '; // Makes all of the spots a blank.
gameBoard[computerX] [computerY] = '*'; // Makes the starting spot for the AI.
gameBoard[playerX] [playerY] = 'X'; // Makes the starting spot for the player.
cout << gameBoard[i] [j]; // Displays the game board.
}
}
}
void result()
{
system("cls");
if (computerX == 0 || computerX == 22 || computerY == 0 || computerY == 79 || gameBoard[computerX] [computerY] == 'X')
{
// If the AI hits the wall then the player wins.
cout << "Computer Loses! You win!" << endl;
}
else if (playerX == 0 || playerX == 22 || playerY == 0 || playerY == 79 || gameBoard[playerX] [playerY] == '*')
{
// If the player hits the wall then the computer wins.
cout << "You lose! Computer wins!" << endl;
}
system("pause");
}