Aufgabe 4: eine Ampel am richtigen Ort
Das Auto soll bei rot langsamer fahren, wenn es sich in einem Bereich bis zu 100 Pixeln vor der Ampel befindet. Direkt vor einer roten Ampel soll es natürlich stehen bleiben.
Lösung als Struktogramm und im Quellcode
|
|
repeat
if Ampel1.AmpelFarbe = gruen then
Auto1.fahre(1);
else
begin
if (Auto1.xPosVorne > Ampel1.xPos - 1) and
(Auto1.xPosVorne <= Ampel1.xPos) then
begin
// tue nix
end
else
begin
if (Auto1.xPosVorne > Ampel1.xPos - 100) and
(Auto1.xPosVorne < Ampel1.xPos - 1) then
Auto1.fahre(0.2)
else
Auto1.fahre(1);
end
end;
if Auto1.xPosHinten > derBildschirm.breite then
Auto1.setzePosition(-100, Auto1.yPosVorne);
if dieTastatur.wurdegedrueckt then
begin
dieTastatur.weiter;
Ampel1.schalte;
end;
dieUhr.warte(1);
Until dieMaus.istGedrueckt;
|
Erläuterungen
Die Spitze des Autos ist weiter rechts als 100 Pixel vor der Ampel und gleichzeitig (das heißt and) weiter links als 1 Pixel vor der Ampel. |
begin
if (Auto1.xPosVorne > Ampel1.xPos - 100) and
(Auto1.xPosVorne < Ampel1.xPos - 1) then
Auto1.fahre(0.2)
else
Auto1.fahre(1);
end
|
if Auto1.xPosVorne = Ampel1.xPos then |
if (Auto1.xPosVorne > Ampel1.xPos - 1) and
(Auto1.xPosVorne <= Ampel1.xPos) then
|
| Zur
nächsten Seite |
| © 2004 GK 11 If und G. Kubitz Hannah-Arendt-Gymnasium, Lengerich Letztes Update: 17.11.04 |