Moving letter to next line in real time

Pages: 12
So i have my code, and i was wondering how i would move my letter A down to the next line in real time when i press W. Here is the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include<iostream>
#include<windows.h>
#include<fstream>
#include <string>

using namespace std;

int main()
{
    //int up;
    int down;
    //int left;
    //int right;
    string character = "A";

    cout << "\n";
    cout << character;

    while(true){
        down=GetAsyncKeyState(0x41);
        if(down){
        cout << "\n" << character;
        }
    }

    return 0;
}
As far as I know, it's not possible to erase text from a console windows in real time.
I'm not sure about that... What about system("cls")??? And before anyone jumps down my throat for suggesting bad programming, just want to clarify that it will do the job, won't it? It'll clear the screen, and then he can print the new position of A..

(I've tried this, and yes, it works, I used a similar thing for my Game of Life Program...)
Yes, it doesnt actually have to move A to a new line, it just has to give the illusion that it does when the player presses W (S later). so what would the code look like?
The best way to do this would be to use a library like NCurses.
Stupebrett: A link or a tutorial please? Even I would like to know about this...

Ch1156: I don't know what you use to identify the pressing of the keys, [GetAsyncKeyState(0x41)] and how you use it, so I'm just making a small change in your code: You can modify it according to your own needs...

1
2
3
4
5
6
7
8
9
10
11
12
13
    cout << "\n";
    cout << character;

    while(true){
        down=GetAsyncKeyState(0x41);
        if(down){
        system("cls");
        cout << "\n \n" << character;
        }
    }

    return 0;
}


Though if you're making some kind of a game of some sorts, may I suggest you use a map of some sort...? Like a 2D array... I'm assuming you'll be wanting similar functionality for the other keys as well, so basically you can shift your character a row up, down, or a column left or right depending on the key pressed... Of course, if you're trying something else, don't mind me... :)
Thank you caprico, and yes i am trying to make a game :D i have been playing with my code and i got it working, but it doesnt appear that the character is moving down, i know it is because when i comment out system("cls") the A moves down but if i leave CLS in then it moves one space down and then just looks as if its blinking. But then again there is no background or anything. Can someone show me how to make a background? I tried making one but it messed up, the character either appeared next to the lines or on top or in front. just making a simple hallway would be fine, also you said something about Arrays to make a map, can you give me a good tutorial on Arrays i need to learn those. Also here is my newly modified code:

Oh and by the way 0x53 is S on the keyboard, so when you press S then it will move down.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include<iostream>
#include<windows.h>
#include<fstream>
#include <string>

using namespace std;

void chctr();
void clear();

//Global
string character = "A";
//End of Global

int main()
{
    //int up;
    int down;
    //int left;
    //int right;

    cout << character;

    while(true){
        down=GetAsyncKeyState(0x53);
        if(down){
        clear();
        chctr();
        }
    }

    return 0;
}

void chctr()
{
    Sleep(200);
    cout << "\n";
    cout << character;
}

void clear()
{
    system("cls");
}
Last edited on
Oooh... I think I understand the blinking... See, your clear screen function is in the while loop, right? So it keeps clearing the screen, and then printing your character, again and again and......
As for understanding arrays, I'd suggest this very site..
http://www.cplusplus.com/doc/tutorial/arrays/

If you're making a game in the console, I don't think there's a way to make your character appear over something... See, you could have like a printmap function, which analyses the key pressed, and accordingly prints the next instance of the map.. Like a frame-by-frame game..
Your map would be the array... Basically, it would be like a grid, in which your character would move, one cell at a time... That's my idea of a console game...
Yeah its in the while loop, it doesnt blink unless i press the S key which moves the player. Yeah thats all i wanted to do is just make a simple corridor or something for now and i'll add stuff later :P Thanks :D
I need some help with something, im trying to make the character go left but it gives me an error, here is the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
#include <windows.h>
#include <fstream>
#include <string>

using namespace std;

void chctrdwn();
void chctrrght();
void clear();

//Global
string character = "A";
//End of Global

