#include <iostream>
#include <windows.h>
#include <conio.h>
usingnamespace std;
void GameLoop();
int main()
{
char key = ' ';
HANDLE hConsole;
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
cout << "Welcome to\n";
SetConsoleTextAttribute(hConsole, 9);
cout << "SHOOTER\n";
SetConsoleTextAttribute(hConsole, 7);
cout << "A C++ game developed by Justin\n";
cout << "Press any key to begin\n";
key = _getch();
if(_kbhit())
{
GameLoop();
}
}
void GameLoop()
{
int i;
char field[79][24];
field[0][12] = 1; // Player 1's avatar
field[79][12] = 2; // Player 2's avatar
bool bKeepGoing = true;
int iP1Health = 100;
int iP2Health = 100;
char key = ' ';
while(bKeepGoing)
{
i++;
if(i == 1250)
{
for(int j = 0; j < 79; j++)
{
cout << endl;
for(int k = 0; k < 24; k++)
{
cout << field[j][k];
}
}
i = 0;
}
while(_kbhit())
{
key = _getch();
cout << "You entered: " << key << endl;
if(key == 't')
bKeepGoing = false;
}
}
}
The problem with this code is that it should be updating quite often showing the field array, including the 2 ASCII characters. However, once I enter a letter in the main() function, the program simply ends. What am I doing wrong here?
I assumed key = _getch(); didn't wait for user input (like cin does), so that's why I used kbhit(), so it would only continue once a key had been pressed. I tried your suggestion, and I get the same problem as I did with the previous solution.