Making ascii characters move aound the screen

Started by
2 comments, last by popcorn 17 years, 11 months ago
I am just learning C++ programming using several online tutorials for basic learning. (www.cplusplus.com, www.programmingtutorials.com, etc.) I have been searching around for any good tutorials that deal in any way with little programs for making ascii characters move around on the screen. Does anyone know of any good online tutorials for this?
Advertisement
You can't do it with IOstream, and you can't do it with C's IO functions. You need a console API. Here is the MSDN reference to the windows one:

linky

Since I cant find the tutorial that I used so I post up some old code where you be able extract the imformation from.

This is only VERY basic and you can far more in the console then what is shown here (eg Back buffering) but hopefully it get you started.

/*----------------------------------------------// |   Date: 28/03/2003							| | Author: Yaustar								| |  Title: Fireman Test							|//----------------------------------------------*/


#include <iostream>
#include <conio.h>
#include <windows.h>

using namespace std;

/*----------------------------------------------*/

void gotoxy(int X, char Z){ /*Used to print a character anywhere on the
screen*/

COORD coord;
coord.X = X;
coord.Y = 20;

HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hConsole, coord);

cout << Z;
}

/*----------------------------------------------*/

int main(void){

int x = 0, currentLetter = 0;
char sequence[] = {'r', 'q', 'w', 'e', 'r', 'q'}, input = 'p';

while(1){ /*While loops does not stop*/

Sleep(100); /*Pause for a 1/2 of a second*/

if(kbhit()){ /*If a key is pressed*/
input = tolower(getch());
gotoxy(x, ' ');
}

// gotoxy(x, '@'); /*Display the updated character position*/

// gotoxy(x, ' ');

if(input == sequence[currentLetter + 1]){
/*If the button press is in direction right*/
// gotoxy(x, ' '); /*Blank the last position of the character*/
x = x + 1;
gotoxy(x, '@');
currentLetter = currentLetter + 1;
if(currentLetter == 5){currentLetter = 1;}
}

if(input == sequence[currentLetter - 1]){
/*If the button press is in direction left*/
// gotoxy(x, ' '); /*Blank the last position of the character*/
x = x - 1;
gotoxy(x, '@');
currentLetter = currentLetter - 1;
if(currentLetter == 0){currentLetter = 4;}
}

if(input == 27){return 0;} /*If esc is pressed, quit program*/

}

return 0;

}

/*----------------------------------------------*/

Steven Yau
[Blog] [Portfolio]

http://www.adrianxw.dk/SoftwareSite has some good tutorials that can help you get started on using the win32 console.

yaustar - Maybe its just me but I fing your program quite confusing, you have a function called gotoxy but only set the x coordinate. Shouldn't it just be called gotox? Also what is char sequence[] = {'r', 'q', 'w', 'e', 'r', 'q'} meant to represent?

[Edited by - popcorn on April 28, 2006 9:26:20 PM]
How about them apples?

This topic is closed to new replies.

Advertisement