int main()
{
    //int up; //W is up
    int down; //S is down.
    //int left; //A is left
    int right; //D is right

    cout << character;

    while(true){
        down=GetAsyncKeyState(0x53);
        if(down){
        clear();
        chctrrght();
        }

        right=GetAsyncKeyState(0x57);
        if(right){
        clear();

        }
    }

    return 0;
}

void chctr()
{
    Sleep(200);
    cout << "\n";
    cout << character;
}

void chctrrght();
{
    Sleep(200);
    cout << " " << character;
}

void clear()
{
    system("cls");
}


And here is the Error:

C:\Users\Chay Hawk\Desktop\Text Game\main.cpp|50|error: expected unqualified-id before '{' token|
||=== Build finished: 1 errors, 0 warnings ===|
dude...... its easy!!

just remove the ";" semi-colon from line number 49!!

THATS IT!!!!...... ;)
Ok thanks :D i feel like a dumbass :P

Now i have a new proble, the character can go right and down but if i go right and press down he goes back to the first line and goes down, he doesnt go down from where he is at. This is how it looks.

(diagram 1)

aaaaaaaaaaaa
a
a
a
aaaaaaaaaa
a
a
aaaaaa

It should look like this:

(diagram 2)

1
2
3
4
5
6
7
8
aaaaaaaaaaaaaa
             a
             a
             a
             aaaaaaaaaaaa
                        a
                        a
                        a

Also note i commented out both of the the clear(); functions in the while loop to see what was going on exactly.

So my question is how do i make the character do what i want him to do in the diagram 2?

Here is the updated source:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
#include <windows.h>
#include <fstream>
#include <string>

using namespace std;

void chctrdwn();
void chctrrght();
void clear();

//Global
string character = "A";
//End of Global

int main()
{
    //int up; //W is up
    int down; //S is down.
    //int left; //A is left
    int right; //D is right

    cout << character;

    while(true){
        down=GetAsyncKeyState(0x53);
        if(down){
        //clear();
        chctrdwn();
        }

        right=GetAsyncKeyState(0x44);
        if(right){
        //clear();
        chctrrght();
        }
    }

    return 0;
}

void chctrdwn()
{
    Sleep(200);
    cout << "\n";
    cout << character;
}

void chctrrght()
{
    Sleep(200);
    cout << " " << character;
}

void clear()
{
    system("cls");
}
Last edited on
Best option... Use a grid map like I suggested... I'll post a snippet of code as to how it would look like a lil while later... :)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//#include stuff over here...

void mapreset(char **map)        //Sets all grid cells to a blank.
{
	int i, j;
	
	for (i=0; i<80; i++)
		for (j=0; j<80; j++)
			map[i][j]=" ";
}

void printmap(char **map)    //Accepts a 2D array. Dunno d exact syntax for accepting arrays
{			 //since I had used classes..Though I'm pretty sure this will work.
	int i, j;
	
	for (i=0; i<80; i++)
		for (j=0; j<80; j++)
			cout<<map[i][j];

}

int main()
{
	map[80][80];      //Square map of size 80x80.. You can experiment according to your screen size..
	
	mapreset(map);
	
	//Accept the initial position of the character here if you want..
	//I'm just gonna expicitly set it somewhere in the middle..

	map[40][40]="a";

	//Here, your algorithm comes into play..
	//If user presses down, just set map[i+1][j]="a"; (In this case, map[41][40]="a";
	//Similarly, for other keys, do the same..
	//Keep the printmap function in the while loop, where you're accepting the key..
	//Somewhat like this:

	 while(true)
	{
        	down=GetAsyncKeyState(0x53);
       		if(down)
		{
       			chctrdwn();
			printmap(map);
      		}
	}

	//Your chrctrdwn fnctn can simply accept the map, and the current position of your character
	//(You might need another function for the current position thing)
	//and then accordingly print the character below it.. 
	//PS: I'd suggest using classes for this kind of a game.. Makes it easier to handle..

	return 0;
}


Hope this helps.. :)
Last edited on
A link or a tutorial please?

Here you go: http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/

OkCaprico, i got an error, here is the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <iostream>
#include <windows.h>
#include <fstream>
#include <string>

using namespace std;

