Introduction: Matrix Code - Visual Basic

New and improved Visual Basic code will display a "Matrix"-like effect, with scrolling One's and Zero's. It then cuts to the Matrix "Wake Up Neo" sequence, and then continues to scroll numbers.

I realize this isn't exactly how the sequence goes within the movie, but there were people asking for this functionality, so I added it.

I think the way I randomize the display of the columns of One's and Zero's is the neatest part of the code. I discuss exactly what I did later.

This Instructable is in response to Brennn10's "Welcome to the Matrix in Command Prompt" Instructable. The code that makes up this Instructable was based initially on code posted in a comment by DarkJoker on the above Instructable.

I've intentionally been rather explicit with my explanation of the code as I've gotten quite a few requests for clarification since I first posted this solution.

Step 1: Grab My Code

I've attached my code below.

Save both files (startMatrix.itsabat and matrix.itsavbs) within the same folder on your machine, and remove the "itsa" portion of the filename, i.e. rename the files to startMatrix.bat and matrix.vbs. Bat files and vbs files are executable, and therefore I can't upload them with those extensions here. This is why you must save and rename them locally.

Step 2: Executing the Code

If you want to run my code, follow the below instructions. If you don't want to execute something on your machine that you don't know anything about, you're smart. You should go on to the next step, where I walk through the code and show you that I haven't done anything devious.

However, if you just want to get to it (or you're on someone else's machine and don't care), here's how to kick off the code:

Step 1: double-click on "startMatrix.bat".

That's it. Now go on to the next step to read about the coolness you just saw.

Step 3: Understanding the Code, Part 1

Let's take a look at the code, starting from the beginning. Hopefully you can learn enough about VB from my code that you'll go off and learn how to code in VB better than me :)

So first things first, we have 2 files: a bat file and a vbs file. If you edit the bat file, you'll notice that it executes the vbs file, after setting up the window, text color and window title. Let's move on to the more interesting stuff in the vbs file.

FYI, I'm only going to touch on the most interesting lines of code here.

Also, pay particular note to the lines starting in a ' ( tick, or single quote ). These are comments, and WON'T BE EXECUTED.

The first few lines of matrix.vbs say "Set my error messages to the explicit level" but at the same time "Don't let any mistakes I made be fatal." No real discussion needed.

Line 5: Our dim statement here sets up all of the variables that we'll use throughout the life of our little program. We don't need to declare types for these variables, or initial values for them, like we might in some other languages. VB is "powerful" like this.

Line 12: objSysInfo lets us gather some information on the system on which we're running. We create the object here, and on Line 13, objSysInfo.UserName returns the name of the currently logged in user that's executing this code. This will be used later in place of "Neo", so that our Script can say "Wake up, Martin..." if it's being run on my machine, or "Wake up, Brennn10..." if it's being run on Brennn10's machine and if that was his username.

Using the System's user name allows you to throw it on your buddy's unlocked machine and it will pull his name automatically, without having to alter the code.

Let's move on.

Step 4: Understanding the Code, Part 2

Line 16: sub waitfor is our first subroutine. A subroutine can be called from a different place in the code, will execute the code within the body of the subroutine, and will then return program control to the calling code. waitfor sets up our shell for us. By doing this in a loop with a "sleep" within the loop, we allow the activation of the shell to complete prior to control being returned, which is a very good thing.

Line 26: sub matrix is our second subroutine, and takes a parameter called "elements". This subroutine does the printing of the cool numbers that appear to "fall", as in the movie.

I've taken a few things for granted here. Hopefully by explaining them you'll understand where I'm coming from and can do it better than I.

