Launch rocket with countdown and acceleration in stdout: Difference between revisions
Launch rocket with countdown and acceleration in stdout (view source)
Revision as of 09:49, 17 December 2023
, 5 months ago→{{header|Wren}}: Changed to Wren S/H
Not a robot (talk | contribs) (Add 8080 assembly) |
m (→{{header|Wren}}: Changed to Wren S/H) |
||
(6 intermediate revisions by 5 users not shown) | |||
Line 1:
{{draft task}}
;Task
Simulate the countdown of a rocket launch from '''5''' down to '''0''' seconds, and then display the moving, accelerating rocket on the standard output device as a simple ASCII art animation.
<br><br>
=={{header|8080 Assembly}}==
Line 17:
In SIMH, the command <code>d clock 2000</code> might help.
<
lxi d,rocket ; Clear screen and print rocket
mvi c,9
Line 100:
count: db ' _____ '
number: db '5 _____',13,'$'
newln: db 13,10,'$'</
=={{header|FreeBASIC}}==
<
dim as string rocket(1 to 7) = { " ^",_
" / " + chr(92),_
Line 149:
cls
dt = timer - t
wend</
=={{header|Go}}==
Line 155:
<br>
...though my rocket is a bit fancier :)
<
import (
Line 208:
}
}
}</
=={{header|Julia}}==
<
" | |\n /____\\\n | |\n |SATU|\n | |\n",
" | |\n /| | |\\\n / | | | \\\n /__|_|__|__\\\n /_\\/_\\\n")
Line 234:
testrocket()
</
<pre>
Countdown...T minus 5... 4... 3...
Line 252:
/_\/_\
</pre>
=={{header|Nim}}==
{{trans|Julia}}
Using <code>terminal</code> module from standard library rather the escape codes.
<syntaxhighlight lang="nim">import os, math, terminal
proc rocket() =
echo " /..\\\n |==|\n | |\n | |\n",
" | |\n /____\\\n | |\n |SATU|\n | |\n",
" | |\n /| | |\\\n / | | | \\\n /__|_|__|__\\\n /_\\/_\\\n"
proc exhaust() =
echo " *****"
proc countDown(secs: Natural) =
stdout.write "Countdown...T minus "
stdout.flushFile
for i in countdown(secs, 1):
stdout.write i, "... "
stdout.flushFile
os.sleep(1000)
stdout.write "LIFTOFF!"
stdout.flushFile
proc engineBurn(rows: Natural) =
echo '\n'
for i in 1..rows:
exhaust()
sleep (0.9^i * 1000).toInt
proc testRocket() =
eraseScreen()
rocket()
cursorUp(16)
countDown(5)
cursorDown(13)
engineBurn(30)
testRocket()</syntaxhighlight>
{{out}}
Same as Julia program output.
=={{header|Pascal}}==
Pascal includes the standard routine <tt>page</tt>.
However its specific behavior is implementation-defined, i. e. up to the compiler vendor.
''An'' implementation of <tt>page</tt> ''could'', for instance, translate into <tt>\014</tt> (ASCII form feed) or <tt>\033[2J\033[H</tt>, the ANSI escape sequence blanking the entire screen and moving the cursor to the upper left-hand corner.
If the terminal interprets them properly, the following <tt>program</tt> could satisfy the task’s requirements:
<syntaxhighlight lang="pascal">program launchRocketWithCountdownAndAccelerationOnOutput(output);
const
screenWidth = 80;
type
wholeNumber = 0..maxInt;
natural = 1..maxInt;
var
n: wholeNumber;
{
Pascal, as defined by ISO standard 7185, does not provide any
facilities to time actions. Nevertheless, most compiler vendors
invented their own `sleep` or `delay` procedures.
}
procedure sleep(n: natural);
begin
{ Yes, in Pascal an empty statement is valid. }
{ There is really nothing after `do`. }
for n := n downto 1 do
end;
procedure drawRocket(column: natural);
begin
{ `page` is shorthand for `page(output)`, }
{ as is `writeLn(…)` shorthand for `writeLn(output, …)`. }
page;
{ It should be an easy feat to adapt this to contain ASCII only. }
writeLn(' ':column, '🙮')
end;
{ === MAIN ============================================================= }
begin
for n := 5 downto 0 do
begin
drawRocket(1);
writeLn;
writeLn(n);
sleep(5318008)
end;
n := 1;
while n < screenWidth do
begin
n := round(n * 1.5);
sleep(58008);
drawRocket(n)
end
end.</syntaxhighlight>Note, conventionally main engines (liquid fuel) are ignited a few seconds prior liftoff and SRBs are ignited just at/before liftoff, so the graphical depiction of exhaust visible at T−5 is ''about'' accurate.
=={{header|Perl}}==
{{trans|Raku}}
<
use warnings;
use Time::HiRes qw(sleep);
Line 328 ⟶ 427:
# clean up on exit, reset ANSI codes, scroll, re-show the cursor & clear screen
sub clean_up { print "\e[0m", ("\n")x50, "\e[H\e[J\e[?25h"; exit(0) }</
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">-->
<span style="color: #004080;">sequence</span> <span style="color: #000000;">rocket</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">split</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"""
| |
/
/_||||_\
"""</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">lines</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">l</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">t</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">10</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0.25</span>
<span style="color: #008080;">while</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rocket</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">t</span><span style="color: #0000FF;">></span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">rocket</span><span style="color: #0000FF;">[$]</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"T minus %d... "</span><span style="color: #0000FF;">,</span><span style="color: #000000;">t</span><span style="color: #0000FF;">)</span>
<span style="color: #000080;font-style:italic;">-- allow console resize during countdown:</span>
<span style="color: #000000;">lines</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">video_config</span><span style="color: #0000FF;">()[</span><span style="color: #004600;">VC_SCRNLINES</span><span style="color: #0000FF;">]</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">l</span><span style="color: #0000FF;">!=</span><span style="color: #000000;">lines</span><span style="color: #0000FF;">-</span><span style="color: #000000;">7</span> <span style="color: #008080;">then</span> <span style="color: #000000;">l</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">else</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">l</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">rocket</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">rocket</span><span style="color: #0000FF;">[</span><span style="color: #000000;">2</span><span style="color: #0000FF;">..$]</span>
<span
<span style="color: #000000;">l</span> <span style="color: #0000FF;">-=</span> <span style="color: #0000FF;">(</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rocket</span><span style="color: #0000FF;">)></span><span style="color: #000000;">6</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rocket</span><span style="color: #0000FF;">)<</span><span style="color: #000000;">12</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">rocket</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rocket</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" ** "</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">elsif</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rocket</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">12</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">rocket</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rocket</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" "</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">s</span> <span style="color: #0000FF;">*=</span> <span style="color: #000000;">0.95</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">l</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
<span style="color: #7060A8;">clear_screen</span><span style="color: #0000FF;">()</span>
<span style="color: #7060A8;">cursor</span><span style="color: #0000FF;">(</span><span style="color: #004600;">NO_CURSOR</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">l</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">lines</span><span style="color: #0000FF;">-</span><span style="color: #000000;">7</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #7060A8;">position</span><span style="color: #0000FF;">(</span><span style="color: #000000;">l</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rocket</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">))</span>
<span style="color: #7060A8;">sleep</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">t</span> <span style="color: #0000FF;">-=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">t</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span> <span style="color: #000000;">rocket</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">rocket</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..$-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #7060A8;">cursor</span><span style="color: #0000FF;">(</span><span style="color: #004600;">BLOCK_CURSOR</span><span style="color: #0000FF;">)</span>
<!--</syntaxhighlight>-->
=={{header|Racket}}==
Line 379 ⟶ 480:
{{trans|Go}}
<
(define rocket #<<EOF
Line 411 ⟶ 512:
(print-rocket n)
(sleep (/ ms 1000))
(if (>= ms 40) (- ms 40) 0))</
=={{header|Raku}}==
Line 422 ⟶ 523:
The motion is a little "notchy" as the vertical resolution in a terminal is rather low. Exits after the rocket leaves the visible area of the terminal. See the [https://github.com/thundergnat/rc/blob/master/img/rocket-perl6.gif example animated gif]
<syntaxhighlight lang="raku"
my ($rows,$cols) = qx/stty size/.words;
Line 489 ⟶ 590:
# clean up on exit, reset ANSI codes, scroll, re-show the cursor & clear screen
sub cleanup () { print "\e[0m", "\n" xx 50, "\e[H\e[J\e[?25h"; exit(0) }</
{{out|Sample output}}
Line 496 ⟶ 597:
=={{header|REXX}}==
This REXX program hard-codes the name of the (OS) command to clear the terminal screen ('''CLS''').
<
parse arg cntDown . /*obtain optional argument from the CL.*/
if cntDown=='' | cntDown=="," then cntDown= 5 /*Not specified? Then use the default.*/
Line 536 ⟶ 637:
/*──────────────────────────────────────────────────────────────────────────────────────*/
sky: do air; say; end /*air*/; return /*display the sky above the rocket. */
rocket: do ship=1 for rs; say left('', sw%2 - 5) @.ship; end /*ship*/; return</
This REXX program makes use of '''SCRSIZE''' REXX program (or
BIF) which is used to determine the screen
Line 546 ⟶ 647:
=={{header|Rust}}==
<
use std::{thread, time};
Line 590 ⟶ 691:
}
</syntaxhighlight>
=={{header|Wren}}==
{{trans|Go}}
<
var rocket = "
Line 633 ⟶ 734:
Timer.sleep(ms)
ms = (ms >= 40) ? ms - 40 : 0
}</
=={{header|zkl}}==
{{trans|Go}}
Uses ANSI terminal codes.
<
#<<<
0'~
Line 669 ⟶ 770:
ms=(ms - 0.04).max(0); // 40 milliseconds faster than last time
}
}</
{{out}}
<pre>
|