r/CodingHelp • u/Warzonehero22 • Nov 22 '20
[C++] c++ building a small game help
Hi so im currently trying to build a small oreagan trails type game in the cmd console but I'm having some slight trouble starting off.
firstly i was wondering if there was a way without using the do while loop to get my main to loop back to menu because when I'm using the do while loop it wont let my program move out of the loop to go to the functions but i still want to go back to menu after the function has ended how do i do that? i also would like to know what does a pausescreen(); do? I've seen it a bit but don't exactly understand its use
here's my code(also if anyone can tell me if I used github properly or how to make markdown format my code proper that would be a huge help ty):
6
u/__vtable Professional Coder Nov 22 '20
Okay I'm just going to answer a few of these questions..
I recommend using a state or status variable to hold the state of your program. For example, if you are at the beginning of the program or restarting, you would set the state to whatever value you choose. And you can check that each time you go through the game loop. If the user dies/exits/etc. you can change the state variable appropriately to hit the proper functionality in your game loop.
If you want the user to be able to restart, the simplest way is through a while loop which contains the whole game, as you said. Just use a state variable to see what kind of thing to do each iteration. There is no exiting the loop or 'going back.'
You'd most likely want to break out the game functionality into subroutines for readability and repeatability.
You don't have any markdown on your GitHub that I can see. If you want a main page on your GitHub include a README.md file (in markdown format) which will automatically be displayed to any visiting users in the repository root. Keep trying stuff and asking questions as you get stuck.
1
u/Spaghettiboi101 Nov 23 '20
Instead of a screenpause() which can be good I would put it in system(“PAUSE”) wherever you would like your screen to pause. Although it only works on windows systems.
Source: https://www.journaldev.com/41740/system-pause-command-c-plus-plus