Assignment 7
Type: Group Assignment
Due: by 10:00 PM on Tuesday, October 30, 2018
- Prologue
- Overview
- LunarLander Class
- Main Function
- Documentation
- Hints and Suggestions
- Sample Simulation
- How to Turn in Your Code
Prologue
This is a group assignment. Be sure to review our Code of Conduct and be careful to adhere to it when working on the assignment. Keep in mind that you both must be authors of all code submitted and are strongly encouraged to only work on the assignment when you are with your partner.
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.
LunarLander Class
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
function controls how the simulation works.
Instance Variables
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.
-
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.
- fuel: how many units of fuel are left in the tank.
Constructor and Methods
Your LunarLander
class should have the following constructor and methods (you may have more if you wish):
- a default constructor that takes no parameters and initializes all the instance variables to their default values which are:
- altitude: 1000 meters
- velocity: 40 meters/second
- fuel: 25 units
-
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:- 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. - For each of unit of fuel that you burn, call the
thrust()
method to decrease your velocity. - 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.
- Expend
Main Function
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.
Documentation
Detailed docstrings should be provided for all of the following:
- For the
lunarlanding.py
file: At the very top of the file, include a short description of the file and listing of the authors. - For the
LunarLanding
class:- A short description of the class and a longer description that describes what its instance variables are.
- Full documentation for all of its methods, including a short description of the method, parameters, return value, preconditions, and postconditions.
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.)
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!
How to Turn in Your Code
a. Make sure that you and your partner are clearly identified at the top of every file you have created for this assignment.
b. All of your files need to be placed into the Hand-in/USERNAME/assignment7
directory. You can do this visually in Finder
by copying all of your files in your StuWork/USERNAME/assignment7
directory into your Hand-in/USERNAME/assignment7
directory. (Note that you only need to do this once—it is not necessary for every member of your group to submit separately.)
c. Send all of your files to your partner so they have access to them, too!
- Acknowledgments
- This assignment was originally written by Dave Musicant and used with permission. Later modifications were added by Titus Klinge.