void chctrdwn();
void chctrrght();
void clear();

void mapreset(char **map)        //Sets all grid cells to a blank.
{
	int i, j;

	for (i=0; i<80; i++)
		for (j=0; j<80; j++)
			map[i][j]=" ";
}

void printmap(char **map)    //Accepts a 2D array. Dunno d exact syntax for accepting arrays
{			 //since I had used classes..Though I'm pretty sure this will work.
	int i, j;

	for (i=0; i<80; i++)
		for (j=0; j<80; j++)
			cout<<map[i][j];

}


//Global
string character = "A";
//End of Global

int main()
{
    map[80][80];
	mapreset(map);
	map[40][40]="a";


    //int up; //W is up
    int down; //S is down.
    //int left; //A is left
    int right; //D is right

    cout << character;

    while(true){
        down=GetAsyncKeyState(0x53);
        if(down){
        //clear();
        chctrdwn();
        printmap(map);

        }

        right=GetAsyncKeyState(0x44);
        if(right){
        //clear();
        chctrrght();
        }
    }

    return 0;
}

void chctrdwn()
{
    Sleep(150);
    cout << "\n";
    cout << character;
}

void chctrrght()
{
    Sleep(150);
    cout << "" << character;
}

void clear()
{
    system("cls");
}


Here is the error:

C:\Users\Chay Hawk\Desktop\fstream test\main.cpp||In function 'void mapreset(char**)':|
C:\Users\Chay Hawk\Desktop\fstream test\main.cpp|18|error: invalid conversion from 'const char*' to 'char'|
C:\Users\Chay Hawk\Desktop\fstream test\main.cpp||In function 'int main()':|
C:\Users\Chay Hawk\Desktop\fstream test\main.cpp|38|error: 'map' was not declared in this scope|
||=== Build finished: 2 errors, 0 warnings ===|
How silly of me... Line 38: Declare it as a character array as so:

char map[80][80]

Like I said, I'm not sure about passing 2D arrays to a function... Try changing **map to *map, map[80][80], or map[][]... Either of those should work.. If not, let me know, and I'll look into it deeper, and help you out.. :)

Also, change your chrctrdwn function... According to the 2D map way, it should change the element directly below the current position of the character, to change into that character... Like I have mentioned in the comments...
Im still getting errors. I dont have a clue on what im doing, i tried reading your comments but they dont help me, im a visual learner. I cant see snippets of code and then plug it in i have to see it working before i can understand it.

Here is the code again, I tried making some changes you saifd but gave me twice as many errors:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <iostream>
#include <windows.h>
#include <fstream>
#include <string>

using namespace std;

void chctrdwn();
void chctrrght();
void clear();

void mapreset(char **map)        //Sets all grid cells to a blank.
{
	int i, j;

	for (i=0; i<80; i++)
		for (j=0; j<80; j++)
            map[i][j]=" ";
}

void printmap(char **map)    //Accepts a 2D array. Dunno d exact syntax for accepting arrays
{			 //since I had used classes..Though I'm pretty sure this will work.
	int i, j;

	for (i=0; i<80; i++)
		for (j=0; j<80; j++)
			cout << map[i][j];
}


//Global
string character = "A";
//End of Global

int main()
{
    char map[80][80];
	mapreset(map);
	map[40][40]="a";


    //int up; //W is up
    int down; //S is down.
    //int left; //A is left
    int right; //D is right

    cout << character;

    while(true){
        down=GetAsyncKeyState(0x53);
        if(down){
        //clear();
        chctrdwn();
        printmap(map);

        }

        right=GetAsyncKeyState(0x44);
        if(right){
        //clear();
        chctrrght();
        }
    }

    return 0;
}

void chctrdwn()
{
    Sleep(150);
    cout << "\n";
    cout << character;
}

void chctrrght()
{
    Sleep(150);
    cout << "" << character;
}

void clear()
{
    system("cls");
}
Hmm... This is a pickle... Okay, tell you what, I'll try writing the code from scratch, if it works, I'll PM you... Okay? Till then wait around, I'm sure someone will be able to help you out better...
ok :)
Pages: 12