3rd blog post of 10 Week Project – The Game Menu’s Code

Something I’ve been working on this week is implementing the menu for the game.

The states and state manager for our game had already been made, thus that was not a problem of mine. We had also gone through states in programming class, and therefore it was easy for me to know how they work and how to switch between them.

I have made a menu before, but as our game is controlled with the keyboard and no mouse, the menu for this game would be a little different. The options in our game’s menu are; Start, How To Play, Options and Quit. My first thought was to have an integer that increases/decreases when S/W is pressed. However, having an enumeration with the names of the menu options would be easier for both me and my fellow group members to understand when reading the code.

The enum “MenuOptions” was made with its four members named after the options of the menu. To be able to choose and switch between these options, I also made variable called “current_option” of the MenuOptions type. Then, switch case-statements were used to see which option is currently chosen and then change the current option to the next one, depending on which key is pressed.

To see if this worked I output current_option when W/S was pressed. It did, but the options were switched between in a very fast speed. Therefore I had to create a delay to make the switching between options easier. I had code written by my groupmate André to work from. A function updating the game’s deltatime was added to the menu state’s .cpp file. When a key is pressed it will also check if the boolean “options_changed” is set as false. If this boolean is set as true, a float variable will increase with the deltatime, and when half a second has passed (the float is greater than 0.5) the options_changed boolean is set as false and the float is reset as zero.

As we use the space bar to shoot in our game, I thought choosing a menu option could be done with this key as well. For this I used the same method that I used for switching between options. However, this time the switch case statement does not change the currently chosen option, but change the current state instead(or quit the game when quit is chosen).

I am sorry about the lack of pictures in this blog post. Since the graphics for our menu are not done, I do not have much to show you. Anyhow, here is the output showing that the menu works;

menuswitch

The numbers are the members of the enum’s orders; 0 is Play, 1 is How To Play, 2 is Options and 3 is Quit. I first press the S-key to go down in the menu, and then W to go up. I then press space to choose the Play option in the menu, which takes me to the game state.

The visuals for the menu will be a background with the menu’s options written on it, and a sprite showing what option is currently active by changing its position when W/S is pressed. However, this will be fixed when the graphics for the menu are done!