MATLAB Fun Toolbox

This page contains my library of MATLAB m-files for a simple game and everything else that I make just for fun. All of these m-files are free and you may use or modify them as you like. To download the m-files, simply click the link on each m-file name and a pop-up window will appear, allowing you to download the m-files from my dropbox (or right-click on the link and choose ‘save target as’). Enjoy…

Turn-Based Board Game M-Files

TicTacToe.m – a simple game of Tic-Tac-Toe (or noughts and crosses) with MATLAB GUI.

TicTacToeLite.m – a lite version of TicTacToe.m (does not require MATLAB Image Processing Toolbox).

TicTacToeReloaded.m – another version of TicTacToe.m.

Othello.m – a game of Othello with MATLAB GUI.

SudokuSolver.m – solves an incomplete 9×9 Sudoku board or generates a random 9×9 Sudoku board.

Sudoku.m – a game of Sudoku with MATLAB GUI and its solver (requires SudokuSolver.m).

Congklak.m – a traditional Indonesian board game with MATLAB GUI.

Congklak_custom.m – a traditional Indonesian board game with MATLAB GUI (with customizable number of houses and number of beans).

CongklakReloaded.m – another version of Congklak_custom.m which incorporates doubleplay for first turn.

Minesweeper.m – a game of minesweeper with MATLAB GUI.


Arcade Game M-Files

Pong.m – a game of classic Atari’s Pong with MATLAB GUI.

Snake.m – a game of classic snake with MATLAB GUI.

Tetris.m – a game of classic Tetris and its two alternate modes in one MATLAB GUI.

DoodleJump.m – a simplified version of DoodleJump game with MATLAB GUI.

Bull_n_Cow.m – a number guessing game, Bull and Cows, with MATLAB GUI.

VirusShooter.m – a webcam-based shooting game with MATLAB GUI (requires image processing toolbox).


Not-A-Game-But-Still-Fun-Or-Unique M-File

LineScreenSaver.m – an animation of Windows XP’s line screen saver.

Balls.m – a class library for 2-D elastic collision simulation (includes example script).

Those m-files that I listed above might be fun, but it is also not perfect. If you have any comments, critics, and suggestion for my m-files, please feel free to message me at or leave a comment.

