Skip to content

Latest commit

 

History

History
175 lines (126 loc) · 4.63 KB

File metadata and controls

175 lines (126 loc) · 4.63 KB

Appendix A: Mars Lander

This is some code to show you how you might write a simple Mars lander simulation in Python. It’s taken from history, way back in the 1970’s - this idea was passed around as some of the very first open source.

Meant as an example of a longer program (159 lines) to get you thinking, it’s really not very complicated. The general idea is you have a series of "burns" in a list, and the game (or simulator, if you will) steps through the list applying each burn. If you run out of altitude (or height) while you’re going too fast, you will crash.

The tricky bit would be for you to figure out what burn_array would be used to safely land at a vehicle speed 1 or 2. That could be hard.

# Mars Lander Source Code.
import math
import random

GRAVITY = 100
# The rate in which the spaceship descents in free fall (in ten seconds)

version = "1.2" # The Version of the program

# various end-of-game messages.
dead = "\nThere were no survivors.\n\n"
crashed = "\nThe Spaceship crashed. Good luck getting back home.\n\n"
success = "\nYou made it! Good job!\n\n"
emptyfuel = "\nThere is no fuel left. You're floating around like Wheatley.\n\n"

def randomheight():
    # start from a random altitude
    max = 20000
    min = 10000
    r = math.floor(random.random() * (max - min)) + min
    return (r % 15000 + 4000)


def game_header():
    s = ""
    s = s + "\nMars Lander - Version " + version + "\n"
    s = s + "This is a computer simulation of an Apollo Mars landing capsule.\n"
    s = s + "The on-board computer has failed so you have to land the capsule manually.\n"
    s = s + "Set burn rate of retro rockets to any value between 0 (free fall) and 200\n"
    s = s + "(maximum burn) kilo per second. Set burn rate every 10 seconds.\n"
    # /* That's why we have to go with 10 second-steps. */
    s = s + "You must land at a speed of 2 or 1. Good Luck!\n\n"
    return s


def get_header():
    s = ""
    s = s + "\nTime\t"
    s = s + "Speed\t\t"
    s = s + "Fuel\t\t"
    s = s + "Height\t\t"
    s = s + "Burn\n"
    s = s + "----\t"
    s = s + "-----\t\t"
    s = s + "----\t\t"
    s = s + "------\t\t"
    s = s + "----\n"
    return s



def compute_delta_v(vehicle):
    return (vehicle['Speed'] + GRAVITY - vehicle['Burn'])


def check_status(vehicle):
    s = ""
    if (vehicle['Height'] <= 0):
        if (vehicle['Speed'] > 10):
            s = dead

        if (vehicle['Speed'] < 10 and vehicle['Speed'] > 3):
            s = crashed

        if (vehicle['Speed'] < 3):
            s = success
    else:
        if (vehicle['Height'] > 0):
            s = emptyfuel


    return s


def adjust_for_burn(vehicle):
    # save previous_height
    vehicle['PrevHeight'] = vehicle['Height']
    # compute new velocity
    vehicle['Speed'] = compute_delta_v(vehicle)
    # compute new height of vehicle
    vehicle['Height'] = vehicle['Height'] - vehicle['Speed']
    # subtract fuel used from tank
    vehicle['Fuel'] = vehicle['Fuel'] - vehicle['Burn']


def still_flying():
    return (vehicle['Height'] > 0)


def out_of_fuel(vehicle):
    return (vehicle['Fuel'] <= 0)


def get_status(vehicle):
    # create a string with the vehicle status on it.
    s = ""
    s = str(vehicle['Tensec']) + "0 \t\t" + str(vehicle['Speed']) \
        + " \t\t" + str(vehicle['Fuel']) + " \t\t" + str(vehicle['Height'])
    return s


def print_string(string):
    # print long strings with new lines the them.
    # a = string.split(/\r?\n/)
    #for (i = 0 i < a.length i++):
    #    print(a[i])
    print(string)



# this is initial vehicle setup
vehicle = {
    'Height': 8000,
    'Speed': 1000,
    'Fuel': 12000,
    'Tensec': 0,
    'Burn': 0,
    'PrevHeight': 8000,
    'Step': 1,
}


# main game loop
def run_game(burns):
    status = ""

    # Set initial vehicle parameters
    h = randomheight()
    vehicle['Height'] = h
    vehicle['PrevHeight'] = h

    burn_idx = 0

    print_string(game_header())
    print_string(get_header())

    while (still_flying() == True):
        status = get_status(vehicle)
        vehicle['Burn'] = burns[burn_idx]
        print_string(status + "\t\t" + str(vehicle['Burn']))
        adjust_for_burn(vehicle)
        if (out_of_fuel(vehicle) == True):
            break

        vehicle['Tensec'] += 1
        burn_idx += 1



    status = check_status(vehicle)
    print_string(status)


# these are the series of burns made each 10 secs by the lander.
# change them to see if you can get the lander to make a soft landing.
# burns are between 0 and 200. This burn array usually crashes.
burn_array = [100, 100, 200, 200, 100, 100, 0, 0, 200, 100, 100, 0, 0, 0, 0]

run_game(burn_array)