What's up GameDev.net!
Now, I'm working on my first "game" but I'm really having some trouble with my menu (I'm just a noob C programmer btw).
So basically, I have a menu, right? And then there are 4 choices (Namely 1. Start Game, 2. Tutorial, 3. Credits, 4. Quit Game). The user then types the number of his choice.
So let's say the user types 2, so the program goes to the tutorial screen and here's where I've been having trouble. I want the user to have the ability to go back to the previous screen. here's my current code in that section:
main()
{ int status;
char cOption;
system("color 2F");
WelcomeScreen();
MainMenu();
printf("\n\t\t\tPlease enter your choice: ");
scanf("%c", &cOption);
if(cOption == '2')
{system("cls");
Tutorial();}
if (cOption == '3')
{system("cls");
Credits();}
if (cOption == '4')
{system("cls");
ClosingScreen();}
getch();
}
Now to fully understand the problem here, here's my code for function "Tutorial"
void Tutorial()
{
char cOption;
printf("\n\t\t1. Guide your character using the arrow keys\n");
printf("\n\t\t2. Press E to eat when beside a food item\n");
printf("\n\t\t3. Press M to move a block when beside a movable block\n");
printf("\n\t\t4. Have fun!");
printf("\n\n\n\t\tPress B to go back to the previous screen: ");
scanf("%c", &cOption);
if(cOption == 'B' || cOption == 'b')
{system("cls");
main();}
}
I think my logic is correct, but everytime the tutorial menu pops up, and I press B( which will be used to go back to the previous screen), the program just closes.
Need help with Menu
A couple things. Use the [code ] tags to get proper formatting on your code snippets on the forum. It would help greatly.
You should run your stuff in a loop. The way you are doing it you will end up with a crazy stack because you go main->tutorial->main->tutorial etc. You want to go main->tutorial and then remove tutorial, not add another main.
You should run your stuff in a loop. The way you are doing it you will end up with a crazy stack because you go main->tutorial->main->tutorial etc. You want to go main->tutorial and then remove tutorial, not add another main.
So you're suggesting that I should use loops?
How do they work? (I'm a real noob ) ) How about a sample code?
How do they work? (I'm a real noob ) ) How about a sample code?
So you're suggesting that I should use loops?
How do they work? (I'm a real noob ) ) How about a sample code?
Ok, for a loop, you start like this:
int main()
{
//Functions that get called once
init_stuff();
//the loop boolean
bool quit = false
while(quit == false)
{
//functions you want called during course of the game
handle_input();
logic();
render();
}
if(quit == true)
{
clean_up();
save_data();
}
return 0;
}
Ask any questions you need.
I'm still kinda confused (When I say I'm a total noob, I mean that in the most literal way possible ) )
Can you help me out here because I'm having a serious migraine by just thinking about this one )
That's my code so far, my functions for ClosingScreen and WelcomeScreen are too big so I kinda neglected him here.
What should I do? thanks a lot man!
Can you help me out here because I'm having a serious migraine by just thinking about this one )
void MainMenu()
{
printf("\t\tPress the number corresponding to your choice\n");
printf("\n\t\t\t1. Start a New Game\n");
printf("\n\t\t\t2. How to Play\n");
printf("\n\t\t\t3. Credits\n");
printf("\n\t\t\t4. Quit Game\n\n");
}
void Tutorial()
{
char cOption;
printf("\n\t\t1. Guide your character using the arrow keys\n");
printf("\n\t\t2. Press E to eat when beside a food item\n");
printf("\n\t\t3. Press M to move a block when beside a movable block\n");
printf("\n\t\t4. Have fun!");
printf("\n\n\n\t\tPress B to go back to the previous screen: ");
scanf("%c", &cOption);
if(cOption == 'B' || cOption == 'b')
{system("cls");
main();}
}
void Credits()
{ char cOption;
printf("\n\t\t Animac was created by Jerome Ibanez\n");
printf("\n\t\t I would like to thank Dr. Nelson Marcos\n");
printf("\n\t\t for all the advice he has given me\n");
}
main()
{ int status;
char cOption;
system("color 2F");
WelcomeScreen();
MainMenu();
printf("\n\t\t\tPlease enter your choice: ");
scanf("%c", &cOption);
if(cOption == '2')
{system("cls");
Tutorial();}
else if(cOption == '3')
{system("cls");
Credits();}
else
ClosingScreen();
getch();
}
That's my code so far, my functions for ClosingScreen and WelcomeScreen are too big so I kinda neglected him here.
What should I do? thanks a lot man!
You could try this. Sorry for it being in C++, i'm not too familiar with C. But looking at it you will get the general idea I hope.
#include <iostream>
using namespace std;
int MenuOptions();
void DoMenu();
int main(int argc, char** argv)
{
DoMenu();
return 0;
}
int MenuOptions()
{
int option;
cout << "\t1. Start Game" << endl;
cout << "\t2. Tutorial" << endl;
cout << "\t3. Credits" << endl;
cout << "\t4. Exit Game" << endl;
cout << "\t\t Enter choice: ";
cin >> option;
return option;
}
void DoMenu()
{
bool finished = false;
while(!finished)
{
int option = MenuOptions();
switch(option)
{
case 1:
//Start New Game
break;
case 2:
//Start Tutorial
break;
case 3:
//Credits
break;
case 4:
finished = true;
break;
}
}
}
You can use a finite-state machine. Usually used when programming AI, but makes a great menu system.
If you dont know what loops are you should get a programming book man. Trying to write a game before you even know what a loop is, is kind of silly. Learn to program a bit first. Once you do, this will all be very obvious I think. There are a ton of tutorial sites on C.
I second the suggestion of a state machine of some sort. Your game will only ever be in one "state" at a time. Menu state, tutorial state, game state, credit state etc. Each renders differently and each handles input differently.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement