Lunar Lander

 As the final exam for a programming class, I had to replicate the lunar lander video game in MATLAB. This included simulating the physics engine, creating a UI, creating usable controls, and calculating the response to the controls. 

My program starts by initializing all the required variables, creating and formatting the figure window to the correct size, and setting up the function that is going to read the user inputs. (KeyDownListener)

Figure 1: Initializing code

Terrain Generation

I then generated the terrain for the lander to land on. I did this by generating a curve, then plotting the curve in the figure. To generate the points of the curve, I used an RNG to generate an initial y value. Then, I used an RNG to generate a slope, then used that to calculate the next 3 points. 

    The challenge with this is that the lander needs a flat surface to land on, while still having an interesting and challenging terrain. To ensure this, I created bounds for the slope generator. Every time the slope is generated, new bounds are set. I then weight the likelihood of the any given bound to occur. This allows me to control how often the terrain is flat, and how often it can be steep. After some testing, I found the following weights to be ideal.

50% for +/- 1

20% for +/- 3

20% for +/- 5

10% for +/- 10 

This allows the slope to be between (+/-) 1pt 50% of the time, between (+/-) 3pts 20% of the time, and so on. 

Figure 2: Terrain Generator

The final step is ensuring the terrain stays within certain bounds. These bounds were the bottom of the figure, and whatever I want the max height to be. The figure is 1000 pixels tall, and after some testing, I found that a max height of 350 pixels to be ideal. To limit the terrain, for every point generated, I checked if it is outside the bounds, and set it back to the bounds if it was.  

Example terrain: Notice how there is always one or two flat portions for the lander to land on, while still being jagged and unpredictable. 





Lunar Lander Code

Comments