Matrix digital rain: Difference between revisions

m
Uploaded a new screenshot.
m (Removed erroneously inserted code)
m (Uploaded a new screenshot.)
 
(45 intermediate revisions by 10 users not shown)
Line 8:
=={{header|Ada}}==
Should work with ANSI terminals.
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO;
with Ada.Numerics.Discrete_Random;
with Ada.Strings.Fixed;
Line 100:
Put (ESC & "[49m"); -- Default backgound
Put (ESC & "[2J"); -- Clear Terminal
end Digital_Rain;</langsyntaxhighlight>
 
=={{header|Amazing Hopper}}==
{{Trans|BASIC}}
<p>VERSION 1:</p>
<syntaxhighlight lang="c">
#include <basico.h>
 
#proto desplegarmatrixrain(_X_,_Y_,_Z_,_W_)
 
algoritmo
permitir interrupción
dimensionar (80) matriz aleatoria entera ( -50, t )
 
y=0
 
color(232,232)
borrar pantalla
enfatizado
iterar
 
iterar para(i=1, #(i<=80), ++i )
cuando ( #(t[i]>50) ){ #(t[i]=0) }
#basic{
t[i] += 1
y = t[i]
desplegar matrix rain(232,232,i,(y-10))
desplegar matrix rain(22,232,i,(y-9))
desplegar matrix rain(22,232,i,(y-8))
desplegar matrix rain(22,232,i,(y-7))
desplegar matrix rain(28,232,i,(y-6))
desplegar matrix rain(28,232,i,(y-5))
desplegar matrix rain(34,232,i,(y-4))
desplegar matrix rain(34,232,i,(y-3))
desplegar matrix rain(40,232,i,(y-2))
desplegar matrix rain(46,232,i,(y-1))
desplegar matrix rain(232,40,i,y)
}
siguiente
 
microsegundos(5000)
hasta que una tecla sea presionada
color(7,0)
borrar pantalla
terminar
 
subrutinas
 
desplegar matrix rain(p,s,x,y)
si ( y, está entre-excluyendo-(0,41) )
color (p,s)
imprimir en ( y,x, #(utf8(chr(33+((x*y) % 200)) )) )
fin si
 
retornar
 
</syntaxhighlight>
{{out}}
<pre>
semejante a la imagen de la VERSION 2, pero con menos "cola"
</pre>
<p>VERSION 2:</p>
<syntaxhighlight lang="c">
#include <basico.h>
 
#proto desplegarmatrixrain(_X_,_Y_)
 
algoritmo
dimensionar con (80) matriz aleatoria entera ( -50, t )
matrices (ctexto, cfondo)
'232,22,22,22,22,28,28,28,34,34,40,40,46,232' enlistar en 'ctexto'
'232,232,232,232,232,232,232,232,232,232,232,232,232,40' enlistar en 'cfondo'
 
color(232,232)
borrar pantalla
enfatizado
iterar
 
iterar para(i=1, #(i<=80), ++i )
cuando ( #(t[i]>53) ){ #(t[i]=rand(-50)) }
#basic{
t[i] += 1
desplegar matrix rain(i,t[i])
}
siguiente
 
microsegundos(50000)
 
hasta que una tecla sea presionada
color(7,0)
borrar pantalla
terminar
 
subrutinas
 
desplegar matrix rain(x,y)
i=13, j=1
iterar
si ( #(y-i), está entre-excluyendo-(0,41) )
color ( #(ctexto[j]), #(cfondo[j]) )
imprimir en ( #(y-i),x, #(utf8(chr(33+((x*(y-i)) % 200)) )) )
fin si
--i, ++j
mientras ' no es negativo(i)'
retornar
 
</syntaxhighlight>
{{out}}
[[File:Matrix_rain_terminal_linux_2.png]]
 
=={{header|BASIC}}==
==={{header|FreeBASIC}}===
{{trans|QBasic}}
<syntaxhighlight lang="vb">Sub d(p As Ulong, s As Ulong, x As Long, y As Long)
Color p, s
If y > 0 And y < 24 Then Locate y, x: Print Chr(33 + (x * y) Mod 200);
End Sub
 
Dim As Long t(80)
For i As Integer = 1 To 80
t(i) = Int(-50 * Rnd)
Next
Dim As Double s = Timer
 
Cls
Do
For i As Integer = 1 To 80
If t(i) > 28 Then t(i) = 0
t(i) += 1
Dim As Long y = t(i)
d( 0, 0, i, y - 6)
d( 2, 0, i, y - 5)
d( 2, 0, i, y - 4)
d(10, 0, i, y - 2)
d(11, 0, i, y - 1)
d( 0, 2, i, y)
Next i
Dim As Double l = Timer
While l = Timer: Wend
Loop</syntaxhighlight>
 
==={{header|QBasic}}===
Original code programmed by nitro2k01
[https://codegolf.stackexchange.com/questions/17285/make-the-matrix-digital-rain-using-the-shortest-amount-of-code]<syntaxhighlight lang="qbasic">DECLARE SUB d (p!, s!, x!, y!)
DIM t(80)
FOR i = 1 TO 80
t(i) = INT(-50 * RND)
NEXT
s = TIMER
 
CLS
WHILE 1
FOR i = 1 TO 80
IF t(i) > 28 THEN t(i) = 0
t(i) = t(i) + 1
y = t(i)
d 0, 0, i, y - 6
d 2, 0, i, y - 5
d 2, 0, i, y - 4
d 10, 0, i, y - 3
d 10, 0, i, y - 2
d 11, 0, i, y - 1
d 0, 2, i, y
NEXT
 
l = TIMER
WHILE l = TIMER
WEND
WEND
 
SUB d (p, s, x, y)
COLOR p, s
IF y > 0 AND y < 24 THEN LOCATE y, x: PRINT CHR$(33 + (x * y) MOD 200);
END SUB</syntaxhighlight>
 
==={{header|True BASIC}}===
{{trans|QBasic}}
<syntaxhighlight lang="qbasic">SUB d (p,s,x,y)
SET COLOR p
SET BACKGROUND COLOR s
 
IF y > 0 AND y < 24 THEN
SET CURSOR y, x
PRINT CHR$(33+REMAINDER((x*y),200));
END IF
END SUB
 
DIM t(80)
FOR i = 1 TO 80
LET t(i) = INT(-50*RND)
NEXT i
LET s = TIME
 
CLEAR
DO
FOR i = 1 TO 80
IF t(i) > 28 THEN LET t(i) = 0
LET t(i) = t(i)+1
LET y = t(i)
CALL d (0, 0, i, y-6)
CALL d (2, 0, i, y-5)
CALL d (2, 0, i, y-4)
CALL d (10, 0, i, y-2)
CALL d (11, 0, i, y-1)
CALL d (0, 2, i, y)
NEXT i
LET l = TIME
DO WHILE l = TIME
LOOP
LOOP
END</syntaxhighlight>
 
=={{header|Batch File}}==
{{Works with|Windows 10}}
Adding more '''%RANDOM%''' will increase the number of columns.
This code uses Windows 10 VT100 escape sequences.
<lang dos>
<syntaxhighlight lang="dos">:: Matrix Digital Rain Task from RosettaCode
:: Batch File Implementation
 
@echo off
setlocal enabledelayedexpansion
 
rem escape character (for Windows 10 VT100 escape sequences)
rem info: https://docs.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences
for /f %%e in ('echo prompt $e^| cmd') do @set "esc=%%e"
 
rem set window size
set "col=120" %== please don't make this too large ==%
set "row=30" %== please don't make this too large ==%
mode con cols=%col% lines=%row%
 
rem set up the variables for display
set "rain_length=12"
for /l %%y in (1,1,%col%) do set "disp_col[%%y]= " %== what to display ==%
for /l %%y in (1,1,%col%) do set "ctr_col[%%y]=0" %== counter for rain length ==%
 
rem hide the cursor, and clear the screen
<nul set /p "=%esc%[?25l"
cls
 
color a
:matrix_loop
:start
for /l %%y in (1,1,%col%) do (
echo %RANDOM%%RANDOM%%RANDOM%%RANDOM%%RANDOM%%RANDOM%%RANDOM%%RANDOM%%RANDOM%%RANDOM%%RANDOM%%RANDOM%%RANDOM%%RANDOM%%RANDOM%%RANDOM%
if !ctr_col[%%y]! equ 0 (
goto start
set "disp_col[%%y]= "
</lang>
) else (
set /a "rnd_digit=!random! %% 10"
if !ctr_col[%%y]! equ 1 (
set "disp_col[%%y]=%esc%[97m!rnd_digit!%esc%[32m"
) else if !ctr_col[%%y]! equ 2 (
set "disp_col[%%y]=%esc%[92m!rnd_digit!%esc%[32m"
) else (
set "disp_col[%%y]=!rnd_digit!"
)
set /a "ctr_col[%%y]=(!ctr_col[%%y]! + 1) %% (%rain_length% + 1)"
)
rem drop rain randomly
set /a "rnd_drop=!random! %% 20"
if !rnd_drop! equ 0 set "ctr_col[%%y]=1"
)
set "disp_line=%esc%[32m"
for /l %%y in (1,1,%col%) do set "disp_line=!disp_line!!disp_col[%%y]!"
<nul set /p "=%esc%[1T%esc%[1;1H" %== scroll down and set cursor position to home ==%
echo(%disp_line%
goto matrix_loop</syntaxhighlight>
 
=={{header|C}}==
Line 132 ⟶ 394:
 
And here's the code :
<syntaxhighlight lang="c">
<lang C>
/**
* Loosely emulates the "digital rain" effect from The Matrix.
Line 255 ⟶ 517:
return 0;
}
</syntaxhighlight>
</lang>
===Microsoft Windows console version===
====Single threaded====
{{Works with|Microsoft Visual Studio C}}
<langsyntaxhighlight Clang="c">/*******************************************************************************
*
* Digital ASCII rain - the single thread variant.
Line 443 ⟶ 705:
 
return 0;
}</langsyntaxhighlight>
 
====Multiple threads====
{{Works with|Microsoft Visual Studio C}}
<langsyntaxhighlight Clang="c">/*******************************************************************************
*
* Digital ASCII rain - multithreaded.
Line 650 ⟶ 912:
 
return 0;
}</langsyntaxhighlight>
 
====Fibers====
{{Works with|Microsoft Visual Studio C}}
<langsyntaxhighlight Clang="c">/*******************************************************************************
*
* Digital ASCII rain - multithreaded.
Line 849 ⟶ 1,111:
 
return 0;
}</langsyntaxhighlight>
 
=={{header|Common Lisp}}==
Line 857 ⟶ 1,119:
Runs in the terminal (using the Ncurses C library and the croatoan Lisp wrapper).
 
<langsyntaxhighlight lang="lisp">
(defun matrix-digital-rain ()
(with-screen (scr :input-echoing nil :input-blocking nil :cursor-visible nil)
Line 890 ⟶ 1,152:
(setf (frame-rate scr) 20)
(run-event-loop scr))))
</syntaxhighlight>
</lang>
 
{{out|Sample output}}
Line 900 ⟶ 1,162:
 
Rather than pressing Ctrl+C to stop the program, I've added code so that it stops automatically after 1 minute and restores the terminal to its original state.
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,005 ⟶ 1,267:
}
}
}</langsyntaxhighlight>
 
=={{header|J}}==
This implementation was written for jqt under j903, after studying https://youtu.be/MvEXkd3O2ow
 
Some key issues are the (deliberate) relatively low resolution of the screen in the movie, a somewhat slow update rate and some variation over time in the update rate of the screen. This implementation is, of course, only an approximation...
 
<syntaxhighlight lang="j">require'gl2'
coinsert'jgl2'
 
junk=: 7 u:;48 65 16b30a1(+i.)&.>10 26 90
sz=:40 25
len=: <.1.4*{:sz
heat=: (224 255 255),~(<.0.5+255*(%>./)(-<./)^>:(% >./)i.len)*/0 1 0
 
cols=: i.0
rows=: i.0
scale=: 24
live=: (#heat)#<i.0 3
 
rain_timer=: {{
try.
wd 'psel rain'
glsel 'green'
glfill 0 0 0 255
glfont font=.'courier ',":0.8*scale
upd=. 0>._3++/?2 2 2 2 4
cols=: cols,upd{.(?~{.sz)-.(-<.0.3*{:sz){.cols
rows=: (#cols){.rows
live=: }.live,<(scale*cols,.rows),.?(#cols)##junk
for_p. live do.
gltextcolor glrgb p_index{heat
if.p_index=<:#live do.
glfont font,' bold'
end.
for_xyj.;p do.
gltextxy 2{.xyj
gltext 8 u:junk{~{:xyj
end.
end.
glpaintx''
keep=: rows<{:sz-1
cols=: keep#cols
rows=: keep#rows+1
EMPTY
catch.
wd'ptimer 0'
end.
}}
 
wd rplc&('DIMS';":scale*sz) {{)n
pc rain closeok;
setp wh DIMS;
cc green isidraw flush;
pshow;
ptimer 100
}}</syntaxhighlight>
 
[[File:J-matrix-digital-rain.png|thumb]]
 
Notes:
 
<tt>ptimer 100</tt> to roughly match the update rate used in the matrix movie.
 
In the movie, the display was somewhat pixelated, and had some other artifacts which were characteristic of cathode display tubes. The font support we use here does not emulate all of that.
 
Conceptually, we are emulating an emulation of a cathode ray tube with a long phosphor persistence time. Thus, there's an initial "burst" of light when the phosphor is being painted followed by a lingering glow which gradually fades out. Here, we use a <span style="background-color: black; color: #e0ffff"> light cyan </span> to represent the initial paint event and <span style="color: #00ff00">f</span><span style="color: #00ec00">a</span><span style="color: #00da00">d</span><span style="color: #00c900">i</span><span style="color: #00b900">n</span><span style="color: #00aa00">g</span><span style="color: #009b00"> </span><span style="color: #008d00">s</span><span style="color: #007f00">h</span><span style="color: #007200">a</span><span style="color: #006600">d</span><span style="color: #005b00">e</span><span style="color: #004f00">s</span><span style="color: #004500"> </span><span style="color: #003b00">o</span><span style="color: #003100">f</span><span style="color: #002800"> </span><span style="color: #001f00">g</span><span style="color: #001700">r</span><span style="color: #000f00">e</span><span style="color: #000700">e</span><span style="color: #000000">n</span> to represent the fading phosphors. To better approximate the phosphor persistence mechanism, we have intensity fall off exponentially (and then we adjust the numeric range of the result so it still fades from our brightest green to black).
 
=={{header|Java}}==
<syntaxhighlight lang="java">
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.stream.Stream;
 
import javax.swing.JFrame;
 
public final class MatrixDigitalRain {
 
public static void main(String[] args) {
EventQueue.invokeLater( () -> {
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("Matrix Digital Rain");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
DigitalRain digitalRain = new DigitalRain(800, 600);
frame.add(digitalRain);
frame.setLocationByPlatform(true);
frame.pack();
frame.setVisible(true);
digitalRain.start();
} );
}
 
private static final class DigitalRain extends Canvas {
 
public DigitalRain(int aWidth, int aHeight) {
setPreferredSize( new Dimension(aWidth, aHeight) );
setBackground(Color.BLACK);
columnCount = aWidth / ( 2 * halfColumnWidth ) - 1;
rowCount = aHeight / ( 2 * halfFontSize );
setCursor(getToolkit().createCustomCursor(
new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB), new Point(0, 0), "transparent"));
executiveService = Executors.newSingleThreadExecutor();
random = ThreadLocalRandom.current();
}
 
public void start() {
requestFocus();
createBufferStrategy(2);
executiveService.execute( new DrawingCycle(rowCount) );
}
 
private final class DrawingCycle implements Runnable {
public DrawingCycle(int rowCount) {
columns = Stream.generate( () -> new Column(rowCount) )
.limit(columnCount).collect(Collectors.toList());
bufferStrategy = getBufferStrategy();
scheduler = Executors.newSingleThreadScheduledExecutor();
}
 
@Override
public void run() {
scheduler.scheduleAtFixedRate( () -> { draw(); update(); }, 0, 100, TimeUnit.MILLISECONDS);
}
 
private void draw() {
Graphics2D graphics2D = (Graphics2D) bufferStrategy.getDrawGraphics();
graphics2D.setColor(Color.BLACK);
graphics2D.fillRect(0, 0, getWidth(), getHeight());
for ( int col = 0; col < columnCount; col++ ) {
for ( int row = 0; row < rowCount; row++ ) {
Symbol symbol = columns.get(col).symbols.get(row);
graphics2D.setFont(symbol.font);
graphics2D.setColor(symbol.color());
final int size = symbol.font.getSize();
graphics2D.drawString(
symbol.element,
2 * halfColumnWidth * col + halfColumnWidth + ( 6 * halfFontSize - size ) / 2,
row * 2 * halfFontSize);
}
}
graphics2D.dispose();
bufferStrategy.show();
}
private void update() {
for ( Column column : columns ) {
if ( column.index >= 0 ) {
String element = elements.get(random.nextInt(elements.size()));
column.symbols.set(column.index, new Symbol(element, column.font, 255) );
}
column.index = Math.min(column.index + 1, rowCount);
column.darken();
if ( column.index == rowCount ) {
column.reset();
}
}
}
private final List<Column> columns;
private final BufferStrategy bufferStrategy;
private final ScheduledExecutorService scheduler;
 
} // End DrawingCycle class
private final class Column {
public Column(int aRowCount) {
rowCount = aRowCount;
index = random.nextInt(-rowCount, rowCount);
setFont();
symbols = Stream.generate( () -> new Symbol(font) ).limit(rowCount).collect(Collectors.toList());
}
public void darken() {
symbols.stream().forEach(Symbol::darken);
}
public void reset() {
index = random.nextInt(-rowCount, rowCount / 2);
setFont();
}
private void setFont() {
final int fontSize = ( random.nextInt(2) == 0 ) ?
2 * halfFontSize : ( random.nextInt(2) == 0 ) ?
(int) ( 1.5 * halfFontSize ) : 3 * halfFontSize;
final int fontStyle = ( random.nextInt(3) == 0 ) ? Font.BOLD : Font.PLAIN;
font = new Font("Dialog", fontStyle, fontSize);
}
private int index;
private Font font;
private List<Symbol> symbols;
private final int rowCount;
} // End Column class
private final class Symbol {
public Symbol(String aElement, Font aFont, int aBrightness) {
element = aElement;
font = aFont;
brightness = aBrightness;
}
public Symbol(Font font) {
this(" ", font, 0);
}
public Color color() {
return new Color(0, 255, 0, brightness);
}
public void darken() {
brightness = Math.max(0, brightness - 5);
}
public String toString() {
return element;
}
private int brightness;
private final Font font;
private final String element;
} // End Symbol class
private final int columnCount;
private final int rowCount;
private final ExecutorService executiveService;
private final int halfFontSize = 6;
private final int halfColumnWidth = 10;
private final ThreadLocalRandom random;
private final List<String> elements = List.of(
"M", "Ї", "Љ", "Њ", "Ћ", "Ќ", "Ѝ", "Ў", "Џ", "Б", "Г", "Д", "Ж", "И", "Й", "Л", "П", "Ф", "Ц", "Ч", "Ш",
"Щ", "Ъ", "Ы", "Э", "Ю", "Я", "в", "д", "ж", "з", "и", "й", "к", "л", "м", "н", "п", "т", "ф", "ц", "ч",
"ш", "щ", "ъ", "ы", "ь", "э", "ю", "я", "ѐ", "ё", "ђ", "ѓ", "є", "ї", "љ", "њ", "ћ", "ќ", "ѝ", "ў", "џ",
"Ѣ", "ѣ", "ѧ", "Ѯ", "ѱ", "Ѳ", "ѳ", "ҋ", "Ҍ", "ҍ", "Ҏ", "ҏ", "Ґ", "ґ", "Ғ", "ғ", "Ҕ", "ҕ", "Җ", "җ", "Ҙ",
"ҙ", "Қ", "қ", "ҝ", "ҟ", "ҡ", "Ң", "ң", "Ҥ", "ҥ", "ҩ", "Ҫ", "ҫ", "Ҭ", "ҭ", "Ұ", "ұ", "Ҳ", "ҳ", "ҵ", "ҷ",
"ҹ", "Һ", "ҿ", "Ӂ", "ӂ", "Ӄ", "ӄ", "ӆ", "Ӈ", "ӈ", "ӊ", "Ӌ", "ӌ", "ӎ", "Ӑ", "ӑ", "Ӓ", "ӓ", "Ӕ", "ӕ", "Ӗ",
"ӗ", "Ә", "ә", "Ӛ", "ӛ", "Ӝ", "ӝ", "Ӟ", "ӟ", "ӡ", "Ӣ", "ӣ", "Ӥ", "ӥ", "Ӧ", "ӧ", "Ө", "ө", "Ӫ", "ӫ", "Ӭ",
"ӭ", "Ӯ", "ӯ", "Ӱ", "ӱ", "Ӳ", "ӳ", "Ӵ", "ӵ", "Ӷ", "ӷ", "Ӹ", "ӹ", "Ӻ", "ӽ", "ӿ", "Ԁ", "ԍ", "ԏ", "Ԑ", "ԑ",
"ԓ", "Ԛ", "ԟ", "Ԧ", "ԧ", "Ϥ", "ϥ", "ϫ", "ϭ", "ゥ", "ェ", "ォ", "ャ", "ュ", "ョ", "ッ", "ー", "ア", "イ", "ウ", "エ",
"オ", "カ", "キ", "ク", "ケ", "コ", "サ", "シ", "ス", "セ", "ソ", "タ", "チ", "ツ", "テ", "ト", "ナ", "ニ", "ヌ", "ネ", "ノ",
"ハ", "ヒ", "フ", "ヘ", "ホ", "マ", "ミ", "ム", "メ", "モ", "ヤ", "ユ", "ヨ", "ラ", "リ", "ル", "レ", "ロ", "ワ", "ン", "ⲁ",
"Ⲃ", "ⲃ", "Ⲅ", "Γ", "Δ", "Θ", "Λ", "Ξ", "Π", "Ѐ", "Ё", "Ђ", "Ѓ", "Є", "ⲉ", "Ⲋ", "ⲋ", "Ⲍ", "ⲍ", "ⲏ", "ⲑ",
"ⲓ", "ⲕ", "ⲗ", "ⲙ", "ⲛ", "Ⲝ", "ⲝ", "ⲡ", "ⲧ", "ⲩ", "ⲫ", "ⲭ", "ⲯ", "ⳁ", "Ⳉ", "ⳉ", "ⳋ", "ⳤ", "⳥", "⳦", "⳨",
"⳩", "∀", "∁", "∂", "∃", "∄", "∅", "∆", "∇", "∈", "∉", "∊", "∋", "∌", "∍", "∎", "∏", "∐", "∑", "∓",
"ℇ", "ℏ", "℥", "Ⅎ", "ℷ", "⩫", "⨀", "⨅", "⨆", "⨉", "⨍", "⨎", "⨏", "⨐", "⨑", "⨒", "⨓", "⨔", "⨕", "⨖",
"⨗", "⨘", "⨙", "⨚", "⨛", "⨜", "⨝", "⨿", "⩪" );
} // End DigitalRain class
 
} // End MatrixDigitalRain class
</syntaxhighlight>
{{ out }}
A screenshot from the running program.
[[Media: JavaMatrixDigitalRain.png]]
 
=={{header|Javascript}}==
El código es de Christian Behler (christian@pingpoli.de)
<syntaxhighlight lang="javascript">var tileSize = 20;
// a higher fade factor will make the characters fade quicker
var fadeFactor = 0.05;
 
var canvas;
var ctx;
 
var columns = [];
var maxStackHeight;
 
function init() {
canvas = document.getElementById('canvas');
ctx = canvas.getContext('2d');
 
// https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserver
const resizeObserver = new ResizeObserver(entries =>
{
for (let entry of entries)
{
if (entry.contentBoxSize)
{
// Firefox implements `contentBoxSize` as a single content rect, rather than an array
const contentBoxSize = Array.isArray(entry.contentBoxSize) ? entry.contentBoxSize[0] : entry.contentBoxSize;
 
canvas.width = contentBoxSize.inlineSize;
canvas.height = window.innerHeight;
 
initMatrix();
}
}
});
 
// observe the size of the document
resizeObserver.observe(document.documentElement);
 
// start the main loop
tick();
}
 
function initMatrix() {
columns = [];
 
maxStackHeight = Math.ceil(canvas.height/tileSize);
 
// divide the canvas into columns
for (let i = 0 ; i < canvas.width/tileSize ; ++i) {
var column = {};
// save the x position of the column
column.x = i*tileSize;
// create a random stack height for the column
column.stackHeight = 10+Math.random()*maxStackHeight;
// add a counter to count the stack height
column.stackCounter = 0;
// add the column to the list
columns.push(column);
}
}
 
function draw() {
// draw a semi transparent black rectangle on top of the scene to slowly fade older characters
ctx.fillStyle = "rgba(0 , 0 , 0 , "+fadeFactor+")";
ctx.fillRect(0 , 0 , canvas.width , canvas.height);
 
ctx.font = (tileSize-2)+"px monospace";
ctx.fillStyle = "rgb(0 , 255 , 0)";
for (let i = 0 ; i < columns.length ; ++i) {
// pick a random ascii character (change the 94 to a higher number to include more characters)
var randomCharacter = String.fromCharCode(33+Math.floor(Math.random()*94));
ctx.fillText(randomCharacter , columns[i].x , columns[i].stackCounter*tileSize+tileSize);
 
// if the stack is at its height limit, pick a new random height and reset the counter
if (++columns[i].stackCounter >= columns[i].stackHeight)
{
columns[i].stackHeight = 10+Math.random()*maxStackHeight;
columns[i].stackCounter = 0;
}
}
}
 
// MAIN LOOP
function tick() {
draw();
setTimeout(tick , 50);
}
 
var b_isFullscreen = false;
 
function fullscreen() {
var elem = document.documentElement;
if (elem.requestFullscreen) {
elem.requestFullscreen();
}
else if (elem.webkitRequestFullscreen) {
elem.webkitRequestFullscreen(); // Safari
}
else if (elem.msRequestFullscreen) {
elem.msRequestFullscreen(); // IE11
}
}
 
function exitFullscreen() {
if (document.exitFullscreen) {
document.exitFullscreen();
}
else if (document.webkitExitFullscreen) {
document.webkitExitFullscreen(); // Safari
}
else if (document.msExitFullscreen) {
document.msExitFullscreen(); // IE11
}
}
 
function toggleFullscreen() {
if (!b_isFullscreen) {
fullscreen();
b_isFullscreen = true;
}
else {
exitFullscreen();
b_isFullscreen = false;
}
}
 
function updateTileSize() {
tileSize = Math.min(Math.max(document.getElementById("tileSize").value , 10) , 100);
initMatrix();
}
 
function updateFadeFactor() {
fadeFactor = Math.min(Math.max(document.getElementById("fadeFactor").value , 0.0) , 1.0);
initMatrix();
}</syntaxhighlight>
Html to test:
<pre><!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<script src="The_Matrix_rain.js"></script>
<title>The Matrix rain</title>
<style>
body {padding: 50px;width: 50%;box-shadow: 0 0 15px 0 rgba(0, 0, 0, 0.25);margin: 15px auto;font-family: "Gill Sans", "Gill Sans MT", Calibri, "Trebuchet MS", sans-serif;letter-spacing: 1px;}
a {color: #00aadd;text-decoration: none;}
input {width: 50px;text-align: center;}
ul {list-style: none;padding: 0;margin: 0;width: 25%;margin: auto;border: 1px solid #aaa;}
li {text-align: center;background-color: #eaeaea;}
li:nth-child(even) {background: #fff;}
</style>
</head>
<body onload="init();" style="margin: 0; padding: 0; background-color:#000000;">
<canvas id="canvas" style="display:block;margin:0 auto;" width="1920" height="955"></canvas>
<div id="options" style="position: absolute; right: 10px; bottom: 10px; padding: 5px; background-color: #aaaaaa; font-family: monospace;">
<button onclick="toggleFullscreen()" style="font-family: monospace;">Toggle Fullscreen</button>
<table>
<tbody><tr>
<td>Tile Size</td>
<td><input type="number" id="tileSize" value="20" onchange="updateTileSize()" onkeyup="updateTileSize()" style="width: 80px; font-family: monospace; outline: none;" min="10" max="100" step="1"></td>
</tr>
<tr>
<td>Fade Factor</td>
<td><input type="number" id="fadeFactor" value="0.05" onchange="updateFadeFactor()" onkeyup="updateFadeFactor()" style="width: 80px; font-family: monospace; outline: none;" min="0" max="1" step="0.01"></td>
</tr>
</tbody></table>
</div>
</body>
</html></pre>
 
=={{header|Julia}}==
The font used is based on Leonardo da Vinci's notebooks. The font is freely obtainable at https://www.wfonts.com/font/leonardos-mirrorwriting.
<langsyntaxhighlight Julialang="julia">using Gtk, Colors, Cairo
import Base.iterate, Base.IteratorSize, Base.IteratorEltype
 
Line 1,090 ⟶ 1,806:
 
digitalrain()
</syntaxhighlight>
</lang>
 
=={{header|Locomotive Basic}}==
 
<langsyntaxhighlight lang="locobasic">10 mode 0:defint a-z:randomize time:ink 0,0:ink 1,26:ink 2,19:border 0
20 dim p(20):mm=5:dim act(mm):for i=1 to mm:act(i)=rnd*19+1:next
30 md=mm-2:dim del(md):for i=1 to md:del(i)=rnd*19+1:next
Line 1,104 ⟶ 1,820:
90 p(x)=p(x)+1:if p(x)=25 then p(x)=0:del(i)=rnd*19+1
100 next
110 goto 40</langsyntaxhighlight>
The program above runs at an acceptable speed on a 4 MHz Z80, but can be made much faster by using the [https://benchmarko.github.io/CPCBasic/cpcbasic.html CPCBasic JavaScript emulator]. CPCBasic also adds an 80x50 display mode ("mode 3") which real CPC hardware lacks. Changing screen size from 20x25 to 80x50 and adding a delay with "frame" in line 100 results in a far more impressive display in CPCBasic:
<langsyntaxhighlight lang="locobasic">10 mode 3:defint a-z:randomize time:ink 0,0:ink 1,26:ink 2,19:border 0
20 dim p(80):mm=12:dim act(mm):for i=1 to mm:act(i)=rnd*79+1:next
30 md=mm-2:dim del(md):for i=1 to md:del(i)=rnd*79+1:next
Line 1,116 ⟶ 1,832:
90 p(x)=p(x)+1:if p(x)=50 then p(x)=0:del(i)=rnd*79+1
100 next:frame
110 goto 40</langsyntaxhighlight>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">SeedRandom[1234];
ClearAll[ColorFunc]
chars = RandomSample[Flatten[CharacterRange @@@ Partition[Characters["\[CapitalAlpha]\[CapitalPi]ЀѵҊԯ\:03e2\:03efヲンⲀ⳩\[ForAll]∗℀℺⨀⫿"], 2]]];
charlen = Length[chars];
fadelength = 6;
ColorFunc[fade_] := ColorFunc[fade] = Blend[{Black, Green}, fade/fadelength]
rows = 30;
cols = 50;
n = 10;
trailpos = {RandomInteger[{1, cols}, n], RandomInteger[{1, rows}, n]} // Transpose;
trailchars = RandomInteger[{1, Length[chars]}, n];
charmap = ConstantArray[".", {rows, cols}];
fade = ConstantArray[5, {rows, cols}];
indices = MapIndexed[#2 &, fade, {2}];
Dynamic[Graphics[{txts}, PlotRange -> {{1, cols}, {1, rows}}, PlotRangePadding -> 1, Background -> Black]]
Do[
trailpos[[All, 2]] += 1;
fade = Ramp[fade - 1];
trailchars = Mod[trailchars + 1, charlen, 1];
Do[
If[trailpos[[i, 2]] >= rows,
trailpos[[i, 2]] = 1;
trailpos[[i, 1]] = RandomInteger[{1, cols}];
];
charmap[[trailpos[[i, 2]], trailpos[[i, 1]]]] =
chars[[trailchars[[i]]]];
fade[[trailpos[[i, 2]], trailpos[[i, 1]]]] = fadelength
,
{i, n}
];
txts = MapThread[If[#2 > 0, Text[Style[#1, ColorFunc[#2]], {#1, rows - #2 + 1} & @@ Reverse[#3]], {}] &, {charmap, fade, indices}, 2];
Pause[0.1]
,
{1000}
]</syntaxhighlight>
{{out}}
Outputs a animating graphic of the matrix digital rain.
 
=={{header|Nim}}==
{{trans|C}}
{{libheader|nim-ncurses}}
<langsyntaxhighlight Nimlang="nim">import os, random, sequtils
import ncurses
 
Line 1,187 ⟶ 1,942:
refresh()
 
run()</langsyntaxhighlight>
 
=={{header|Perl}}==
Line 1,194 ⟶ 1,949:
Follow the bouncing Neo!
 
<langsyntaxhighlight lang="perl">#!/user/bin/perl
 
use strict;
Line 1,526 ⟶ 2,281:
';
}
</syntaxhighlight>
</lang>
===Another Perl Solution===
<langsyntaxhighlight lang="perl">#!/usr/bin/perl
 
use strict; # http://www.rosettacode.org/wiki/Matrix_Digital_Rain
Line 1,564 ⟶ 2,319:
@queue = grep $_->[2] > 0 && $_->[1] < $rows * 20, @queue, @new;
$mw->after( 63, \&step );
}</langsyntaxhighlight>
 
=={{header|Phix}}==
{{libheader|Phix/pGUI}}
{{libheader|Phix/online}}
<lang Phix>-- demo\rosetta\Matrix_Digital_Rain.exw
You can run this online [http://phix.x10.mx/p2js/matrix.htm here].
sequence sushii = {}, -- w x h of unicode char strings
<!--<syntaxhighlight lang="phix">(phixonline)-->
colours, -- """ of their fading colours
<span style="color: #000080;font-style:italic;">--
droplets -- w column droplets, or zeroes
-- demo\rosetta\Matrix_Digital_Rain.exw
 
--</span>
include pGUI.e
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
 
<span style="color: #004080;">sequence</span> <span style="color: #000000;">sushii</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{},</span> <span style="color: #000080;font-style:italic;">-- w x h of unicode char strings</span>
Ihandle dlg, canvas, timer
<span style="color: #000000;">colours</span><span style="color: #0000FF;">,</span> <span style="color: #000080;font-style:italic;">-- """ of their fading colours</span>
cdCanvas cddbuffer, cdcanvas
<span style="color: #000000;">droplets</span> <span style="color: #000080;font-style:italic;">-- w column droplets, or zeroes</span>
 
procedure rain(integer w,h)
<span style="color: #008080;">include</span> <span style="color: #000000;">pGUI</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
for x=1 to w do
integer y = droplets[x]
<span style="color: #004080;">Ihandle</span> <span style="color: #000000;">dlg</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">canvas</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">timer</span>
if y or rand(40)=1 then
<span style="color: #004080;">cdCanvas</span> <span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">cdcanvas</span>
if y<h then
droplets[x] = y+1
<span style="color: #008080;">procedure</span> <span style="color: #000000;">rain</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">w</span><span style="color: #0000FF;">,</span><span style="color: #000000;">h</span><span style="color: #0000FF;">)</span>
sushii[y+1][x] = utf32_to_utf8({0x30A0 + rand(96)})
<span style="color: #008080;">for</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">w</span> <span style="color: #008080;">do</span>
colours[y+1][x] = CD_WHITE
<span style="color: #004080;">integer</span> <span style="color: #000000;">y</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">droplets</span><span style="color: #0000FF;">[</span><span style="color: #000000;">x</span><span style="color: #0000FF;">]</span>
end if
<span style="color: #008080;">if</span> <span style="color: #000000;">y</span> <span style="color: #008080;">or</span> <span style="color: #7060A8;">rand</span><span style="color: #0000FF;">(</span><span style="color: #000000;">40</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span>
if y then
<span style="color: #008080;">if</span> <span style="color: #000000;">y</span><span style="color: #0000FF;"><</span><span style="color: #000000;">h</span> <span style="color: #008080;">then</span>
bool clear_droplet = true
<span style="color: #000000;">droplets</span><span style="color: #0000FF;">[</span><span style="color: #000000;">x</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">y</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span>
if colours[y][x]=CD_WHITE then
<span style="color: #000000;">sushii</span><span style="color: #0000FF;">[</span><span style="color: #000000;">y</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">][</span><span style="color: #000000;">x</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">utf32_to_utf8</span><span style="color: #0000FF;">({</span><span style="color: #000000;">0x30A0</span> <span style="color: #0000FF;">+</span> <span style="color: #7060A8;">rand</span><span style="color: #0000FF;">(</span><span style="color: #000000;">96</span><span style="color: #0000FF;">)})</span>
colours[y][x] = #00F800 -- (CD_GREEN to nearest #800)
<span style="color: #000000;">colours</span><span style="color: #0000FF;">[</span><span style="color: #000000;">y</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">][</span><span style="color: #000000;">x</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">CD_WHITE</span>
clear_droplet = false
<span style="color: #008080;">end</span> <span y -style="color: 1#008080;">if</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">y</span> <span style="color: #008080;">then</span>
end if
<span style="color: #004080;">bool</span> <span style="color: #000000;">clear_droplet</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">true</span>
for y=y to 1 by -1 do
<span style="color: #008080;">if</span> <span style="color: #000000;">colours</span><span style="color: #0000FF;">[</span><span style="color: #000000;">y</span><span style="color: #0000FF;">][</span><span style="color: #000000;">x</span><span style="color: #0000FF;">]=</span><span style="color: #004600;">CD_WHITE</span> <span style="color: #008080;">then</span>
integer cy = colours[y][x]
<span style="color: #000000;">colours</span><span style="color: #0000FF;">[</span><span style="color: #000000;">y</span><span style="color: #0000FF;">][</span><span style="color: #000000;">x</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">#00F800</span> <span style="color: #000080;font-style:italic;">-- (CD_GREEN to nearest #800)</span>
if cy=0 then exit end if
<span style="color: #000000;">clear_droplet</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">false</span>
<span style="color: #000000;">y</span> <span style="color: #0000FF;">-=</span> <span style="color: #000000;">1</span>
cy -= #000800
<span style="color: #008080;">end</span> colours[y][x]<span style="color: cy#008080;">if</span>
<span style="color: #008080;">while</span> <span style="color: #000000;">y</span><span style="color: #0000FF;">>=</span><span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
end for
<span style="color: #004080;">integer</span> <span style="color: #000000;">cy</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">colours</span><span style="color: #0000FF;">[</span><span style="color: #000000;">y</span><span style="color: #0000FF;">][</span><span style="color: #000000;">x</span><span style="color: #0000FF;">]</span>
if clear_droplet then
<span style="color: #008080;">if</span> <span style="color: #000000;">cy</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span> <span style="color: #008080;">exit</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
droplets[x] = 0
<span style="color: #000000;">clear_droplet</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">false</span>
end if
<span style="color: #000000;">cy</span> <span style="color: #0000FF;">-=</span> <span style="color: #000000;">#000800</span>
end if
<span style="color: #000000;">colours</span><span style="color: #0000FF;">[</span><span style="color: #000000;">y</span><span style="color: #0000FF;">][</span><span style="color: #000000;">x</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">cy</span>
end if
<span style="color: #000000;">y</span> <span style="color: #0000FF;">-=</span> <span style="color: #000000;">1</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
end procedure
<span style="color: #008080;">if</span> <span style="color: #000000;">clear_droplet</span> <span style="color: #008080;">then</span>
 
<span style="color: #000000;">droplets</span><span style="color: #0000FF;">[</span><span style="color: #000000;">x</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
function redraw_cb(Ihandle /*ih*/, integer /*posx*/, integer /*posy*/)
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
integer {w,h} = IupGetIntInt(canvas, "DRAWSIZE"),
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
{dx,dy} = cdCanvasGetTextSize(cddbuffer, "W")
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
w = max(1,floor(w/dx))
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
h = max(1,floor(h/dy))
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
if length(sushii)!=h
or length(sushii[1])!=w then
<span style="color: #008080;">function</span> <span style="color: #000000;">redraw_cb</span><span style="color: #0000FF;">(</span><span style="color: #004080;">Ihandle</span> <span style="color: #000080;font-style:italic;">/*ih*/</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000080;font-style:italic;">/*posx*/</span><span style="color: #0000FF;">,</span> <span style="color: #000080;font-style:italic;">/*posy*/</span><span style="color: #0000FF;">)</span>
sushii = repeat(repeat(" ",w),h)
<span style="color: #004080;">integer</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">w</span><span style="color: #0000FF;">,</span><span style="color: #000000;">h</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupGetIntInt</span><span style="color: #0000FF;">(</span><span style="color: #000000;">canvas</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"DRAWSIZE"</span><span style="color: #0000FF;">),</span>
colours = repeat(repeat(CD_BLACK,w),h)
<span style="color: #0000FF;">{</span><span style="color: #000000;">dx</span><span style="color: #0000FF;">,</span><span style="color: #000000;">dy</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">cdCanvasGetTextSize</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"W"</span><span style="color: #0000FF;">)</span>
droplets = repeat(0,w)
<span style="color: #000000;">w</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">max</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">w</span><span style="color: #0000FF;">/</span><span style="color: #000000;">dx</span><span style="color: #0000FF;">))</span>
end if
<span style="color: #000000;">h</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">max</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">h</span><span style="color: #0000FF;">/</span><span style="color: #000000;">dy</span><span style="color: #0000FF;">))</span>
cdCanvasActivate(cddbuffer)
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">sushii</span><span style="color: #0000FF;">)!=</span><span style="color: #000000;">h</span>
cdCanvasClear(cddbuffer)
<span style="color: #008080;">or</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">sushii</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">])!=</span><span style="color: #000000;">w</span> <span style="color: #008080;">then</span>
rain(w,h)
<span style="color: #000000;">sushii</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #008000;">" "</span><span style="color: #0000FF;">,</span><span style="color: #000000;">w</span><span style="color: #0000FF;">),</span><span style="color: #000000;">h</span><span style="color: #0000FF;">)</span>
for x=1 to w do
<span style="color: #000000;">colours</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #004600;">CD_BLACK</span><span style="color: #0000FF;">,</span><span style="color: #000000;">w</span><span style="color: #0000FF;">),</span><span style="color: #000000;">h</span><span style="color: #0000FF;">)</span>
for y=1 to h do
<span style="color: #000000;">droplets</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">w</span><span style="color: #0000FF;">)</span>
cdCanvasSetForeground(cddbuffer, colours[y][x])
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
cdCanvasText(cddbuffer,x*dx, (h-y)*dy, sushii[y][x])
<span style="color: #7060A8;">cdCanvasActivate</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">)</span>
end for
<span style="color: #7060A8;">cdCanvasClear</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">)</span>
end for
<span style="color: #000000;">rain</span><span style="color: #0000FF;">(</span><span style="color: #000000;">w</span><span style="color: #0000FF;">,</span><span style="color: #000000;">h</span><span style="color: #0000FF;">)</span>
cdCanvasFlush(cddbuffer)
<span style="color: #008080;">for</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">w</span> <span style="color: #008080;">do</span>
return IUP_DEFAULT
<span style="color: #008080;">for</span> <span style="color: #000000;">y</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">h</span> <span style="color: #008080;">do</span>
end function
<span style="color: #008080;">if</span> <span style="color: #000000;">colours</span><span style="color: #0000FF;">[</span><span style="color: #000000;">y</span><span style="color: #0000FF;">][</span><span style="color: #000000;">x</span><span style="color: #0000FF;">]</span> <span style="color: #008080;">then</span>
 
<span style="color: #7060A8;">cdCanvasSetForeground</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">colours</span><span style="color: #0000FF;">[</span><span style="color: #000000;">y</span><span style="color: #0000FF;">][</span><span style="color: #000000;">x</span><span style="color: #0000FF;">])</span>
function timer_cb(Ihandle /*ih*/)
<span style="color: #7060A8;">cdCanvasText</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">,</span><span style="color: #000000;">x</span><span style="color: #0000FF;">*</span><span style="color: #000000;">dx</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">h</span><span style="color: #0000FF;">-</span><span style="color: #000000;">y</span><span style="color: #0000FF;">)*</span><span style="color: #000000;">dy</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">sushii</span><span style="color: #0000FF;">[</span><span style="color: #000000;">y</span><span style="color: #0000FF;">][</span><span style="color: #000000;">x</span><span style="color: #0000FF;">])</span>
IupUpdate(canvas)
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
return IUP_IGNORE
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
end function
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
 
<span style="color: #7060A8;">cdCanvasFlush</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">)</span>
function map_cb(Ihandle ih)
<span style="color: #008080;">return</span> <span style="color: #004600;">IUP_DEFAULT</span>
cdcanvas = cdCreateCanvas(CD_IUP, ih)
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
cddbuffer = cdCreateCanvas(CD_DBUFFER, cdcanvas)
cdCanvasSetBackground(cddbuffer, CD_BLACK)
<span style="color: #008080;">function</span> <span style="color: #000000;">timer_cb</span><span style="color: #0000FF;">(</span><span style="color: #004080;">Ihandle</span> <span style="color: #000080;font-style:italic;">/*ih*/</span><span style="color: #0000FF;">)</span>
return IUP_DEFAULT
<span style="color: #7060A8;">IupUpdate</span><span style="color: #0000FF;">(</span><span style="color: #000000;">canvas</span><span style="color: #0000FF;">)</span>
end function
<span style="color: #008080;">return</span> <span style="color: #004600;">IUP_IGNORE</span>
 
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
procedure main()
IupOpen()
<span style="color: #008080;">function</span> <span style="color: #000000;">map_cb</span><span style="color: #0000FF;">(</span><span style="color: #004080;">Ihandle</span> <span style="color: #000000;">ih</span><span style="color: #0000FF;">)</span>
IupSetGlobal("UTF8MODE","YES")
<span style="color: #000000;">cdcanvas</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">cdCreateCanvas</span><span style="color: #0000FF;">(</span><span style="color: #004600;">CD_IUP</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">ih</span><span style="color: #0000FF;">)</span>
 
<span style="color: #000000;">cddbuffer</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">cdCreateCanvas</span><span style="color: #0000FF;">(</span><span style="color: #004600;">CD_DBUFFER</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">cdcanvas</span><span style="color: #0000FF;">)</span>
canvas = IupCanvas(NULL)
<span style="color: #7060A8;">cdCanvasSetBackground</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">,</span> <span style="color: #004600;">CD_BLACK</span><span style="color: #0000FF;">)</span>
IupSetAttribute(canvas, "RASTERSIZE", "640x480")
<span style="color: #008080;">return</span> <span style="color: #004600;">IUP_DEFAULT</span>
IupSetCallback(canvas, "MAP_CB", Icallback("map_cb"))
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
IupSetCallback(canvas, "ACTION", Icallback("redraw_cb"))
 
<span style="color: #008080;">procedure</span> <span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
timer = IupTimer(Icallback("timer_cb"), 50)
<span style="color: #7060A8;">IupOpen</span><span style="color: #0000FF;">()</span>
 
<span style="color: #7060A8;">IupSetGlobal</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"UTF8MODE"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"YES"</span><span style="color: #0000FF;">)</span>
dlg = IupDialog(canvas)
IupSetAttribute(dlg, "TITLE", "Matrix Digital Rain")
<span style="color: #000000;">canvas</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupCanvas</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"RASTERSIZE=640x480"</span><span style="color: #0000FF;">)</span>
 
<span style="color: #7060A8;">IupSetCallbacks</span><span style="color: #0000FF;">(</span><span style="color: #000000;">canvas</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"MAP_CB"</span><span style="color: #0000FF;">,</span> <span style="color: #7060A8;">Icallback</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"map_cb"</span><span style="color: #0000FF;">),</span>
IupShow(dlg)
<span style="color: #008000;">"ACTION"</span><span style="color: #0000FF;">,</span> <span style="color: #7060A8;">Icallback</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"redraw_cb"</span><span style="color: #0000FF;">)})</span>
IupSetAttribute(canvas, "RASTERSIZE", NULL)
IupMainLoop()
<span style="color: #000000;">timer</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupTimer</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">Icallback</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"timer_cb"</span><span style="color: #0000FF;">),</span> <span style="color: #000000;">50</span><span style="color: #0000FF;">)</span>
IupClose()
end procedure
<span style="color: #000000;">dlg</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupDialog</span><span style="color: #0000FF;">(</span><span style="color: #000000;">canvas</span><span style="color: #0000FF;">,</span><span style="color: #008000;">`TITLE="Matrix Digital Rain"`</span><span style="color: #0000FF;">)</span>
main()</lang>
<span style="color: #7060A8;">IupShow</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dlg</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">IupSetAttribute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">canvas</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"RASTERSIZE"</span><span style="color: #0000FF;">,</span> <span style="color: #004600;">NULL</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">platform</span><span style="color: #0000FF;">()!=</span><span style="color: #004600;">JS</span> <span style="color: #008080;">then</span>
<span style="color: #7060A8;">IupMainLoop</span><span style="color: #0000FF;">()</span>
<span style="color: #7060A8;">IupClose</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;">procedure</span>
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<!--</syntaxhighlight>-->
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">
import curses
import random
Line 1,763 ⟶ 2,529:
except KeyboardInterrupt as err:
curses.endwin()
</syntaxhighlight>
</lang>
 
=={{header|Racket}}==
Line 1,769 ⟶ 2,535:
{{trans|Raku}}
 
<langsyntaxhighlight lang="racket">#lang racket
 
(define codes '((Α Π) (Ѐ ѵ) (Ҋ ԯ) (Ϣ ϯ) (ヲ ン) (Ⲁ ⳩) (∀ ∗) (℀ ℺) (⨀ ⫿)))
Line 1,813 ⟶ 2,579:
(display "\e[0m")
(display "\e[H\e[J\e[?25h"))])
(main))</langsyntaxhighlight>
 
=={{header|Raku}}==
Line 1,822 ⟶ 2,588:
The "lightning" effect is actually a bug, but I liked it so I kept it.
 
<syntaxhighlight lang="raku" perl6line># clean up on exit, reset ANSI codes, scroll, re-show the cursor & clear screen
signal(SIGINT).tap: { print "\e[0m", "\n" xx 50, "\e[H\e[J\e[?25h"; exit(0) }
 
Line 1,866 ⟶ 2,632:
@offset = (^@offset).map: {(@offset[$_] - ($_ % 3)) % +@palette};
}
}</langsyntaxhighlight>
 
{{out|Sample output}}
Line 1,874 ⟶ 2,640:
Digital rain note: &nbsp; This REXX program favors the use of Latin letters &nbsp; (both lower and uppercase) &nbsp; letters
over all others characters (glyphs) by a 25% factor.
<langsyntaxhighlight lang="rexx">/*REXX program creates/displays Matrix (the movie) digital rain; favors non-Latin chars.*/
signal on halt /*allow the user to halt/stop this pgm.*/
parse arg pc seed . /*obtain optional arguments from the CL*/
Line 1,918 ⟶ 2,684:
fogger: do f=1 for sw /*display a screen full of rain streams*/
if substr(cloud, f, 1) \== ' ' then cloud= overlay( drop(), cloud, f)
end /*f*/; call show; return /* [↑] if raindrop, then change drop. */</langsyntaxhighlight>
Programming note:
 
Line 1,998 ⟶ 2,764:
├ ╚ K & G R ╗
</pre>
 
=={{header|Rust}}==
[https://github.com/kawogi/matrix-digital-rain Github repository]
 
Cargo.toml
<syntaxhighlight lang="toml">
[package]
name = "matrix-digital-rain"
version = "0.1.0"
edition = "2021"
 
[dependencies]
termion = "1.5.6"
rand = "0.8.5"
</syntaxhighlight>
 
main.rs
<syntaxhighlight lang="rust">
#![warn(clippy::pedantic)] // make sure that clippy is even more annoying
 
use rand::prelude::{ThreadRng, SliceRandom};
use rand::{thread_rng, Rng};
use termion::{color::Rgb};
use termion::input::TermRead;
use termion::raw::IntoRawMode;
use std::sync::mpsc::{channel, TryRecvError};
use std::thread;
use std::{io::{Write, stdout, stdin}, iter::repeat, time::Duration};
 
/// Character pool to pick from
/// If your terminal is struggling with that choice, replace them by ordinary latin characters.
/// There's currently no nicer way to initialize constant array in Rust
const CHARS: [char; 322] = [
'M', 'Ї', 'Љ', 'Њ', 'Ћ', 'Ќ', 'Ѝ', 'Ў', 'Џ', 'Б', 'Г', 'Д', 'Ж', 'И', 'Й', 'Л', 'П', 'Ф', 'Ц', 'Ч', 'Ш', 'Щ', 'Ъ',
'Ы', 'Э', 'Ю', 'Я', 'в', 'д', 'ж', 'з', 'и', 'й', 'к', 'л', 'м', 'н', 'п', 'т', 'ф', 'ц', 'ч', 'ш', 'щ', 'ъ', 'ы',
'ь', 'э', 'ю', 'я', 'ѐ', 'ё', 'ђ', 'ѓ', 'є', 'ї', 'љ', 'њ', 'ћ', 'ќ', 'ѝ', 'ў', 'џ', 'Ѣ', 'ѣ', 'ѧ', 'Ѯ', 'ѱ', 'Ѳ',
'ѳ', 'ҋ', 'Ҍ', 'ҍ', 'Ҏ', 'ҏ', 'Ґ', 'ґ', 'Ғ', 'ғ', 'Ҕ', 'ҕ', 'Җ', 'җ', 'Ҙ', 'ҙ', 'Қ', 'қ', 'ҝ', 'ҟ', 'ҡ', 'Ң', 'ң',
'Ҥ', 'ҥ', 'ҩ', 'Ҫ', 'ҫ', 'Ҭ', 'ҭ', 'Ұ', 'ұ', 'Ҳ', 'ҳ', 'ҵ', 'ҷ', 'ҹ', 'Һ', 'ҿ', 'Ӂ', 'ӂ', 'Ӄ', 'ӄ', 'ӆ', 'Ӈ', 'ӈ',
'ӊ', 'Ӌ', 'ӌ', 'ӎ', 'Ӑ', 'ӑ', 'Ӓ', 'ӓ', 'Ӕ', 'ӕ', 'Ӗ', 'ӗ', 'Ә', 'ә', 'Ӛ', 'ӛ', 'Ӝ', 'ӝ', 'Ӟ', 'ӟ', 'ӡ', 'Ӣ', 'ӣ',
'Ӥ', 'ӥ', 'Ӧ', 'ӧ', 'Ө', 'ө', 'Ӫ', 'ӫ', 'Ӭ', 'ӭ', 'Ӯ', 'ӯ', 'Ӱ', 'ӱ', 'Ӳ', 'ӳ', 'Ӵ', 'ӵ', 'Ӷ', 'ӷ', 'Ӹ', 'ӹ', 'Ӻ',
'ӽ', 'ӿ', 'Ԁ', 'ԍ', 'ԏ', 'Ԑ', 'ԑ', 'ԓ', 'Ԛ', 'ԟ', 'Ԧ', 'ԧ', 'Ϥ', 'ϥ', 'ϫ', 'ϭ', 'ゥ', 'ェ', 'ォ', 'ャ', 'ュ', 'ョ', 'ッ',
'ー', 'ア', 'イ', 'ウ', 'エ', 'オ', 'カ', 'キ', 'ク', 'ケ', 'コ', 'サ', 'シ', 'ス', 'セ', 'ソ', 'タ', 'チ', 'ツ', 'テ', 'ト', 'ナ', 'ニ',
'ヌ', 'ネ', 'ノ', 'ハ', 'ヒ', 'フ', 'ヘ', 'ホ', 'マ', 'ミ', 'ム', 'メ', 'モ', 'ヤ', 'ユ', 'ヨ', 'ラ', 'リ', 'ル', 'レ', 'ロ', 'ワ', 'ン',
'ⲁ', 'Ⲃ', 'ⲃ', 'Ⲅ', 'Γ', 'Δ', 'Θ', 'Λ', 'Ξ', 'Π', 'Ѐ', 'Ё', 'Ђ', 'Ѓ', 'Є', 'ⲉ', 'Ⲋ', 'ⲋ', 'Ⲍ', 'ⲍ', 'ⲏ', 'ⲑ', 'ⲓ',
'ⲕ', 'ⲗ', 'ⲙ', 'ⲛ', 'Ⲝ', 'ⲝ', 'ⲡ', 'ⲧ', 'ⲩ', 'ⲫ', 'ⲭ', 'ⲯ', 'ⳁ', 'Ⳉ', 'ⳉ', 'ⳋ', 'ⳤ', '⳥', '⳦', '⳨', '⳩', '∀', '∁',
'∂', '∃', '∄', '∅', '∆', '∇', '∈', '∉', '∊', '∋', '∌', '∍', '∎', '∏', '∐', '∑', '∓', 'ℇ', 'ℏ', '℥', 'Ⅎ', 'ℷ', '⩫',
'⨀', '⨅', '⨆', '⨉', '⨍', '⨎', '⨏', '⨐', '⨑', '⨒', '⨓', '⨔', '⨕', '⨖', '⨗', '⨘', '⨙', '⨚', '⨛', '⨜', '⨝', '⨿', '⩪',
];
 
/// convert a brightness value to a green-ish gradient color
fn color(brightness: u8) -> Rgb {
let v = f32::from(brightness) / 255.0;
let r = v.powi(7);
let g = v.powi(1);
let b = v.powi(4);
// r, g, b will be in 0.0..=1.0 so there's no risk of exceeding the u8's range
#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
Rgb ((r * 255.0).round() as u8, (g * 255.0).round() as u8, (b * 255.0).round() as u8)
}
 
/// A single character on the screen with its current brightness
#[derive(Clone, Copy)]
struct Symbol {
char: char,
brightness: u8,
}
 
/// Start with a black space by default
impl Default for Symbol {
fn default() -> Self {
Self { char: ' ', brightness: 0 }
}
}
 
impl Symbol {
/// output the colored symbol at the current cursor position
fn print<W: Write>(self, out: &mut W) {
write!(out, "{}{}", termion::color::Fg(color(self.brightness)), self.char).unwrap();
}
 
/// reduce the brightness of the symbol by a certain amount and make sure the value doesn't underrun
fn darken(&mut self) {
self.brightness = self.brightness.saturating_sub(10);
}
 
/// replace the character for this symbol and bring it to full brightness
fn set(&mut self, char: char) {
self.char = char;
self.brightness = 255;
}
}
 
/// a single column of symbols
#[derive(Clone)]
struct Column {
symbols: Vec<Symbol>,
}
 
impl Column {
/// create a new column with a given height
fn new(height: usize) -> Self {
Self {
symbols: vec![Symbol::default(); height],
}
}
 
/// print out a single colored symbol of this column
fn print_symbol<W: Write>(&self, out: &mut W, row: usize) {
self.symbols[row].print(out);
}
 
/// reduce the brightness of the entire column
fn darken(&mut self) {
self.symbols.iter_mut().for_each(Symbol::darken);
}
 
fn set(&mut self, row: usize, char: char) {
self.symbols[row].set(char);
}
}
 
/// Current position of a _falling symbol_
struct Droplet {
/// For the start of the animation we want to be able to place the symbol _above_ the screen,
/// that's we need negative row values as well.
row: isize,
col: usize,
}
 
impl Droplet {
/// create a new Droplet at a random location somewhere above the actual screen
fn new_random(rng: &mut ThreadRng, width: usize, height: usize) -> Self {
// the height of the terminal is expected lie within a sane range of this type
#[allow(clippy::cast_possible_wrap)]
Self {
row: -(rng.gen_range(0..height) as isize),
col: rng.gen_range(0..width),
}
}
 
/// move the droplet down by one row
/// if it hits the bottom row, move it back up to a random column
fn update(&mut self, width: usize, height: usize) {
self.row += 1;
// the height of the terminal is expected lie within a sane range of this type
#[allow(clippy::cast_possible_wrap)]
if self.row >= height as isize {
let mut rng = thread_rng();
self.col = rng.gen_range(0..width);
self.row = 0;
}
}
}
 
/// The entire screen filled with colored symbols
struct Screen {
width: usize,
height: usize,
columns: Vec<Column>,
droplets: Vec<Droplet>,
}
 
impl Screen {
 
/// create a new empty screen with the given dimensions
fn new(width: usize, height: usize) -> Self {
let mut rng = thread_rng();
Self {
width,
height,
columns: repeat(Column::new(height)).take(width).collect(),
droplets: (0..width).map(|_| Droplet::new_random(&mut rng, width, height)).collect(),
}
}
 
/// print the entire screen to the terminal
fn print<W: Write>(&self, out: &mut W) {
for row in 0..self.height {
for column in &self.columns {
column.print_symbol(out, row);
}
write!(out, "\r\n").unwrap();
}
}
 
// make all droplets fall down by one row
fn update_droplets(&mut self) {
let mut rng = thread_rng();
for droplet in &mut self.droplets {
droplet.update(self.width, self.height);
if let Ok(row) = droplet.row.try_into() {
let ch = CHARS.choose(&mut rng).copied().unwrap_or(' ');
self.columns[droplet.col].set(row, ch);
}
}
}
 
// reduce the brightness of all symbols in this screen
fn darken(&mut self) {
self.columns.iter_mut().for_each(Column::darken);
}
 
}
 
fn main() {
// create the screen with the terminal's dimensions (omit the last row to prevent auto-scrolling)
let (width, height) = termion::terminal_size().unwrap();
let mut screen = Screen::new(width as usize, height as usize - 1);
 
// create a channel which allows to send stuff between thread boundaries
let (tx, rx) = channel();
// spawn a new thread which will blockingly wait for a key to be pressed
thread::spawn(move || {
stdin().keys().next();
// send something down the channel to notify the main thread that a key has been pressed
tx.send(()).expect("Could not send signal on channel.");
});
 
// get write access to the terminal
let mut stdout = stdout().into_raw_mode().unwrap();
 
// clear the screen and hide the cursor
write!(stdout, "{}{}", termion::clear::All, termion::cursor::Hide).unwrap();
 
// continue while no key has been pressed (i.e. the notification channel is empty)
while rx.try_recv() == Err(TryRecvError::Empty) {
// move cursor to the top left and set background color to black
write!(stdout, "{}{}", termion::cursor::Goto(1, 1), termion::color::Bg(termion::color::Rgb(0, 0, 0))).unwrap();
 
// screen update
screen.print(&mut stdout);
screen.darken();
screen.update_droplets();
// make sure the terminal updates _now_
stdout.flush().unwrap();
 
// slow down animation
std::thread::sleep(Duration::from_millis(50));
}
 
// reset Terminal back to normal
write!(stdout, "{}", termion::style::Reset).unwrap();
write!(stdout, "{}", termion::clear::All).unwrap();
write!(stdout, "{}", termion::cursor::Goto(1, 1)).unwrap();
write!(stdout, "{}", termion::cursor::Show).unwrap();
}
</syntaxhighlight>
 
=={{header|Wren}}==
{{trans|Yabasic}}
{{libheader|DOME}}
The ''memory.ttf'' file is included with the DOME 'fonts' example and can be downloaded from [https://github.com/domeengine/dome/blob/main/examples/fonts/memory.ttf here].
<syntaxhighlight lang="wren">import "dome" for Window
import "graphics" for Canvas, Color, Font
import "random" for Random
 
var Rand = Random.new()
 
class MatrixDigitalRain {
construct new(width, height) {
Window.resize(width, height)
Canvas.resize(width, height)
Window.title = "Matrix digital rain"
Font.load("Mem12", "memory.ttf", 24)
Canvas.font = "Mem12"
}
 
letter(x, y, r, g, b) {
if (y < 0 || y >= _my) return
var col = Color.rgb(r, g, b)
var c = String.fromByte(_scr[x][y])
Canvas.print(c, x * 12.8 , y * 12.8 , col)
}
 
init() {
_mx = 50
_my = 42
_scr = List.filled(_mx, null)
for (x in 0..._mx) {
_scr[x] = List.filled(_my, 0)
for (y in 0..._my) _scr[x][y] = Rand.int(33, 128)
}
_ms = 50
_sx = List.filled(_ms, 0)
_sy = List.filled(_ms, 0)
}
 
update() {
for (a in 0..._ms) {
_sx[a] = Rand.int(_mx)
_sy[a] = Rand.int(_my)
}
}
 
draw(alpha) {
for (s in 0..._ms) {
var x = _sx[s]
var y = _sy[s]
letter(x, y, 0, 255, 0)
y = y - 1
letter(x, y, 0, 200, 0)
y = y - 1
letter(x, y, 0, 150, 0)
y = y - 1
if (y*12.8 + 3 < 0) y = 0
var c = Color.rgb(0, 0, 0)
Canvas.rectfill(x*12.8, y*12.8 + 3, 13, 14, c)
letter(x, y, 0, 70, 0)
y = y - 24
if (y*12.8 + 3 < 0) y = 0
Canvas.rectfill(x*12.8, y*12.8 + 3, 13, 14, c)
}
for (s in 0..._ms) {
if (Rand.int(1, 6) == 1) _sy[s] = _sy[s] + 1
if (_sy[s] > _my + 25) {
_sy[s] = 0
_sx[s] = Rand.int(_mx)
}
}
}
}
 
var Game = MatrixDigitalRain.new(640, 550)</syntaxhighlight>
 
=={{header|Yabasic}}==
<langsyntaxhighlight Yabasiclang="yabasic">open window 640,512,"swiss12"
backcolor 0,0,0
clear window
Line 2,054 ⟶ 3,144:
color r,g,b
text x*12.8,y*12.8,chr$(c)
end sub</langsyntaxhighlight>
 
=={{header|zkl}}==
{{trans|Raku}}
<langsyntaxhighlight lang="zkl">var [const] codes=Walker.chain( // a bunch of UTF non ascii chars
[0x0391..0x03a0], [0x03a3..0x0475], [0x0400..0x0475],
[0x048a..0x052f], [0x03e2..0x03ef], [0x2c80..0x2ce9],
Line 2,096 ⟶ 3,186:
}
return(o,s,fg);
}</langsyntaxhighlight>
Offsite Image: [http://www.zenkinetic.com/Images/RosettaCode/matrixRainDance.jpg Matrix rain dance]
 
Line 2,110 ⟶ 3,200:
The next step for this routine would be a way to randomise the length of the character trails (currently fixed at 12).
 
<langsyntaxhighlight lang="zxbasic">10 CLEAR 61999
20 BORDER 0: POKE 23624,4: POKE 23693,0: CLS: REM easier than "bright 0: flash 0: ink 0: paper 0"
30 PRINT INK 4; FLASH 1;"Initialising": GO SUB 9000: LET m=USR 62000: CLS: REM set up and run machine code; USR is the call function
Line 2,194 ⟶ 3,284:
9960 DATA "eof"
 
9999 POKE 23606,0: POKE 23607,60: INK 4: REM reset to default character set and colour if you get lost</langsyntaxhighlight>
 
Offsite Image: [https://imgur.com/sz6YkfI Spectrum rain at imgur]
884

edits