Play2048: The main game file part 2: electric boogaloo - acdop100/Matlab-2048 GitHub Wiki

So we have the basic figure. Now we will create the 4x4 array of boxes that the numbers appear in.

First, though, I will explain a little bit how my game actually works.

  • First, I initiate an empty 4x4 array within Matlab. This stores numbers that my other function can manipulate.
  • This is separate from the 4x4 Textbox array in the figure window. Each of those have a text box that has a background color based on the number in the box's string. I keep the arrays separate because you can manipulate an array easier and input that into external functions instead of collecting the strings from all 16 textboxes every time.
  • After the game is loaded, an external function will randomly insert either 1 or 2 numbers into the array. Those are then loaded into the correct textbox. That is all done within the myTileGenerator file, more on that later.
  • The game is now ready to play.

Now that you see that, lets go into how this is made:

First I initialized two arrays, one that has the 4 vertical positions and one with 4 horizontal positions. Since we are making a 4x4 matrix your tiles will fills a combination of these 8 numbers.

Next we are going to make the tiles themselves. All these are are 16 individual textboxes placed at the locations from the arrays we made.

  • for 'Position' we can use [width(1), height(1), 55, 55) to make a box at the specified position with a width of 55 pixels and height of 55 pixels. Vary as you want.
  • You will want to name all of these boxes an easy name since you will need to refer to all of them later in your code to change the text.

Next we are going to make a welcome screen. We add this after all of this other stuff because the ui elements are added on on top of each other, so the lower the ui item is in your code, the higher "layer" it will have on your GUI.

Here I make the quit button, splash screen, and the start button. If you look above the splash uicontrol you will see I have a name variable. I use this to pull the actual username of the Matlab account and use it as the player's username within the game. Got a lot of cool looks from the TA's B).

Below this I have the start and quit button callbacks. The start button runs a function that just removes the elements from the splash screen.

Honestly the rest of the file is self explanatory / not relevant to this section so just read my code and you'll be able to figure it out.

Next we'll learn how to manipulate the data from an outside function.