Hello. I'm working on what I consider to be my first "real" attempt at making a game. Of course, its a simple console app using ascii characters. So far I have managed to print the character to the screen implement movement. However, I can't seem to figure out how to print an array to the screen and have it remain there constantly. I have tried a bunch of differnt loops and placing the function to print the array all over the place, but it always results in the cursor jumping around the screen, causing horrible flickering. Heres my work so far.
Code:
#include <iostream>
#include <conio.h> // kbhit() and getch()
#include <windows.h>
using namespace std;
const int X =100; // max width of maze
const int Y =40; // max height of maze
char ascii = 0x01;
char maze[X][Y];
int x=10, y=10; //starting position of player
//not my code - don't remeber where I found it
void gotoxy(short x, short y) {
HANDLE hConsoleOutput;
COORD Cursor_Pos = {x, y};
hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hConsoleOutput, Cursor_Pos);
}
//ditto
void clrscr(void) {
CONSOLE_SCREEN_BUFFER_INFO csbi;
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
COORD coord = {0, 0};
DWORD count;
GetConsoleScreenBufferInfo(hStdOut, &csbi);
FillConsoleOutputCharacter(hStdOut, ' ', csbi.dwSize.X * csbi.dwSize.Y, coord, &count);
SetConsoleCursorPosition(hStdOut, coord);
}
void fillmaze()
{
for(int i=0; i<X; i++)
{
for(int j=0; j<Y; j++)
{
//arrayname[x] = 1st dimenstion, "xxxxxx" = elements of 2nd dimenstion
//right now these are just arbitrary elements. soon to be turned into a map/maze
strcpy(maze[0],"|234568910");
strcpy(maze[1],"|bccvbnmop");
strcpy(maze[2],"|wertuiqwe");
strcpy(maze[3],"|;;;;;;;;;");
strcpy(maze[4],"|---------");
strcpy(maze[5],"|fffffffff");
}
}
}
void printmaze()
{
for(int i=0; i<X; i++)
{
for(int j=0; j<Y; j++)
{
cout << maze[i][j];
}
cout<<"\n";
}
}
int setCursor(int x, int y)
{
//sets the position of the cursor
// could be used instead of gotoxy
COORD init = {x,y};
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hStdOut, init);
}
void clear() //what is going Onnn!
{
clrscr();
printmaze();
//gotoxy(x,y);
//cout << ascii;
}
void move()
{
//setCursor(x,y);
//gotoxy(x,y); // causes the cursor to stay with ascii
if (kbhit())
{
// while (x != 0 || y != 0)
//{
char input;
input = getch();
switch (input)
{
case 'w':
//clear();
clrscr();
gotoxy(x,y-=1);
//printmaze();
cout << ascii;
break;
case 'a':
clrscr();
gotoxy(x-=1,y);
cout << ascii;
break;
case 's':
clrscr();
gotoxy(x,y+=1);
cout << ascii;
break;
case 'd':
clrscr();
gotoxy(x+=1,y);
cout << ascii;
break;
case 'e':
exit(0);
break;
}
//}
}
}
int main()
{
fillmaze(); // load the maze
//printmaze(); // print the maze
gotoxy(x,y); //place the cursor at initial position of the character
cout << ascii; // prints character to screen
gotoxy(0,0);// place the cursor back to 0,0 because it gets moved to the [x][] location after the array is printed
int infinite = 0;
while (infinite != 1)
{
// printmaze();
move();
// gotoxy(0,0);
}
return 0;
}