0
\$\begingroup\$

I want to create a state machine for menu in my SDL game. So this is my code without the SDL I just want to ask if this is a good way to create it. here is a code:

#include <stdio.h> #include <windows.h> enum states { Menu, Game, Game_over, Exit }; int main() { int game_is_running = 1; enum states state = Menu; while(game_is_running == 1) { switch(state) { case Menu: state = Game; printf("menu screen with play and exit button.\n"); //need to add if //exit or play is pressed // if exit then exit program if play than you know... break; case Game: state = Game_over; printf("after play button is pressed game screen will show up.\n"); //after this i will come back to menu. break; case Game_over: state = Exit; printf("screen after game.\n"); break; case Exit: printf("turn game off.\n"); //this will be in the if function with a game. game_is_running = 0; break; } Sleep(1000); // sleep for a second just for test } return 0; } 

\$\endgroup\$
3
  • 2
    \$\begingroup\$ "Is this a good way to do it" is something that codereview.stackexchange.com is better suited to. \$\endgroup\$ Commented Mar 22, 2021 at 17:18
  • 1
    \$\begingroup\$ ou i didn't know about that page. Thank you \$\endgroup\$ Commented Mar 22, 2021 at 17:21
  • \$\begingroup\$ you should probably move the code in the break to methods called as states, mostly because the code under each case will grow rapidly. \$\endgroup\$ Commented Oct 31, 2024 at 12:55

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.