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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
|
#include <iostream.h>
#include<conio.h>
#include <windows.h>
int mx=30;
int my=10;
int i, j;
char direction;
const char left_key='a', right_key='d';
char keypress;
char grid[24][79];
void gotoxy(short x, short y)
{
HANDLE hConsoleOutput;
COORD Cursor = {x,y};
hConsoleOutput = GetStdHandle (STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hConsoleOutput, Cursor);
}
void printgrid()
{
for (i=0; i<24; i++)
{
for (j=0; j<79; j++)
{
cout<<grid[i][j];
}
cout<<endl;
}
}
#define VMAX 3
#define HMAX 3
void printRobot(char robot[][3],int x, int y);
void fillIn(int x,int y);
int main()
{
int x,y;
// clock_t pause = 50;
char robot[VMAX][HMAX]={
{'.','o','.'},
{'-','¦','-'},
{'/',' ','\\'}
};
HANDLE hOut;
hOut = GetStdHandle(STD_OUTPUT_HANDLE);
//SetConsoleTextAttribute(hOut, BACKGROUND_BLUE|
//BACKGROUND_INTENSITY);
SetConsoleTextAttribute(hOut, BACKGROUND_GREEN|
BACKGROUND_INTENSITY);
//loadgrid();
printgrid();
gotoxy(mx,my);
printRobot(robot,x,y);
while (mx<70 && my<20 && my>0 && mx>0)
{
if (kbhit())
{
keypress=getch(); //keypress=(char)getchar()
if ((keypress == right_key) || (keypress == left_key))
direction = keypress;
if (direction == 'a')
{
gotoxy(mx,my);
cout<<" "<<flush;
mx=mx-1;
gotoxy(mx,my);
printRobot(robot,x,y);
fillIn(x,y);
gotoxy(mx,my);
}
if (direction == 'd')
{
gotoxy(mx,my);
cout<<" "<<flush;
mx=mx+1;
gotoxy(mx,my);
printRobot(robot,x,y);
fillIn(x,y);
gotoxy(mx,my);
}
direction = ' ';
}
}
// cout << "NOTE: MAKE SURE ALL ENTERED LETTERS ARE LOWER CASE!!!!!!!";
return 0;
}
void printRobot(char robot[][3],int x, int y) {
int r,c;
for(c=0,r=2;c<HMAX;c++)
cout<<robot[r][c]<<flush;
gotoxy(x,y-1);
for(c=0,r=1;c<HMAX;c++)
cout<<robot[r][c]<<flush;
gotoxy(x,y-2);
for(c=0,r=0;c<HMAX;c++)
cout<<robot[r][c]<<flush;
}
void fillIn(int x,int y) {
int i;
for(i=VMAX;i>-1;i--) {
gotoxy(x,y-i);
cout<<" "<<flush;
}
}
|