Lunar Lander

Table of Contents

This is a pair programming assignment. If you are working in a pair, this means that you and your partner should be doing the entirety of this assignment side-by-side, on a single computer, where one person is "driving" and the other is "navigating." Set a timer to swap every 15 minutes. You can choose your favorite from this online timer page. Make sure your sound volume is audible, but not so loud to disturb the people around you.

If you are working in a pair, only one of you needs to submit your work via Moodle. That said, you should both have a copy of your work in case you want it someday, so make sure that both of you have copies of it; you can email it or use some other mechanism to transfer it.

We will use anonymous grading on Moodle, which means that the grader won't see your name until after the grading is done. This is an easy way to help add an extra element of fairness to the grading. Therefore, make sure your name doesn't appear on your actual submission. When you submit via Moodle, it will know you are. Thanks!

1 Overview

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.

2 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:

  • altitude: how far the ship is from the surface of the moon, in meters. Initial value: 1000 meters.
  • velocity: how fast the ship is moving, in meters/second. A positive velocity means that that ship is moving toward the moon's surface. A negative velocity means that the ship is moving away. Initial value: 40 meters/second.
  • fuel: how many units of fuel are left in the tank. Initial value: 25 units.

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

  • a constructor that initializes all your instance variables
  • getVelocity() returns the velocity your lander
  • getAltitude() returns the altitude of your lander
  • getFuel() returns the units of fuel left in your lander
  • thrust() applies one unit of fuel to give thrust to the ship, and decreases the velocity by 4 meters/sec
  • doOneSecond(fuelUnits) simulates one complete second of activity, where the astronaut has requested to burn fuelUnits units of fuel. Specifically, it should do the following:
    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 minus 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.

3 Parts 1 and 2

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

  • LunarLander constructor, getVelocity, getAltitude, and getFuel methods
  • main that creates a LunarLander object, and uses its methods in some way (such as printing out altitude, velocity, and fuel to the screen)

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.

4 Design and style

Make sure that your program follows good design and style. See the guidelines from recent assignments. If you really want to learn lots about Python style, the official style guide for Python is a great read.

5 The last point

If you complete the above functions (and other things like style, etc. are correct), you will receive nearly all of the points for this assignment. You should feel proud and good about yourself that you have gotten this far, and feel free to stop here! If you want to try to earn the remaining 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.

6 Hints and suggestions

  • Landing without crashing is tricky. To test your program, make the initial altitude smaller and the initial fuel amount bigger. This makes the game quicker to play and easier to win.
  • Any numbers that you see scattered throughout this assignment that are not used for initializing the lander (gravity increases velocity by 2 meters/second, 4 meters/second or less to land successfully, etc.) should be stored as instance variables in your program.
  • It is useful to be able to automatically exit the program, rather than having to wait until a successful land or crash. Set up your program so that if the player enters -1 for thrust, the game automatically ends. (The function exit() can be used to automatically exit your program.)

7 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!

8 Submit your work

When finished, zip up your code and submit your work through Moodle.

Good luck, and have fun! Remember that lab assistants are available in the evenings in CMC 102 and CMC 306 to help out if you need it, and you can attend prefect sessions as well.

Author: Dave Musicant

Emacs 24.5.1 (Org mode 8.2.10)

Validate