Why wont the esc key work?

Hi all,

i cant get the escape button to work on option 3 of code. can anyone help?

Here is the code.


#include <iostream>
#include <conio.h>
#include <windows.h>
#include <ctime>

#define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
#define KEY_UP (vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)

using namespace std;

void gotoxy(int, int); //declare a function called gotoxy
void clrscr(void); //declare clear screen function
void textcolor(int); //declare text colour function
void hline(int, int, int, int);
void vline(int, int, int, int);
void invader1(int, int, int); //draw invader(x,y,colour)
void invader2(int, int, int); //draw invader(x,y,colour)
char z = 219;

int a[80][25];
int b[80][25];
int x, y, c = 0, i;


void main(void) //main program start
{
int choice;

do
{
do
{ // menu
textcolor(15);
clrscr();
gotoxy(30, 18);
cout << "Press (1) Flying Ironman";
gotoxy(30, 20);
cout << "Press (2) Game of Life";
gotoxy(30, 22);
cout << "Press (3) Random blocks";
gotoxy(30, 24);
cout << "Press (4) Exit";
gotoxy(30, 26);
cout << "Enter 1, 2, 3, or 4 - ";
choice = _getch();
} while (choice != '1' && choice != '2' && choice != '3' && choice != '4');
//------------------------------------------------------------------1-----------------------------------------------------------
if (choice == '1') {
int x = 10, y = 10, vx = 1, vt = 0;


DWORD t1, t2;

t1 = GetTickCount();
do
{
t2 = GetTickCount();//check time
if (t2 - t1>100)
{
t1 = t2;


if (KEY_DOWN(VK_LEFT))
{
x--;
if (x<1) x = 1;
}
if (KEY_DOWN(VK_RIGHT))
{
x++;
if (x>49) x = 49;
}

if (KEY_DOWN(VK_UP))
{
y--;
if (y<1) y = 1;
}

if (KEY_DOWN(VK_DOWN))
{
y++;
if (y>23) y = 23;
}



//now draw
clrscr();
if (vt == 0)//vt is a flip flop switch to draw vader1 then vader2
{
invader1(x, y, 10);
vt = 1;
}
else
{
invader2(x, y, 12);
vt = 0;
}

}
} while (!KEY_DOWN(VK_ESCAPE));
}
//------------------------------------------------------------------2-----------------------------------------------------------
if (choice == '2') {
//randomise the random number generator
srand((unsigned)time(NULL));
//randomly set array a
for (x = 0; x<80; x++)
{
for (y = 0; y<25; y++)
{
a[x][y] = (int)((rand() / (float)RAND_MAX)*2.0);
//cout<<a[x][y]<<" ";
}
}
{
do {
clrscr();
//view array a
for (x = 0; x<80; x++)
{
for (y = 0; y<25; y++)
{
gotoxy(x, y);
if (a[x][y] == 1) cout << z;
textcolor(3);
}
}
//calculate next generation
for (x = 1; x<79; x++)
{
for (y = 1; y<24; y++)
{
c = 0;
if (a[x - 1][y - 1] == 1) c++;//top left
if (a[x][y - 1] == 1) c++;//top middle
if (a[x + 1][y - 1] == 1) c++;//top right
if (a[x - 1][y] == 1) c++;//middle left
if (a[x + 1][y] == 1) c++;//middle right
if (a[x - 1][y + 1] == 1) c++;//bottom left
if (a[x][y + 1] == 1) c++;//bottom middle
if (a[x + 1][y + 1] == 1) c++;//bottom right

//game of life rules
if (a[x][y] == 1 && c<2) b[x][y] = 0;
if (a[x][y] == 1 && (c == 2 || c == 3)) b[x][y] = 1;
if (a[x][y] == 1 && c>3) b[x][y] = 0;
if (a[x][y] == 0 && c == 3) b[x][y] = 1;
}
}

//copy b to a
for (x = 0; x<80; x++)
{
for (y = 0; y<25; y++)
{
a[x][y] = b[x][y];
}
}
Sleep(500);
if (KEY_DOWN(VK_SPACE))
{
for (x = 0; x<80; x++)
{
for (y = 0; y<25; y++)
{
a[x][y] = rand() % 2;
//cout<<a[x][y]<<" ";
}
}
}


} while (!KEY_DOWN(VK_ESCAPE));
}
}
//------------------------------------------------------------------3-----------------------------------------------------------
if (choice == '3')

{
//randomise the random number generator
srand((unsigned)time(NULL));
//randomly set array a
for (x = 0; x < 80; x++)
{
for (y = 0; y < 25; y++)
{
a[x][y] = (int)((rand() / (float)RAND_MAX)*2.0);
//cout<<a[x][y]<<" ";
}
}
{
for (i = 0; i < 1000; i++) {
clrscr();
//view array a
for (x = 0; x < 80; x++)
{
for (y = 0; y < 25; y++)
{
gotoxy(x, y);
if (a[x][y] == 1) cout << z;
textcolor(rand() % 16);

}
}
Sleep(500);
}
}
}

while (!KEY_DOWN(VK_ESCAPE));} //this ESC is the problem
while (choice != '4');
}//end of the main code


