Lunar Lander Text Game - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: General (https://python-forum.io/forum-1.html) +--- Forum: Code sharing (https://python-forum.io/forum-5.html) +--- Thread: Lunar Lander Text Game (/thread-26765.html) |
Lunar Lander Text Game - hendry1 - May-13-2020 Making the classic Lunar Lander game... But with a twist! Instead of using pygame or other graphics, I went completely old-school! Hope you enjoy! # PLEASE CHANGE THE SPEED, FUEL, AND ALTITUDE VARIABLES TO YOUR LIKING # you are the pilot and need to control how much fuel you burn in the # retro rockets so that your descent speed slows to zero just as your # altitude above the moon's surface reaches zero. If you impact the moon # more than 5 m below the surface, or your speed on impact is # greater than 5 m/s then you have considered to have crashed. # else, it is considered to be a 'done' landing. # If you run out of fuel, the spaceship will accelerate towards the moon by gravity. # set up the initial parameters speed = 20 # speed approaching the moon fuel = 1500 # how much fuel is left altitude = 100 # altitude above moon gravity = 1 # acceleration due to gravity burn = 0 # initial rate of burning fuel in rockets # while ship is above the moon's surface, # calculate flight data and take input from user while altitude > 0: # calculate how long until ship will impact moon at current speed (impact) if speed <= 0: impact = 1000 else: impact = altitude / speed # display flight data print('Altitude = ', altitude, 'Speed = ', speed, 'Fuel = ', fuel, 'Impact = ', impact, 'Previous burn', burn) burn = float(input("Enter fuel to burn (0-50)?")) # ensure rate of fuel burning is within rocket's capability and doesn't exceed remaining fuel if burn < 0: burn = 0 if burn > 50: burn = 50 if burn > fuel: burn = fuel #calculate new flight data altitude -= speed speed += gravity - burn/10 fuel -= burn # Hit moon surface # display good landing or not print('Altitude = ', altitude, 'Speed = ', speed, 'Fuel = ', fuel, 'Impact = ', impact, 'Previous burn', burn) if altitude <- 5 or speed > 5: print("You have crashed") else: print("You have landed") RE: Lunar Lander Text Game - TomToad - May-17-2020 Nice. took 3 tries to not crash. Reminds me of this BASIC game from 1979. |