Help with movement

Hello guys,

I want to make a console movement charecter something like $ to move up down right left and I have no clue how to get started with the code of the movement like what do I need

I thought it will be simple like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
char charecter;
int x = 0;
int y = 0;
// that means center position
char key;

int main(){ 
   cout << "Enter a charecter (example $, &, #, @)" << endl;
   cin >> charecter;

   while(1) {
key=getch();
if(key == 'w') {
     // charecter will go up
}
if(key == 's') { 
     // charecter will go down
}
if(key == 'a') {
     // charecter will go left
}
if(key == 'd') {
     // charecter will go right
}


Thanks for the helpers


Using Turbo C++ is it? (assuming this because I saw key=getch() and modern compilers like VS that support <conio.h> have _ prefix for their functions).

If so then look in your compiler for the functions gotoxy(), wherex(), wherey(), clrscr(), kbhit() and getch(). They come under <conio.h> so simply go to the index and see the page for <conio.h>.

Those functions are awesome for console games.
It's the only good thing about having to use Turbo C++.

kbhit() can be used to verify whether the user has pressed any key.

while(kbhit())
getch();

can be used to clear keyboard buffer.

kbhit() is useful for when you want to make something like the retro snake game where the snake moves even when the user isn't pressing on a key.


By the way if you are using Turbo C++ at home then it is a really really bad compiler you should download a modern C++ compiler.

If you're using it at lab.. HIGH FIVE bro. Alas my next CS lab will be next year and after that year no more Turbo C++. Wasted one entire year of Turbo C++.. all I made was this stupid version of snake game with only one block and some other small games like guess the number and tictactoe.

But you go study now ;) no time for this stuff

Thank you for the help man :)
And btw I am using dev-C++ its pretty comfy to use :D
If it's Turbo C++ then not a lot of people might be able to help you but I will. I'll write code snippets for the movement and explain and all a bit later but for now I need to study because I have tests and am sure you have tests too.

Have you thought about anything else with the game? Maybe I can help a bit. I'm learning too.. we can learn together!

If it's not Turbo C++ then there's plenty of people here that will help you.


edit: Posted this the exact minute that you did :P!

