It's raining in my terminal!
MATL, 52 bytes
xxx:"1GY.Xx2e3Z@25eHG>~47*cD]Xx12:~c!3G80yn-H/kZ"why
Inputs are, in this order: pause between updates, number of drops, message, number of repetitions. The monitor has size 80×25 characters (hard-coded).
GIF or it didn't happen! (Example with inputs 0.2
, 100
, 'THE END'
, 30
)
Or try it at MATL Online.
Explanation
xxx % Take first three inputs implicitly and delete them (but they get
% copied into clipboard G)
:" % Take fourth input implicitly. Repeat that many times
1G % Push first input (pause time)
Y. % Pause that many seconds
Xx % Clear screen
2e3 % Push 2000 (number of chars in the monitor, 80*25)
Z@ % Push random permutation of the integers from 1 to 2000
25e % Reshape as a 25×80 matrix, which contains the numbers from 1 to 2000
% in random positions
HG % Push second input (number of drops)
>~ % Set numbers that are <= second input to 1, and the rest to 0
47*c % Multiply by 47 (ASCII for '/') and convert to char. Char 0 will
% be displayed as a space
D % Display
] % End
Xx % Clear screen
12:~ % Push row vector of twelve zeros
c! % Convert to char and transpose. This will produce 12 lines containing
% a space, to vertically center the message in the 25-row monitor
3G % Push third input (message string)
80 % Push 80
yn % Duplicate message string and push its length
- % Subtract
H/k % Divide by 2 and round down
Z" % Push string of that many spaces, to horizontally center the message
% in the 80-column monitor
w % Swap
h % Concatenate horizontally
y % Duplicate the column vector of 12 spaces to fill the monitor
% Implicitly display
JavaScript (ES6), 268 261 bytes
t=
(o,f,d,r,m,g=(r,_,x=Math.random()*78|0,y=Math.random()*9|0)=>r?g(r-(d[y][x]<`/`),d[y][x]=`/`):d.map(a=>a.join``).join`
`)=>i=setInterval(_=>o.data=f--?g(r,d=[...Array(9)].map(_=>[...` `.repeat(78)])):`
`+` `.repeat(40-m.length/2,clearInterval(i))+m,d*1e3)
<input id=f size=10 placeholder="# of frames"><input id=d placeholder="Interval between frames"><input id=r size=10 placeholder="# of raindrops"><input id=m placeholder="End message"><input type=button value=Go onclick=t(o.firstChild,+f.value,+d.value,+r.value,m.value)><pre id=o>
At least on my browser, the output is designed to fit into the Stack Snippet area without having to go "Full page", so if you ask for more than 702 raindrops it will crash.
Edit: Saved 7 bytes by using a text node as my output area.
R, 196 192 185 bytes
Just a mock version I wrote based on the description. Hopefully it's somewhat what OP was looking for.
Saved some bytes thanks to @plannapus.
f=function(w,t,m,n){for(i in 1:t){x=matrix(" ",100,23);x[sample(2300,n)]="/";cat("\f",rbind(x,"\n"),sep="");Sys.sleep(w)};cat("\f",g<-rep("\n",11),rep(" ",(100-nchar(m))/2),m,g,sep="")}
The arguments:
w
: Wait time between framest
: Total number of framesm
: Custom messagen
: Number of rain drops
Example
Why does it look like it's raining upwards?
Edit: I should mention that this is my customized 23x100 character R-studio console. The dimensions are hardcoded into the function but one could in principle use getOption("width")
to make it flexible to console size.
Ungolfed and explained
f=function(w,t,m,n){
for(i in 1:t){
x=matrix(" ",100,23); # Initialize matrix of console size
x[sample(2300,n)]="/"; # Insert t randomly sampled "/"
cat("\f",rbind(x,"\n"),sep=""); # Add newlines and print one frame
Sys.sleep(w) # Sleep
};
cat("\f",g<-rep("\n",11),rep(" ",(100-nchar(m))/2),m,g,sep="") # Print centered msg
}