跳到主要內容

[C++] 電影駭客任務 Matrix 文字效果

1999 年上映的電影駭客任務(Matrix)於明年要推出第四集了。這部電影不論在哲學觀以及拍攝效果,在當年的影史上都是一個突破。
其中這部電影中最著名的電腦執行畫面深植在多少技術愛好者的心中:



本篇文章記錄我如何用 C++ 實作這種電影 matrix 文字下雨(raining)效果,這種文字效果有很多種作法,可以做得很精緻,我採取比較簡單的一種實現方法:
首先程式的邏輯從如何刷新 M x N 的文字 random 陣列開始。

假設有一個 M x N 的 random 文字陣列,我們可以從第 0 個 row 開始刷新每個字元。
假設現在正在處理第 0 個 row,我們走訪每個字元,然後隨機挑選某幾個文字更新,更新的方法有將當前字元用空白字元替換,或用任意非空白字元替換兩種情況。

當第 0 個 row 更新完之後,就刷新螢幕,然後繼續更新下一條 row,也就是更新第 1 個 row。

重點來了。
刷新螢幕的方法,順序為:

  1. 先清空螢幕
  2. 先顯示最新刷新的 row。
  3. 依序顯示之前刷新的 row。

原始碼如下:

// C++ program for implementation of falling matrix effect
#include<iostream> 
#include<string> 
#include<thread> 
#include<cstdlib> 
#include<ctime> 
#include<chrono> 
  
const int cols       = 60; 
const int rows       = 30;
const int switch_num = 5; 
const int delay      = 90; 
char      screen[rows][cols];

using namespace std; 
  
int main(int argc, char** argv) 
{ 
    srand(time(NULL)); 
    bool status[cols] = {0}; 
  
    // screen initialization
    for(int i=0; i<rows; i++)
    {
        for(int j=0; j<cols; j++)
        {
            screen[i][j] = (rand() % 95) + 32;  // 32 <= ascii_code <= 126
        }
    }
 
    int line_counter = 0;
    while (true) 
    { 
        for (int i=0; i<cols; i++) 
        { 
            if (status[i] & screen[line_counter][i] == ' ')   
                                screen[line_counter][i] = (rand() % 95) + 32; 
            else                screen[line_counter][i] = ' '; 
        } 
  
        // update status:
        for (int i=0; i < switch_num; i++) 
        { 
            int pos = rand() % cols;
            status[pos] = !status[pos]; 
        } 
  
        
        // show screen:
        for(int i=line_counter+rows; i!=line_counter; i--) 
        {
            for(int j=0; j< cols; j++)
            {
                cout << " ";
                cout << screen[i%rows][j];
            }
            cout << endl;
        }

        this_thread::sleep_for(chrono::milliseconds(delay)); 
        
        cout << "\033[2J\033[1;1H"; // clear screen.
         
        line_counter++;
        line_counter = line_counter % rows;
    } 

    
    return 0; 
} 

留言

這個網誌中的熱門文章

[Linux] Elementary OS 字體調校

用 gesetting 取得 elementary os 的等寬字體(這也是終端機默認字體): gsettings get org.gnome.desktop.interface monospace-font-name 會顯示目前字型跟字體大小: Roboto Mono 10 設定字體大小: gsettings set org.gnome.desktop.interface monospace-font-name 'Roboto Mono 12' 可以微調 text-scaling-factor: gsettings set org.gnome.desktop.interface text-scaling-factor <value>

[Vim] 我的 .vimrc

我用過的文字編輯器有 sublime text/atom/notepad++/gedit 等等。 後來我在 window 和 Linux 寫程式還是比較慣用 vim。 以下整理我常用的 .vimrc:  set nu "show line number. set rnu "related line number. nmap ; : "replace ';' with ':' in normal mode. set ai "auto indent set mouse=a "activate mouse set expandtab "replace tab with space. set tabstop=4 "replace tab with 4 spaces. set bg=dark "background set to dark. color desert "color scheme. syntax on "enable syntax highlight. " auto complete: inoremap ( ()<Esc>i inoremap ' ''<Esc>i inoremap " ""<Esc>i inoremap [ []<Esc>i inoremap { {}<Esc>i set cursorline " active cursorline 到此 vim 搭配 Ctrl + N 當自動補全,已經很好用了,之後有好用設定會再補上來。

[心得] 復古、老派的程式設計之路

最近看到 John Carmack 2018 年的貼文中的幾段話: I’m not a Unix geek.  I get around ok, but I am most comfortable developing in Visual Studio on Windows.  I thought a week of full immersion work in the old school Unix style would be interesting, even if it meant working at a slower pace.  It was sort of an adventure in retro computing — this was fvwm and vi.  Not vim, actual BSD vi. In the end, I didn’t really explore the system all that much, with 95% of my time in just the basic vi / make / gdb operations.  I appreciated the good man pages, as I tried to do everything within the self contained system, without resorting to internet searches.  Seeing references to 30+ year old things like Tektronix terminals was amusing. In the spirit of my retro theme, I had printed out several of Yann LeCun’s old papers and was considering doing everything completely off line, as if I was actually in a mountain cabin somewhere, but I wound up watching a lot of the Stanford CS231N lectur...