Need some help with text based rpg

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.

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
 cout<<"                           ||||||||||||||||||||      Gold:"<<Gold <<endl;
    cout<<"                                 HP: "<<HealthPoints <<endl;
    cout<<""<<endl;
    cout<<""<<endl;
    cout<<""<<endl;
    cout<<""<<endl;
    cout<<""<<endl;
    cout<<""<<endl;
    cout<<""<<endl;
    cout<<""<<endl;
    cout<<""<<endl;
    cout<<""<<endl;
    cout<<""<<endl;
    cout<<""<<endl;
    cout<<""<<endl;
    cout<<""<<endl;
    cout<<""<<endl;
    cout<<"..................................................................."<<endl;
    cout<<".Choose what you would like to do                                 ."<<endl;
    cout<<".1- Go, to shop                                                   ."<<endl;
    cout<<".2- FIGHT!!                                                       ."<<endl;
    cout<<".3- Credits                                                       ."<<endl;                                      
    cout<<".                                                                 ."<<endl;
    cout<<"..................................................................."<<endl;
    cin>> OptionMenu;
    if(OptionMenu == "1"){
                  system("cls");
    cout<<"   ===================================================================="<<endl;
    cout<<"                          WELCOM TO THE SHOP"<<endl;
    cout<<"   ===================================================================="<<endl;
    cout<<"#########################################################################"<<endl;
    cout<<"#                                                                       #"<<endl;
    cout<<"#                                                                       #"<<endl;
    cout<<"#                                                                       #"<<endl;
    cout<<"#                                                                       #"<<endl;
    cout<<"#                                                                       #"<<endl;
    cout<<"#                                                                       #"<<endl;
    cout<<"#                                                                       #"<<endl;
    cout<<"#                                                                       #"<<endl;
    cout<<"#                                                                       #"<<endl;
    cout<<"#                                                                       #"<<endl;
    cout<<"#                                                                       #"<<endl;
    cout<<"#                                                                       #"<<endl;
    cout<<"#                                                                       #"<<endl;
    cout<<"#                                                                       #"<<endl;
    cout<<"#                                                                       #"<<endl;
    cout<<"#                                                                       #"<<endl;
    cout<<"#                                                                       #"<<endl;
    cout<<"#                                                                       #"<<endl;
    cout<<"#                                                                       #"<<endl;
    cout<<"#########################################################################"<<endl;
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)


That's just basically how it works.
ok i understand but where would i put that code? and thank you
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.
Last edited on
ok thank il try to see if i can work that out
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;
Last edited on
I've said this before and I'll say it again.

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.

Get a lib like SFML: http://sfml-dev.org/
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!
closed account (j3Rz8vqX)
With the assumption that you want to learn C++ and not produce graphics for the console:
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
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
#include <iostream>
using namespace 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.
Last edited on
Disch wrote:
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.
Last edited on
Topic archived. No new replies allowed.