HOW TO CREATE TEXT GAMES USING C! : PART 3 (Constructing a menu)

This is the third part of the series of how to create text games using C. If you haven't seen the previous posts check them out by clicking the links below:
Coming to part 3, here we are going to discuss how to create a menu for the game.


If we look at any game, having a menu screen is quintessential as it provides the user with various options. Generally big games have some of the following options in their menu screen:
  • Play: Enters you into the game
  • Options/Setting: Generally, helps you change the video, audio and other settings
  • Leader Board: To show a record of top scores by players
  • Help/Instructions: Gives instructions for playing the game
  • About: Gives details of the game such as who created the game, copyrights etc.
  • Exit: To exit the game
For example, here's a screenshot of the game Assassin's creed:


So now coming to our game which we are going to develop using C, what options can we provide? We definitely need to have Play and Exit options. We can even include About and Help. We might be even able to provide Leaderboard option but it's a little complicated and you can develop it if you have a bit of knowledge on how to handle files in C. In this part, we will be only concentrating the following four options:
  1. Play    : Enter the game
  2. Help   : To know instructions of the game
  3. About : To give details about who developed the game. you can maybe add more ;)
  4. Exit    : To exit the game
So now the question is 'how do we actually provide the various options to the user in our code ad make him select one?'. Well there are two ways of doing this:
  1. using the switch statement.
  2. using the else-if ladder.
Let's do a menu using the switch statement
char option; //to store the option selected by the user

//printing menu onto screen:
printf("\t::MENU::\n\n"); 
printf("1.Play\n");
printf("2.Help\n");
printf("3.About\n");
printf("4.Exit\n\n");

//Alert the user to enter an option
printf("select your option(1-4): ");
//reading the entered option
scanf(" %c", &option); //or `scanf_s(" %c", &option, 1);`

//the switch block to check the option entered and take appropriate action
switch(option){
case '1':
    //help user enter the game!
    break;
case '2':
    //show instructions to the user
    break;
case '3':
    //show game details to the user
    break;
case '4':
    //exit the game;
default:
    //print invalid entry and make user enter again
}

In the above code, the switch statement can be replaced by the following else-if ladder:
//the else-if ladder to check the option entered and take appropriate action
if (option == '1'){ 
    //enter the game
}
else if (option == '2'){
    //show instructions
}
else if (option == '3'){
    //show details
}
else if (option == '4'){
    //exit the game
}
else{
    //print invalid entry and make the user enter again
}

Now we have our basic foundations laid down for our menu, all we need to do is just write the appropriate code below the appropriate case in a switch statement, or write the appropriate code in the appropriate if block in an else-if ladder. Instead of writing the code below the case or in the if block, you can call a build an appropriate function and call it! something like this:
//the switch block to check the option entered and take appropriate action
switch(option){
case '1':
    play(); //calling play function
    break;
case '2':
    help(); //calling help function
    break;
case '3':
    about(); //calling about function
    break;
case '4':
    exit(0); //there's already a pre-built function 'exit'
default:
    printf("invalid entry");
}

Now all you need to do is to provide the appropriate set of statements in the appropriate functions! Let's create a program which shows the option selected by the user from the menu.
#include "stdio.h";

//function proto-type declarations
void play(void);
void help(void);
void about(void);

//main function
int main(void) {
    char option; //to store the option selected by the user

    while(1){ //this would make the program run until the user selects exit
            
        //printing menu onto screen:
        printf("\t::MENU::\n\n");
        printf("1.Play\n");
        printf("2.Help\n");
        printf("3.About\n");
        printf("4.Exit\n\n");
  
        //Alert the user to enter an option
        printf("select your option(1-4): ");
        //reading the entered option
        scanf(" %c", &option); //or `scanf_s(" %c", &option, 1);`
  
        //the switch block to check the option entered and take appropriate action
        switch(option){
        case '1':
            play(); //calling play function
            break;
        case '2':
            help(); //calling help function
            break;
        case '3':
            about(); //calling about function
            break;
        case '4':
            exit(0); //there's already a pre-built function 'exit'
        default:
            printf("invalid entry");
    }
}
 
    return 0;
}

//function definition:
void play(void)
{
     printf("you selected play!\n");
}

void help(void)
{
     printf("you selected help!\n");
}

void about(void)
{
     printf("you selected about!\n");
}

Now the problem is that each time the menu gets redrawn, the previous one is not cleared. Generally, you'd want to clear the previous menu display. For this, you can construct the following function:
void clear(void)
{
    system("cls");
}

You can call the function at the beginning of each iteration of the while loop
//main function
int main(void) {

    char option; //to store the option selected by the user

    while(1){ //this would make the program run until the user selects exit
            
         clear(); //!!!NOTICE!!! calling the clear function!

         //printing menu onto the screen:

I hope you get the idea of constructing a menu for your game. All you need to additionally do is to provide the appropriate code for the respective functions and then your game would be ready! In the next part, you'll see how to build the famous rock, paper and scissors game using the knowledge from the previous three parts!

Part 4: Rock, Paper & Scissor game

Comments

Popular posts from this blog

Beginner's guide to Solving the N-Queens problem using backtracking method

PvP Chain reaction game using Python

Guide to Solving Maximal Subarray Problem using Kadane's Algorithm