This is fix. If we use the other windows console routines we can create a "PrintAt" function which can place the graphic where we want it.
CODE
/*First Visual (using ASCII characters) Program*/
/*Include Standard Input Output Stream Capabilities As Well As CONIO.H, STDLIB,H, and WINDOWS.H*/
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<windows.h>
void clrscr();
void gotoxy(int x, int y);
void PrintAt(int x, int y, char *CharBuffer, int len);
/*Start Main Function*/
int main(void)
{
/*Set Up Some Variables Note: Not All Variables Are Used*/
char key; //which key is down
char player[] = "<-o->"; //visual player
char bullet[] =".";
short playerx = 20; //players x coordinate
short playery = 20; //players y coordinate
short endprog = 0; //variable for ending the program
short clearScreen = 50;
double pauseTimer; //For Slowing Down The Program
short bulletx[50]; //Bullets X and Y
short bullety[50];
int n = 0;
short currentBulletNum = 1;
long checkForBullets;
int NeedRedraw; //Used to ensure we only redraw if we need to...
// this will minimize flicker.
//Too Set All The Bullet X and Y Values To 0
while(n < 51)
{
bulletx[n] = 0;
bullety[n] = 0;
n = n + 1;
}
/*Start Main Loop*/
while(endprog == 0)
{
/* Slow Down The Program*/
pauseTimer = 0;
while(pauseTimer < 5000000)
{
pauseTimer = pauseTimer + 1;
}
NeedRedraw = 0;
if(kbhit()) //if a key is down ...
{
key = getch(); //check which key it is ...
if(key == 'a') //if its the 'a' key ...
{
playerx = playerx - 1; //move the player one to the left
NeedRedraw = 1;
}
else if(key == 'd') //if its the 'd' key ...
{
playerx = playerx + 1; //move the player one to the right
NeedRedraw = 1;
}
else if(key == '0') //End The Program
{
endprog = 1;
}
else if(key == ' ') //Create A Bullet And ...
{
//Set Its X and Y Values
bulletx[currentBulletNum] = playerx + 2;
bullety[currentBulletNum] = playery - 1;
currentBulletNum = currentBulletNum + 1;
//If The Current Bullet Number Gets Over 50 Set It To One So We Can Reuse That Bullet
if(currentBulletNum > 50)
{
currentBulletNum = 1;
}
}
else if(key == ';') //Interesting Little Thing
{
PrintAt(0, 0, "YOU SUCK", 8);
//Pause The Program So You Can Read 'YOU SUCK'
pauseTimer = 0;
while(pauseTimer < 100000000)
{
pauseTimer = pauseTimer + 1;
}
}
}
// if (NeedRedraw)
// {
clrscr();
PrintAt(playerx, playery, player, 6);
checkForBullets = 1;
while(checkForBullets < 51)
{
if((bulletx[checkForBullets] > 0) && (bullety[checkForBullets] >0))
{
PrintAt(bulletx[checkForBullets], bullety[checkForBullets], bullet, 1);
}
checkForBullets = checkForBullets + 1;
}
// }
//Move All The Bullets Up One
checkForBullets = 1;
while(checkForBullets < 51)
{
if (bullety[checkForBullets]>0)
{
bullety[checkForBullets] = bullety[checkForBullets] - 1;
// NeedRedraw=1;
}
checkForBullets = checkForBullets + 1;
}
}
clrscr();
return;
}
/*A Function For Clearing The Screen*/
void clrscr()
{
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
COORD coord = {0, 0};
DWORD count;
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(hStdOut, &csbi);
FillConsoleOutputCharacter(hStdOut, ' ', csbi.dwSize.X * csbi.dwSize.Y, coord, &count);
//note that you can use this function to
//position the cursor in the console.
SetConsoleCursorPosition(hStdOut, coord);
return;
}
//This function will set the cursor to (x, y).
void gotoxy(int x, int y)
{
COORD coord = {x, y};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
return;
}
//This function will print CharBuffer at the location (x, y)
void PrintAt(int x, int y, char *CharBuffer, int len)
{
COORD coord = {x, y};
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hStdOut, coord);
WriteConsole(hStdOut, CharBuffer, len, NULL, NULL);
return;
}
[EDIT] *** There is an error in the code above which causes an access violation, here is the corrected function:
CODE
//This function will print CharBuffer at the location (x, y)
void PrintAt(int x, int y, char *CharBuffer, int len)
{
unsigned long count; needed for WriteConsole...
COORD coord = {x, y};
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hStdOut, coord);
WriteConsole(hStdOut, CharBuffer, len, &count, NULL);
return;
}
This post has been edited by NickDMax: 28 Feb, 2007 - 07:31 PM