//put functions here


//Alter the code below at your peril!!!!
//It defines the textcolor,gotoxy and clrscr functions
void textcolor(int c)
{
HANDLE hConsole;
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, c);
}
void gotoxy(int x, int y) //define gotoxy function
{
static HANDLE hStdout = NULL;
COORD coord;

coord.X = x;
coord.Y = y;
if (!hStdout) { hStdout = GetStdHandle(STD_OUTPUT_HANDLE); }
SetConsoleCursorPosition(hStdout, coord);
}
void clrscr(void)
{
static HANDLE hStdout = NULL;
static CONSOLE_SCREEN_BUFFER_INFO csbi;
const COORD startCoords = { 0,0 };
DWORD dummy;

if (!hStdout)
{
hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(hStdout, &csbi);
}
FillConsoleOutputCharacter(hStdout, ' ', csbi.dwSize.X * csbi.dwSize.Y, startCoords, &dummy);
gotoxy(0, 0);
}


void hline(int x, int y, int l, int c)
{
int i;//notice i is moved from main() to hline()

textcolor(c);
for (i = 0; i<l; i++)
{
gotoxy(i + x, y);
cout << z;
}
}

void invader1(int x, int y, int c)

{ //grey outline
hline(x + 14, y, 3, 8);
hline(x + 12, y + 1, 7, 8);
hline(x + 11, y + 2, 9, 8);
hline(x + 10, y + 3, 11, 8);
hline(x + 10, y + 4, 11, 8);
hline(x + 9, y + 5, 13, 8);
hline(x + 9, y + 6, 13, 8);
hline(x + 9, y + 7, 13, 8);
hline(x + 10, y + 8, 11, 8);
hline(x + 8, y + 9, 15, 8);
hline(x + 5, y + 10, 22, 8);
hline(x, y + 11, 31, 8);
hline(x, y + 12, 31, 8);
hline(x + 1, y + 13, 29, 8);
hline(x + 9, y + 14, 14, 8);
hline(x + 10, y + 15, 11, 8);
hline(x + 10, y + 16, 11, 8);
hline(x + 11, y + 17, 9, 8);
hline(x + 11, y + 18, 9, 8);
hline(x + 11, y + 19, 9, 8);
hline(x + 10, y + 20, 11, 8);
hline(x + 9, y + 21, 13, 8);
hline(x + 9, y + 22, 6, 8);
hline(x + 8, y + 23, 7, 8);
hline(x + 6, y + 24, 8, 8);
hline(x + 5, y + 25, 9, 8);
hline(x + 5, y + 26, 9, 8);
hline(x + 16, y + 22, 6, 8);
hline(x + 17, y + 23, 6, 8);
hline(x + 17, y + 24, 8, 8);
hline(x + 17, y + 25, 9, 8);
hline(x + 17, y + 26, 9, 8);
}

void invader2(int x, int y, int c)

{ //grey outline
hline(x + 14, y, 3, 8);
hline(x + 12, y + 1, 7, 8);
hline(x + 11, y + 2, 9, 8);
hline(x + 10, y + 3, 11, 8);
hline(x + 10, y + 4, 11, 8);
hline(x + 9, y + 5, 13, 8);
hline(x + 9, y + 6, 13, 8);
hline(x + 9, y + 7, 13, 8);
hline(x + 10, y + 8, 11, 8);
hline(x + 8, y + 9, 15, 8);
hline(x + 5, y + 10, 22, 8);
hline(x, y + 11, 31, 8);
hline(x, y + 12, 31, 8);
hline(x + 1, y + 13, 29, 8);
hline(x + 9, y + 14, 14, 8);
hline(x + 10, y + 15, 11, 8);
hline(x + 10, y + 16, 11, 8);
hline(x + 11, y + 17, 9, 8);
hline(x + 11, y + 18, 9, 8);
hline(x + 11, y + 19, 9, 8);
hline(x + 10, y + 20, 11, 8);
hline(x + 9, y + 21, 13, 8);
hline(x + 9, y + 22, 6, 8);
hline(x + 8, y + 23, 7, 8);
hline(x + 6, y + 24, 8, 8);
hline(x + 5, y + 25, 9, 8);
hline(x + 5, y + 26, 9, 8);
hline(x + 16, y + 22, 6, 8);
hline(x + 17, y + 23, 6, 8);
hline(x + 17, y + 24, 8, 8);
hline(x + 17, y + 25, 9, 8);
hline(x + 17, y + 26, 9, 8);
}
Topic archived. No new replies allowed.