-
Mar 27th, 2011, 12:09 AM
#1
Matrix Effect
I was browsing YouTube today and I came across this video that shows a sort of matrix effect made in a C# console application.
The link to the code is in the description, so I followed the link, copied and pasted the code it into an online C# to VB converter. I had to make a few changes, but I was able to get it to work in VB just fine and thought to share it with everyone.
Enjoy!
VB.NET Code:
Module Module1 Sub Main() Console.Title = "Matrix Effect" Console.ForegroundColor = ConsoleColor.DarkGreen Console.WindowLeft = InlineAssignHelper(0, 0) Console.WindowHeight = InlineAssignHelper(Console.BufferHeight, Console.LargestWindowHeight) Console.WindowWidth = InlineAssignHelper(Console.BufferWidth, Console.LargestWindowWidth) Console.CursorVisible = False Dim width As Integer, height As Integer Dim y As Integer() Dim l As Integer() Initialize(width, height, y, l) Dim ms As Integer While True Dim t1 As DateTime = DateTime.Now MatrixStep(width, height, y, l) ms = 10 - CInt(Math.Truncate(CType(DateTime.Now - t1, TimeSpan).TotalMilliseconds)) If ms > 0 Then System.Threading.Thread.Sleep(ms) End If If Console.KeyAvailable Then If Console.ReadKey().Key = ConsoleKey.F5 Then Initialize(width, height, y, l) End If End If End While End Sub Dim thistime As Boolean = False Private Sub MatrixStep(ByVal width As Integer, ByVal height As Integer, ByVal y As Integer(), ByVal l As Integer()) Dim x As Integer thistime = Not thistime For x = 0 To width - 1 If x Mod 11 = 10 Then If Not thistime Then Continue For End If Console.ForegroundColor = ConsoleColor.White Else Console.ForegroundColor = ConsoleColor.DarkGreen Console.SetCursorPosition(x, inBoxY(y(x) - 2 - ((l(x) \ 40) * 2), height)) Console.Write(R) Console.ForegroundColor = ConsoleColor.Green End If Console.SetCursorPosition(x, y(x)) Console.Write(R) y(x) = inBoxY(y(x) + 1, height) Console.SetCursorPosition(x, inBoxY(y(x) - l(x), height)) Console.Write(" "c) Next End Sub Private Sub Initialize(ByRef width As Integer, ByRef height As Integer, ByRef y As Integer(), ByRef l As Integer()) Dim h1 As Integer Dim h2 As Integer = (InlineAssignHelper(h1, (InlineAssignHelper(height, Console.WindowHeight)) \ 2)) \ 2 width = Console.WindowWidth - 1 y = New Integer(width - 1) {} l = New Integer(width - 1) {} Dim x As Integer Console.Clear() For x = 0 To width - 1 y(x) = m_r.[Next](height) l(x) = m_r.[Next](h2 * (If((x Mod 11 <> 10), 2, 1)), h1 * (If((x Mod 11 <> 10), 2, 1))) Next End Sub Dim m_r As New Random() Private ReadOnly Property R() As Char Get Dim t As Integer = m_r.[Next](10) If t <= 2 Then Return ChrW(CInt(AscW("0"c)) + m_r.[Next](10)) ElseIf t <= 4 Then Return ChrW(CInt(AscW("a"c)) + m_r.[Next](27)) ElseIf t <= 6 Then Return ChrW(CInt(AscW("A"c) + m_r.[Next](27))) Else Return ChrW(m_r.[Next](32, 255)) End If End Get End Property Public Function inBoxY(ByVal n As Integer, ByVal height As Integer) As Integer n = n Mod height If n < 0 Then Return n + height Else Return n End If End Function Private Function InlineAssignHelper(Of T)(ByRef target As T, ByVal value As T) As T target = value Return value End Function End Module
*Edit:
I forgot to mention that when the app is opened, it expands completely, but the top left is offset for some reason. Also, resizing the window to make it smaller or clicking the maximize button for the console screws it up.
Last edited by weirddemon; Mar 27th, 2011 at 12:13 AM.
CodeBank contributions: Process Manager, Temp File Cleaner
Originally Posted by SJWhiteley
"game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....
-
Mar 27th, 2011, 11:44 AM
#2
Re: Matrix Effect
Cool
If my post was helpful to you, then express your gratitude using Rate this Post.
And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet
Social Group: VBForums - Developers from India
Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...
-
Mar 30th, 2011, 04:52 PM
#3
Re: Matrix Effect
I like
-
Mar 30th, 2011, 05:20 PM
#4
Re: Matrix Effect
interesting, but the location is annoying + the way it runs when you maximize the console window is not so good
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Apr 2nd, 2011, 07:06 PM
#5
Re: Matrix Effect
Originally Posted by .paul.
interesting, but the location is annoying + the way it runs when you maximize the console window is not so good
Yeah. I tried playing around with it, but I couldn't get it figured out immediately and I didn't want to spend a lot of time playing with it :P
CodeBank contributions: Process Manager, Temp File Cleaner
Originally Posted by SJWhiteley
"game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....
-
Aug 27th, 2012, 06:40 AM
#6
-
Sep 3rd, 2012, 07:13 AM
#7
Re: Matrix Effect
you should probably post the link to the original one that you converted if you're going to use other peoples work
Great find btw...
Kris
-
Sep 3rd, 2012, 10:17 AM
#8
Re: Matrix Effect
Originally Posted by i00
you should probably post the link to the original one that you converted if you're going to use other peoples work
Great find btw...
Kris
Read the original post again. There's a link to the YouTube page. You'll notice that the last time I edited that post, was May of last year =/
CodeBank contributions: Process Manager, Temp File Cleaner
Originally Posted by SJWhiteley
"game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....
-
Oct 25th, 2015, 08:45 AM
#9
Member
Re: Matrix Effect
How do you get this to work? Where do you paste the code?
could you try uploading a zip file of it so that, I or other people can understand how it works?
Thanks,
Jonathan
-
Oct 25th, 2015, 04:05 PM
#10
Re: Matrix Effect
If you have a Visual Basic .Net Development environment, simply choose to create a new project, and select Console Application.
You should see the template code generated (Module1 and Sub Main), as is in the first post.
Paste the code from the first post in place of the template code.
It says it is a console application on the first line of the first post.
-
Oct 25th, 2015, 04:10 PM
#11
Re: Matrix Effect
Originally Posted by weirddemon
...
I forgot to mention that when the app is opened, it expands completely, but the top left is offset for some reason. Also, resizing the window to make it smaller or clicking the maximize button for the console screws it up.
Which is why you see the check for the f5 key in the loop.
If you resize the form, press f5 to recalculate the height and width, so the characters fall vertically again.
-
Oct 25th, 2015, 05:03 PM
#12
Member
Re: Matrix Effect
Originally Posted by passel
If you have a Visual Basic .Net Development environment, simply choose to create a new project, and select Console Application.
You should see the template code generated (Module1 and Sub Main), as is in the first post.
Paste the code from the first post in place of the template code.
It says it is a console application on the first line of the first post.
Thank you,
This is an awesome app
-
Oct 26th, 2015, 12:40 PM
#13
Re: Matrix Effect
Here's a way to have your forms in an app have falling snowflakes:
vb Code:
Option Explicit On Option Strict On Option Infer Off Imports System.ComponentModel Imports System.Drawing.Drawing2D Public Class SnowBaseForm Private Shared ReadOnly Random As New Random() Private Shared ReadOnly m_SnowFlakes As New List(Of SnowFlake) Private WithEvents Timer1 As System.Windows.Forms.Timer Private m_Tick As Integer = 0I Private m_Snow As Bitmap #Region " Constructor " Public Sub New() ' This call is required by the Windows Form Designer. Call InitializeComponent() ' Add any initialization after the InitializeComponent() call. 'We paint our control ourself and need a double buffer to prevent flimmering Me.SetStyle(ControlStyles.UserPaint Or ControlStyles.AllPaintingInWmPaint Or ControlStyles.DoubleBuffer, True) Timer1 = New System.Windows.Forms.Timer Timer1.Interval = 20I End Sub #End Region #Region " BaseForm: Paint, Dispose " 'Form overrides dispose to clean up the component list. <System.Diagnostics.DebuggerNonUserCode()> _ Protected Overrides Sub Dispose(ByVal disposing As Boolean) Try If disposing AndAlso components IsNot Nothing Then components.Dispose() End If If disposing Then Timer1.Enabled = False Timer1.Dispose() End If Finally MyBase.Dispose(disposing) End Try End Sub <System.Diagnostics.DebuggerNonUserCode()> _ Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs) MyBase.OnPaint(e) Dim g As Graphics = e.Graphics g.SmoothingMode = SmoothingMode.HighSpeed 'other things may be to slow For Each s As SnowFlake In m_SnowFlakes g.ResetTransform() g.TranslateTransform(-16I, -16I, MatrixOrder.Append) 'Align our flakes to the center g.ScaleTransform(s.Scale, s.Scale, MatrixOrder.Append) 'Scale them.. g.RotateTransform(s.Rotation, MatrixOrder.Append) 'Rotate them.. g.TranslateTransform(s.X, s.Y, MatrixOrder.Append) 'Move them to their appropriate location 'draw them g.DrawImage(Snow, 0I, 0I) Next s End Sub #End Region #Region " Properties " Private ReadOnly Property Snow() As Bitmap Get If m_Snow Is Nothing Then 'First Time - Create Image m_Snow = New Bitmap(32I, 32I) Using g As Graphics = Graphics.FromImage(m_Snow) g.SmoothingMode = SmoothingMode.AntiAlias g.Clear(Color.Transparent) g.TranslateTransform(16.0F, 16.0F, MatrixOrder.Append) Dim black As Color = Color.FromArgb(1I, 1I, 1I) Dim white As Color = Color.FromArgb(255I, 255I, 255I) DrawSnow(g, New SolidBrush(black), New Pen(black, 3.0F)) DrawSnow(g, New SolidBrush(white), New Pen(white, 2.0F)) g.Save() End Using End If Return m_Snow End Get End Property <Category("Snow"), DefaultValue("True")> _ Public Property Snowing() As Boolean Get Return Timer1.Enabled End Get Set(ByVal value As Boolean) If value <> Timer1.Enabled Then Timer1.Enabled = value End Set End Property <Category("Snow"), DefaultValue(20)> _ Public Property SnowSpeed() As Integer Get Return IntervalToSnowSpeed(Timer1.Interval) End Get Set(ByVal value As Integer) Select Case value Case Is > 41I : value = 41I Case Is < 1I : value = 1I End Select Timer1.Interval = SnowSpeedToInterval(value) End Set End Property #End Region #Region " Methods " Public Sub StartSnow() Timer1.Start() End Sub Public Sub StopSnow() Timer1.Stop() End Sub Public Sub ClearSnow() m_SnowFlakes.Clear() m_Tick = 0I Me.Refresh() End Sub #End Region #Region " Conversion Functions " Private Function SnowSpeedToInterval(ByVal Speed As Integer) As Integer Dim output As Integer = 10I Select Case Speed Case 0I : output = 50I Case 1I : output = 49I Case 2I : output = 48I Case 3I : output = 47I Case 4I : output = 46I Case 5I : output = 45I Case 6I : output = 44I Case 7I : output = 43I Case 8I : output = 42I Case 9I : output = 41I Case 10I : output = 40I Case 11I : output = 39I Case 12I : output = 38I Case 13I : output = 37I Case 14I : output = 36I Case 15I : output = 35I Case 16I : output = 34I Case 17I : output = 33I Case 18I : output = 32I Case 19I : output = 31I Case 20I : output = 30I Case 21I : output = 29I Case 22I : output = 28I Case 23I : output = 27I Case 24I : output = 26I Case 25I : output = 25I Case 26I : output = 24I Case 27I : output = 23I Case 28I : output = 22I Case 29I : output = 21I Case 30I : output = 20I Case 31I : output = 19I Case 32I : output = 18I Case 33I : output = 17I Case 34I : output = 16I Case 35I : output = 15I Case 36I : output = 14I Case 37I : output = 13I Case 38I : output = 12I Case 39I : output = 11I Case 40I : output = 10I End Select Return output End Function Private Function IntervalToSnowSpeed(ByVal Interval As Integer) As Integer Dim output As Integer = 1I Select Case Interval Case 50I : output = 0I Case 49I : output = 1I Case 48I : output = 2I Case 47I : output = 3I Case 46I : output = 4I Case 45I : output = 5I Case 44I : output = 6I Case 43I : output = 7I Case 42I : output = 8I Case 41I : output = 9I Case 40I : output = 10I Case 39I : output = 11I Case 38I : output = 12I Case 37I : output = 13I Case 36I : output = 14I Case 35I : output = 15I Case 34I : output = 16I Case 33I : output = 17I Case 32I : output = 18I Case 31I : output = 19I Case 30I : output = 20I Case 29I : output = 21I Case 28I : output = 22I Case 27I : output = 23I Case 26I : output = 24I Case 25I : output = 25I Case 24I : output = 26I Case 23I : output = 27I Case 22I : output = 28I Case 21I : output = 29I Case 20I : output = 30I Case 19I : output = 31I Case 18I : output = 32I Case 17I : output = 33I Case 16I : output = 34I Case 15I : output = 35I Case 14I : output = 36I Case 13I : output = 37I Case 12I : output = 38I Case 11I : output = 39I Case 10I : output = 40I End Select Return output End Function #End Region #Region " Timer1_Tick " Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick 'Tick.. If m_Tick = Integer.MaxValue Then m_Tick = 0I m_Tick += 1I 'Spawn new Flakes If m_Tick Mod 3I = 0I AndAlso Random.NextDouble() < 0.7R Then Dim s As New SnowFlake() s.X = Random.Next(-50I, Width + 50I) 'All over the screen... s.Y = Random.Next(-20I, -7I) 'Customize height further s.XVelocity = CSng((Random.NextDouble() - 0.5F)) * 2.0F s.YVelocity = CSng((Random.NextDouble() * 3.0F)) + 1.0F s.Rotation = Random.Next(0I, 359I) s.RotVelocity = Random.Next(-3I, 3I) * 2.0F If s.RotVelocity.Equals(0.0F) Then s.RotVelocity = 3.0F s.Scale = CSng((Random.NextDouble() / 2I)) + 0.75F m_SnowFlakes.Add(s) End If 'Move current flakes (and add them to del list, if they exceed the screen) Dim del As New List(Of SnowFlake) For Each s As SnowFlake In m_SnowFlakes s.X += s.XVelocity s.Y += s.YVelocity s.Rotation += s.RotVelocity 'Make them move snowflake like s.XVelocity += (CSng(Random.NextDouble()) - 0.5F) * 0.7F s.XVelocity = Math.Max(s.XVelocity, -2.0F) s.XVelocity = Math.Min(s.XVelocity, 2.0F) If s.YVelocity > Me.Height + 10I Then 'Out of Screen del.Add(s) End If Next s 'Delete them For Each s As SnowFlake In del m_SnowFlakes.Remove(s) Next s 'Redraw our control Me.Refresh() End Sub #End Region #Region " Helpers " ''' <summary> ''' Draws a snow flake on the specified graphics object ''' </summary> ''' <param name="g">Graphics object to draw on</param> ''' <param name="b">Brush (Color) of the middle part</param> ''' <param name="p">Pen (Color, Size) of the lines</param> Private Shared Sub DrawSnow(ByVal g As Graphics, ByVal b As Brush, ByVal p As Pen) Const a As Integer = 6I Const a2 As Integer = a + 2I Const r As Integer = 2I g.DrawLine(p, -a, -a, +a, +a) g.DrawLine(p, -a, +a, +a, -a) g.DrawLine(p, -a2, 0I, +a2, 0I) g.DrawLine(p, 0I, -a2, 0I, +a2) g.FillEllipse(b, -r, -r, r * 2I, r * 2I) End Sub #End Region #Region " Classes " ''' <summary> ''' This Container class represents the snowflake falling and rendered to the screen ''' </summary> Private Class SnowFlake Public Rotation As Single Public RotVelocity As Single Public Scale As Single Public X As Single Public XVelocity As Single Public Y As Single Public YVelocity As Single End Class #End Region End Class
Just have your forms inherit from SnowBaseForm instead of System.Windows.Forms.Form
-
Nov 12th, 2015, 06:41 PM
#14
Lively Member
-
Sep 7th, 2016, 02:03 PM
#15
New Member
Re: Matrix Effect
thanks op for code snippet...i want to give back by providing two alternative solutions: 1) re-initialize when height or width of console changed (instead of when F5 is pressed), 2) implement within a WPF application
alt solution 1: re-initialize when height or width of console changed (instead of when F5 is pressed)
1) declare a width and height variable before the whileloop
2) within while loop, save height and width of console
3) within while loop, modify if/then clause to initialize if current (new) console height/width is not the same as console's last height/width
Code:
Dim lastConsoleWidth, lastConsoleHeight As Integer
While True
lastConsoleWidth = Console.WindowWidth
lastConsoleHeight = Console.WindowHeight
Dim t1 As DateTime = DateTime.Now
MatrixStep(width, height, y, l)
ms = 10 - CInt(Math.Truncate(CType(DateTime.Now - t1, TimeSpan).TotalMilliseconds))
If ms > 0 Then
System.Threading.Thread.Sleep(ms)
End If
If Console.WindowHeight <> lastConsoleHeight Or Console.WindowWidth <> lastConsoleWidth Then
Initialize(width, height, y, l)
End If
End While
alt solution 2: implement within a WPF application
1) create class Win32 (source: http://stackoverflow.com/questions/2...-console-in-vb)
2) don't forget to declare your global variables
Code:
Dim thistime As Boolean = False
Dim m_r As New Random()
2) call Win32.AllocConsole() wherever/whenever you want console to run
Code:
Win32.AllocConsole()
Code:
Public Class Win32
<DllImport("kernel32.dll")> Public Shared Function AllocConsole() As Boolean
End Function
<DllImport("kernel32.dll")> Public Shared Function FreeConsole() As Boolean
End Function
End Class
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|