C # mini-game: menu screen, how to achieve?

I am making a simple graphical game in WinForms, and currently I would like the menu to display when the game starts. My only problem is that I'm not sure about the structure of the application itself, this is what I still have:

1) To have the form of a menu and the form of a game. When a new game is selected, create a game form and attach it to the menu form - I cannot delete the menu form since the application will exit. Or can I switch messageloop to another form? I doubt that 2) To have some basic form that allows me to create and place both menus and game forms 3) Is it completely different?

+4
source share
1 answer

One of the ways that I often use when developing applications is to use the main form as a container for views (a view), which contains a panel for adding views.

What can you do:

  • Define two user controls: one for the menu, one for the logic of the game. The menu view should display one event for each menu item to which the main form can be attached.
  • At startup, show the menu control. When an event for "Play" is triggered, the main form should switch to game mode.

The following pseudo code will help you switch views:

private void SwitchView(Panel container, UserControl newView) { if (container.Controls.Count > 0) { UserControl oldView = container.Controls[0] as UserControl; container.Controls.Remove(0); oldView.Dispose(); } if (newView != null) { newView.Dock = Dock.Fill; // Attach events if (newView is ...) { ... } container.Controls.Add(newView); } } 

Please note that this code cannot be compiled properly, I am writing this from my head. But it will give you a general idea of ​​this.

+2
source

Source: https://habr.com/ru/post/1313251/


All Articles