Simple Console Game Slight Error

The following code compiles fine, well at least on my compiler that I know of :P
I seem to have a small issue with moving the sprite. The only part that I cannot seem to quite figure out is why when I press the arrow keys the character does not move into the directed position until after the second press.

Basically if I start pressing right key he will go right, but when I change the direction to left, he will go right one more time and then he will go left.

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
#include <iostream> 
#include <Windows.h> 
#include <conio.h> 
 
void gotoXY(int, int); 
void removeCursor(void);

//Global scope variables 
int posX(5), posY(10);
int x, y;
char key;

int main(){
	removeCursor();
	
	std::cout<<"\tUse arrow keys: \x1a \x1b \x19 \x18";

	while(key != 'q'){
		key = getch();

		gotoXY(x, y);
		std::cout<<' ';

		gotoXY(posX,posY);
		std::cout<<((char)('\x01'));

		x = posX, y = posY;

		if(key == 0x4d){ posX++; } //Go Right - VK_RIGHT, 77, 0x4d
		if(key == 0x4b){ posX--; } //Go Left - VK_LEFT, 75, 0x4b
		if(key == 0x50){ posY++; } //Go Down - VK_DOWN, 80, 0x50
		if(key == 0x48){ posY--; } //Go Up - VK_UP, 72, 0x48
	}
	return 0;
}
 
void gotoXY(int x, int y){
	COORD c;
    c.X = x, c.Y = y;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
}

void removeCursor(void){
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
	CONSOLE_CURSOR_INFO cci;
	cci.dwSize = 1; 
	cci.bVisible = false; //false = invisible
	SetConsoleCursorInfo(handle, &cci);
}
@BLooDiEMuRDA
I changed your code a bit. Now, no lag..

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
#include <iostream> 
#include <Windows.h> 
#include <conio.h> 

void gotoXY(int, int); 
void removeCursor(void);

//Global scope variables 
//int posX(5), posY(10);
int x = 5, y=10; // Set where to start printing
int key; // Changed to an integer, instead of a char
#define ENTER 13
#define UP 72
#define LEFT 75
#define RIGHT 77
#define DOWN 80

int main()
{
 removeCursor();

 std::cout<<"\tUse arrow keys: \x1a \x1b \x19 \x18";
   gotoXY(x, y);
	 std::cout<<((char)('\x01'));
 do	
 {
	key = _getch();
	switch(key)
	{
	case RIGHT:
	 gotoXY(x, y);
	 std::cout<<' ';
	 x++;
	 gotoXY(x, y);
	 std::cout<<((char)('\x01'));
	 break;
	case LEFT:
	 gotoXY(x, y);
	 std::cout<<' ';
	 x--;
	 gotoXY(x, y);
	 std::cout<<((char)('\x01'));
	 break;
	case UP:
	 gotoXY(x, y);
	 std::cout<<' ';
	 y--;
	 gotoXY(x, y);
	 std::cout<<((char)('\x01'));
	 break;
	case DOWN:
	 gotoXY(x, y);
	 std::cout<<' ';
	 y++;
	 gotoXY(x, y);
	 std::cout<<((char)('\x01'));

	}
 }while(key != ENTER);
 return 0;
}

void gotoXY(int x, int y){
 COORD c;
 c.X = x, c.Y = y;
 SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
}

void removeCursor(void){
 HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
 CONSOLE_CURSOR_INFO cci;
 cci.dwSize = 1; 
 cci.bVisible = false; //false = invisible
 SetConsoleCursorInfo(handle, &cci);
}
@whitenite1
Wow, thank you very much, that certainly solved the issue. Looks neater too, although I have 1 question. What is the difference between getch() & _getch()
I noticed how you changed it by adding a simple underscore.

Also this may not really sound like a big deal which is not at all, in fact it probably is not even important in this matter lol.
I changed #define ENTER 13 to #define ESC 27
Just in case someone else wants to copy and make sure to change the while part of the do loop to
do{}while(key != ESC)
@BLooDiEMuRDA

the difference between getch() & _getch()
I noticed how you changed it by adding a simple underscore.


The reason I changed it, is because I get this warning from my compiler, otherwise.

Warning C4996: 'getch': The POSIX name for this item is deprecated. Instead, use ISO C++ conformant name: _getch. See online help for details.


And changing the #define to ESC, is all a matter of taste on which key you would prefer to use to end the program. I've use both.

You may also want to limit to distance your graphic character can travel on screen. Take going right, for instance.
1
2
3
4
5
6
7
8
9
10
case RIGHT:
	 if ( x+1 < 80) // So it doesn't go off screen. 
          {
           gotoXY(x, y);
	 std::cout<<' ';
	 x++;
	 gotoXY(x, y);
	 std::cout<<((char)('\x01'));
         }
	 break;
Oh ok, very helpful information especially for the collision part (:
Last edited on
Topic archived. No new replies allowed.