I am making a game for a school project with gotoxy and getch and 2d character arrays. there is supposed to be a robot type figure that can move left and right by pressing 'a' and 'd'. so far i've been able to make the cursor go left or right on the grid but can't make a charactar appear at its place. my code,
#include <iostream.h>
#include<conio.h>
#include <windows.h>
// char moveR;
// char moveL;
int mx=30;
int my=10;
int i, j;
char direction;
const char left_key='a', right_key='d';
char keypress;
char a[24][79];
void gotoxy(short x, short y)
{
HANDLE hConsoleOutput;
COORD Cursor = {x,y};
hConsoleOutput = GetStdHandle (STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hConsoleOutput, Cursor);
}
/* void loadgrid()
{
for (i=0; i<24; i++)
{
for (j=0; j<79; j++)
{
a[i][j]='.';
}
}
}*/
void printgrid()
{
for (i=0; i<24; i++)
{
for (j=0; j<79; j++)
{
cout<<a[i][j];
}
cout<<endl;
}
}
void main()
{
/* HANDLE hOut;
hOut = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hOut, BACKGROUND_BLUE);*/
//loadgrid();
printgrid();
while (mx>0 && my>0)
{
if (kbhit())
{
keypress=getch(); //keypress=(char)getchar()
if ((keypress == right_key) || (keypress == left_key))
direction = keypress;
if (direction == 'a')
{
gotoxy(mx,my);
cout<<"@";
mx=mx-1;
gotoxy(mx,my);
cout<<"@";
gotoxy(mx,my);
}
if (direction == 'd')
{
gotoxy(mx,my);
cout<<" ";
mx=mx+1;
gotoxy(mx,my);
cout<<"@";
gotoxy(mx,my);
}
direction = ' ';
}
}
// cout << "NOTE: MAKE SURE ALL ENTERED LETTERS ARE LOWER CASE!!!!!!!";
}