Input issue

Hi guys,

Working on a little stub program for a console rpg. However, I am new to the #include <Windows.h> header and it's causing me some issues. Here's 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149

#include <iostream>
#include <Windows.h>
#include <cstdlib>
#include <ctime>
#include <conio.h>
#include <string>

using namespace std;


char world[20][20]={'0','0','0','0','0','0','0','0',' ','0','0','0','0','0','0','0','0','0',' ','0',
					'0',' ',' ',' ','0','0','0',' ',' ',' ','0','0','0',' ','0','0','0','0',' ','0',
					'0',' ',' ','0','0','0','0',' ','0',' ',' ','0','0',' ',' ','0','0','0',' ','0',
					'0',' ',' ','0','0','0','0',' ','0','0',' ','0','0',' ',' ','0','0','0',' ','0',
					'0',' ',' ','0','0','0','0',' ','0','0',' ','0',' ',' ',' ',' ','0','0',' ','0',
					'0','0',' ',' ',' ','0',' ',' ','0','0',' ',' ',' ',' ',' ',' ','0','0',' ','0',
					'0','0',' ',' ',' ','0',' ','0','0','0','0','0',' ',' ',' ',' ','0','0',' ','0',
					'0','0',' ',' ',' ',' ',' ',' ',' ','0','0','0',' ',' ',' ',' ',' ','0',' ','0',
					'0','0','0','0','0','0','0','0',' ','0','0',' ',' ',' ','0',' ',' ','0',' ','0',
					'0','0','0','0','0','0','0','0',' ','0','0',' ',' ','0','0','0',' ','0',' ','0',
					'0','0','0','0','0','0','0','0',' ',' ',' ',' ',' ','0','0','0',' ',' ',' ','0',
					'0',' ',' ',' ','0','0','0',' ',' ','0',' ',' ',' ','0','0','0','0',' ',' ','0',
					'0',' ','0','0','0','0','0',' ','0','0',' ',' ',' ',' ','0','0','0',' ',' ','0',
					'0',' ','0','0','0','0','0',' ','0','0','0',' ',' ',' ','0','0','0','0',' ','0',
					'0',' ',' ','0','0','0','0',' ','0','0','0','0',' ','0','0','0',' ',' ',' ','0',
					'0','0',' ',' ',' ','0',' ',' ','0','0','0','0',' ','0','0','0',' ','0',' ','0',
					'0','0',' ',' ',' ','0',' ','0','0','0','0','0',' ','0','0','0',' ','0',' ','0',
					'0','0',' ',' ',' ',' ',' ',' ',' ','0','0','0',' ','0','0','0',' ','0',' ','0',
					'0','0','0','0','0','0','0','0',' ','0','0','0',' ',' ',' ',' ',' ','0',' ','0',
					'0','0','0','0','0','0','0','0',' ','0','0','0','0','0','0','0','0','0',' ','0',

					};


void print_world(char w[20][20],const int number, int& the_x, int& the_y);
void clear_screen();
bool check_collision(char w[][20], int the_size, int the_x, int the_y);

int main(){

	char dummy;
	int x=1, y=1;
	bool collision;

	string direction;
	do{
			
		clear_screen();

		if(GetAsyncKeyState(VK_RIGHT)){
			x++;
			collision=check_collision(world,20,x,y);
			if(collision)
				x--;
			direction="west";

		}
		if(GetAsyncKeyState(VK_LEFT)){
			x--;
			collision=check_collision(world,20,x,y);
			if(collision)
				x++;
			direction="east";
		}
		if(GetAsyncKeyState(VK_UP)){
			y--;
			collision=check_collision(world,20,x,y);
			if(collision)
				y++;

			direction="north";
		}
		if(GetAsyncKeyState(VK_DOWN)){
			y++;
			collision=check_collision(world,20,x,y);
			if(collision)
				y--;
			direction="south";
		}


		print_world(world,20,x,y);
		
		cout<<direction<<endl;//problem here

		

		

		Sleep(100);


	}while(true);

	cin>>dummy;


	return 0;
}

void print_world(char w[20][20],const int number, int& the_x, int& the_y){

	for(int i=0;i<number;i++){

		for(int j=0;j<number;j++){
			
		
			if(the_x==j && the_y==i)
				cout<<'x';

			else if(w[i][j]=='0')
				cout<<static_cast<char>(178);

			else 
				cout<<world[i][j];	

		}
		
		cout<<endl;
	
	}
}
bool check_collision(char w[][20], int the_size, int the_x, int the_y){

	for(int i=0;i<the_size;i++){
	
		for(int j=0;j<the_size;j++){
			
			if(w[the_y][the_x]=='0')
				return true;
			else
				return false;
		}
	
	}
}
void clear_screen(){//need windows header file for this

	HANDLE handle_out;
	COORD position;

	handle_out=GetStdHandle(STD_OUTPUT_HANDLE);//std output handle is the monitor

	position.X=0;
	position.Y=0;

	SetConsoleCursorPosition(handle_out,position);//sets cursor in the specified position
}



The issue is simple (albeit not for me). When I enter a number key, it displays a direction at the bottom of the screen. However, next loop, the old direction is not destroyed, rather written over. Does anyone know how to solve this problem?

Thanks,

Mike
@toomanystars

Changed it up just a bit. If no collision is detected with the direction you're trying to go, THEN it's allowed and x or y is adjusted. Otherwise, direction will equal " WALL ". setcursor() just turns off the blinking cursor on the screen. Hope this was helpful..

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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Console RPG.cpp : main project file.
#include <iostream>
#include <Windows.h>
#include <cstdlib>
#include <ctime>
#include <conio.h>
#include <string>

using namespace std;

#define UP 72
#define LEFT 75
#define RIGHT 77
#define DOWN 80

char world[20][20]={
	{'0','0','0','0','0','0','0','0',' ','0','0','0','0','0','0','0','0','0',' ','0'},
	{'0',' ',' ',' ','0','0','0',' ',' ',' ','0','0','0',' ','0','0','0','0',' ','0'},
	{'0',' ',' ','0','0','0','0',' ','0',' ',' ','0','0',' ',' ','0','0','0',' ','0'},
	{'0',' ',' ','0','0','0','0',' ','0','0',' ','0','0',' ',' ','0','0','0',' ','0'},
	{'0',' ',' ','0','0','0','0',' ','0','0',' ','0',' ',' ',' ',' ','0','0',' ','0'},
	{'0','0',' ',' ',' ','0',' ',' ','0','0',' ',' ',' ',' ',' ',' ','0','0',' ','0'},
	{'0','0',' ',' ',' ','0',' ','0','0','0','0','0',' ',' ',' ',' ','0','0',' ','0'},
	{'0','0',' ',' ',' ',' ',' ',' ',' ','0','0','0',' ',' ',' ',' ',' ','0',' ','0'},
	{'0','0','0','0','0','0','0','0',' ','0','0',' ',' ',' ','0',' ',' ','0',' ','0'},
	{'0','0','0','0','0','0','0','0',' ','0','0',' ',' ','0','0','0',' ','0',' ','0'},
	{'0','0','0','0','0','0','0','0',' ',' ',' ',' ',' ','0','0','0',' ',' ',' ','0'},
	{'0',' ',' ',' ','0','0','0',' ',' ','0',' ',' ',' ','0','0','0','0',' ',' ','0'},
	{'0',' ','0','0','0','0','0',' ','0','0',' ',' ',' ',' ','0','0','0',' ',' ','0'},
	{'0',' ','0','0','0','0','0',' ','0','0','0',' ',' ',' ','0','0','0','0',' ','0'},
	{'0',' ',' ','0','0','0','0',' ','0','0','0','0',' ','0','0','0',' ',' ',' ','0'},
	{'0','0',' ',' ',' ','0',' ',' ','0','0','0','0',' ','0','0','0',' ','0',' ','0'},
	{'0','0',' ',' ',' ','0',' ','0','0','0','0','0',' ','0','0','0',' ','0',' ','0'},
	{'0','0',' ',' ',' ',' ',' ',' ',' ','0','0','0',' ','0','0','0',' ','0',' ','0'},
	{'0','0','0','0','0','0','0','0',' ','0','0','0',' ',' ',' ',' ',' ','0',' ','0'},
	{'0','0','0','0','0','0','0','0',' ','0','0','0','0','0','0','0','0','0',' ','0'}
};

void print_world(char w[20][20],const int number, int& the_x, int& the_y);
void clear_screen();
bool check_collision(char w[][20], int the_size, int dir1, int dir2, int the_x, int the_y);
// dir1 -1 for up, 1 for down, otherwise 0
// dir2 -1 for left, 1 for right, otherwise 0
// If adding or subtracting the numbers, does nor cause a collision, then x or y is added or subtracted

void setcursor(bool, DWORD);

int main()
{
	
	setcursor(0, 0);
	char dummy = ' ';
	int x=1, y=1;
	bool collision;
	print_world(world,20,x,y);
	string direction;
	for (;;)
	{
		clear_screen();
		int ch = _getch();
		direction = "     ";
		switch(ch)
		{
		case RIGHT:
			{
				collision=check_collision(world,20,0,1,x,y);
				if(!collision)
				{
					x++;
					direction="East";
				}
				break;
			}
		case LEFT:
			{
				collision=check_collision(world,20,0,-1,x,y);
				if(!collision)
				{
					x--;
					direction="West";
				}
				break;
			}
		case UP:
			{
				collision=check_collision(world,20,-1,0,x,y);
				if(!collision)
				{
					y--;
					direction="North";
				}
				break;
			}
		case DOWN:
			{
				collision=check_collision(world,20,1,0,x,y);
				if(!collision)
				{
					y++;
					direction="South";
				}
				break;
			}
		}

		print_world(world,20,x,y);
		if(collision)
			direction = "WALL ";

		cout << direction << endl;

		Sleep(100);

	};

	cin>>dummy;
	return 0;
}

void print_world(char w[][20],const int number, int& the_x, int& the_y)
{
	for(int i=0;i<number;i++)
	{
		for(int j=0;j<number;j++)
		{
			if(the_x==j && the_y==i)
				cout<<'x';

			else if (w[i][j]=='0')
				cout << '\xB2';

			else 
				cout<<world[i][j];	

		}

		cout<<endl;

	}
}

bool check_collision(char w[][20], int the_size, int dir1, int dir2,int the_x, int the_y)
{
	if(w[the_y+dir1][the_x+dir2]=='0')
		return true;
	else
		return false;
}

void clear_screen()
{
	HANDLE handle_out;
	COORD position;

	handle_out=GetStdHandle(STD_OUTPUT_HANDLE);//std output handle is the monitor

	position.X=0;
	position.Y=0;

	SetConsoleCursorPosition(handle_out,position);//sets cursor in the specified position
}

void setcursor(bool visible, DWORD size)
{
	HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
	if(size == 0)
	{
		size = 20;	// default cursor size Changing to numbers from 1 to 20, decreases cursor width
	}
	CONSOLE_CURSOR_INFO lpCursor;	
	lpCursor.bVisible = visible;
	lpCursor.dwSize = size;
	SetConsoleCursorInfo(console,&lpCursor);
}
Last edited on
Thanks whitenite1, it works splendidly after I initialize "collision". Sorry for the belated reply!


Mike
Topic archived. No new replies allowed.