Intergalactic Games

There’s fierce competition among the world’s TV companies for exclusive coverage of the First Intergalactic Games. Everything depends on which company wins the race to put satellite into orbit at the right height. You are the engineer in charge of the launch for New Century TV. The crucial decisions about the angle and speed of the launching rocket rests on your shoulders. Can you do it?

The code

# import randint, so we can get a random integer
# import atan and pi, so we can calculate the correct angle
# import sqrt, so we can calculate the correct speed
from random import randint
from math import atan, pi
from math import sqrt


# Set up the number of guesses we're going to get
guesses = 8

# Create a flag to keep track if we've won
won_game = False

print("INTERGALACTIC GAMES")

# Chooses the height to which you must launch your satellite,
# puts the value in height, and prints it.
height = randint(1, 100)
print("You must launch a satellite to a height of", height)

# As long as we have guesses left and haven't won yet, we'll keep trying
while guesses:
    guesses -= 1

    # Asks you for an angle and puts it in angle
    angle = int(input("Enter an angle (0-90): "))

    # Asks you for a speed and puts it in speed
    speed = int(input("Enter a speed (0-40000): "))

    # Subtracts the correct angle from your guess to determine
    # how close you were and stores the value in angle_diff
    angle_diff = angle - (atan(height / 3) * 180 / pi)

    # Subtracts the correct speed from your guess to determine
    # how close you were and stores the value in speed_diff
    speed_diff = speed - (3000 * sqrt(height + 1 / height))

    # Checks if you were close enough to win
    if abs(angle_diff) < 2 and abs(speed_diff) < 100:
        won_game = True
        break

    # Prints appropriate messages to help you with your next guess
    if angle_diff < -2:
        print("Too shallow.")
    elif angle_diff > 2:
        print("Too steep.")

    if speed_diff < -100:
        print("Too slow.")
    elif speed_diff > 100:
        print("Too fast.")
    print()

if won_game:
    print("You've done it!")
    print("NCTV WINS--thanks to you!")
else:
    print("You've failed.")
    print("You're fired!")

Adding to the program

These changes will make the computer give you bonus points depending on how quickly you make a successful launch.

After line 41, insert:

if guesses:
    bonus = 1000 * guesses
    print("You've earned a bonus of", bonus, "credits")

Puzzle corner

Can you change the program so that, if you win, it automatically goes back for another game, adding the bonus you’ve already earned to any new bonus? See how long you can play before and NCTV fires you.