( sorry didn't see your reply when I posted this. )

edited part:

Does dev-C++ have gotoxy() and etc? I think not.

What OS are you using? On windows you can set console cursor using <windows.h>
If you're not using windows then I think you will have to rely on some more logic or you will have to rely on some dependency, and I know nothing when it comes to C++ libraries.

Logic wise you will have to write your own function for displaying is what I thought, this seems complex though. Basically the idea is to use a 2D array and print the array for displaying output. And then we change the index values of the 2D array. But this seems COMPLEX and INEFFICIENT to say the least.

That is what I was thinking before I got to know about gotoxy() hopefully somebody can give a better approach.
Last edited on
Hey Grime,
I have tests as well and I its fine if it will take you time to reply to this topic
and I will be glad to learn together so far I learned from the internet about c++ and its very hard to learn it by my self.

I am using Windows and dev-C++ supports conio.h and gotoxy(); but I need to do some alot of researches about this

Last edited on
Okay and does the screen have a scrollbar?

I'll write a code snippet later on when I'm free just make sure you have these functions:
kbhit(), getch(), gotoxy(), wherex(), wherey(), clrscr()

Can you write system("cls); after including iostream in your compiler?
(just curious, we don't want to use that because it's very very very slow. But if we can't use clrscr() then we're stuck with system("cls"))

I'll write a code snippet later.

If you're using dev-c++ for school then it's okay, but if you're using it at home then you should opt for a more modern C++ compiler because dev-c++ is outdated. I'm sure you will find many results when you google about it. I use Microsoft Visual Studio 2017 but there are other good choices like code::blocks.
Last edited on
@OshriMakk

Here's a small program I wrote to move a character around a specified part of the screen. Program ends when you place the moving character onto the 'H' (Home)

Hope this helps.

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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// Using ARROW keys.cpp : Defines the entry point for the console application.

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

using namespace std;

void Draw(int, int, int, int, int, int);
void gotoXY(int, int);
void gotoXY(int, int, string);
void pause(unsigned int milliseconds);

HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
COORD CursorPosition;

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

int main()
{
	int x, y, i, ch;
	x = 20;
	y = 10;
	char yn = 'y';
	int wins = false;
	string rub(5, '\xB0');
	gotoXY(10, 6, "To move, use the arrow keys.");
	gotoXY(10, 7, "'Enter' to change '\x01' to '\x02'");
	Draw(1, 1, 1, 78, 23, 1);
	Draw(2, 15, 9, 15, 12, 1);
	gotoXY(22, 14, "H");
	do
	{


		if (x == 22 && y == 14)
		{
			gotoXY(19, 12, "Yippee!!");
			gotoXY(18, 13, "I'm HOME!!");
			for (i = 0; i<8; i++)
			{
				gotoXY(x, y, "\x02");
				pause(140);
				gotoXY(x, y, "\x01");
				pause(140);
			}
			wins = true;
		}
		gotoXY(10, 22);
		cout << "The '\x01' is at column " << x << " row " << y - 9 << " ";
		gotoXY(x, y, "\x01");

		ch = _getch();
		switch (ch)
		{
			//Movement by pressing the arrow keys
		case LEFT:
			{
				if (x - 1>15)
				{
					gotoXY(x, y, " ");
					x--;
				}
				else
				{
					gotoXY(x - 6, y, "Ouch!");
					pause(500);
					gotoXY(x - 6, y, "     ");
				}
				break;
			}
		case RIGHT:
			{
				if (x + 1< 29)
				{
					gotoXY(x, y, " ");
					x++;
				}
				else
				{
					gotoXY(x + 2, y, "Ouch!");
					pause(500);
					gotoXY(x + 2, y, "\xB0    ");
				}
				break;
			}
		case UP:
			{
				if (y - 1>9)
				{
					gotoXY(x, y, " ");
					y--;
				}
				else
				{
					gotoXY(x - 2, y - 2, "Ouch!");
					pause(500);
					gotoXY(x - 2, y - 2, "     ");
				}
				break;
			}
		case DOWN:
			{
				if (y + 1<20)
				{
					gotoXY(x, y, " ");
					y++;
				}
				else
				{
					gotoXY(x - 2, y + 2, "Ouch!");
					pause(500);
					if (x-15<3)
					{
						gotoXY(14, y + 2, "  ");
						cout << rub;
					}
					else
						gotoXY(x - 2, y + 2, rub);
				}
				break;
			}
		case ENTER:
			{
				gotoXY(x, y, "\x02");
				pause(800);
			}
			pause(100);
			break;
		}
	} while (!wins);
	gotoXY(10, 22);
	return 0;
}

void Draw(int style, int col, int row, int length, int height, int shadow)
{
	// col and row, is where you want the box top left corner to start
	// Draws a 1 or 2 line box 
	int a;
	style--;
	//style=b*6;
	char box[2][6];
	box[0][0] = '\xDA';
	box[0][1] = '\xBF';
	box[0][2] = '\xC0';
	box[0][3] = '\xD9';
	box[0][4] = '\xB3';
	box[0][5] = '\xC4';
	box[1][0] = '\xC9';
	box[1][1] = '\xBB';
	box[1][2] = '\xC8';
	box[1][3] = '\xBC';
	box[1][4] = '\xBA';
	box[1][5] = '\xCD';
	char tl, tr, bl, br, side, edge;
	tl = box[style][0];
	tr = box[style][1];
	bl = box[style][2];
	br = box[style][3];
	side = box[style][4];
	edge = box[style][5];

	string Line(length - 2, edge);
	string Shadow(length, '\xB0');
	gotoXY(col, row);
	cout << tl << Line << tr;
	for (a = 1; a <height - 1; a++)
	{
		gotoXY(col, row + a);
		cout << side;
		gotoXY(col + length - 1, row + a);
		cout << side;
		if (shadow)
			cout << "\xB0";
	}
	gotoXY(col, (height + row) - 1);
	cout << bl << Line << br;
	if (shadow)
	{
		cout << "\xB0";
		gotoXY(col + 1, row + height, Shadow);
	}
}

void gotoXY(int x, int y)
{
	CursorPosition.X = x;
	CursorPosition.Y = y;
	SetConsoleCursorPosition(console, CursorPosition);
}

void gotoXY(int x, int y, string text)
{

	CursorPosition.X = x;
	CursorPosition.Y = y;
	SetConsoleCursorPosition(console, CursorPosition);
	cout << text;
}

void pause(unsigned int milliseconds)
{
	time_t final = milliseconds + clock();
	while (milliseconds < final)
	{
		milliseconds = clock();
	}
}
@whitenite thanks for the example man.

@Grime I do have system("CLS") and those functions. I will download code::blocks but not visual studio man its kinda hard taking alot of memory
Topic archived. No new replies allowed.