Jump to content

  • Log In with Google      Sign In   
  • Create Account

Need help with Menu


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
8 replies to this topic

#1 ChaosMohawk   Members   -  Reputation: 94

Like
0Likes
Like

Posted 07 July 2011 - 07:24 AM

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.

Sponsor:

#2 way2lazy2care   Members   -  Reputation: 782

Like
0Likes
Like

Posted 07 July 2011 - 07:36 AM

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.

#3 ChaosMohawk   Members   -  Reputation: 94

Like
0Likes
Like

Posted 08 July 2011 - 03:31 AM

So you're suggesting that I should use loops?

How do they work? (I'm a real noob :)) ) How about a sample code?

#4 A1P4A_0M3GA   Members   -  Reputation: 89

Like
0Likes
Like

Posted 08 July 2011 - 10:20 AM

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.
-A1P4A 0M3GA
Lead script writer on Scutum [http://www.gamedev.n...-entertainment/]
Team Member of Forcas Entertainment
Amateur programmer with C++ and SDL knowledge
Game Enthusiast

#5 ChaosMohawk   Members   -  Reputation: 94

Like
0Likes
Like

Posted 09 July 2011 - 06:01 AM

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 :))



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!

#6 ScoreX   Members   -  Reputation: 344

Like
0Likes
Like

Posted 09 July 2011 - 09:02 AM

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;

		}
	}
}


#7 DaveMS   Members   -  Reputation: 185

Like
0Likes
Like

Posted 09 July 2011 - 03:32 PM

You can use a finite-state machine. Usually used when programming AI, but makes a great menu system.

#8 Joe P   Members   -  Reputation: 166

Like
0Likes
Like

Posted 09 July 2011 - 06:38 PM

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.
Never, ever stop learning.
- Me

#9 Nanoha   Members   -  Reputation: 300

Like
0Likes
Like

Posted 09 July 2011 - 07:02 PM

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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS