Play2048: The main game file part 1 - acdop100/Matlab-2048 GitHub Wiki

Play2048 is the first Matlab file you should open for this project. This file contains the code for the actual interface (GUI) which is what you are probably here for. I will go through the file from top to bottom.

Everything is initiated with a variable, by the way. You can see that in my code. You will also see I initiated some variables and an array in the beginning.

First, we have to initiate the figure. This means that we are telling Matlab to actually create a GUI window. In the figure(...) parenthesis we define the properties of the window.

  • Here, we define the name ('2048') and the window color. I used an RGB value but you can use whatever you want that's compatible.
  • Next, I defined the position, where the first 2 numbers are where the window spawns relative to pixels on your screen, and the second 2 numbers are the dimensions of your window. In the case of my window, my project loads at pixel XY coordinates of (710,315) and with dimensions of 560x550 pixels.
  • Next were some QOL options. I removed the menubar because it is not necessary for a game, turned off the ability to resize the screen so I don't have to worry about scaling, and turned NumberTitle off.
  • Next put the 'KeyReleaseFcn' here! This lets your users interact with your game using the keyboard! after 'KeyReleaseFcn', you have the function that runs after a key is pressed and any arguments that get passed to that same function. That is all put in {} to denote they are all for 'KeyReleaseFcn'. Note: 'KeyReleaseFcn' is not the only option that can go here for inputs, check the links for more!

Next, we will place the text for the game's title, the "score" text, a pause button, and a text box for the player's actual score. Here, we use uicontrol to generate an object within the figure. The Matlab link in the previous page shows all of the many options you can put here but I will outline a few of my choices:

  • The first spot is reserved for your figure's variable. Mine is simply 'f' as you can see in my code.
  • Second, I chose the style of the control which is a text box ('text') or button ('pushbutton'.
  • Third, 'String' denotes the text that will be present in the box when the figure is launched. leave blank to make a textbox that will be filled in later.
  • Skipping a few, 'Position' again means where the position of the uicontrol will be, but in uicontrol position is relative to the figure, not the user's computer screen like for figure.
  • The next few again are just other customizations that you can probably figure out for yourself from what we've done or the links I gave you.
  • At the end of your button, it is a good idea to put your callback function. This is what tells Matlab to execute a function at a button press. In my case, I executed the 'pauseGame' function for my pausebtn callback. You denote the function by putting the @ symbol before the function name. If you don't have a callback function, your button will do nothing.

We will get to my pauseGame function later...

Cool! Now that we've gone through the basic setup of a figure, we can go through the 2048 aspect of the GUI.