I am using an array for my background, if you compile you will see what I mean... I want it to put the char back where it was when the smiley moves forward, but when I compile its putting it one step down, or not at all . If anyone can tell me whats wrong I would be grateful.. Here is the code:
Code:
#include <iostream>
#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int ch;
static int get_code ( void )
{
int ch = getch();
if ( ch == 0 || ch == 224 )
ch = 256 + getch();
return ch;
}
enum
{
KEY_ESC = 27,
ARROW_UP = 256 + 72,
ARROW_DOWN = 256 + 80,
ARROW_LEFT = 256 + 75,
ARROW_RIGHT = 256 + 77
};
using namespace std;
int x=0,y=0;
void gotoxy(int x, int y)
{
HANDLE hConsoleOutput;
hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
COORD dwCursorPosition;
dwCursorPosition.X = x;
dwCursorPosition.Y = y;
SetConsoleCursorPosition(hConsoleOutput,dwCursorPosition);
}
char map[100][100] = {"+++++++++++++++++++++++++++++++++++++++++++++++++++++++++",
"---------------------------------------------------------",
"fffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
"rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr"
} ;
int main ()
{
gotoxy(0,0);
for (int j = 0; j < 4; j++)
{
cout << map[j] << endl;
}
while ( ( ch = get_code() ) != KEY_ESC ) {
switch ( ch ) {
case ARROW_UP:
gotoxy(x,y--);
putchar(1);
gotoxy(x,y++);
putchar(map[y][x]);
gotoxy(x,y--);
break;
case ARROW_DOWN:
gotoxy(x,y++);
putchar(1);
gotoxy(x,y--);
putchar(map[y][x]);
gotoxy(x,y++);
break;
case ARROW_LEFT:
gotoxy(x--,y);
putchar(1);
gotoxy(x++,y);
putchar(map[y][x]);
gotoxy(x--,y);
break;
case ARROW_RIGHT:
gotoxy(x++,y);
putchar(1);
gotoxy(x--,y);
putchar(map[y][x]);
gotoxy(x++,y);
break;
}
}
return 0;
}