Launch rocket with countdown and acceleration in stdout: Difference between revisions
(→{{header|REXX}}: aligned a comment, added a programming note about the use of the BIF SCRSIZE.) |
m (→{{header|REXX}}: used a better acceleration computation.) |
||
Line 99: | Line 99: | ||
cls /*use this command to clear the screen.*/ |
cls /*use this command to clear the screen.*/ |
||
call sky /*display the sky above the rocket. */ |
call sky /*display the sky above the rocket. */ |
||
period= 1 |
|||
dt= period / sd /*acceleration (period is decreasing).*/ |
|||
call rocket /*display the rocket (in flight). */ |
call rocket /*display the rocket (in flight). */ |
||
do sd+4; say /*"make" the rocket appear to fly. */ |
|||
period= format(period-period*dt,,3) /*calculate the decrease in the period.*/ |
|||
call delay max(period, .001) /*wait for a diminishing time interval.*/ |
|||
end /*sd+4*/ |
|||
exit /*stick a fork in it, da rocket is gone*/ |
exit /*stick a fork in it, da rocket is gone*/ |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
Revision as of 17:54, 6 August 2019
- Task Description
The task is to 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.
Go
...though my rocket is a bit fancier :)
<lang go>package main
import (
"fmt" "time"
)
const rocket = `
/\ ( ) ( ) /|/\|\ /_||||_\
`
func printRocket(above int) {
fmt.Print(rocket) for i := 1; i <= above; i++ { fmt.Println(" ||") }
}
func cls() {
fmt.Print("\x1B[2J")
}
func main() {
// counting for n := 5; n >= 1; n-- { cls() fmt.Printf("%d =>\n", n) printRocket(0) time.Sleep(time.Second) }
// ignition cls() fmt.Println("Liftoff !") printRocket(1) time.Sleep(time.Second)
// liftoff ms := time.Duration(1000) for n := 2; n < 100; n++ { cls() printRocket(n) time.Sleep(ms * time.Millisecond) if ms >= 40 { ms -= 40 } else { ms = 0 } }
}</lang>
REXX
This REXX program hard-codes the name of the (OS) command to clear the terminal screen (CLS). <lang rexx>/*REXX pgm does a countdown and then display the launching of a rocket (ASCII animation)*/ parse arg cntDown . /*obtain optional argument from the CL.*/ if cntDown== | cntDown=="," then cntDown= 5 /*Not specified? Then use the default.*/
@. = /* [↓] glyphs for the rocket ship. */ @.1= ' /\ ' @.2= ' | | ' @.3= ' | | ' @.4= ' | | ' @.5= ' /|/\|\ ' @.6= '/_||||_\' do rs=1 while @.rs\== /*determine size of the rocket (height)*/ end /*rs*/
rs= rs - 1 /*the true rocket size (height). */ cls= 'CLS' /*the command used to clear the screen.*/ parse value scrsize() with sd sw . sw= sw - 1 /*usable screen width on some systems. */ sd= sd - 3 /* " " depth " " " */ air= sd - 1 - rs /*"amount" of sky above the rocket. */ say
do j=cntDown by -1 to 1 /* [↓] perform countdown; show rocket.*/ cls /*use this command to clear the screen.*/ say right(j, 9) 'seconds' /*display the amount of seconds to go. */ call sky /*display the sky above the rocket. */ call rocket /*display the rocket (on the ground). */ call delay 1 /*wait one second during the countdown.*/ end /*j*/
say left(,9) "liftoff!" /*announce liftoff of the rocket. */ cls /*use this command to clear the screen.*/ call sky /*display the sky above the rocket. */ period= 1 dt= period / sd /*acceleration (period is decreasing).*/ call rocket /*display the rocket (in flight). */
do sd+4; say /*"make" the rocket appear to fly. */ period= format(period-period*dt,,3) /*calculate the decrease in the period.*/ call delay max(period, .001) /*wait for a diminishing time interval.*/ end /*sd+4*/
exit /*stick a fork in it, da rocket is gone*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
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</lang>
This REXX program makes use of SCRSIZE REXX program (or
BIF) which is used to determine the screen
width and depth of the terminal (console). Some REXXes don't
have this BIF.
The SCRSIZE.REX REXX program is included
here ───► SCRSIZE.REX.
Rust
<lang rust> use std::{thread, time};
fn print_rocket(above: u32) { print!( " oo
oooo oooo oooo
"); for _num in 1..above+1 {
println!(" ||");
} }
fn main() {
// counting for number in (1..6).rev() { print!("\x1B[2J"); println!("{} =>", number); print_rocket(0);
let dur = time::Duration::from_millis(1000);
thread::sleep(dur); }
// ignition print!("\x1B[2J"); println!("Liftoff !"); print_rocket(1); let dur = time::Duration::from_millis(1000); thread::sleep(dur);
// liftoff let mut dur_time : u64 = 1000; for number in 2..100 { print!("\x1B[2J"); print_rocket(number);
let dur = time::Duration::from_millis(dur_time);
thread::sleep(dur);
dur_time -= if dur_time >= 30 {30} else {dur_time};
}
}
</lang>