function simulation() { var v = -1000; var s = 50000; var fuel = 10000; var schub = false; update(); function switchOn() { update(); schub = true; } function switchOff() { update(); schub = false; } function a() { if (schub == false || fuel <= 0) return -1.63; else { fuel = fuel - 100; return -1.63 + 12; } } function update() { if (checkGameOver() == true) { setStartValues(); showCurrentValues() } else if ((checkGameOver() == false) && (s < 0) && (v > -10)) { setStartValues(); showCurrentValues(); } else { v = v + a(); s = s + v; showCurrentValues(); } } function checkGameOver() { if (s < 0) if (v < -10) { alert("Zerschellt. Game Over"); return true; } else { return false; } else { return false; } } function setStartValues() { v = -1000; s = 50000; fuel = 10000; schub = false; return; } function showCurrentValues() { $("#height").html("Höhe: " + s + " m"); $("#speed").html("Geschwindigkeit: " + v + " m/s"); $("#fuel").html("Treibstoffvorrat: " + fuel + " l"); return; } $("body").append("
Höhe:
"); $("body").append("
Geschwindigkeit:
"); $("body").append("
Treibstoffvorrat:
"); $("body").append(""); $("body").append(""); $("#energy").click(switchOn); $("#no-energy").click(switchOff); } $(document).ready(simulation);