Using MATLAB to Write Games

This is a somewhat odd topic in that one does not think of MATLAB when one thinks of writing games. I mean, seriously? MATLAB is an interpreted language, and thus slow, and is made for crunching math and plotting graphs. Not exactly game-worthy.

It turns out that despite its drawbacks it is possible to write some simple games. Yes, they are slower than their counterparts in other languages. Nevertheless it undoubtedly works, and was a good educational experience.

I started by punching “Matlab Game” into Google, and came up with this on the file exchange. It is an old-style arcade bullet-hell shooter implemented in basically a single script file. The code is well outlined and relatively easy to follow. Highly recommended for others with a desire to create a MATLAB game.

The structure for my game is the same as for any other. The main script looks as follows:

timer = now();
while ~quitGame
    if ~paused
    secToWait = FRAME_DELAY - (now() - timer)/86400;
    if secToWait > 0
        pause( secToWait ); % freeze program for each frame
    timer = now();

The functions’ purpose should be fairly self-explanatory. You create the figure which acts as you main window, you initialize everything and create your game object, you show an intro screen (which blocks until completed), then reset your game object and begin a timer loop for frame rate control.

Note that this is already a little different than how it was done in Shooter03. Asteroids uses several object files and tries to control a framerate rather than just waiting a set amount of time.

From there it is basically a straight port of C++ to MATLAB. GameObjects don’t have a separate Visual object anymore. Instead, each GameObject has a plot handle which Matlab takes care of behind the scenes. By changing the data using set() you can change the location of your plot, thus changing the object’s position on the screen. Note that the plots are superimposed in the order they are created, so the first plot created is on bottom. This wasn’t much of a problem with Asteroids as everything was just white lines. One more thing: remember to delete your plot objects if the GameObject is destroyed.

If there was one thing that I feel MATLAB was worst suited for game-wise, I’ll have to go with playing sound. I ended up finding an ugly solution where I create n sound samples for every sound I might use, where n is the max number of times I expect the sound to overlap. For instance, if someone hammers away at the fire button, and firing lasts 10 ticks, then the fire sound might overlap 5 times. To I’d pick n = 6, and whenever I needed to fire I’d find a sample that wasn’t actively playing and run it. For all I know, maybe this is how sound libraries work under the hood. Anyways, I think it feels ugly and if anyone finds a better way to do it, please let me know.

Overall the game is a bit slow (I do create and destroy a lot of debris particles), but is playable. I haven’t tried it on a slow computer yet, so maybe it isn’t great on other machines. As always, code is available. Here is the link.