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!
#include "stdafx.h"
#include "iostream"
#include "string"
#include "targetver.h"
#include "windows.h"
#include "Windows.h"
usingnamespace 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
#include "stdafx.h"
#include "iostream"
#include "string"
#include "targetver.h"
#include "windows.h"
#include "Windows.h"
usingnamespace 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;
}
#include <iostream>
#include <string>
#include <Windows.h>
#include <tchar.h>
usingnamespace 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;
}
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?
// testing.cpp : main project file.
#include "stdafx.h"
#include <iostream>
#include <string>
#include <conio.h> // For using _getch();
//#include <targetver>
#include <windows.h>
usingnamespace std;
usingnamespace 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;
}
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:
#include "stdafx.h"
#include <iostream>
#include <string>
#include <windows.h>
usingnamespace 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;
}
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
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..
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?
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.