Certainly! Below is a complete Lua program using the LÖVE2D framework that simulates the iconic Matrix digital rain effect. The program initializes multiple streams of characters that fall vertically across the screen, creating the mesmerizing green code rain effect seen in the Matrix movies.
Prerequisites
Install LÖVE2D: Ensure you have LÖVE2D installed on your machine. You can download it from love2d.org.
Save the Code: Save the following code in a file named main.lua.
Run the Program: Navigate to the directory containing main.lua in your terminal or command prompt and execute the program using LÖVE2D.
bash
love .
Complete main.lua Code
lua
-- Matrix Digital Rain Effect using LÖVE2D
-- Author: OpenAI ChatGPT
-- Date: 2023-10
function love.load()
-- Screen dimensions
love.window.setTitle("Matrix Digital Rain")
window_width, window_height = love.graphics.getDimensions()
-- Use a monospaced font for uniform character width
font_size = 20
font = love.graphics.newFont("courier.ttf", font_size)
love.graphics.setFont(font)
-- Calculate the number of columns based on the font width
char_width = font:getWidth("A")
char_height = font:getHeight()
columns = math.floor(window_width / char_width)
-- Characters to display (ASCII range 33 to 126)
characters = {}
for i = 33, 126 do
table.insert(characters, string.char(i))
end
-- Stream class
Stream = {}
Stream.__index = Stream
function Stream:new(x)
local stream = {}
setmetatable(stream, Stream)
stream.x = x -- Position in pixels
stream.y = math.random(-1000, 0) -- Start y at a random position above the screen
stream.speed = math.random(50, 300) -- Pixels per second
stream.resetY = nil
stream.length = math.random(5, 30) -- Number of characters in the stream
stream.characters = {}
return stream
end
function Stream:update(dt)
self.y = self.y + self.speed * dt
if self.y > window_height + self.length * char_height then
self:reset()
end
end
function Stream:reset()
self.y = math.random(-1000, 0)
self.speed = math.random(50, 300)
self.length = math.random(5, 30)
end
function Stream:draw()
for i = 1, self.length do
local char_y = self.y - (i * char_height)
if char_y > 0 and char_y < window_height then
-- Fade effect: the leading character is bright white, trailing characters are green with decreasing opacity
if i == 1 then
love.graphics.setColor(1, 1, 1, 1) -- White for the leading character
else
local fade = 1 - (i / self.length)
love.graphics.setColor(0, 1, 0, fade) -- Green with fading opacity
end
local char = characters[math.random(1, #characters)]
love.graphics.print(char, self.x, char_y)
end
end
end
-- Initialize streams
streams = {}
for i = 1, columns do
local x = (i - 1) * char_width
table.insert(streams, Stream:new(x))
end
-- Black background
love.graphics.setBackgroundColor(0, 0, 0)
end
function love.update(dt)
for _, stream in ipairs(streams) do
stream:update(dt)
end
end
function love.draw()
for _, stream in ipairs(streams) do
stream:draw()
end
end
-- Optional: Restart streams when the window is resized to adjust the number of columns
function love.resize(w, h)
window_width, window_height = w, h
columns = math.floor(window_width / char_width)
-- Adjust the number of streams based on new column count
if #streams < columns then
for i = #streams + 1, columns do
local x = (i - 1) * char_width
table.insert(streams, Stream:new(x))
end
elseif #streams > columns then
for i = #streams, columns + 1, -1 do
table.remove(streams, i)
end
end
end
Explanation of the Code
Initialization (love.load):
Window Setup: Sets the window title and retrieves the screen dimensions.
Font Setup: Uses a monospaced font (Courier) to ensure uniform character width. Adjust the font_size as needed. If courier.ttf is not available, LÖVE2D will use a default font.
Character Setup: Initializes a table of printable ASCII characters (from ! to ~) to display in the rain effect.
Stream Class: Defines a Stream class to manage individual columns of falling characters. Each stream has properties like position (x and y), speed, length, and the characters it displays.
Streams Initialization: Creates a stream for each column based on the screen width and font size.
Updating Streams (love.update):
Each frame, the update function is called with dt (delta time), updating the position of each stream based on its speed.
If a stream moves beyond the bottom of the screen (considering its length), it resets to start above the screen with new random properties.
Drawing Streams (love.draw):
Iterates through each stream and draws its characters on the screen.
The leading character of each stream is bright white, while the trailing characters are green with decreasing opacity to create a fading effect.
Handling Window Resize (love.resize):
Adjusts the number of streams dynamically if the window is resized to maintain the effect across different screen sizes.
Customization
Font and Size: You can change the font_size variable to increase or decrease the size of the characters. Ensure that the specified font (courier.ttf in this case) is available. If not, LÖVE2D will default to a standard font.
Character Set: Modify the loop that fills the characters table to include different characters or ranges.
Colors: Adjust the RGBA values in setColor to change the colors of the characters. The current setup uses white for the leading character and green with fading opacity for the trailing characters.
Running the Program
Ensure LÖVE2D is Installed: Download and install LÖVE2D from here.
Save the Code: Create a directory for the project, save the above code in a file named main.lua within that directory.
Execute the Program:
Open your terminal or command prompt.
Navigate to the directory containing main.lua.
Run the program by executing:
bash
love .
This command tells LÖVE2D to run the project in the current directory.
You should now see the Matrix digital rain effect on your screen! Feel free to experiment with the parameters to customize the appearance and behavior of the rain effect.