Hi im brand new to coding and have been practicing by making this text based RPG. And my problem is that when it gives you the option to go to shop or to fight i want you to go back to the option menu after you go to shop or you fight but can i just have it go back to that code or will i have to code the same menu.
Are you familiar with loops (for, while, do-while)?
The solution would be, to put your entire game into a loop. Then, when you exit out of the shop, you automatically go back to the menu.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
do{
while(menu item wasn't selected){
display menu
}
if(shop was selected){
while(blabla){
display shop
}
}
etc.
}while(game isn't over)
The other thing that helps to organize your program is to isolate code to functions.
For example, lines 27-51 could go into a function such as "do_shop". do_shop handles everything relating to your shop. do_shop may in turn call other functions to handle actions that occur within the shop. When do_shop exits, it returns to your main loop which will prompt for another top-level action. This makes the code in your main loop much cleaner.
where would i put that code?
The code Gaius gave you should go in your main function. Everything eles should be in functions that are called. i.e. The details of your shop and actions taken in your shop don't belong in main. Same thing for fighting.
Also, you don't have to write cout<<""<<endl;. You can make it much simpler by either typing cout << endl; or cout << "\n"; or even cout << "\n\n\n\n"; and cout << endl << endl << endl << endl;
If you want to start with games... don't use the console. Graphics are not as hard as you might think. Often times it is actually much harder to use the console to make a game than it would be to just throw some images on the screen.
If you want to start with games... don't use the console.
I'll second this. I spent a year working on a text RPG in the console, and while it turned out pretty cool, there's a certain cool-ceiling you hit pretty quickly when developing for the console. I soon after got a job where I have to deal with a graphical interface, and it wasn't all that difficult to comprehend. If only I had known about it earlier!
#include <iostream>
usingnamespace std;
void shop()
{
cout<<"Entered Shop, but nothing implemented..."<<endl;
}
void fight()
{
cout<<"Entered Fight, but nothing implemented..."<<endl;
}
void credits()
{
cout<<"Entered Credit, but nothing implemented..."<<endl;
}
int main()
{
bool play = true;
int choice;
do//<---Game loop:
{
cout<<"Choose what you would like to do: \n.1- Go, to shop\n.2- FIGHT!!\n.3- Credits \n.4- Exit"<<endl;
cin>>choice;
cin.clear();
cin.ignore(255,'\n');
switch(choice)
{
case 1:
shop();
break;
case 2:
fight();
break;
case 3:
credits();
break;
case 4:
play=false;//<--- A game usually needs an exit condition.
break;
default:
cout<<"Invalid choice!"<<endl;
break;
}
}while(play);//<---Game break condition
return 0;
}
Loops will let you repeat data untill you're satisfied.
You can use functions to jump between data; after a function is done, it will return you to where the function had been call.
You should not be too concern with graphics, when developing on console, it is not inheritable; the logic and algorithms are though.
With the assumption that you want to learn C++ and not produce graphics for the console:
You can learn C++ just as easily with SFML as you can with STL.
The only difference is SFML is way more fun because you get to make something vibrant and interactive instead of just boring old text on a black screen.
More fun = you stay interested longer = you learn more quickly.
I stand by this: If you're interested in game dev... working in the console is a complete waste of time. Don't do it.
I stand by this: If you're interested in game dev... working in the console is a complete waste of time. Don't do it.
Personally, I don't think it is, but I view everything as a learning experience even console applications.
Posted your quote to a game development site with a poll on if game developers agreed with you or disagreed. So far, 64 people have voted...4 agree with you and 60 don't think it is a waste at all.