Creating the Game Loop and Menu System


When I started working on my game, the first thing I knew I needed was a way to control the different parts of the game. I decided to use a finite state machine for this. 

I began by creating an enum with a few states to start with, but I knew that I could add more states as the game developed. Here's an example of the GameStates enum I created:

public enum GameStates 
{
    MAINMENU,
    NEWGAME,
    LOADGAME,
    QUIT 
}  
public static GameStates gameState = GameStates.NEWGAME; 

To create the game loop, I wrote a method that uses a while loop and a switch statement to handle the different states. Here's the code for the GameLoop method:

public static void GameLoop() 
{
     while (!exit)
     {
         switch (gameState)
         {             
            case GameStates.MAINMENU:
                 // handle main menu state
                 break;             
            case GameStates.NEWGAME:
                 // handle new game state
                 break;             
            case GameStates.LOADGAME:                 
                 // handle load game state
                 break;             
            case GameStates.QUIT:                 
                 exit = true;
                 break;
         }
     }
} 

Next, I turned my attention to the interface system. Since the game interface was mostly menus, I decided to create a menu system, I knew I would have a lot of menus so I decided to make menus a class. I created a Menu class with a constructor that takes a title string and an options string. The options string is split by commas to create an array of options. The SelectOption() method will be used to receive the user's input for which option they want to choose. Here's the code for the Menu class:

class Menu
{
     string title;
     string[] options;
     public Menu(string p_title, string p_options)
     {
         title = p_title;
         options = p_options.Split(',');
     }
     public void Print()
     {
         Console.Clear();
         Console.ForegroundColor = ConsoleColor.White;
         Console.WriteLine(title);
         Console.ForegroundColor = ConsoleColor.Gray;
         for (int i = 0; i < options.Length; i++)
         {
             Console.WriteLine(i + ": " + options[i]);
         }
     }
     public int SelectOption()
     {
         int selection = -1;
         bool isValid = false;
         while (!isValid || selection > options.Length - 1 || selection < 0)
         {
             string input = Console.ReadLine();
             isValid = Int32.TryParse(input, out selection);
             // handle errors
             if (!isValid)
             {
                 Console.ForegroundColor = ConsoleColor.Red;
                 Console.WriteLine("Error! Input a number between 0 and " + (options.Length - 1));
                 Console.ForegroundColor = ConsoleColor.Gray;
             }
             if (selection > options.Length - 1)
             {
                 Console.ForegroundColor = ConsoleColor.Red;
                 Console.WriteLine("Error! Number too large!");
                 Console.ForegroundColor = ConsoleColor.Gray;
             }
             if (selection < 0)
             {
                 Console.ForegroundColor = ConsoleColor.Red;
                 Console.WriteLine("Error! Number too small! Enter a non negative number.");
                 Console.ForegroundColor = ConsoleColor.Gray;
             }
         }
         //Console.WriteLine(options[selection]);
         return selection;
     }
} 

After setting up the menu system, I created an instance of the Menu class for the main menu with the title "Main Menu" and the options "New Game", "Load", "Options", and "Quit". This instance will be used to display the main menu to the user and receive their input for which option to choose.

Here is the code for creating the Menu instance:

static Menu mainMenu = new Menu("Main Menu", "New Game,Load,Options,Quit");

To make the game procedurally generated, I needed a random system that could produce consistent results based on a given input. I decided to use the hash code of a name as the random seed value.  

name.GetHashCode();

With the state machine, menu system, and random system in place, I felt like I had a good foundation for the game. The state machine allowed me to control the different parts of the game

Leave a comment

Log in with itch.io to leave a comment.