Arrow Keys?!?!

Pages: 12
I was wondering if there is a way to program with arrow in C++. I want to create a text based game where you push the arrow keys to move. How do I do this? Thanks in advance!
You could use the Windows function, so it would be.

1
2
3
4
5
6
#include <Windows.h>

if (GetAsyncKeyState(VK_UP))
{
	// Move up..
}
Thanks for the help!
Last edited on
I have used the method Callum5042 suggested, and I cannot get it to work. Here is my 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
#include "stdafx.h"
#include "iostream"
#include "string"
#include "targetver.h"
#include "windows.h"
#include "Windows.h"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int health=15;
int mana=15;
int partylimit=1;
string move;
string character="*";
string party;
string weapon;
cout<<"Welcome to Dungeon Explorers!\n";
cout<<"In Dungeon Explorers, you have a two basic objectives.\n";
cout<<"The first of those objectives is to survive.\n";
cout<<"The second is to escape the dimension you're in.\n";
cout<<"An evil witch sent you to this dimension, so you must escape and kill her.\n";
Sleep(4000);
system("CLS");
cout<<"You wake up to see that you are in a valley. You have only a dagger.\n";
cout<<"You soon see a small baby demon not far from you\n";
cout<<"You should attack it.\n";
Sleep(3000);
system("CLS");
cout<<"HOW TO PLAY:\n";
cout<<"* Use the arrow keys to move around the world.\n";
cout<<"* Press the space bar to attack.\n";
cout<<"* Press E to open your inventory\n";
cout<<"* Press Esc. to pause the game.\n";
if (GetAsyncKeyState(VK_UP))
{
cout<<character;
}
return 0;
}


What is wrong with my code? When I execute and push the up arrow, it just exits. Please help
1
Put cin.get(); before return 0;

It because once you press it, it goes on and carrys on with the code, and the last part is return 0; which is exiting the program.
I added cin.get(); before return 0; but it still doesn't work. Here is my 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
#include "stdafx.h"
#include "iostream"
#include "string"
#include "targetver.h"
#include "windows.h"
#include "Windows.h"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int health=15;
int mana=15;
int partylimit=1;
string move;
string character="*";
string party;
string weapon;
cout<<"Welcome to Dungeon Explorers!\n";
cout<<"In Dungeon Explorers, you have a two basic objectives.\n";
cout<<"The first of those objectives is to survive.\n";
cout<<"The second is to escape the dimension you're in.\n";
cout<<"An evil witch sent you to this dimension, so you must escape and kill her.\n";
Sleep(4000);
system("CLS");
cout<<"You wake up to see that you are in a valley. You have only a dagger.\n";
cout<<"You soon see a small baby demon not far from you\n";
cout<<"You should attack it.\n";
Sleep(3000);
system("CLS");
cout<<"HOW TO PLAY:\n";
cout<<"* Use the arrow keys to move around the world.\n";
cout<<"* Press the space bar to attack.\n";
cout<<"* Press E to open your inventory\n";
cout<<"* Press Esc. to pause the game.\n";
Sleep(3000);
system("CLS");
if (GetAsyncKeyState(VK_UP))
{
cout<<character;
}
cin.get();
return 0;
}


What is wrong now? Thanks for the help so far!
Not to sound rude, but your code is quite messy and hard to read.

But when I use your code it doesn't work, so I rewrote it quickly and it works for me.

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
#include <iostream>
#include <string>
#include <Windows.h>
#include <tchar.h>
using namespace std;

int main()
{
	string character="*";

	cout<<"Welcome to Dungeon Explorers!\n";
	cout<<"In Dungeon Explorers, you have a two basic objectives.\n";
	cout<<"The first of those objectives is to survive.\n";
	cout<<"The second is to escape the dimension you're in.\n";
	cout<<"An evil witch sent you to this dimension, so you must escape and kill her.\n";
	Sleep(4000);
	system("CLS");
	cout<<"You wake up to see that you are in a valley. You have only a dagger.\n";
	cout<<"You soon see a small baby demon not far from you\n";
	cout<<"You should attack it.\n";
	Sleep(3000);
	system("CLS");
	cout<<"HOW TO PLAY:\n";
	cout<<"* Use the arrow keys to move around the world.\n";
	cout<<"* Press the space bar to attack.\n";
	cout<<"* Press E to open your inventory\n";
	cout<<"* Press Esc. to pause the game.\n";

	// Game loop
	while (true)
	{
		if (GetAsyncKeyState(VK_UP))
		{
			cout << character;
		}
	}

	return 0;
}
After plugging in your code with my other functions libraries and variables, it doesn't run. Here are the errors:

1>------ Build started: Project: Dungeon Fighters, Configuration: Debug Win32 ------
1> Dungeon Fighters.cpp
1>c:\users\michael\documents\visual studio 2010\projects\dungeon fighters\dungeon fighters\dungeon fighters.cpp(1): warning C4627: '#include "iostream"': skipped when looking for precompiled header use
1> Add directive to 'StdAfx.h' or rebuild precompiled header
1>c:\users\michael\documents\visual studio 2010\projects\dungeon fighters\dungeon fighters\dungeon fighters.cpp(2): warning C4627: '#include "string"': skipped when looking for precompiled header use
1> Add directive to 'StdAfx.h' or rebuild precompiled header
1>c:\users\michael\documents\visual studio 2010\projects\dungeon fighters\dungeon fighters\dungeon fighters.cpp(3): warning C4627: '#include "Windows.h"': skipped when looking for precompiled header use
1> Add directive to 'StdAfx.h' or rebuild precompiled header
1>c:\users\michael\documents\visual studio 2010\projects\dungeon fighters\dungeon fighters\dungeon fighters.cpp(10): error C2065: 'string' : undeclared identifier
1>c:\users\michael\documents\visual studio 2010\projects\dungeon fighters\dungeon fighters\dungeon fighters.cpp(10): error C2146: syntax error : missing ';' before identifier 'character'
1>c:\users\michael\documents\visual studio 2010\projects\dungeon fighters\dungeon fighters\dungeon fighters.cpp(10): error C2065: 'character' : undeclared identifier
1>c:\users\michael\documents\visual studio 2010\projects\dungeon fighters\dungeon fighters\dungeon fighters.cpp(11): error C2065: 'string' : undeclared identifier
1>c:\users\michael\documents\visual studio 2010\projects\dungeon fighters\dungeon fighters\dungeon fighters.cpp(11): error C2146: syntax error : missing ';' before identifier 'weapon'
1>c:\users\michael\documents\visual studio 2010\projects\dungeon fighters\dungeon fighters\dungeon fighters.cpp(11): error C2065: 'weapon' : undeclared identifier
1>c:\users\michael\documents\visual studio 2010\projects\dungeon fighters\dungeon fighters\dungeon fighters.cpp(12): error C2065: 'string' : undeclared identifier
1>c:\users\michael\documents\visual studio 2010\projects\dungeon fighters\dungeon fighters\dungeon fighters.cpp(12): error C2146: syntax error : missing ';' before identifier 'party'
1>c:\users\michael\documents\visual studio 2010\projects\dungeon fighters\dungeon fighters\dungeon fighters.cpp(12): error C2065: 'party' : undeclared identifier
1>c:\users\michael\documents\visual studio 2010\projects\dungeon fighters\dungeon fighters\dungeon fighters.cpp(16): error C2065: 'cout' : undeclared identifier
1>c:\users\michael\documents\visual studio 2010\projects\dungeon fighters\dungeon fighters\dungeon fighters.cpp(17): error C2065: 'cout' : undeclared identifier
1>c:\users\michael\documents\visual studio 2010\projects\dungeon fighters\dungeon fighters\dungeon fighters.cpp(18): error C2065: 'cout' : undeclared identifier
1>c:\users\michael\documents\visual studio 2010\projects\dungeon fighters\dungeon fighters\dungeon fighters.cpp(19): error C2065: 'cout' : undeclared identifier
1>c:\users\michael\documents\visual studio 2010\projects\dungeon fighters\dungeon fighters\dungeon fighters.cpp(20): error C2065: 'cout' : undeclared identifier
1>c:\users\michael\documents\visual studio 2010\projects\dungeon fighters\dungeon fighters\dungeon fighters.cpp(21): error C3861: 'Sleep': identifier not found
1>c:\users\michael\documents\visual studio 2010\projects\dungeon fighters\dungeon fighters\dungeon fighters.cpp(22): error C3861: 'system': identifier not found
1>c:\users\michael\documents\visual studio 2010\projects\dungeon fighters\dungeon fighters\dungeon fighters.cpp(23): error C2065: 'cout' : undeclared identifier
1>c:\users\michael\documents\visual studio 2010\projects\dungeon fighters\dungeon fighters\dungeon fighters.cpp(24): error C2065: 'cout' : undeclared identifier
1>c:\users\michael\documents\visual studio 2010\projects\dungeon fighters\dungeon fighters\dungeon fighters.cpp(25): error C2065: 'cout' : undeclared identifier
1>c:\users\michael\documents\visual studio 2010\projects\dungeon fighters\dungeon fighters\dungeon fighters.cpp(26): error C3861: 'Sleep': identifier not found
1>c:\users\michael\documents\visual studio 2010\projects\dungeon fighters\dungeon fighters\dungeon fighters.cpp(27): error C3861: 'system': identifier not found
1>c:\users\michael\documents\visual studio 2010\projects\dungeon fighters\dungeon fighters\dungeon fighters.cpp(28): error C2065: 'cout' : undeclared identifier
1>c:\users\michael\documents\visual studio 2010\projects\dungeon fighters\dungeon fighters\dungeon fighters.cpp(29): error C2065: 'cout' : undeclared identifier
1>c:\users\michael\documents\visual studio 2010\projects\dungeon fighters\dungeon fighters\dungeon fighters.cpp(30): error C2065: 'cout' : undeclared identifier
1>c:\users\michael\documents\visual studio 2010\projects\dungeon fighters\dungeon fighters\dungeon fighters.cpp(31): error C2065: 'cout' : undeclared identifier
1>c:\users\michael\documents\visual studio 2010\projects\dungeon fighters\dungeon fighters\dungeon fighters.cpp(32): error C2065: 'cout' : undeclared identifier
1>c:\users\michael\documents\visual studio 2010\projects\dungeon fighters\dungeon fighters\dungeon fighters.cpp(37): error C2065: 'VK_UP' : undeclared identifier
1>c:\users\michael\documents\visual studio 2010\projects\dungeon fighters\dungeon fighters\dungeon fighters.cpp(37): error C3861: 'GetAsyncKeyState': identifier not found
1>c:\users\michael\documents\visual studio 2010\projects\dungeon fighters\dungeon fighters\dungeon fighters.cpp(39): error C2065: 'cout' : undeclared identifier
1>c:\users\michael\documents\visual studio 2010\projects\dungeon fighters\dungeon fighters\dungeon fighters.cpp(39): error C2065: 'character' : undeclared identifier
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


Does this have something to do with the compiler I use? Or it just the code? Thanks!
Your code is using
1
2
3
#include "iostream"
#include "string"
#include "Windows.h" 

when it should be instead
1
2
3
#include <iostream>
#include <string>
#include <Windows.h> 

Last edited on
Thanks for helping me with that! But now at the end my program when I push the up arrow, nothing happens. However, if I push the up arrow before that, it registers it. What is wrong with my code that makes this happen?
Post your code again, and which compiler are you using btw?
@cstarter2000

Here is the program, without using GetAsyncKeyState(). Hope it's 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
// testing.cpp : main project file.

#include "stdafx.h"
#include <iostream>
#include <string>
#include <conio.h> // For using _getch();
//#include <targetver>
#include <windows.h>

using namespace std;
using namespace System;

#define ESC 27
#define UP 72
#define LEFT 75
#define RIGHT 77
#define DOWN 80
#define SPACE 32

void drawmap(char maze[][10])
{
	for(int row=0;row<10;row++)
	{
		for(int col=0;col<10;col++)
		{
			cout << maze[col][row];
		}
		cout << endl;
	}
}

int main()
{
	char maze[10][10] = {'o'};
	int row,col;
	for(row=0;row<10;row++)
		for(col=0;col<10;col++)
			maze[col][row] = 'o';

	maze[5][5]='X';
	int health=15;
	int mana=15;
	int partylimit=1;
	string move;
	string character="*";
	string party;
	string weapon;
	cout<<"Welcome to Dungeon Explorers!\n";
	cout<<"In Dungeon Explorers, you have a two basic objectives.\n";
	cout<<"The first of those objectives is to survive.\n";
	cout<<"The second is to escape the dimension you're in.\n";
	cout<<"An evil witch sent you to this dimension, so you must escape and kill her.\n";
	Sleep(4000);
	system("CLS");
	cout<<"You wake up to see that you are in a valley. You have only a dagger.\n";
	cout<<"You soon see a small baby demon not far from you\n";
	cout<<"You should attack it.\n";
	Sleep(3000);
	system("CLS");
	cout<<"HOW TO PLAY:\n";
	cout<<"* Use the arrow keys to move around the world.\n";
	cout<<"* Press the space bar to attack.\n";
	cout<<"* Press I to open your inventory\n";
	cout<<"* Press Esc. to pause the game.\n";
	Sleep(3000);
	system("CLS");
	int x=5, y=5;
	drawmap(maze);
	for ( ;; )
	{
		int ch = 0;
		ch = _getch();
		switch(ch)
		{
		case EOF:
		case ESC:
			cout << "Pause program..." << endl;
			break;
		case SPACE:
			cout << "We attack..  ( we die.....)" << endl;
			break;
		case UP:
			if (y-1>=0)
			{
				system("CLS");
				maze[x][y]='o';
				y--;
				maze[x][y]='X';
				drawmap(maze);
				cout << "You're moving UP" << endl;
			}
			else
				cout << "Can't move in that direction!!" << endl;
			break;
		case LEFT:
			if (x-1>=0)
			{
				system("CLS");
				maze[x][y]='o';
				x--;
				maze[x][y]='X';
				drawmap(maze);
				cout << "You're moving LEFT" << endl;
			}
			else
				cout << "Can't move in that direction!!" << endl;
			break;
		case RIGHT:
			if (x+1<10)
			{
				system("CLS");
				maze[x][y]='o';
				x++;
				maze[x][y]='X';
				drawmap(maze);
				cout << "You're moving RIGHT" << endl;
			}
			else
				cout << "Can't move in that direction!!" << endl;
			break;
		case DOWN:
			if (y+1<10)
			{
				system("CLS");
				maze[x][y]='o';
				y++;
				maze[x][y]='X';
				drawmap(maze);
				cout << "You're moving DOWN" << endl;
			}
			else
				cout << "Can't move in that direction!!" << endl;
			break;
		case  73: // Uppercase I or use case 69: for Uppercase E 
		case 105: // Lowercase i or use case 101: for Lowercase e
			cout << "Open Inventory.." << endl;
			break;
		default: 
			if(isprint(ch))
			{
				cout << "Use arrow keys, ESC or I (for Inventory) only" << endl;
			}
		}

	}	
	return 0;
}
Last edited on
whitenite1, can you please explain to me what that code does? I want to be able to understand this code and use the concept later. I use Microsoft Visual C++ 2012 Express. Here's my 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
#include "stdafx.h"
#include <iostream>
#include <string>
#include <windows.h>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int health=15;
int mana=15;
int partylimit=1;
string move;
string character="*";
string party;
string weapon;
cout<<"Welcome to Dungeon Explorers!\n";
cout<<"In Dungeon Explorers, you have a two basic objectives.\n";
cout<<"The first of those objectives is to survive.\n";
cout<<"The second is to escape the dimension you're in.\n";
cout<<"An evil witch sent you to this dimension, so you must escape and kill her.\n";
Sleep(4000);
system("CLS");
cout<<"You wake up to see that you are in a valley. You have only a dagger.\n";
cout<<"You soon see a small baby demon not far from you\n";
cout<<"You should attack it.\n";
Sleep(3000);
system("CLS");
cout<<"HOW TO PLAY:\n";
cout<<"* Use the arrow keys to move around the world.\n";
cout<<"* Press the space bar to attack.\n";
cout<<"* Press E to open your inventory\n";
cout<<"* Press Esc. to pause the game.\n";
Sleep(3000);
system("CLS");
if (GetAsyncKeyState(VK_UP))
{
cout<<"Hello!\n";
}
cin.get();
return 0;
}


