Hi!
I'm trying to make a text-based game and I'd like to use an arrow pointing at my current option during the start-up menu as well as during the game, for example:
-> 1. Singleplayer
2. Multiplayer
and when I click up or down using the arrow keypads the arrow will skip to the next option. Following the previous example, if I press down it will look like this:
1. Singleplayer
-> 2. Multiplayer
Using system ("cls") to clear screen and then re-printing everything is not an acceptable solution because it's too slow.
I'm using Visual Studio C++ with windows 7.
Thanks in advance! :)
You can make a text-based game, but you should not do it in the console. Learn a graphics library like SFML or SDL - you can still make the game text-based, but I and others strong recommend leaving the console behind at this point.
I agree with L B. There are ways to do what you want... but learning how to do them would be a waste of time IMO, because it'd be just as easy to learn how to use a graphic lib.
In order to move the arrow to Multiplayer you'll have to use gotoxy statement in which you can move your cursor anywhere on the console screen.
Print Space where you want character to become removed. Space will replace arrow.
//Press Right and Left key and see what Happens?
#include <iomanip>
#include <windows.h>
usingnamespace std;
int main()
{
int i = 0;
while(1) //infinite loop!
{
if(GetAsyncKeyState(VK_RIGHT))
{
i++;
cout<<setw(i)<<"---------->" ;
}
if(GetAsyncKeyState(VK_LEFT))
{
i--;
cout<<setw(i)<<"<----------";
}
Sleep(10);
system("cls");
}
return 0;
}
Though I've used syste("cls"), you can edit this code. Try to figure out how helpful space is as an alternate to system("cls")
Thank you for such fast replies, really appreciated.
I'm afraid I'll have to do it in the console window because it's a task from my professor and one of the requirements is to create the game in the console.
Solution understood, thread closed.
Thanks again :)