First off, I assume that you only want 5 columns of numbers, and that "elements" is divisible by the number of columns (5). This was easy to guarantee because later on I declare the parameters passed to "matrix" to be 200 and 100 (it's called twice).

I also assume that you want all 5 columns turned on by default. I think that it looks OK, but you could change a few of these to FALSE if you wanted the first line of numbers to seem as random as the rest of the rows will be.

OK, so we have 5 columns, we have 200 / 5 = 40 lines of numbers to display (linesToWrite), and all of our columns are turned on by default.

Line 41: we enter into two "for" loops, the first of which (Loop A) cycles through our linesToWrite and the second (Loop B) that cycles through the number of columns. In Loop B, we Select on the value of the loop variable loopB, and depending it's value we evaluate an If-Else statement. If the column in question is Enabled ("TRUE", or "turned on"), we will print, via "wshshell.SendKeys", the value of Int(Round(rnd())), followed by a tab. Otherwise, we'll print only a tab, such that the column appears empty.

So what's the value of Int(Round(rnd()))? rnd() returns a random floating point number between 0.0 and 1.0, Round() will take that value and round it to the nearest Integer value, and Int() casts the result to an Integer. This last step could be considered overkill, since the Round() call should give us 0 or 1. I did it just because of habit. At some point someone must have told me that it was safer, and I apparently haven't forgotten it yet.

On to Part 3...

Step 5: Understanding the Code, Part 3

Continuing sub Matrix, here are the next few lines of VB...

' This flip-flops columns,
' so if a column is OFF, it will be turned ON 30 percent of the time,
' and if a column is ON, it will be turned OFF 30 percent of the time.

turnOffMax = 0.30
turnOnMax = 0.30

Toggling each column 30% of the time makes the number columns "fall" in a random fashion similar to how they do in the movie. Not exact, but close.

' Column 1 flip-flop
flipMe = rnd()
If ((col1 = turnedOn) and (flipMe < turnOffMax)) Then
col1 = turnedOff
Else If ((col1 = turnedOff) and (flipMe < turnOnMax)) Then
col1 = turnedOn
End If

The above block handles how we determine whether we're going to display a number in the given column during the next iteration, for the next row of numbers. The code reads "if the column is on, and the random number called flipMe is less than 30%, turn off the column. Otherwise, if the column is off and flipMe is less than 30%, turn the column on."

That wraps up sub Matrix.

On to sub key...

Step 6: Sub Key()

The key method is below.

sub key(msg)
' do printouts of "the matrix" messages, and erase them
wscript.sleep 1500
length = len(msg)
for position = 1 to length
wshshell.SendKeys mid(msg, position, 1)
wscript.sleep 250
wscript.sleep 3000
for position = 1 to length
wshshell.SendKeys "{BACKSPACE}"
wscript.sleep 75
end sub

This method prints out a string "msg". It'll print anything you pass it. This method is called later on in the code.

The first FOR loop prints out each character in the string, one at a time, and pauses for 250 milliseconds in between each character. We then sleep for 3000 millis (3 seconds) and write out a backspace for each character we previously printed, sleeping this time a little less.

That's all there is to it. On to the main body of the program, where everything comes together.

Step 7: Main

The main execution of the program calls waitFor and matrix (which we've already covered) to set everything up and write out 200 binary digits, spread over 5 columns.

The name of the current system user is then grabbed and manipulated to be printed out as a part of the "wake up" sequence.

We then enter a FOR loop, which we know we'll only iterate over 4 times because of the "1 To 4" constraint. We then SELECT on our loop variable (loopA), which matches the first CASE statement the first time through, the second CASE statement the second time through the loop, etc. Each iteration of the loop, myString is set to the correct string representation of the verbiage we want to print, and the key subroutine is called to actually do the printing. There are obviously many other ways to design this loop, and I'll leave that as an exercise to the reader.

Once we exit the FOR loop, we once again call the matrix subroutine to print out more numbers. As the comment in the code says, you can put this final call to matrix() in a loop if you'd like, or you could just call it with a larger number, etc.

Finally, our call to quit() aborts program execution (always good practice :P ).

Hope everyone has fun taking my example and making it better! If this Instructable has piqued your curiosity with VB, Google can provide a ton of good resources to learn more about it than I can tell you. Happy coding!



JuanV4 (author)2014-12-11

hi, followed your instructions but when I try to open all I see is a Notepad work (probably yours) and nothing else happening. Do I need to do anything else?

leroy.dyer (author)2014-11-08

just down load the first file and name it startmatrix.bat

save the second as matrix.vbs

works fine

thanks mate

Matrix-technician (author)2014-03-22

It says could not find script file C:\users\name\documents\matrix.vbs"

sonic667 (author)2010-09-18

having trouble getting it to work

Laserman595 (author)2010-02-18

i got it to run fine but it made a message box pop up infinuntly i had to go to the task manager to get it to stop

please help i dont like it

Mirror Mage (author)2009-10-11

I am having trouble getting it to work

welcome to the matrix (author)2009-10-03

mmm, matrix is not any system .actually matrix use for hacking hacking a computer ,making viruses more more more and more welcome to the matrix this is my email if any one want a help iam ready

eldytho (author)2009-07-29

i don't know what ewent wrong but it popped up for about 1 sec and then went away?


you probably forgot to type PAUSE at the end and then hit enter then type :start then enter then in the next line put start matrix. if this does not work i will send you the code i used to make mine

bruinsxxbby (author)2009-02-06

this is all great, but i still cant figure it out... cant you just post the code you use? lol... i can get a basic matrix working, i just use: @echo off color 0A :top echo %random% %random% %random% %random% %random% %random% %random% %random% %random% %random% %random% %random% %random% %random% goto top and i save it as matrix.vbs i wish it were slower and more random, but i cant figure it out!

will421 (author)bruinsxxbby2009-05-16

It`s matrix.bat, right?

smith2martin (author)bruinsxxbby2009-02-06

My code is above. There are two files: startMatrix.itsabat and matrix.itsavbs. Rename them to remove the "itsa" portion from the extension, so you have startMatrix.bat and matrix.vbs. Put them both in the same folder and double-click the startMatrix.bat file and you should be off and running.

mandeep_singh (author)2009-05-08

not so nice.....

Marno1 (author)2009-02-09

When I try to download the files, I get the files "FF0Q2K8F9AZJOPC.tmp" and "FFGUGZSF9AZJOPB.tmp"

bdblock94 (author)Marno12009-02-10

step 1

Marno1 (author)bdblock942009-02-21

I think there is a problem in my settings.

fwjs28 (author)Marno12009-04-14

tools>folder options view tab>show extensions for common file types, then goto step 1

smith2martin (author)Marno12009-02-23

Did you rename the files as in Step 1? So you should have two files, one named startMatrix.bat, and one called matrix.vbs.

bdblock94 (author)2009-02-10

wont work

smith2martin (author)bdblock942009-02-11

Could you be more specific?

bdblock94 (author)smith2martin2009-02-12

I copy/pasted the codes and they wont work on my computer

smith2martin (author)bdblock942009-02-13

Ok, so you have two files, a .bat file and a .vbs file, in the same folder (say, on your desktop). What effect do you see after executing (double clicking) the .bat file? Do you receive an error of any kind? Also, try opening the .bat with notepad to verify its contents are correct. You'll see that it attempts to launch the .vbs file by name, so make sure your new .vbs file is named identically to the way the .bat expects it to be named.

bdblock94 (author)smith2martin2009-02-13

pc, and yes they are both on desktop. no, no effect whats so ever.

smith2martin (author)bdblock942009-02-23

The error may have been a result of you copying and pasting the code. The Invalid Character error message in the screenshot you posted indicates this.

Here's a link to more information on this error:

Looks like you may have tick marks, like `, instead of single quotes, like '.


I was able to replicate your error when I changed a single quote to a tick mark, so definitely check that out.

smith2martin (author)bdblock942009-02-13

Also, are you on a mac or pc?

god_gave_knex_to_you (author)2008-10-05

how do i save them on the same file, and how do i get it to go through slower? it goes WAY too fast


you don't want to save both files to the same file (if I'm understanding you correctly). Save both files as separate files, rename them as I describe above and double-click the .bat file. As far as slowing it down, look for "wscript.sleep 5" in the matrix subroutine. Changing the number 5 to a higher number will slow down the printing of the digits.