Please help!
@cstarter2000

char maze[10][10] = {'o'}; // Just creates a 10x10 grid for the 'X' to roam on
1
2
3
4
int row,col;
	for(row=0;row<10;row++)
		for(col=0;col<10;col++)
			maze[col][row] = 'o';// Fills the Grid with 'o''s so I can see the edges 

1
2
3
4
5
6
7
#define ESC 27
#define UP 72        // arrow key up
#define LEFT 75    // arrow key left
#define RIGHT 77 // etc.
#define DOWN 80
#define SPACE 32
// defines a name for key values 

1
2
for ( ;; )
	{ // empty for loop. Keeps running. Could also have used 'while (true); 

1
2
3
ch = _getch(); // waits for key press and assigns ch to it
		switch(ch) // switches to the result of the key
		{

1
2
3
4
5
6
7
8
9
10
11
12
13
14
case UP: // if ch == 27
			if (y-1>=0)// checks if subtracting 1 will cause character to leave grid
			{  // Do this if it doesn't
				system("CLS");
				maze[x][y]='o';// erases where your character was
				y--; decreases grid location
				maze[x][y]='X';  // marks the new grid location with your character
				drawmap(maze);// redraws the grid showing your location
				cout << "You're moving UP" << endl;  // Let you know you moved upward
			}
			else  // If you can't
				cout << "Can't move in that direction!!" << endl; // prints the message you can't move
			break;//ends case
// Same thing for other 3 directions 

1
2
3
4
5
6
7
8
9
10
11
case  73: // Uppercase I or use case 69: for Uppercase E 
		case 105: // Lowercase i or use case 101: for Lowercase e
  // Used letter I instead of E, since Invenory starts with I 
			cout << "Open Inventory.." << endl; // Letting you know the 'I' was pressed
// You add in here the Inventory code or a jump to a function ( Which is better )
			break;
		default: 
			if(isprint(ch))
			{
				cout << "Use arrow keys, ESC or I (for Inventory) only" << end
// Prints if something other than those keys were pressed 

Thanks! This seems like it will help!
The code whitenite1 gave me worked before, but now my compiler gives me an error whenever there is drawmap (maze). How can I fix this?
@cstarter2000

Without seeing the error code shown, or better yet, your new program code, it would be difficult to near impossible, to fix. Need a little help then, in those departments..
Sorry! Here's my code:

// testing.cpp : main project file.

#include "stdafx.h"
#include <iostream>
#include <string>
#include <conio.h>
#include "targetver.h"
#include <windows.h>
using namespace std;
#define ESC 27
#define UP 72
#define LEFT 75
#define RIGHT 77
#define DOWN 80
#define SPACE 32

void drawmap(char maze[][10])
{
for(int row=0;row<10;row++)
{
for(int col=0;col<10;col++)
{
cout << maze[col][row];
}
cout << endl;
}
}

int main()
{
char maze[40][40] = {'o'};
int row,col;
for(row=0;row<10;row++)
for(col=0;col<10;col++)
maze[col][row] = 'o';

maze[10][10]='X';
int health=15;
int mana=15;
int partylimit=1;
string move;
string character="*";
string party;
string weapon;
cout<<"Welcome to Dungeon Explorers!\n";
cout<<"In Dungeon Explorers, you have a two basic objectives.\n";
cout<<"The first of those objectives is to survive.\n";
cout<<"The second is to escape the dimension you're in.\n";
cout<<"An evil witch sent you to this dimension, so you must escape and kill her.\n";
Sleep(4000);
system("CLS");
cout<<"You wake up to see that you are in a valley. You have only a dagger.\n";
cout<<"You soon see a small baby demon not far from you\n";
cout<<"You should attack it.\n";
Sleep(3000);
system("CLS");
cout<<"HOW TO PLAY:\n";
cout<<"* Use the arrow keys to move around the world.\n";
cout<<"* Press the space bar to attack.\n";
cout<<"* Press I to open your inventory\n";
cout<<"* Press Esc. to pause the game.\n";
Sleep(3000);
system("CLS");
int x=5, y=5;
drawmap(maze);
for ( ;; )
{
int ch = 0;
ch = _getch();
switch(ch)
{
case EOF:
case ESC:
cout << "Pause program..." << endl;
break;
case SPACE:
cout << "We attack.. ( we die.....)" << endl;
break;
case UP:
if (y-1>=0)
{
system("CLS");
maze[x][y]='o';
y--;
maze[x][y]='X';
drawmap(maze);
cout << "You're moving UP" << endl;
}
else
cout << "Can't move in that direction!!" << endl;
break;
case LEFT:
if (x-1>=0)
{
system("CLS");
maze[x][y]='o';
x--;
maze[x][y]='X';
drawmap(maze);
cout << "You're moving LEFT" << endl;
}
else
cout << "Can't move in that direction!!" << endl;
break;
case RIGHT:
if (x+1<10)
{
system("CLS");
maze[x][y]='o';
x++;
maze[x][y]='X';
drawmap(maze);
cout << "You're moving RIGHT" << endl;
}
else
cout << "Can't move in that direction!!" << endl;
break;
case DOWN:
if (y+1<10)
{
system("CLS");
maze[x][y]='o';
y++;
maze[x][y]='X';
drawmap(maze);
cout << "You're moving DOWN" << endl;
}
else
cout << "Can't move in that direction!!" << endl;
break;
case 73: // Uppercase I or use case 69: for Uppercase E
case 105: // Lowercase i or use case 101: for Lowercase e
cout << "Open Inventory.." << endl;
break;
default:
if(isprint(ch))
{
cout << "Use arrow keys, ESC or I (for Inventory) only" << endl;
}
}

}
return 0;
}




The error appears at maze right after drawmap. I'm aware that this occurs several times. How come these errors occur?
@cstarter2000

You have a few problems.

1 - You create an array of 40x40, but you're trying to send it to the function as 10
1
2
3
4
5
6
7
8
9
10
11
void drawmap(char maze[][10]) // change 10 to 40
{
   for(int row=0;row<10;row++) // Need these also to be 40, to show whole map
   {
      for(int col=0;col<10;col++)
      {
          cout << maze[col][row];
      }
   cout << endl;
}
}


2 - You also are only assigning a 10x10 grid with 'o'
1
2
3
for(row=0;row<10;row++) // These 10's need to be 40's also
    for(col=0;col<10;col++)
       maze[col][row] = 'o';


3 - You start the user at maze[10][10], but make the starting position at 5x5
int x=5, y=5; // Should be int x=10, y=10;
Then you could have just coded

maze[x][y]='X'; right afterward, and remove it after the for loop in the beginning. To change up the game, you could also make x and y, to be random numbers from 0 to 40 each, and have the player start there.

4 - In order to compile it, I have to remove #include "targetver.h" . What is this?? My MS Visual C++ 2010, doesn't have it.
Last edited on
Thanks! And targetver.h is just a recommended library that VC++ suggests for me.
Pages: 12