Sure! Here's a simple implementation of a main menu in GameMaker using GML (GameMaker Language). This example covers creating a main menu room with a Start button and an Options button.
Steps
- Create a new room for the main menu (e.g.,
rm_main_menu).
- Create two objects for the
Start button and Options button (e.g., obj_button_start and obj_button_options).
- Use GML code to create button functionality.
1. Main Menu Logic (Buttons)
obj_button_start
Create Event (Code):
// Define properties of the Start button
button_text = "Start Game";
button_action = "start";
Draw Event (Code):
// Draw a simple button
draw_set_color(c_white);
draw_rectangle_color(x, y, x+200, y+50, c_black, c_black, c_black, c_black, false);
draw_set_color(c_white);
draw_text(x + 10, y + 15, button_text);
Mouse Left Pressed Event (Code):
// Check if the button is clicked and perform the action
if (point_in_rectangle(mouse_x, mouse_y, x, y, x+200, y+50)) {
if (button_action == "start") {
// Switch to the main game room (replace `rm_game` with your actual game room name)
room_goto(rm_game);
}
}
obj_button_options
Create Event (Code):
// Define properties of the Options button
button_text = "Options";
button_action = "options";
Draw Event (Code):
// Draw a simple button
draw_set_color(c_white);
draw_rectangle_color(x, y, x+200, y+50, c_black, c_black, c_black, c_black, false);
draw_set_color(c_white);
draw_text(x + 10, y + 15, button_text);
Mouse Left Pressed Event (Code):
// Check if the button is clicked and perform the action
if (point_in_rectangle(mouse_x, mouse_y, x, y, x+200, y+50)) {
if (button_action == "options") {
// Go to the options menu (replace `rm_options` with your actual options room name)
room_goto(rm_options);
}
}
2. Main Menu Controller (Background and Initialization)
You can create an extra object (obj_menu_controller) for general control of the main menu.
obj_menu_controller
Create Event (Code):
// Set the background color and initialize variables if needed
background_color = c_black; // Set the background color of the menu
Draw Event (Code):
// Title text for the main menu
draw_set_halign(fa_center);
draw_set_color(c_white);
draw_text(room_width / 2, 50, "Main Menu");
// Draw instructions, etc.
draw_text(room_width / 2, room_height - 50, "Press a button to continue");
3. Place Items in Room
-
In the rm_main_menu room, add the objects:
obj_menu_controller
- Place
obj_button_start at a specific position (e.g., (100, 200)).
- Place
obj_button_options directly below it (e.g., (100, 300)).
-
Customize the button sizes/appearance as desired. You can also use sprites instead of draw_* for buttons.
That's it! When you run the game, the main menu will have a Start Game and an Options button. When the player clicks the buttons, the respective rooms will load. You can expand this further with more buttons, animations, or a polished UI design!