1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
|
// Conditional While loop.cpp : main project file.
#include <iostream>
#include <conio.h>
#include <windows.h>
using namespace std;
void gotoxy(int x, int y)
{
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
int main()
{
int x = 11;
int y = 11;
char move=' ';
bool home = false;
cout << "To move, use the keys w,a,s and d. 'q' to quit";
gotoxy(x,y);
cout << "@";
do
{
gotoxy(5,3);
cout << "x = " << x << " y = " << y << " ";
move = _getch();
move = tolower(move); // Making sure to check lower letters, even if CAPS lock on
//system("CLS"); Best not to use 'System' commands.
switch (move)
{
case 'w':
gotoxy(x,y);
cout << " ";
y-- ;
gotoxy(x,y);
cout << "@";
break;
case 's':
gotoxy(x,y);
cout << " ";
y++ ;
gotoxy(x,y);
cout << "@";
break;
case 'a':
gotoxy(x,y);
cout << " ";
x-- ;
gotoxy(x,y);
cout << "@";
break;
case 'd':
gotoxy(x,y);
cout << " ";
x++ ;
gotoxy(x,y);
cout << "@";
break;
case 'q':
gotoxy(x,y);
cout << "*";
gotoxy(x-5,y+1); //Position text under asterisk
cout <<"I'm quitting";
home = true;
//x=9;
//y=9;
break;
}
if (x==9 && y==9)
home = true;
} while(!home);
gotoxy(5,3);
cout << "x = " << x << " y = " << y << " ";
if(x==9 && y==9)
{
gotoxy(9,9);
cout << "*";
gotoxy(5,10);
cout <<"I'm home!!";
}
gotoxy(25,23);// position text at bottom of screen
return 0;
}
| |