Saturday, January 29, 2022

Excel Matrix Digital Rain

Matrix digital rain is the programming code featured in the Matrix series. In this post we see how to create the Matrix digital rain in Excel using VBA macros. The macro uses the SetTimer function that we have seen in previous posts to change the position of the characters and create the falling effect of the digital rain from the Matrix movie. The code keeps changing and fading away while falling using a formula to get random characters from a list.

Matrix Digital Rain

The Matrix digital rain is the computer code featured in the science fiction movie “The Matrix” (and following movies of the Matrix’s series). It consists of numbers and special characters representing the activity of the virtual reality environment of the Matrix on the screen.

Among some of the characters and symbols in the code, it appears to be many mirrored Japanese letters, along with some mirrored numbers and punctuation symbols. They show up green (even white at first sometimes) on a dark background and fade away while falling down the screen.

The digital rain code is now a classic of the Matrix’s series and appears in every movie. The series comprises the following movies – you can watch them on Prime following the links.



Adding the Characters

The list of characters and symbols that resemble those of the original Matrix digital rain have been added to the first column of the active sheet in Excel. Those include most (if not all) the Japanese alphabet (not mirrored), most numbers either normal or mirrored (or rather a symbol that looks like it when mirrored), and some punctuation characters and other symbols that seem to appear in the original rain code.


The following formula is added programmatically to each randomly selected cell on the top row. The formula gets a random character each time by targeting a cell in column A between rows 1 and 57.

=OFFSET($A$1,RANDBETWEEN(1,57),0)


The color of the first character is white, and then it changes to several shades of green until becoming black and fade away with the background. We use the RGB function to get the different shades of green. That tries to resemble how the code is falling in the original Matrix movie. However, there are different versions of the digital rain and some have white characters while other don’t. The font size of the characters is also changing in this Excel version of the Matrix digital rain. It is bigger for the first character, and then gets smaller while fading.

The FormatLayout subroutine is only used if wished to change the formatting of the worksheet where the digital rain is displayed. It sets the size of cells, alignment, and interior color. This formatting along with the type and color of the characters have been chosen to look as close as possible to the original digital rain of the Matrix movie.

 

Creating the Falling Movement

In order to create the Matrix digital falling rain effect in Excel, we need to move the characters to the row below every time the macro is called. We do that copying the whole range with characters (i.e. with the formulas inserted previously), and pasting everything one row below. When pasted, the value is updated with a new random character as per the formula.

Note that several ranges are copied/pasted with three columns overlapping and pasted twice, thus creating the effect of the code falling faster for those columns. That’s something we can observe in the original code rain of the Matrix movie.

  Range(Cells(1, 2), Cells(31, 8)).Copy Range(Cells(2, 2), Cells(32, 8))
  Range(Cells(1, 8), Cells(31, 18)).Copy Range(Cells(2, 8), Cells(32, 18))
  Range(Cells(1, 18), Cells(31, 30)).Copy Range(Cells(2, 18), Cells(32, 30))
  Range(Cells(1, 30), Cells(31, 46)).Copy Range(Cells(2, 30), Cells(32, 46))


Then we need a timer routine to call the macro repeatedly within a certain time interval (in milliseconds). We can do that using the SetTimer function of the Windows API library (user32.dll). This is the same function that we have used in previous Excel games with movement such as Excel Tron Game, Excel Circles Movement, Excel Falling Letters, Excel Geometry Dash, and Excel Snake Game. The SetTimer declaration and related procedures used here are explained in the Excel Snake Game post.


In this Excel version of the Matrix digital rain, we use an interval of 100 milliseconds to call the MoveRain macro. That can be changed to slow down the movement (setting a higher value), but may not speed it up much if a lower value is selected. The digital rain spans over most of the worksheet and copy/paste methods slow down the macro execution considerably.

The SetTimer function calls the MoveRain procedure every 100 milliseconds. It actually calls the TimerEvent procedure to handle any error occurring, and then TimerEvent calls the MoveRain subroutine. We have to press the Stop button to stop the Excel Matrix digital rain movement, otherwise it will continue progressing. The Stop button calls the StopTimer procedure that runs the KillTimer function to cancel or kill the process. It requires the exact TimerID set with SetTimer.

And that’s how we emulate the Matrix digital rain effect in Excel using VBA macros.


Download Excel Matrix Digital Rain


No comments:

Post a Comment

Popular Posts