Lunar Lander

This is a team assignment.

Computer simulation is used by engineers to test out ideas before actually building expensive machines or putting people in dangerous situations. Simulation is a critical part of the the space program by NASA, for example.

For this program, you will create a simulation of a vehicle landing on the moon. (It turns out that this assignment is also a rather old computer game that has been around for decades.)

Here is how it works: You are in control of a lunar lander ship, descending to the surface of the moon for a landing. Gravity steadily accelerates your ship faster and faster toward the surface of the moon. You, the astronaut piloting the ship, have a single control: a button with the label "thrust" on it. Applying thrust slows your ship down. Your goal is to get your ship to land on the moon at a slow enough speed so that it doesn't crash on impact. What's the catch? You have only a limited amount of fuel. If you slow down your ship too much too early, you will run out of fuel and crash into the surface of the moon.

Your mission: Program this simulation in Python.

Details

Create a file called lunarLander.py In that file, you should create a LunarLander class, and also a main function. Your main function (and the line that calls it) should be the only code you write that should be outside of the LunarLander class. This class represents the ship itself. The main method controls how the simulation works. In your LunarLander class, you will need to keep the following information in instance variables:

LunarLander should have the following methods (you may have more if you wish):

  1. Expend fuelUnits of fuel from the fuel tank. Make sure you check to see if you actually have this much fuel. If not, burn all that you have.
  2. For each of unit of fuel that you burn, call the thrust() method to decrease your velocity.
  3. Determine the new altitude for your lander. Your new altitude is your old altitude plus your velocity, plus an additional 2 meters/second for gravity. Gravity adds 2 meters/second to your velocity every second.

Your main function should create a LunarLander object, provide the player with a welcome message, then repeatedly show the player the current information for the lander followed by a question as to how many units of thrust to burn. If the lander ends up hitting the surface of the moon with a velocity of 4 meters/second or less, the ship lands successfully! If the lander hits the surface with a velocity of 5 meters/second or more, the ship crashes. You'll find a sample game at the bottom of this assignment.

Parts 1 and 2

This assignment is broken into two parts to help you pace yourself. Part 1 should have at least:

Part 2 should, of course, finish the assignment. You are welcome to turn in more than the minimum for Part 1, including the complete assignment, at the first deadline if you like. The brunt of the work is probably in Part 2, not Part 1, so if you have more time to work on Part 1 than Part 2, you should probably go further ahead. In my mind, the part of the assignment I have allocated to Part 1 is a "bare minimum" to keep you from being overburdened later.

Bonus work

If you achieve everything specified in this assignment, you will receive 19 points out of 20, which is to be congratulated! In my mind, that means that you've done wonderful work, and you've satisfied my expectations. If you want the 20th point: create an alternative version of main that is very similar to the one you've just created, but has two LunarLander objects descending to the moon, each controlled by different users. The two players alternate back and forth entering thrust for their own landers, until one them lands or until they both crash. I'm leaving the specifications of this fairly vague: get something working that involves two players with two landers working successfully, and you'll earn your point. But make sure that the one player version is working regardless. You might take all of your code from main, move it to another function called onePlayer, and create another similar function called twoPlayer. Your main function can then ask the user whether it should be a one or two player game.

Hints, Suggestions, Etc.

Here is a sample simulation:

Welcome to Lunar Lander!

Alt = 1000 Vel = 40 Fuel = 25
How much thrust this round? 0
Alt = 958 Vel = 42 Fuel = 25
How much thrust this round? 0
Alt = 914 Vel = 44 Fuel = 25
How much thrust this round? 0
Alt = 868 Vel = 46 Fuel = 25
How much thrust this round? 0
Alt = 820 Vel = 48 Fuel = 25
How much thrust this round? 0
Alt = 770 Vel = 50 Fuel = 25
How much thrust this round? 0
Alt = 718 Vel = 52 Fuel = 25
How much thrust this round? 0
Alt = 664 Vel = 54 Fuel = 25
How much thrust this round? 0
Alt = 608 Vel = 56 Fuel = 25
How much thrust this round? 0
Alt = 550 Vel = 58 Fuel = 25
How much thrust this round? 0
Alt = 490 Vel = 60 Fuel = 25
How much thrust this round? 0
Alt = 428 Vel = 62 Fuel = 25
How much thrust this round? 0
Alt = 364 Vel = 64 Fuel = 25
How much thrust this round? 0
Alt = 298 Vel = 66 Fuel = 25
How much thrust this round? 0
Alt = 230 Vel = 68 Fuel = 25
How much thrust this round? 0
Alt = 160 Vel = 70 Fuel = 25
How much thrust this round? 2
Alt = 96 Vel = 64 Fuel = 23
How much thrust this round? 0
Alt = 30 Vel = 66 Fuel = 23
How much thrust this round? 0
Alt = -38 Vel = 68 Fuel = 23
Oh no, you crashed!