이 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```

관련 정보