寻求有关此 Bash 脚本的帮助

寻求有关此 Bash 脚本的帮助

成立这个矩阵模仿脚本由 Derrick.blarg 创作,在所有其他效果中最喜欢它的效果。

在过去的 5 天内,我一直在尝试重新设计轨迹渲染,以便它打印一半的行减去控制​​台窗口大小的 1。我尝试过的任何方法都不会产生这种效果。除了让循环为每列生成两次轨迹并从中心列开始之外,还可以动态修复中间列,以在控制台中心拼出一个字符串文本变量,当该变量拼写出字符串中的字符时结束轨迹。多变的。


# This script creates a Matrix-style falling text effect in the terminal.

# Define strings for extra characters (Japanese Katakana) and extended ASCII characters
extra_chars="カキクケコサシスセソタチツテトナニヌネノハヒフヘホマミムメモヤユヨラリルレロワン"
extended_ascii="│┤┐└┴┬├─┼┘┌≡"

# Define arrays of color codes for a fading green color effect, and a static color
fade_colors=('\033[38;2;0;255;0m' '\033[38;2;0;192;0m' '\033[38;2;0;128;0m' '\033[38;2;0;64;0m' '\033[38;2;0;32;0m' '\033[38;2;0;32;0m' '\033[38;2;0;32;0m' '\033[38;2;0;32;0m' '\033[38;2;0;32;0m' '\033[38;2;0;32;0m' '\033[38;2;0;32;0m' '\033[38;2;0;32;0m' '\033[38;2;0;32;0m' '\033[38;2;0;32;0m' '\033[38;2;0;32;0m' '\033[38;2;0;32;0m' '\033[38;2;0;32;0m' '\033[38;2;0;32;0m' '\033[38;2;0;32;0m' '\033[38;2;0;32;0m' '\033[38;2;0;16;0m' '\033[38;2;0;8;0m') # Fading green colors
static_color='\033[38;2;0;0;0m' # Static dark green color
white_bold='\033[1;37m' # White and bold for the primary character

# Get terminal dimensions
COLUMNS=$(tput cols) # Number of columns in the terminal
ROWS=$(tput lines) # Number of rows in the terminal


# Hide the cursor for a cleaner effect and clear the screen
echo -ne '\033[?25l'
clear

# Function to generate a random character from the set of extra characters and extended ASCII
random_char() {
    local chars="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789${extra_chars}${extended_ascii}"
    echo -n "${chars:RANDOM%${#chars}:1}"
}

# Generate a list of 1000 random characters
random_chars=""
for (( i=0; i<1000; i++ )); do
    random_chars+=$(random_char) # Add a random character to the end of the string
done

# Initialize a counter for cycling through the random characters
char_counter=0 # Counter for cycling through the random characters

# Initialize arrays to keep track of the position and trail characters of each column
positions=() # Array to store the current position in each column
trail_chars=() # Array to store the trail characters in each column
for (( c=1; c<=COLUMNS; c++ )); do
    positions[$c]=$((RANDOM % ROWS)) # Random starting position for each column
    trail_chars[$c]="" # Start with an empty trail for each column
done

# Function to update the display with the falling text effect
update_line() {
    local last_pos=0  # Track the last position to optimize cursor movement

    for (( c=1; c<=COLUMNS; c++ )); do
        # Randomly skip updating some columns to create a dynamic effect
        if [ $((RANDOM % 4)) -ne 0 ]; then
            continue
        fi

        local new_char=${random_chars:$char_counter:1} # Select the next character from the random string
        char_counter=$(( (char_counter + 1) % 1000 )) # Update the counter, cycling back after 1000

        local pos=${positions[$c]} # Current position in this column
        local trail=${trail_chars[$c]} # Current trail of characters in this column

        trail_chars[$c]="${new_char}${trail:0:$((ROWS - 1))}" # Update the trail by adding new character at the top

        # Render the trail of characters
        for (( i=0; i<${#trail}; i++ )); do
            local trail_pos=$((pos - i)) # Calculate the position for each character in the trail
            if [ $trail_pos -ge 0 ] && [ $trail_pos -lt $ROWS ]; then
                local color=${fade_colors[i]:-$static_color} # Choose color from the fade array or static color if beyond the array
                if [ $i -eq 0 ]; then
                    color=$white_bold # First character in the trail is white and bold
                fi
                if [ $last_pos -ne $trail_pos ]; then
                    printf "%b" "\033[${trail_pos};${c}H" # Move cursor to the right position
                    last_pos=$trail_pos
                fi
                printf "%b" "${color}${trail:$i:1}\033[0m" # Print the character with color
            fi
        done

        positions[$c]=$((pos + 1)) # Update the position for the next cycle
        if [ $pos -ge $((ROWS + ${#fade_colors[@]})) ]; then
            positions[$c]=0 # Reset position if it moves off screen
            trail_chars[$c]=""
        fi
    done
}

# Main loop for continuous execution of the update_line function
while true; do
    update_line
done

# Reset terminal settings on exit (show cursor, clear screen, reset text format)
echo -ne '\033[?25h' # Show cursor
clear
tput sgr0 # Reset text format```

相关内容