MATLAB Lunar Lander
During Spring 2021 I took the class "Intro to Programming for Engineers" where we learned how to formulate, write, and troubleshoot MATLAB scripts. The final project for the course was to write a game with realistic physics modeled after the Apollo program. My game was one of the best written and actually led to my becoming a TA for the class the following year.
New Beginnings
It's always difficult starting something brand new, staring at a blank canvas. The first step, logically, would be to create the figure window and put the moon's surface on it. Next is to create a ship of sorts (originally a rectangular pink blob) and get it to move around. Slowly and methodically features were added, bugs found and fixed, and code refined.
Unfortunately there are no screenshots of the game during this process, but specific sections and the thought process behind them can still be explained. Feel free to download the code and play they game for yourself if you have access to MATLAB.
Moon Generation
To land on the moon there needs to be a moon. This would be a solid filled graph in the end, meaning we need X and Y values. The first thing is to create a set of X values in a set range, from 0 to 250 and the associated Y values. The problem with random numbers is that the terrain becomes very spikey and therefore needs to be smoothed. To smooth the terrain the commands 'movmedian' and 'movmean' are used- the first to smooth points together but maintain the general shape, then the second to dampen that shape without totally eliminating the hills.
To expand the moon these values are flipped or replicated three times, leading to a surface 4 times as large but containing some duplicate values. These values locations are indexed and deleted in the X array, then also deleted in the Y array leaving us with two arrays, moonX and moonY, forming the full surface of the moon.
The reason the terrain needs to be copied and flipped is because of what is known as 'The infinite moon statement' to be described in the next section.
Looping the Moon's Surface
This is a crucial part of the game design and fundamental to why the moon generation isn't random all the way along. When the player hits the edge of the playable area, the ship gets teleported to the opposite end of the map while maintaining the same velocity. The moon's surface is mirrored specifically so that the user doesn't notice the teleportation, leading to a seamless playing experience. To see the loop in action, the player must fly up so high the map zooms out past the created edges.
Physics
Proper physics are central to making the game 'feel' right. There are case scenarios for flight and when a single or both legs are touching the ground. The hardest problem here was accurately modeling rotational momentum when a leg touches the ground, and was quite a pain. Mathematically the problem is fairly straight forward, but implementing it just wouldn't work. At one point there was code to plot in real time the velocity and position to get a visual on the physics in action. In the end I just used a direct relation to velocity and spent my time solving other problems that were more pertinent to the game's function.
Flames
This is one of the coolest little features of the game. The engine isn't just on or off, it throttles, meaning the flames coming out of said engine vary in size. Not only that, but flames aren't a solid object we can touch. So in these three lines of code two things are accomplished: the flames vary in size relative to the throttle, and they also 'jiggle' when turned on. It's such a simple task with such a simple solution, yet brought so much joy when it worked and provided that extra hint of reality to the game. The bigger the flame, the more it jiggles!
Finishing Touches
When the game was functionally completed and the bugs worked out, it was time to add the fun components that really bring the user experience to life. The two most notable inclusions here are the flag and ending messages. When the player lands successfully, the game automatically indexes a point on the surface just to the right of the lander and plants a flag. Then, rather than a normal 'boring' message, the game randomly displays one of up to 8 different messages that the player has won the game. Accordingly, there are also 8 random losing messages to discover as well. It's written in such a way that more messages can be added without a problem.

The Completed Game
And a few final thoughts...
It's easy to look back and criticize my own work. It was typical to have to go find and learn new functions throughout the process. There's no doubt that I would write it differently next time- changing how functions worked, how graphics were generated, even adding a few more small features here and there. As it stands however, I'm proud of the result.
So here it is! The culmination of a semester long class, many long afternoons, and countless errors. There's a bit of a learning curve to it, which makes the satisfaction of winning just that much better. Enjoy!
**The website cannot display a .m file, so unfortunately you'll have to copy and paste the code into a MATLAB script manually.