'Lunar Lander Simulation [ LUNSIM.BAS ] SCREEN 12 ' graphical screen 640 x 480 pixels CLS ' clear screen LOCATE 6, 28 'screen location ROW , COLUMN COLOR 9 PRINT "LUNAR LANDING SIMULATOR"; 'heading LOCATE 9, 28 'set up screen for parameter print COLOR 7 PRINT "Time seconds"; LOCATE 11, 28 COLOR 14 PRINT "Fuel %"; LOCATE 13, 28 COLOR 6 PRINT "Height km"; LOCATE 15, 28 COLOR 12 PRINT "Velocity m/s"; LOCATE 25, 1 'print instructions COLOR 7 PRINT " to start engine on engine off to end " 'define initial parameters start = 0 'start flag - must press 's' to start engine = 0 'thrust accel - currently zero (engine off) gm = -1.62 'lunar gravitational acceleration ht = 10000 '10 km = 10,000 metres fuel = 100 '100% fuel esc$ = CHR$(27) 'escape key code 'main simulation loop DO a$ = INKEY$ 'sample keyboard for keypress IF a$ = "s" THEN start = 1: t0 = TIMER: lastime = t0 ' start simulation IF start = 1 THEN 'if simulation is started IF a$ = "o" THEN 'turn engine on engine = 10: etime = TIMER 'engine acceleration = 10 m/s/s LOCATE 17, 33 COLOR 10 PRINT " ENGINE ON "; END IF IF a$ = "f" THEN 'turn engine off LOCATE 17, 33 PRINT " "; engine = 0 END IF IF a$ = esc$ THEN EXIT DO 'get out of simulation nowtime = TIMER 'computer real-time clock elt = nowtime - t0 'elapsed time since start deltat = nowtime - lastime 'loop time increment - 'Delta-t' lastime = nowtime IF fuel <= 0 THEN engine = 0 'engine cannot fire without fuel accel = gm + engine 'add gravity to acceleration vel = vel + accel * deltat 'compute new velocity ht = ht + vel * deltat + .5 * accel * deltat * deltat 'and new height IF ht < 1605 THEN engine = 10 fuel = fuel - engine * deltat / 5 'and remaining fuel GOSUB Printparams 'print newly computed parameters IF engine > 0 THEN SOUND 37, 1 'make an engine noise if engine on IF ht < 0 THEN EXIT DO 'stop if we have hit the surface END IF LOOP 'end of simulation - either from 'ESC' or hit lunar surface IF ht < 0 THEN 'if hit lunar surface then print end messages CLS 'clear screen for final messages COLOR 14 LOCATE 12, 20 PRINT USING "LUNAR CONTACT AT ### secs at ####.# m/s"; elt; vel LOCATE 14, 20 PRINT USING " FUEL REMAINING = ### %"; fuel LOCATE 18, 20 IF vel > -5 THEN COLOR 15 PRINT " SOFT LANDING - AOK" ELSE COLOR 12 PRINT " !!CRASH LANDING -- FATAL!!" END IF DO LOOP WHILE INKEY$ <> esc$ 'wait for escape to end END IF END Printparams: 'parameter print subroutine LOCATE 9, 36 COLOR 7 PRINT USING "###.#"; elt; 'print elapsed time in secs LOCATE 11, 38 COLOR 14 PRINT USING "###"; fuel; 'print % fuel left LOCATE 13, 38 COLOR 6 PRINT USING "##.###"; ht / 1000; 'print height in km LOCATE 15, 39 COLOR 12 PRINT USING "####"; vel; 'print velocity in m/s RETURN