Unit 8
Part 2 - The Main Menu
Tutorial that I used:
Some parts of the code I used is different as I changed them to match my game.
My next step was to create a working title screen that allows the player to go into the game, read the controls and quit out of it. I also want to have the title of the game appear on this screen and many some other detail as well. This is an essential part of any game as it is the first thing people see when they open the game and is what will set the expectations for your game.
My next step was to create a working title screen that allows the player to go into the game, read the controls and quit out of it. I also want to have the title of the game appear on this screen and many some other detail as well. This is an essential part of any game as it is the first thing people see when they open the game and is what will set the expectations for your game.
Before beginning production on the menu, I had to create the assets that would be used. This included an arrow to show the player the option that they have selected, a font for the text and a room for the menu to be made inside.
objects, fonts and sprites used
First I needed to create what's known as an array, this is a form of variables that can be referred to by other pieces of code. I begin by adding what's known as a Create event to an object called 'menu_obj'. Then by adding an execute code action to the event I create my array:
This array will tell the computer what the text will say and how far the space will be. It also allow me to add code to each option and make them work.
Code is in Blue
///Menu
This is a comment that I used to help organize the code
menu [0] = "Start";
menu [1] = "About";
menu [2] = "Quit";
This is what's known as a string variable, this works in a similar manner to normal ones only it allows you to make several ones that are connected. This string is telling the computer that the variable mentioned is equal to the following statement.
space = 64:
This tells the computer that the statement 'space' is 64. This is important as in later code I reference this to set the space between the text.
mpos = 0;
This is where the menu cursor starts. Also after the menu is opened, increasing or decreasing this value will make the arrow highlight different options. So by starting at 0 means that the arrow will be pointing at Start, and changing this will move the arrow.
Now I need to add a step event to the menu object. This will be what tells the computer to move the cursor between the options and how to 'select' them.
///Menu Movement and select
//Movement
var move = 0;
This is create a variable with the name move, which will be coded to create a moving action on the object.
move -= max(keyboard_check(vk_up),keyboard_check_pressed(ord("W")),0);
move += max(keyboard_check(vk_down),keyboard_check_pressed(ord("S")),0);
These are the variables that tell the computer to look for specific key presses. The max statement tells the computer to use the largest of the values given, so it will either use the up key, W key or 0. This means that if one of these keys are not being pressed, then the object will not move. Also the + and - represent the direction that the object will move, with - being up and + being down.
I actually made a mistake here and missed out the 'pressed' part of the statement on the arrow presses. This means that if you use the arrow in the game it will just continually scroll so long as you hold the key down. This is because instead of looking for just the action of a key being pressed, it is looking for if a key is pressed.
if (move != 0)
{
mpos += move;
This tells the computer to add either of the variables to the position of the menu, making it move up or down.
if (mpos < 0) mpos = array_length_1d(menu) - 1;
This tells the computer that if the position of the menu is less than 0 then make the position of the menu equal to the length of the array -1. This will mean that if you move the cursor above the first statement, then it will go to the last one.
if (mpos > array_length_1d(menu) - 1) mpos = 0;
}
This is similar to the previous option only in the other direction, so if the cursor goes below the last option in the array, then make the menu position equal to position 0 in the array
The way the code is means that no matter how many statements that I add to the array, The coding will always work for it, as while 0 will always be the top option, I have set the movement past the bottom to be the bottom of the array, not a set number.
var push;
This creates a variable with the name push. This will be used to tell the computer that if a specific key is pressed then perform an action, such as travel to a new room. This will make the menu work and allow me to make it take me to the game or about page.
push = max(keyboard_check_released(vk_enter),(keyboard_check_released(vk_shift),(keyboard_check_released(vk_space), 0);
This max statement is telling the computer whichever of these is highest then push will equal it, so if one of the following Space, Shift or Enter keys is being pressed then push will Equal them, but if none are being pressed then it equals 0.
if (push == 1) menu_scr();
So if push is equal to a button pressed then refer to the menu_scr. This then perfoms the action that I will create in the mentioned script.
Next I had to create a script that the push statement could refer to.
///menu_scr
switch (mpos)
This is called a switch statement, which is similar to a series of if statements. Basically if one of the statements is true then it will do what the code tells it to do.
{
case 0:
{
instance_create(0,0,fadetogame_obj);
This is a custom statement that I made to create a transition which I will go into on a later post, but basically it spawns an object that takes you to the overworld room. You can also set it to just take you to different rooms, but I did this to add a fade in transition.
break;
This tells the computer to stop once the action is complete.
}
case 1: {
instance_create(0,0,fadetoabout_obj);
break;
}
case 2: {
game_end();
break;
}
default: break;
This tells the computer that if none of the cases are true then do the following action, which is simply break.
}
Now I create a draw event to the menu object as I can now add the visuals to the menu.
draw_set_halign(fa_left);
draw_set_valign(fa_middle);
draw_set_font(menu_fnt);
draw_set_color(c_white);
Now all I had to do was add in the enemy characters and add a collision event to them. Then add the code:
room_goto(x);
By replacing x with the name of the battle room that I want the player to be taken to I can make it so that by touching the character you can battle them.
No comments:
Post a Comment