51 Responses to MATLAB Fun Toolbox

  1. Aniket says:

    Awesome thnxxxxxxxxxxxxxxxxxxxxxxxxxxxx

  2. mirbaz says:

    whenever i run any m.file, it give me an error “unexpected matlab operator”

  3. mirbaz says:

    All of yours m.files gives error that “Unexpected matlab operator”. Thanks for replying

  4. mirbaz says:

    it displays this error, whenever i run any mfile.
    “” ??? Error: File: VirusShooter.m Line: 191 Column: 35 Unexpected MATLAB operator. “”

    i’m using matlab 7.8.0 (32bit)

    thanks 🙂

  5. Bilal says:

    Only the last link works, when i press on the others ones they dont download, its just opens a new page with codes, such as :

    function TicTacToe %————————————————————————– %TicTacToe %Version 1.01 %Created by Stepen %Created 5 February 2011 %Last modified 24 October 2011 %————————————————————————– %TicTacToe starts GUI game of a simple tic-tac-toe. %————————————————————————–

    • Dear Bilal…

      I used dropbox for my file sharing and it seems that dropbox automatically opens any links to text-based files using your browser. The last code works because it is in zip files instead of m-files (Thus, dropbox prompt you to save it instead of opening the file directly in your browser). To download the code, just copy the text into your Matlab file editor and save it, or you can right click the link and choose ‘save link as’, it should work…


      • Bilal Hussein says:

        Ok thanks it worked, btw im taking a computer methods class, and we are asked to make a simple game using matlab, can you help me with that? i saw all ur games but there so long and and are somehow advanced.

      • Yes, all quite long because it involves interactive GUI. Do you have any game in mind that you want to make? If not, I suggest you make tic-tac-toe for two player and without using any GUI, that would make the code a lot simpler. Also, try seraching in MATLAB File Exchange, it has a lot of simple game that maybe you can learn from.

      • Bilal Hussein says:

        I think im gonna make a ”guessing random number game”, but first i have to send a 1-2 page with the objective of the game, its only a draft though so no problem, the draft is for the teacher to see if the game were making is too simple, she dosent want it to be waay to simple because its worth 40% of the grade, this is what i got, so what do you think? see the file below, and thanks alot for the reply,

        this is what the program should contain:

        A 1-2 page document outlining Game Objective How to Play (1 or 2 player, brief overview of rules of game) Program Objectives How the program will work How the computer and players will interact

        This is a first draft to be submitted for my approval at any time before the due date. I will review your document and identify whether the program will be difficult enough to warrant full points or less. If your document is reviewed and it is determined that your final program would not be difficult enough to warrant full points, you may continue with the program with a reduction in mark, or resubmit the document with more challenging Program Objectives or a completely new game. Approximately 20 separate and moderately difficult Program Objectives will be worth full points (20/20) The Game Rules will be the rubric for program marking as the more of your original rules and objectives you satisfy, the more marks you will receive. There are to be no duplicate games between students. Program approval will be given on a first come-first serve basis. The faster you submit your Game Rules document, the more likely you will be to have the project of your choice.

        See attached powerpoint for an example of Game Rules.


      • I can’t see the attached file… Could you maybe just email it to my mail: I think it would be better…

  6. abdul says:

    just wondering, what is the strategy or algorithm(without gui, just if you want to display everything in command window) you used for the othello game(e.g.. set global variables,set board….)

  7. shaveta says:

    i want to design a game in matlab…but all yours codes gives error when i was using matlab version 7.5….please give a correct codes to design a tetris game….its very urgent…please requesting

  8. shaveta says:

    n also please give me a guidance how to make a game in matlab.. i want to done my project in matlab game

  9. shaveta says:

    kindly send me the link from where i can download the matlab latest version software easily in less time or send me the software setup on my mail id as zip file….if its possible

    • Errr, no I can’t…

      Like I said, if you send me the error message, I might be able to fix the code for you so that it will run on your version of MATLAB.

      • shaveta says:

        its ok

      • shaveta says:

        i work on matlab 7.5.0/R2007b…whenever i run your tetris codes…error must be occur… this error is given:-
        (??? Error: File: Tetris.m Line: 403 Column: 26
        Unexpected MATLAB operator)

        thnx if u run this program on version 7.5.0

      • shaveta says:

        where line 403 is :-
        function keypressfcn(~,event)

        why unexpected matlab operator error is shown

      • It is actually pretty simple to fix… All you need is to replace the tilde sign with src. So, you should change (~,event) in line 403 and 437 with (src, event). You should also do the same with (~,~) in line 463, 472, 481, 786, 797, and maybe in other line into (src,event).

        Hope this helps…

      • shaveta says:

        after replace the ~ operator with src…its also gives an error

        ??? Error: File: Tetris1.m Line: 463 Column: 5
        The variable src was mentioned more than once as an input.

      • shaveta says:

        line no.463 is:-
        function movegamespeedsliderfcn(src,src)

      • Sorry, my last comment was a bit confusing… For (~,~), you should change it into (src,event). Basically, wince these functions are callback function, by default it must has two input argument, which is src and event. Since my callback function didnot use any of these input argument, I still have to pass something to the function. In version 2011b, they introduced ~ (tilde sign) that you can use to replace unused input argument or output argument. That’s why you will receive error in previous Matlab because this operator is not supported.

      • shaveta says:

        did u run this progrm on matlab R2007b??

      • shaveta says:

        if not then please run these codes on matlab 2007b…..bcoz i work on this version…and show me the accurate codes for this version

      • shaveta says:

        please reply as soon as possible

      • I can’t run it on R2007 because I don’t have one… If you are still having error with my solution, let me know the error message, maybe I can help solve it… Or maybe you can google it too if my response is too slow….

      • shaveta says:

        if we add background music during played game then what can i do?

  10. shaveta says:

    can u please tell me what is the function of ”~” operator

  11. says:

    Hey, on the tictactoelite game is there anyway to replace the concept of the x and o with an image from google? Instead of marking the squares with xs and os I think it would be awesome to use some comic or funny image to play the game. Could you show me how to input that code?

  12. Wahaj Imam says:

    Hey, for the tictactoelite game, I was wondering if it would be possible to manipulate the x and o concept into using images from google to fill in the squares. Could you show me how to fix the code to allow it to do that?

  13. mlarsm says:

    When i try to start snake in matlab R2007b i get the error:
    ??? Error using ==> figure
    Invalid figure property: ‘WindowKeyPressFcn’.

    Error in ==> Snake at 51

  14. mlarsm says:

    And the (src,event) does not seem to work for me. I do get an error message:
    Input argument ‘src’ appears never to be used.
    Input argument ‘event’ appears never to be used.

  15. Fraz Ahmed says:

    can anyone tell how to collide two balls in an m file and change their sizes as they collide?

  16. JH lee says:

    this is really interesting … (i commented two days ago but it is erased know (with no reason ;;) )

    i want to change program of tetris like , when my score over a specific points, then blocks are chaged like ㄷ ㅠ etc. to make a game more difficult .

    i tried but , i think it is impossible for me ;; ㅠ,.ㅠ can you do it ? ? ?

    • Hello, JH Lee…

      Your comment didnt get erased, I just haven’t read and approve them. My bad :D… Actually, if you download the source code, you can see the cell variable called shapemodifier_x and shapemodifier_y. This variable defines the library of block shape to be used. You can simply add new element to these cells to add new shape. However, note that some code may be adjusted here and there to accomodate the change in the cell size (Not exactly remember if I hardcoded the cell size into the code or not). Hope this helps.


      • JH lee says:

        i just played colormatched game but , down button (s) don’t work . when i push that button, it just change its color array. what is wrong…??ㅠ,.ㅜ

  17. Tushar Gupta says:

    I am thinking of running a MD simulation on matlab can you suggest me some easy topics to work on. I am a beginner in this area.

  18. Javier says:

    hola, primero me gustaría felicitarte y agradecerte.
    Quisiera saber si me podrías facilitar el archivo: TicTacToeLite.fig soy nuevo en esto de matlab y me gustaría revisar un poco mas a fon do el .fig. Me ayudarías muchísimo, gracias.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s